umbrello API Documentation

ast_utils.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2002 by Roberto Raggi                                   *
00003  *   roberto@kdevelop.org                                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00012 #include "ast_utils.h"
00013 #include "ast.h"
00014 
00015 #include <qstringlist.h>
00016 #include <qregexp.h>
00017 
00018 #include <klocale.h>
00019 #include <kdebug.h>
00020 #include <kapplication.h>
00021 
00022 AST* findNodeAt( AST* node, int line, int column )
00023 {
00024     // kdDebug(9007) << "findNodeAt(" << node << ")" << endl;
00025 
00026     if( !node )
00027     return 0;
00028 
00029     int startLine, startColumn;
00030     int endLine, endColumn;
00031 
00032     node->getStartPosition( &startLine, &startColumn );
00033     node->getEndPosition( &endLine, &endColumn );
00034 
00035     if( (line > startLine || (line == startLine && column >= startColumn)) &&
00036         (line < endLine || (line == endLine && column < endColumn)) ){
00037 
00038         QPtrList<AST> children = node->children();
00039     QPtrListIterator<AST> it( children );
00040     while( it.current() ){
00041         AST* a = it.current();
00042         ++it;
00043 
00044         AST* r = findNodeAt( a, line, column );
00045         if( r )
00046         return r;
00047     }
00048 
00049     return node;
00050     }
00051 
00052     return 0;
00053 }
00054 
00055 void scopeOfNode( AST* ast, QStringList& scope )
00056 {
00057     if( !ast )
00058     return;
00059 
00060     if( ast->parent() )
00061     scopeOfNode( ast->parent(), scope );
00062 
00063     QString s;
00064     switch( ast->nodeType() )
00065     {
00066     case NodeType_ClassSpecifier:
00067         if( ((ClassSpecifierAST*)ast)->name() ){
00068         s = ((ClassSpecifierAST*)ast)->name()->text();
00069         s = s.isEmpty() ? QString::fromLatin1("<unnamed>") : s;
00070         scope.push_back( s );
00071     }
00072     break;
00073 
00074     case NodeType_Namespace:
00075     {
00076         AST* namespaceName = ((NamespaceAST*)ast)->namespaceName();
00077     s = namespaceName ? namespaceName->text() : QString::fromLatin1("<unnamed>");
00078     scope.push_back( s );
00079     }
00080     break;
00081 
00082     case NodeType_FunctionDefinition:
00083     {
00084     FunctionDefinitionAST* funDef = static_cast<FunctionDefinitionAST*>( ast );
00085     DeclaratorAST* d = funDef->initDeclarator()->declarator();
00086     
00087     // hotfix for bug #68726
00088     if ( !d->declaratorId() )
00089         break;
00090     
00091     QPtrList<ClassOrNamespaceNameAST> l = d->declaratorId()->classOrNamespaceNameList();
00092     QPtrListIterator<ClassOrNamespaceNameAST> nameIt( l );
00093     while( nameIt.current() ){
00094         AST* name = nameIt.current()->name();
00095         scope.push_back( name->text() );
00096     
00097         ++nameIt;
00098     }
00099     }
00100     break;
00101 
00102     default:
00103     break;
00104     }
00105 }
00106 
00107 
00108 QString typeSpecToString( TypeSpecifierAST* typeSpec )  
00109 {
00110     if( !typeSpec )
00111         return QString::null;
00112 
00113     return typeSpec->text().replace( QRegExp(" :: "), "::" );
00114 }
00115 
00116 QString declaratorToString( DeclaratorAST* declarator, const QString& scope, bool skipPtrOp )
00117 {
00118    if( !declarator )
00119        return QString::null;
00120 
00121    QString text;
00122 
00123    if( !skipPtrOp ){
00124        QPtrList<AST> ptrOpList = declarator->ptrOpList();
00125        for( QPtrListIterator<AST> it(ptrOpList); it.current(); ++it ){
00126       text += it.current()->text();
00127        }
00128        text += ' ';
00129    }
00130 
00131    text += scope;
00132 
00133    if( declarator->subDeclarator() )
00134        text += QString::fromLatin1("(") + declaratorToString(declarator->subDeclarator()) + QString::fromLatin1(")");
00135 
00136    if( declarator->declaratorId() )
00137        text += declarator->declaratorId()->text();
00138 
00139    QPtrList<AST> arrays = declarator->arrayDimensionList();
00140    QPtrListIterator<AST> it( arrays );
00141    while( it.current() ){
00142        text += "[]";
00143        ++it;
00144    }
00145 
00146    if( declarator->parameterDeclarationClause() ){
00147        text += "( ";
00148 
00149        ParameterDeclarationListAST* l = declarator->parameterDeclarationClause()->parameterDeclarationList();
00150        if( l != 0 ){
00151            QPtrList<ParameterDeclarationAST> params = l->parameterList();
00152        QPtrListIterator<ParameterDeclarationAST> it( params );
00153 
00154            while( it.current() ){
00155            QString type = typeSpecToString( it.current()->typeSpec() );
00156            text += type;
00157            if( !type.isEmpty() )
00158            text += ' ';
00159                text += declaratorToString( it.current()->declarator() );
00160 
00161                ++it;
00162 
00163            if( it.current() )
00164                text += ", ";
00165            }
00166        }
00167 
00168        text += " )";
00169 
00170        if( declarator->constant() != 0 )
00171            text += " const";
00172    }
00173 
00174    return text.replace( QRegExp(" :: "), "::" ).simplifyWhiteSpace();
00175 }
00176 
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:07:54 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003