1 /****************************************************************************
2 **
3 ** Copyright (C) 2001-2004 Roberto Raggi
4 ** Copyright (C) 2015 The Qt Company Ltd.
5 ** Contact: http://www.qt.io/licensing/
6 **
7 ** This file is part of the qt3to4 porting application of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see http://www.qt.io/terms-conditions. For further
16 ** information use the contact form at http://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 2.1 or version 3 as published by the Free
21 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
22 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
23 ** following information to ensure the GNU Lesser General Public License
24 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
25 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 **
27 ** As a special exception, The Qt Company gives you certain additional
28 ** rights. These rights are described in The Qt Company LGPL Exception
29 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 **
31 ** GNU General Public License Usage
32 ** Alternatively, this file may be used under the terms of the GNU
33 ** General Public License version 3.0 as published by the Free Software
34 ** Foundation and appearing in the file LICENSE.GPL included in the
35 ** packaging of this file. Please review the following information to
36 ** ensure the GNU General Public License version 3.0 requirements will be
37 ** met: http://www.gnu.org/copyleft/gpl.html.
38 **
39 ** $QT_END_LICENSE$
40 **
41 ****************************************************************************/
42
43 #include "treewalker.h"
44
45 QT_BEGIN_NAMESPACE
46
47 /*
48 template <class T>
49 inline void parseAll(TreeWalker *w, const List<T *> *l)
50 {
51 if (!l)
52 return;
53
54 foreach(T *e, *l)
55 w->parseNode(e);
56 }
57 */
58
59 //Workaround for ICE on MSVC, use macro instead of template.
60 #define PARSE_ALL(ListType, ListValueType) \
61 inline void parseAll(TreeWalker *w, const ListType *l) \
62 { \
63 if (!l) \
64 return; \
65 foreach(ListValueType *e, *l) \
66 w->parseNode(e); \
67 } \
68
PARSE_ALL(List<AST * >,AST)69 PARSE_ALL(List<AST *>, AST)
70 PARSE_ALL(List<ClassOrNamespaceNameAST *>, ClassOrNamespaceNameAST)
71 PARSE_ALL(List<BaseSpecifierAST *>, BaseSpecifierAST)
72 PARSE_ALL(List<DeclarationAST *>, DeclarationAST)
73 PARSE_ALL(List<EnumeratorAST *>, EnumeratorAST)
74 PARSE_ALL(List<ParameterDeclarationAST *>, ParameterDeclarationAST)
75 PARSE_ALL(List<InitDeclaratorAST *>, InitDeclaratorAST)
76 PARSE_ALL(List<TemplateParameterAST *>, TemplateParameterAST)
77 PARSE_ALL(List<StatementAST *>, StatementAST)
78
79
80 void TreeWalker::parseTemplateArgumentList(TemplateArgumentListAST *node)
81 {
82 List<AST *> *arglist = node->argumentList();
83 parseAll(this, arglist);
84 }
85
parseClassOrNamespaceName(ClassOrNamespaceNameAST * node)86 void TreeWalker::parseClassOrNamespaceName(ClassOrNamespaceNameAST *node)
87 {
88 parseNode(node->name());
89 parseNode(node->templateArgumentList());
90 }
91
parseName(NameAST * node)92 void TreeWalker::parseName(NameAST *node)
93 {
94 parseAll(this, node->classOrNamespaceNameList());
95 parseNode(node->unqualifiedName());
96 }
97
parseTypeParameter(TypeParameterAST * node)98 void TreeWalker::parseTypeParameter(TypeParameterAST *node)
99 {
100 parseNode(node->templateParameterList());
101 parseNode(node->name());
102 parseNode(node->typeId());
103 }
104
parseDeclaration(DeclarationAST * node)105 void TreeWalker::parseDeclaration(DeclarationAST *node)
106 {
107 switch (node->nodeType()) {
108 case NodeType_AccessDeclaration:
109 parseAccessDeclaration(static_cast<AccessDeclarationAST*>(node));
110 break;
111 case NodeType_LinkageSpecification:
112 parseLinkageSpecification(static_cast<LinkageSpecificationAST*>(node));
113 break;
114 case NodeType_Namespace:
115 parseNamespace(static_cast<NamespaceAST*>(node));
116 break;
117 case NodeType_NamespaceAlias:
118 parseNamespaceAlias(static_cast<NamespaceAliasAST*>(node));
119 break;
120 case NodeType_Using:
121 parseUsing(static_cast<UsingAST*>(node));
122 break;
123 case NodeType_UsingDirective:
124 parseUsingDirective(static_cast<UsingDirectiveAST*>(node));
125 break;
126 case NodeType_Typedef:
127 parseTypedef(static_cast<TypedefAST*>(node));
128 break;
129 case NodeType_TemplateDeclaration:
130 parseTemplateDeclaration(static_cast<TemplateDeclarationAST*>(node));
131 break;
132 case NodeType_SimpleDeclaration:
133 parseSimpleDeclaration(static_cast<SimpleDeclarationAST*>(node));
134 break;
135 case NodeType_FunctionDefinition:
136 parseFunctionDefinition(static_cast<FunctionDefinitionAST*>(node));
137 break;
138 default:
139 break;
140 }
141 }
142
parseAccessDeclaration(AccessDeclarationAST * node)143 void TreeWalker::parseAccessDeclaration(AccessDeclarationAST *node)
144 {
145 parseAll(this, node->accessList());
146 }
147
parseTypeSpecifier(TypeSpecifierAST * node)148 void TreeWalker::parseTypeSpecifier(TypeSpecifierAST *node)
149 {
150 parseNode(node->name());
151 parseNode(node->cvQualify());
152 parseNode(node->cv2Qualify());
153
154 switch (node->nodeType()) {
155 case NodeType_ClassSpecifier:
156 parseClassSpecifier(static_cast<ClassSpecifierAST*>(node));
157 break;
158 case NodeType_EnumSpecifier:
159 parseEnumSpecifier(static_cast<EnumSpecifierAST*>(node));
160 break;
161 case NodeType_ElaboratedTypeSpecifier:
162 parseElaboratedTypeSpecifier(static_cast<ElaboratedTypeSpecifierAST*>(node));
163 break;
164 default:
165 break;
166 }
167 }
168
parseBaseSpecifier(BaseSpecifierAST * node)169 void TreeWalker::parseBaseSpecifier(BaseSpecifierAST *node)
170 {
171 parseNode(node->isVirtual());
172 parseNode(node->access());
173 parseNode(node->name());
174 }
175
parseBaseClause(BaseClauseAST * node)176 void TreeWalker::parseBaseClause(BaseClauseAST *node)
177 {
178 parseAll(this, node->baseSpecifierList());
179 }
180
parseClassSpecifier(ClassSpecifierAST * node)181 void TreeWalker::parseClassSpecifier(ClassSpecifierAST *node)
182 {
183 parseNode(node->winDeclSpec());
184 parseNode(node->classKey());
185 parseNode(node->baseClause());
186 parseAll(this, node->declarationList());
187 }
188
parseEnumerator(EnumeratorAST * node)189 void TreeWalker::parseEnumerator(EnumeratorAST *node)
190 {
191 parseNode(node->id());
192 parseNode(node->expression());
193 }
194
parseEnumSpecifier(EnumSpecifierAST * node)195 void TreeWalker::parseEnumSpecifier(EnumSpecifierAST *node)
196 {
197 parseAll(this, node->enumeratorList());
198 }
199
parseElaboratedTypeSpecifier(ElaboratedTypeSpecifierAST * node)200 void TreeWalker::parseElaboratedTypeSpecifier(ElaboratedTypeSpecifierAST *node)
201 {
202 parseNode(node->kind());
203 }
204
parseLinkageBody(LinkageBodyAST * node)205 void TreeWalker::parseLinkageBody(LinkageBodyAST *node)
206 {
207 parseAll(this, node->declarationList());
208 }
209
parseLinkageSpecification(LinkageSpecificationAST * node)210 void TreeWalker::parseLinkageSpecification(LinkageSpecificationAST *node)
211 {
212 parseNode(node->externType());
213 parseNode(node->linkageBody());
214 parseNode(node->declaration());
215 }
216
parseNamespace(NamespaceAST * node)217 void TreeWalker::parseNamespace(NamespaceAST *node)
218 {
219 parseNode(node->namespaceName());
220 parseNode(node->linkageBody());
221 }
222
parseNamespaceAlias(NamespaceAliasAST * node)223 void TreeWalker::parseNamespaceAlias(NamespaceAliasAST *node)
224 {
225 parseNode(node->namespaceName());
226 parseNode(node->aliasName());
227 }
228
parseUsing(UsingAST * node)229 void TreeWalker::parseUsing(UsingAST *node)
230 {
231 parseNode(node->typeName());
232 parseNode(node->name());
233 }
234
parseUsingDirective(UsingDirectiveAST * node)235 void TreeWalker::parseUsingDirective(UsingDirectiveAST *node)
236 {
237 parseNode(node->name());
238 }
239
parseDeclarator(DeclaratorAST * node)240 void TreeWalker::parseDeclarator(DeclaratorAST *node)
241 {
242 parseAll(this, node->ptrOpList());
243 parseNode(node->subDeclarator());
244 parseNode(node->declaratorId());
245 parseNode(node->bitfieldInitialization());
246 parseAll(this, node->arrayDimensionList());
247 parseNode(node->parameterDeclarationClause());
248 parseNode(node->constant());
249 parseNode(node->exceptionSpecification());
250 }
251
parseParameterDeclaration(ParameterDeclarationAST * node)252 void TreeWalker::parseParameterDeclaration(ParameterDeclarationAST *node)
253 {
254 parseNode(node->typeSpec());
255 parseNode(node->declarator());
256 parseNode(node->expression());
257 }
258
parseParameterDeclarationList(ParameterDeclarationListAST * node)259 void TreeWalker::parseParameterDeclarationList(ParameterDeclarationListAST *node)
260 {
261 parseAll(this, node->parameterList());
262 }
263
parseParameterDeclarationClause(ParameterDeclarationClauseAST * node)264 void TreeWalker::parseParameterDeclarationClause(ParameterDeclarationClauseAST *node)
265 {
266 parseNode(node->parameterDeclarationList());
267 parseNode(node->ellipsis());
268 }
269
parseInitDeclarator(InitDeclaratorAST * node)270 void TreeWalker::parseInitDeclarator(InitDeclaratorAST *node)
271 {
272 parseNode(node->declarator());
273 parseNode(node->initializer());
274 }
275
parseInitDeclaratorList(InitDeclaratorListAST * node)276 void TreeWalker::parseInitDeclaratorList(InitDeclaratorListAST *node)
277 {
278 parseAll(this, node->initDeclaratorList());
279 }
280
parseTypedef(TypedefAST * node)281 void TreeWalker::parseTypedef(TypedefAST *node)
282 {
283 parseNode(node->typeSpec());
284 parseNode(node->initDeclaratorList());
285 }
286
parseTemplateParameter(TemplateParameterAST * node)287 void TreeWalker::parseTemplateParameter(TemplateParameterAST *node)
288 {
289 parseNode(node->typeParameter());
290 parseNode(node->typeValueParameter());
291 }
292
parseTemplateParameterList(TemplateParameterListAST * node)293 void TreeWalker::parseTemplateParameterList(TemplateParameterListAST *node)
294 {
295 parseAll(this, node->templateParameterList());
296 }
297
parseTemplateDeclaration(TemplateDeclarationAST * node)298 void TreeWalker::parseTemplateDeclaration(TemplateDeclarationAST *node)
299 {
300 parseNode(node->exported());
301 parseNode(node->templateParameterList());
302 parseNode(node->declaration());
303 }
304
parseSimpleDeclaration(SimpleDeclarationAST * node)305 void TreeWalker::parseSimpleDeclaration(SimpleDeclarationAST *node)
306 {
307 parseNode(node->functionSpecifier());
308 parseNode(node->storageSpecifier());
309 parseNode(node->typeSpec());
310 parseNode(node->initDeclaratorList());
311 parseNode(node->winDeclSpec());
312 }
313
parseStatement(StatementAST * node)314 void TreeWalker::parseStatement(StatementAST *node)
315 {
316 switch (node->nodeType()) {
317 case NodeType_ExpressionStatement:
318 parseExpressionStatement(static_cast<ExpressionStatementAST*>(node));
319 break;
320
321 case NodeType_IfStatement:
322 parseIfStatement(static_cast<IfStatementAST*>(node));
323 break;
324
325 case NodeType_WhileStatement:
326 parseWhileStatement(static_cast<WhileStatementAST*>(node));
327 return;
328
329 case NodeType_DoStatement:
330 parseDoStatement(static_cast<DoStatementAST*>(node));
331 break;
332
333 case NodeType_ForStatement:
334 parseForStatement(static_cast<ForStatementAST*>(node));
335 break;
336
337 case NodeType_SwitchStatement:
338 parseSwitchStatement(static_cast<SwitchStatementAST*>(node));
339 break;
340
341 case NodeType_LabeledStatement:
342 parseLabeledStatement(static_cast<LabeledStatementAST*>(node));
343 break;
344
345 case NodeType_StatementList:
346 parseStatementList(static_cast<StatementListAST*>(node));
347 break;
348
349 case NodeType_DeclarationStatement:
350 parseDeclarationStatement(static_cast<DeclarationStatementAST*>(node));
351 break;
352
353 case NodeType_ReturnStatement:
354 parseReturnStatement(static_cast<ReturnStatementAST*>(node));
355 break;
356
357 default:
358 break;
359 }
360 }
361
parseExpressionStatement(ExpressionStatementAST * node)362 void TreeWalker::parseExpressionStatement(ExpressionStatementAST *node)
363 {
364 parseNode(node->expression());
365 }
366
parseCondition(ConditionAST * node)367 void TreeWalker::parseCondition(ConditionAST *node)
368 {
369 parseNode(node->typeSpec());
370 parseNode(node->declarator());
371 parseNode(node->expression());
372 }
373
parseIfStatement(IfStatementAST * node)374 void TreeWalker::parseIfStatement(IfStatementAST *node)
375 {
376 parseNode(node->condition());
377 parseNode(node->statement());
378 parseNode(node->elseStatement());
379 }
380
parseWhileStatement(WhileStatementAST * node)381 void TreeWalker::parseWhileStatement(WhileStatementAST *node)
382 {
383 parseNode(node->condition());
384 parseNode(node->statement());
385 }
386
parseDoStatement(DoStatementAST * node)387 void TreeWalker::parseDoStatement(DoStatementAST *node)
388 {
389 parseNode(node->condition());
390 parseNode(node->statement());
391 }
392
parseForStatement(ForStatementAST * node)393 void TreeWalker::parseForStatement(ForStatementAST *node)
394 {
395 parseNode(node->initStatement());
396 parseNode(node->condition());
397 parseNode(node->expression());
398 parseNode(node->statement());
399 }
400
parseSwitchStatement(SwitchStatementAST * node)401 void TreeWalker::parseSwitchStatement(SwitchStatementAST *node)
402 {
403 parseNode(node->condition());
404 parseNode(node->statement());
405 }
406
parseLabeledStatement(LabeledStatementAST * node)407 void TreeWalker::parseLabeledStatement(LabeledStatementAST *node)
408 {
409 parseNode(node->expression());
410 parseNode(node->statement());
411 }
412
parseStatementList(StatementListAST * node)413 void TreeWalker::parseStatementList(StatementListAST *node)
414 {
415 parseAll(this, node->statementList());
416 }
417
parseDeclarationStatement(DeclarationStatementAST * node)418 void TreeWalker::parseDeclarationStatement(DeclarationStatementAST *node)
419 {
420 parseNode(node->declaration());
421 }
422
parseFunctionDefinition(FunctionDefinitionAST * node)423 void TreeWalker::parseFunctionDefinition(FunctionDefinitionAST *node)
424 {
425 parseNode(node->functionSpecifier());
426 parseNode(node->storageSpecifier());
427 parseNode(node->typeSpec());
428 parseNode(node->initDeclarator());
429 parseNode(node->functionBody());
430 parseNode(node->winDeclSpec());
431 }
432
parseTranslationUnit(TranslationUnitAST * node)433 void TreeWalker::parseTranslationUnit(TranslationUnitAST *node)
434 {
435 parseAll(this, node->declarationList());
436 }
437
438
parseExpression(AbstractExpressionAST * node)439 void TreeWalker::parseExpression(AbstractExpressionAST *node)
440 {
441 parseAll(this, node->children());
442 }
443
444
parseBinaryExpression(BinaryExpressionAST * node)445 void TreeWalker::parseBinaryExpression(BinaryExpressionAST *node)
446 {
447 parseNode(node->op());
448 parseNode(node->leftExpression());
449 parseNode(node->rightExpression());
450 }
451
parseReturnStatement(ReturnStatementAST * node)452 void TreeWalker::parseReturnStatement(ReturnStatementAST *node)
453 {
454 parseNode(node->expression());
455 }
456
457 QT_END_NAMESPACE
458