ast_utils.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
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 #include <ktexteditor/editinterface.h>
00023
00024 AST* findNodeAt( AST* node, int line, int column )
00025 {
00026
00027
00028 if( !node )
00029 return 0;
00030
00031 int startLine, startColumn;
00032 int endLine, endColumn;
00033
00034 node->getStartPosition( &startLine, &startColumn );
00035 node->getEndPosition( &endLine, &endColumn );
00036
00037 if( (line > startLine || (line == startLine && column >= startColumn)) &&
00038 (line < endLine || (line == endLine && column < endColumn)) ){
00039
00040 QPtrList<AST> children = node->children();
00041 QPtrListIterator<AST> it( children );
00042 while( it.current() ){
00043 AST* a = it.current();
00044 ++it;
00045
00046 AST* r = findNodeAt( a, line, column );
00047 if( r )
00048 return r;
00049 }
00050
00051 return node;
00052 }
00053
00054 return 0;
00055 }
00056
00057 void scopeOfNode( AST* ast, QStringList& scope )
00058 {
00059 if( !ast )
00060 return;
00061
00062 if( ast->parent() )
00063 scopeOfNode( ast->parent(), scope );
00064
00065 QString s;
00066 switch( ast->nodeType() )
00067 {
00068 case NodeType_ClassSpecifier:
00069 if( ((ClassSpecifierAST*)ast)->name() ){
00070 s = ((ClassSpecifierAST*)ast)->name()->text();
00071 s = s.isEmpty() ? QString::fromLatin1("<unnamed>") : s;
00072 scope.push_back( s );
00073 }
00074 break;
00075
00076 case NodeType_Namespace:
00077 {
00078 AST* namespaceName = ((NamespaceAST*)ast)->namespaceName();
00079 s = namespaceName ? namespaceName->text() : QString::fromLatin1("<unnamed>");
00080 scope.push_back( s );
00081 }
00082 break;
00083
00084 case NodeType_FunctionDefinition:
00085 {
00086 FunctionDefinitionAST* funDef = static_cast<FunctionDefinitionAST*>( ast );
00087 DeclaratorAST* d = funDef->initDeclarator()->declarator();
00088
00089
00090 if ( !d->declaratorId() )
00091 break;
00092
00093 QPtrList<ClassOrNamespaceNameAST> l = d->declaratorId()->classOrNamespaceNameList();
00094 QPtrListIterator<ClassOrNamespaceNameAST> nameIt( l );
00095 while( nameIt.current() ){
00096 AST* name = nameIt.current()->name();
00097 scope.push_back( name->text() );
00098
00099 ++nameIt;
00100 }
00101 }
00102 break;
00103
00104 default:
00105 break;
00106 }
00107 }
00108
00109
00110 QString typeSpecToString( TypeSpecifierAST* typeSpec )
00111 {
00112 if( !typeSpec )
00113 return QString::null;
00114
00115 return typeSpec->text().replace( QRegExp(" :: "), "::" );
00116 }
00117
00118 QString declaratorToString( DeclaratorAST* declarator, const QString& scope, bool skipPtrOp )
00119 {
00120 if( !declarator )
00121 return QString::null;
00122
00123 QString text;
00124
00125 if( !skipPtrOp ){
00126 QPtrList<AST> ptrOpList = declarator->ptrOpList();
00127 for( QPtrListIterator<AST> it(ptrOpList); it.current(); ++it ){
00128 text += it.current()->text();
00129 }
00130 text += " ";
00131 }
00132
00133 text += scope;
00134
00135 if( declarator->subDeclarator() )
00136 text += QString::fromLatin1("(") + declaratorToString(declarator->subDeclarator()) + QString::fromLatin1(")");
00137
00138 if( declarator->declaratorId() )
00139 text += declarator->declaratorId()->text();
00140
00141 QPtrList<AST> arrays = declarator->arrayDimensionList();
00142 QPtrListIterator<AST> it( arrays );
00143 while( it.current() ){
00144 text += "[]";
00145 ++it;
00146 }
00147
00148 if( declarator->parameterDeclarationClause() ){
00149 text += "( ";
00150
00151 ParameterDeclarationListAST* l = declarator->parameterDeclarationClause()->parameterDeclarationList();
00152 if( l != 0 ){
00153 QPtrList<ParameterDeclarationAST> params = l->parameterList();
00154 QPtrListIterator<ParameterDeclarationAST> it( params );
00155
00156 while( it.current() ){
00157 QString type = typeSpecToString( it.current()->typeSpec() );
00158 text += type;
00159 if( !type.isEmpty() )
00160 text += " ";
00161 text += declaratorToString( it.current()->declarator() );
00162
00163 ++it;
00164
00165 if( it.current() )
00166 text += ", ";
00167 }
00168 }
00169
00170 text += " )";
00171
00172 if( declarator->constant() != 0 )
00173 text += " const";
00174 }
00175
00176 return text.replace( QRegExp(" :: "), "::" ).simplifyWhiteSpace();
00177 }
00178
This file is part of the documentation for KDevelop Version 3.1.2.