/*************************************************************************** * Copyright (C) 2002 by Roberto Raggi * * roberto@tdevelop.org * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "ast_utils.h" #include "ast.h" #include #include #include #include #include #include #include "cppsupport_utils.h" AST* findNodeAt( AST* node, int line, int column ) { // kdDebug(9007) << "findNodeAt(" << node << ")" << endl; if ( !node ) return 0; int startLine, startColumn; int endLine, endColumn; node->getStartPosition( &startLine, &startColumn ); node->getEndPosition( &endLine, &endColumn ); if ( ( line > startLine || ( line == startLine && column >= startColumn ) ) && ( line < endLine || ( line == endLine && column < endColumn ) ) ) { TQPtrList tqchildren = node->tqchildren(); TQPtrListIterator it( tqchildren ); while ( it.current() ) { AST * a = it.current(); ++it; AST* r = findNodeAt( a, line, column ); if ( r ) return r; } return node; } return 0; } void scopeOfNode( AST* ast, TQStringList& scope ) { if ( !ast ) return ; if ( ast->parent() ) scopeOfNode( ast->parent(), scope ); TQString s; switch ( ast->nodeType() ) { case NodeType_ClassSpecifier: if ( ( ( ClassSpecifierAST* ) ast ) ->name() ) { s = ( ( ClassSpecifierAST* ) ast ) ->name() ->text(); s = s.isEmpty() ? TQString::tqfromLatin1( "" ) : s; scope.push_back( s ); } break; case NodeType_Namespace: { AST* namespaceName = ( ( NamespaceAST* ) ast ) ->namespaceName(); s = namespaceName ? namespaceName->text() : TQString::tqfromLatin1( "" ); scope.push_back( s ); } break; case NodeType_FunctionDefinition: { FunctionDefinitionAST* funDef = static_cast( ast ); DeclaratorAST* d = funDef->initDeclarator() ->declarator(); // hotfix for bug #68726 if ( !d->declaratorId() ) break; TQPtrList l = d->declaratorId() ->classOrNamespaceNameList(); TQPtrListIterator nameIt( l ); while ( nameIt.current() ) { AST * name = nameIt.current() ->name(); scope.push_back( name->text() ); ++nameIt; } } break; default: break; } } TQString typeSpecToString( TypeSpecifierAST* typeSpec ) /// @todo remove { if ( !typeSpec ) return TQString(); return typeSpec->text().replace( TQRegExp( " :: " ), "::" ); } TQString declaratorToString( DeclaratorAST* declarator, const TQString& scope, bool skipPtrOp ) { if ( !declarator ) return TQString(); TQString text; if ( !skipPtrOp ) { TQPtrList ptrOpList = declarator->ptrOpList(); for ( TQPtrListIterator it( ptrOpList ); it.current(); ++it ) { text += it.current() ->text(); } text += " "; } text += scope; if ( declarator->subDeclarator() ) text += TQString::tqfromLatin1( "(" ) + declaratorToString( declarator->subDeclarator() ) + TQString::tqfromLatin1( ")" ); if ( declarator->declaratorId() ) text += declarator->declaratorId() ->text(); TQPtrList arrays = declarator->arrayDimensionList(); TQPtrListIterator it( arrays ); while ( it.current() ) { text += "[]"; ++it; } if ( declarator->parameterDeclarationClause() ) { text += formattedOpeningParenthesis(); ParameterDeclarationListAST* l = declarator->parameterDeclarationClause() ->parameterDeclarationList(); if ( l != 0 ) { TQPtrList params = l->parameterList(); TQPtrListIterator it( params ); while ( it.current() ) { TQString type = typeSpecToString( it.current() ->typeSpec() ); text += type; if ( !type.isEmpty() ) text += " "; text += declaratorToString( it.current() ->declarator() ); ++it; if ( it.current() ) text += ", "; } } text += formattedClosingParenthesis(); if ( declarator->constant() != 0 ) text += " const"; } return text.replace( TQRegExp( " :: " ), "::" ).simplifyWhiteSpace(); } //kate: indent-mode csands; tab-width 4; space-indent off;