From 114a878c64ce6f8223cfd22d76a20eb16d177e5e Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdevelop@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- languages/cpp/ast_utils.cpp | 190 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 languages/cpp/ast_utils.cpp (limited to 'languages/cpp/ast_utils.cpp') diff --git a/languages/cpp/ast_utils.cpp b/languages/cpp/ast_utils.cpp new file mode 100644 index 00000000..d21f4782 --- /dev/null +++ b/languages/cpp/ast_utils.cpp @@ -0,0 +1,190 @@ +/*************************************************************************** +* Copyright (C) 2002 by Roberto Raggi * +* roberto@kdevelop.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 ) ) ) + { + + QPtrList children = node->children(); + QPtrListIterator it( children ); + 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, QStringList& scope ) +{ + if ( !ast ) + return ; + + if ( ast->parent() ) + scopeOfNode( ast->parent(), scope ); + + QString s; + switch ( ast->nodeType() ) + { + case NodeType_ClassSpecifier: + if ( ( ( ClassSpecifierAST* ) ast ) ->name() ) + { + s = ( ( ClassSpecifierAST* ) ast ) ->name() ->text(); + s = s.isEmpty() ? QString::fromLatin1( "" ) : s; + scope.push_back( s ); + } + break; + + case NodeType_Namespace: + { + AST* namespaceName = ( ( NamespaceAST* ) ast ) ->namespaceName(); + s = namespaceName ? namespaceName->text() : QString::fromLatin1( "" ); + 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; + + QPtrList l = d->declaratorId() ->classOrNamespaceNameList(); + QPtrListIterator nameIt( l ); + while ( nameIt.current() ) + { + AST * name = nameIt.current() ->name(); + scope.push_back( name->text() ); + + ++nameIt; + } + } + break; + + default: + break; + } +} + + +QString typeSpecToString( TypeSpecifierAST* typeSpec ) /// @todo remove +{ + if ( !typeSpec ) + return QString::null; + + return typeSpec->text().replace( QRegExp( " :: " ), "::" ); +} + +QString declaratorToString( DeclaratorAST* declarator, const QString& scope, bool skipPtrOp ) +{ + if ( !declarator ) + return QString::null; + + QString text; + + if ( !skipPtrOp ) + { + QPtrList ptrOpList = declarator->ptrOpList(); + for ( QPtrListIterator it( ptrOpList ); it.current(); ++it ) + { + text += it.current() ->text(); + } + text += " "; + } + + text += scope; + + if ( declarator->subDeclarator() ) + text += QString::fromLatin1( "(" ) + declaratorToString( declarator->subDeclarator() ) + QString::fromLatin1( ")" ); + + if ( declarator->declaratorId() ) + text += declarator->declaratorId() ->text(); + + QPtrList arrays = declarator->arrayDimensionList(); + QPtrListIterator it( arrays ); + while ( it.current() ) + { + text += "[]"; + ++it; + } + + if ( declarator->parameterDeclarationClause() ) + { + text += formattedOpeningParenthesis(); + + ParameterDeclarationListAST* l = declarator->parameterDeclarationClause() ->parameterDeclarationList(); + if ( l != 0 ) + { + QPtrList params = l->parameterList(); + QPtrListIterator it( params ); + + while ( it.current() ) + { + QString 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( QRegExp( " :: " ), "::" ).simplifyWhiteSpace(); +} +//kate: indent-mode csands; tab-width 4; space-indent off; -- cgit v1.2.3