summaryrefslogtreecommitdiffstats
path: root/languages/cpp/ast_utils.cpp
blob: f6d19c2a58fb0984844bed02e6682198773e8779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/***************************************************************************
*   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 <tqstringlist.h>
#include <tqregexp.h>

#include <klocale.h>
#include <kdebug.h>
#include <kapplication.h>

#include <ktexteditor/editinterface.h>

#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<AST> children = node->children();
		TQPtrListIterator<AST> 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, 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::fromLatin1( "<unnamed>" ) : s;
			scope.push_back( s );
		}
		break;
		
	case NodeType_Namespace:
		{
			AST* namespaceName = ( ( NamespaceAST* ) ast ) ->namespaceName();
			s = namespaceName ? namespaceName->text() : TQString::fromLatin1( "<unnamed>" );
			scope.push_back( s );
		}
		break;
		
	case NodeType_FunctionDefinition:
		{
			FunctionDefinitionAST* funDef = static_cast<FunctionDefinitionAST*>( ast );
			DeclaratorAST* d = funDef->initDeclarator() ->declarator();
			
			// hotfix for bug #68726
			if ( !d->declaratorId() )
				break;
			
			TQPtrList<ClassOrNamespaceNameAST> l = d->declaratorId() ->classOrNamespaceNameList();
			TQPtrListIterator<ClassOrNamespaceNameAST> 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<AST> ptrOpList = declarator->ptrOpList();
		for ( TQPtrListIterator<AST> it( ptrOpList ); it.current(); ++it )
		{
			text += it.current() ->text();
		}
		text += " ";
	}
	
	text += scope;
	
	if ( declarator->subDeclarator() )
		text += TQString::fromLatin1( "(" ) + declaratorToString( declarator->subDeclarator() ) + TQString::fromLatin1( ")" );
	
	if ( declarator->declaratorId() )
		text += declarator->declaratorId() ->text();
	
	TQPtrList<AST> arrays = declarator->arrayDimensionList();
	TQPtrListIterator<AST> it( arrays );
	while ( it.current() )
	{
		text += "[]";
		++it;
	}
	
	if ( declarator->parameterDeclarationClause() )
	{
		text += formattedOpeningParenthesis();
		
		ParameterDeclarationListAST* l = declarator->parameterDeclarationClause() ->parameterDeclarationList();
		if ( l != 0 )
		{
			TQPtrList<ParameterDeclarationAST> params = l->parameterList();
			TQPtrListIterator<ParameterDeclarationAST> 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;