1 //===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the RecursiveASTVisitor interface, which recursively
11 // traverses the entire AST.
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
15 #define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
16
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclFriend.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/ExprCXX.h"
24 #include "clang/AST/ExprObjC.h"
25 #include "clang/AST/NestedNameSpecifier.h"
26 #include "clang/AST/Stmt.h"
27 #include "clang/AST/StmtCXX.h"
28 #include "clang/AST/StmtObjC.h"
29 #include "clang/AST/TemplateBase.h"
30 #include "clang/AST/TemplateName.h"
31 #include "clang/AST/Type.h"
32 #include "clang/AST/TypeLoc.h"
33
34 // The following three macros are used for meta programming. The code
35 // using them is responsible for defining macro OPERATOR().
36
37 // All unary operators.
38 #define UNARYOP_LIST() \
39 OPERATOR(PostInc) OPERATOR(PostDec) \
40 OPERATOR(PreInc) OPERATOR(PreDec) \
41 OPERATOR(AddrOf) OPERATOR(Deref) \
42 OPERATOR(Plus) OPERATOR(Minus) \
43 OPERATOR(Not) OPERATOR(LNot) \
44 OPERATOR(Real) OPERATOR(Imag) \
45 OPERATOR(Extension)
46
47 // All binary operators (excluding compound assign operators).
48 #define BINOP_LIST() \
49 OPERATOR(PtrMemD) OPERATOR(PtrMemI) \
50 OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) \
51 OPERATOR(Add) OPERATOR(Sub) OPERATOR(Shl) \
52 OPERATOR(Shr) \
53 \
54 OPERATOR(LT) OPERATOR(GT) OPERATOR(LE) \
55 OPERATOR(GE) OPERATOR(EQ) OPERATOR(NE) \
56 OPERATOR(And) OPERATOR(Xor) OPERATOR(Or) \
57 OPERATOR(LAnd) OPERATOR(LOr) \
58 \
59 OPERATOR(Assign) \
60 OPERATOR(Comma)
61
62 // All compound assign operators.
63 #define CAO_LIST() \
64 OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) \
65 OPERATOR(Shl) OPERATOR(Shr) OPERATOR(And) OPERATOR(Or) OPERATOR(Xor)
66
67 namespace clang {
68
69 // A helper macro to implement short-circuiting when recursing. It
70 // invokes CALL_EXPR, which must be a method call, on the derived
71 // object (s.t. a user of RecursiveASTVisitor can override the method
72 // in CALL_EXPR).
73 #define TRY_TO(CALL_EXPR) \
74 do { if (!getDerived().CALL_EXPR) return false; } while (0)
75
76 /// \brief A class that does preorder depth-first traversal on the
77 /// entire Clang AST and visits each node.
78 ///
79 /// This class performs three distinct tasks:
80 /// 1. traverse the AST (i.e. go to each node);
81 /// 2. at a given node, walk up the class hierarchy, starting from
82 /// the node's dynamic type, until the top-most class (e.g. Stmt,
83 /// Decl, or Type) is reached.
84 /// 3. given a (node, class) combination, where 'class' is some base
85 /// class of the dynamic type of 'node', call a user-overridable
86 /// function to actually visit the node.
87 ///
88 /// These tasks are done by three groups of methods, respectively:
89 /// 1. TraverseDecl(Decl *x) does task #1. It is the entry point
90 /// for traversing an AST rooted at x. This method simply
91 /// dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo
92 /// is the dynamic type of *x, which calls WalkUpFromFoo(x) and
93 /// then recursively visits the child nodes of x.
94 /// TraverseStmt(Stmt *x) and TraverseType(QualType x) work
95 /// similarly.
96 /// 2. WalkUpFromFoo(Foo *x) does task #2. It does not try to visit
97 /// any child node of x. Instead, it first calls WalkUpFromBar(x)
98 /// where Bar is the direct parent class of Foo (unless Foo has
99 /// no parent), and then calls VisitFoo(x) (see the next list item).
100 /// 3. VisitFoo(Foo *x) does task #3.
101 ///
102 /// These three method groups are tiered (Traverse* > WalkUpFrom* >
103 /// Visit*). A method (e.g. Traverse*) may call methods from the same
104 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*).
105 /// It may not call methods from a higher tier.
106 ///
107 /// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar
108 /// is Foo's super class) before calling VisitFoo(), the result is
109 /// that the Visit*() methods for a given node are called in the
110 /// top-down order (e.g. for a node of type NamedDecl, the order will
111 /// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()).
112 ///
113 /// This scheme guarantees that all Visit*() calls for the same AST
114 /// node are grouped together. In other words, Visit*() methods for
115 /// different nodes are never interleaved.
116 ///
117 /// Clients of this visitor should subclass the visitor (providing
118 /// themselves as the template argument, using the curiously recurring
119 /// template pattern) and override any of the Traverse*, WalkUpFrom*,
120 /// and Visit* methods for declarations, types, statements,
121 /// expressions, or other AST nodes where the visitor should customize
122 /// behavior. Most users only need to override Visit*. Advanced
123 /// users may override Traverse* and WalkUpFrom* to implement custom
124 /// traversal strategies. Returning false from one of these overridden
125 /// functions will abort the entire traversal.
126 ///
127 /// By default, this visitor tries to visit every part of the explicit
128 /// source code exactly once. The default policy towards templates
129 /// is to descend into the 'pattern' class or function body, not any
130 /// explicit or implicit instantiations. Explicit specializations
131 /// are still visited, and the patterns of partial specializations
132 /// are visited separately. This behavior can be changed by
133 /// overriding shouldVisitTemplateInstantiations() in the derived class
134 /// to return true, in which case all known implicit and explicit
135 /// instantiations will be visited at the same time as the pattern
136 /// from which they were produced.
137 template<typename Derived>
138 class RecursiveASTVisitor {
139 public:
140 /// \brief Return a reference to the derived class.
getDerived()141 Derived &getDerived() { return *static_cast<Derived*>(this); }
142
143 /// \brief Return whether this visitor should recurse into
144 /// template instantiations.
shouldVisitTemplateInstantiations()145 bool shouldVisitTemplateInstantiations() const { return false; }
146
147 /// \brief Return whether this visitor should recurse into the types of
148 /// TypeLocs.
shouldWalkTypesOfTypeLocs()149 bool shouldWalkTypesOfTypeLocs() const { return true; }
150
151 /// \brief Recursively visit a statement or expression, by
152 /// dispatching to Traverse*() based on the argument's dynamic type.
153 ///
154 /// \returns false if the visitation was terminated early, true
155 /// otherwise (including when the argument is NULL).
156 bool TraverseStmt(Stmt *S);
157
158 /// \brief Recursively visit a type, by dispatching to
159 /// Traverse*Type() based on the argument's getTypeClass() property.
160 ///
161 /// \returns false if the visitation was terminated early, true
162 /// otherwise (including when the argument is a Null type).
163 bool TraverseType(QualType T);
164
165 /// \brief Recursively visit a type with location, by dispatching to
166 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
167 ///
168 /// \returns false if the visitation was terminated early, true
169 /// otherwise (including when the argument is a Null type location).
170 bool TraverseTypeLoc(TypeLoc TL);
171
172 /// \brief Recursively visit a declaration, by dispatching to
173 /// Traverse*Decl() based on the argument's dynamic type.
174 ///
175 /// \returns false if the visitation was terminated early, true
176 /// otherwise (including when the argument is NULL).
177 bool TraverseDecl(Decl *D);
178
179 /// \brief Recursively visit a C++ nested-name-specifier.
180 ///
181 /// \returns false if the visitation was terminated early, true otherwise.
182 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
183
184 /// \brief Recursively visit a C++ nested-name-specifier with location
185 /// information.
186 ///
187 /// \returns false if the visitation was terminated early, true otherwise.
188 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
189
190 /// \brief Recursively visit a template name and dispatch to the
191 /// appropriate method.
192 ///
193 /// \returns false if the visitation was terminated early, true otherwise.
194 bool TraverseTemplateName(TemplateName Template);
195
196 /// \brief Recursively visit a template argument and dispatch to the
197 /// appropriate method for the argument type.
198 ///
199 /// \returns false if the visitation was terminated early, true otherwise.
200 // FIXME: migrate callers to TemplateArgumentLoc instead.
201 bool TraverseTemplateArgument(const TemplateArgument &Arg);
202
203 /// \brief Recursively visit a template argument location and dispatch to the
204 /// appropriate method for the argument type.
205 ///
206 /// \returns false if the visitation was terminated early, true otherwise.
207 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
208
209 /// \brief Recursively visit a set of template arguments.
210 /// This can be overridden by a subclass, but it's not expected that
211 /// will be needed -- this visitor always dispatches to another.
212 ///
213 /// \returns false if the visitation was terminated early, true otherwise.
214 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
215 bool TraverseTemplateArguments(const TemplateArgument *Args,
216 unsigned NumArgs);
217
218 /// \brief Recursively visit a constructor initializer. This
219 /// automatically dispatches to another visitor for the initializer
220 /// expression, but not for the name of the initializer, so may
221 /// be overridden for clients that need access to the name.
222 ///
223 /// \returns false if the visitation was terminated early, true otherwise.
224 bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
225
226 // ---- Methods on Stmts ----
227
228 // Declare Traverse*() for all concrete Stmt classes.
229 #define ABSTRACT_STMT(STMT)
230 #define STMT(CLASS, PARENT) \
231 bool Traverse##CLASS(CLASS *S);
232 #include "clang/AST/StmtNodes.inc"
233 // The above header #undefs ABSTRACT_STMT and STMT upon exit.
234
235 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
WalkUpFromStmt(Stmt * S)236 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
VisitStmt(Stmt * S)237 bool VisitStmt(Stmt *S) { return true; }
238 #define STMT(CLASS, PARENT) \
239 bool WalkUpFrom##CLASS(CLASS *S) { \
240 TRY_TO(WalkUpFrom##PARENT(S)); \
241 TRY_TO(Visit##CLASS(S)); \
242 return true; \
243 } \
244 bool Visit##CLASS(CLASS *S) { return true; }
245 #include "clang/AST/StmtNodes.inc"
246
247 // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary
248 // operator methods. Unary operators are not classes in themselves
249 // (they're all opcodes in UnaryOperator) but do have visitors.
250 #define OPERATOR(NAME) \
251 bool TraverseUnary##NAME(UnaryOperator *S) { \
252 TRY_TO(WalkUpFromUnary##NAME(S)); \
253 TRY_TO(TraverseStmt(S->getSubExpr())); \
254 return true; \
255 } \
256 bool WalkUpFromUnary##NAME(UnaryOperator *S) { \
257 TRY_TO(WalkUpFromUnaryOperator(S)); \
258 TRY_TO(VisitUnary##NAME(S)); \
259 return true; \
260 } \
261 bool VisitUnary##NAME(UnaryOperator *S) { return true; }
262
263 UNARYOP_LIST()
264 #undef OPERATOR
265
266 // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary
267 // operator methods. Binary operators are not classes in themselves
268 // (they're all opcodes in BinaryOperator) but do have visitors.
269 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE) \
270 bool TraverseBin##NAME(BINOP_TYPE *S) { \
271 TRY_TO(WalkUpFromBin##NAME(S)); \
272 TRY_TO(TraverseStmt(S->getLHS())); \
273 TRY_TO(TraverseStmt(S->getRHS())); \
274 return true; \
275 } \
276 bool WalkUpFromBin##NAME(BINOP_TYPE *S) { \
277 TRY_TO(WalkUpFrom##BINOP_TYPE(S)); \
278 TRY_TO(VisitBin##NAME(S)); \
279 return true; \
280 } \
281 bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
282
283 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
BINOP_LIST()284 BINOP_LIST()
285 #undef OPERATOR
286
287 // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound
288 // assignment methods. Compound assignment operators are not
289 // classes in themselves (they're all opcodes in
290 // CompoundAssignOperator) but do have visitors.
291 #define OPERATOR(NAME) \
292 GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator)
293
294 CAO_LIST()
295 #undef OPERATOR
296 #undef GENERAL_BINOP_FALLBACK
297
298 // ---- Methods on Types ----
299 // FIXME: revamp to take TypeLoc's rather than Types.
300
301 // Declare Traverse*() for all concrete Type classes.
302 #define ABSTRACT_TYPE(CLASS, BASE)
303 #define TYPE(CLASS, BASE) \
304 bool Traverse##CLASS##Type(CLASS##Type *T);
305 #include "clang/AST/TypeNodes.def"
306 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
307
308 // Define WalkUpFrom*() and empty Visit*() for all Type classes.
309 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
VisitType(Type * T)310 bool VisitType(Type *T) { return true; }
311 #define TYPE(CLASS, BASE) \
312 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \
313 TRY_TO(WalkUpFrom##BASE(T)); \
314 TRY_TO(Visit##CLASS##Type(T)); \
315 return true; \
316 } \
317 bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
318 #include "clang/AST/TypeNodes.def"
319
320 // ---- Methods on TypeLocs ----
321 // FIXME: this currently just calls the matching Type methods
322
323 // Declare Traverse*() for all concrete Type classes.
324 #define ABSTRACT_TYPELOC(CLASS, BASE)
325 #define TYPELOC(CLASS, BASE) \
326 bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
327 #include "clang/AST/TypeLocNodes.def"
328 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
329
330 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
WalkUpFromTypeLoc(TypeLoc TL)331 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
VisitTypeLoc(TypeLoc TL)332 bool VisitTypeLoc(TypeLoc TL) { return true; }
333
334 // QualifiedTypeLoc and UnqualTypeLoc are not declared in
335 // TypeNodes.def and thus need to be handled specially.
WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL)336 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
337 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
338 }
VisitQualifiedTypeLoc(QualifiedTypeLoc TL)339 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL)340 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
341 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
342 }
VisitUnqualTypeLoc(UnqualTypeLoc TL)343 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
344
345 // Note that BASE includes trailing 'Type' which CLASS doesn't.
346 #define TYPE(CLASS, BASE) \
347 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
348 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \
349 TRY_TO(Visit##CLASS##TypeLoc(TL)); \
350 return true; \
351 } \
352 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
353 #include "clang/AST/TypeNodes.def"
354
355 // ---- Methods on Decls ----
356
357 // Declare Traverse*() for all concrete Decl classes.
358 #define ABSTRACT_DECL(DECL)
359 #define DECL(CLASS, BASE) \
360 bool Traverse##CLASS##Decl(CLASS##Decl *D);
361 #include "clang/AST/DeclNodes.inc"
362 // The above header #undefs ABSTRACT_DECL and DECL upon exit.
363
364 // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
WalkUpFromDecl(Decl * D)365 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
VisitDecl(Decl * D)366 bool VisitDecl(Decl *D) { return true; }
367 #define DECL(CLASS, BASE) \
368 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \
369 TRY_TO(WalkUpFrom##BASE(D)); \
370 TRY_TO(Visit##CLASS##Decl(D)); \
371 return true; \
372 } \
373 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
374 #include "clang/AST/DeclNodes.inc"
375
376 private:
377 // These are helper methods used by more than one Traverse* method.
378 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
379 bool TraverseClassInstantiations(ClassTemplateDecl* D, Decl *Pattern);
380 bool TraverseFunctionInstantiations(FunctionTemplateDecl* D) ;
381 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
382 unsigned Count);
383 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
384 bool TraverseRecordHelper(RecordDecl *D);
385 bool TraverseCXXRecordHelper(CXXRecordDecl *D);
386 bool TraverseDeclaratorHelper(DeclaratorDecl *D);
387 bool TraverseDeclContextHelper(DeclContext *DC);
388 bool TraverseFunctionHelper(FunctionDecl *D);
389 bool TraverseVarHelper(VarDecl *D);
390 };
391
392 #define DISPATCH(NAME, CLASS, VAR) \
393 return getDerived().Traverse##NAME(static_cast<CLASS*>(VAR))
394
395 template<typename Derived>
TraverseStmt(Stmt * S)396 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S) {
397 if (!S)
398 return true;
399
400 // If we have a binary expr, dispatch to the subcode of the binop. A smart
401 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
402 // below.
403 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
404 switch (BinOp->getOpcode()) {
405 #define OPERATOR(NAME) \
406 case BO_##NAME: DISPATCH(Bin##NAME, BinaryOperator, S);
407
408 BINOP_LIST()
409 #undef OPERATOR
410 #undef BINOP_LIST
411
412 #define OPERATOR(NAME) \
413 case BO_##NAME##Assign: \
414 DISPATCH(Bin##NAME##Assign, CompoundAssignOperator, S);
415
416 CAO_LIST()
417 #undef OPERATOR
418 #undef CAO_LIST
419 }
420 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
421 switch (UnOp->getOpcode()) {
422 #define OPERATOR(NAME) \
423 case UO_##NAME: DISPATCH(Unary##NAME, UnaryOperator, S);
424
425 UNARYOP_LIST()
426 #undef OPERATOR
427 #undef UNARYOP_LIST
428 }
429 }
430
431 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
432 switch (S->getStmtClass()) {
433 case Stmt::NoStmtClass: break;
434 #define ABSTRACT_STMT(STMT)
435 #define STMT(CLASS, PARENT) \
436 case Stmt::CLASS##Class: DISPATCH(CLASS, CLASS, S);
437 #include "clang/AST/StmtNodes.inc"
438 }
439
440 return true;
441 }
442
443 template<typename Derived>
TraverseType(QualType T)444 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
445 if (T.isNull())
446 return true;
447
448 switch (T->getTypeClass()) {
449 #define ABSTRACT_TYPE(CLASS, BASE)
450 #define TYPE(CLASS, BASE) \
451 case Type::CLASS: DISPATCH(CLASS##Type, CLASS##Type, \
452 const_cast<Type*>(T.getTypePtr()));
453 #include "clang/AST/TypeNodes.def"
454 }
455
456 return true;
457 }
458
459 template<typename Derived>
TraverseTypeLoc(TypeLoc TL)460 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
461 if (TL.isNull())
462 return true;
463
464 switch (TL.getTypeLocClass()) {
465 #define ABSTRACT_TYPELOC(CLASS, BASE)
466 #define TYPELOC(CLASS, BASE) \
467 case TypeLoc::CLASS: \
468 return getDerived().Traverse##CLASS##TypeLoc(*cast<CLASS##TypeLoc>(&TL));
469 #include "clang/AST/TypeLocNodes.def"
470 }
471
472 return true;
473 }
474
475
476 template<typename Derived>
TraverseDecl(Decl * D)477 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
478 if (!D)
479 return true;
480
481 // As a syntax visitor, we want to ignore declarations for
482 // implicitly-defined declarations (ones not typed explicitly by the
483 // user).
484 if (D->isImplicit())
485 return true;
486
487 switch (D->getKind()) {
488 #define ABSTRACT_DECL(DECL)
489 #define DECL(CLASS, BASE) \
490 case Decl::CLASS: DISPATCH(CLASS##Decl, CLASS##Decl, D);
491 #include "clang/AST/DeclNodes.inc"
492 }
493
494 return true;
495 }
496
497 #undef DISPATCH
498
499 template<typename Derived>
TraverseNestedNameSpecifier(NestedNameSpecifier * NNS)500 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
501 NestedNameSpecifier *NNS) {
502 if (!NNS)
503 return true;
504
505 if (NNS->getPrefix())
506 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
507
508 switch (NNS->getKind()) {
509 case NestedNameSpecifier::Identifier:
510 case NestedNameSpecifier::Namespace:
511 case NestedNameSpecifier::NamespaceAlias:
512 case NestedNameSpecifier::Global:
513 return true;
514
515 case NestedNameSpecifier::TypeSpec:
516 case NestedNameSpecifier::TypeSpecWithTemplate:
517 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
518 }
519
520 return true;
521 }
522
523 template<typename Derived>
TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)524 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
525 NestedNameSpecifierLoc NNS) {
526 if (!NNS)
527 return true;
528
529 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
530 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
531
532 switch (NNS.getNestedNameSpecifier()->getKind()) {
533 case NestedNameSpecifier::Identifier:
534 case NestedNameSpecifier::Namespace:
535 case NestedNameSpecifier::NamespaceAlias:
536 case NestedNameSpecifier::Global:
537 return true;
538
539 case NestedNameSpecifier::TypeSpec:
540 case NestedNameSpecifier::TypeSpecWithTemplate:
541 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
542 break;
543 }
544
545 return true;
546 }
547
548 template<typename Derived>
TraverseTemplateName(TemplateName Template)549 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
550 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
551 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
552 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
553 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
554
555 return true;
556 }
557
558 template<typename Derived>
TraverseTemplateArgument(const TemplateArgument & Arg)559 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
560 const TemplateArgument &Arg) {
561 switch (Arg.getKind()) {
562 case TemplateArgument::Null:
563 case TemplateArgument::Declaration:
564 case TemplateArgument::Integral:
565 return true;
566
567 case TemplateArgument::Type:
568 return getDerived().TraverseType(Arg.getAsType());
569
570 case TemplateArgument::Template:
571 case TemplateArgument::TemplateExpansion:
572 return getDerived().TraverseTemplateName(
573 Arg.getAsTemplateOrTemplatePattern());
574
575 case TemplateArgument::Expression:
576 return getDerived().TraverseStmt(Arg.getAsExpr());
577
578 case TemplateArgument::Pack:
579 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
580 Arg.pack_size());
581 }
582
583 return true;
584 }
585
586 // FIXME: no template name location?
587 // FIXME: no source locations for a template argument pack?
588 template<typename Derived>
TraverseTemplateArgumentLoc(const TemplateArgumentLoc & ArgLoc)589 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
590 const TemplateArgumentLoc &ArgLoc) {
591 const TemplateArgument &Arg = ArgLoc.getArgument();
592
593 switch (Arg.getKind()) {
594 case TemplateArgument::Null:
595 case TemplateArgument::Declaration:
596 case TemplateArgument::Integral:
597 return true;
598
599 case TemplateArgument::Type: {
600 // FIXME: how can TSI ever be NULL?
601 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
602 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
603 else
604 return getDerived().TraverseType(Arg.getAsType());
605 }
606
607 case TemplateArgument::Template:
608 case TemplateArgument::TemplateExpansion:
609 if (ArgLoc.getTemplateQualifierLoc())
610 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
611 ArgLoc.getTemplateQualifierLoc()));
612 return getDerived().TraverseTemplateName(
613 Arg.getAsTemplateOrTemplatePattern());
614
615 case TemplateArgument::Expression:
616 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
617
618 case TemplateArgument::Pack:
619 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
620 Arg.pack_size());
621 }
622
623 return true;
624 }
625
626 template<typename Derived>
TraverseTemplateArguments(const TemplateArgument * Args,unsigned NumArgs)627 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
628 const TemplateArgument *Args,
629 unsigned NumArgs) {
630 for (unsigned I = 0; I != NumArgs; ++I) {
631 TRY_TO(TraverseTemplateArgument(Args[I]));
632 }
633
634 return true;
635 }
636
637 template<typename Derived>
TraverseConstructorInitializer(CXXCtorInitializer * Init)638 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
639 CXXCtorInitializer *Init) {
640 // FIXME: recurse on TypeLoc of the base initializer if isBaseInitializer()?
641 if (Init->isWritten())
642 TRY_TO(TraverseStmt(Init->getInit()));
643 return true;
644 }
645
646
647 // ----------------- Type traversal -----------------
648
649 // This macro makes available a variable T, the passed-in type.
650 #define DEF_TRAVERSE_TYPE(TYPE, CODE) \
651 template<typename Derived> \
652 bool RecursiveASTVisitor<Derived>::Traverse##TYPE (TYPE *T) { \
653 TRY_TO(WalkUpFrom##TYPE (T)); \
654 { CODE; } \
655 return true; \
656 }
657
658 DEF_TRAVERSE_TYPE(BuiltinType, { })
659
660 DEF_TRAVERSE_TYPE(ComplexType, {
661 TRY_TO(TraverseType(T->getElementType()));
662 })
663
664 DEF_TRAVERSE_TYPE(PointerType, {
665 TRY_TO(TraverseType(T->getPointeeType()));
666 })
667
668 DEF_TRAVERSE_TYPE(BlockPointerType, {
669 TRY_TO(TraverseType(T->getPointeeType()));
670 })
671
672 DEF_TRAVERSE_TYPE(LValueReferenceType, {
673 TRY_TO(TraverseType(T->getPointeeType()));
674 })
675
676 DEF_TRAVERSE_TYPE(RValueReferenceType, {
677 TRY_TO(TraverseType(T->getPointeeType()));
678 })
679
680 DEF_TRAVERSE_TYPE(MemberPointerType, {
681 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
682 TRY_TO(TraverseType(T->getPointeeType()));
683 })
684
685 DEF_TRAVERSE_TYPE(ConstantArrayType, {
686 TRY_TO(TraverseType(T->getElementType()));
687 })
688
689 DEF_TRAVERSE_TYPE(IncompleteArrayType, {
690 TRY_TO(TraverseType(T->getElementType()));
691 })
692
693 DEF_TRAVERSE_TYPE(VariableArrayType, {
694 TRY_TO(TraverseType(T->getElementType()));
695 TRY_TO(TraverseStmt(T->getSizeExpr()));
696 })
697
698 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
699 TRY_TO(TraverseType(T->getElementType()));
700 if (T->getSizeExpr())
701 TRY_TO(TraverseStmt(T->getSizeExpr()));
702 })
703
704 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
705 if (T->getSizeExpr())
706 TRY_TO(TraverseStmt(T->getSizeExpr()));
707 TRY_TO(TraverseType(T->getElementType()));
708 })
709
710 DEF_TRAVERSE_TYPE(VectorType, {
711 TRY_TO(TraverseType(T->getElementType()));
712 })
713
714 DEF_TRAVERSE_TYPE(ExtVectorType, {
715 TRY_TO(TraverseType(T->getElementType()));
716 })
717
718 DEF_TRAVERSE_TYPE(FunctionNoProtoType, {
719 TRY_TO(TraverseType(T->getResultType()));
720 })
721
722 DEF_TRAVERSE_TYPE(FunctionProtoType, {
723 TRY_TO(TraverseType(T->getResultType()));
724
725 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
726 AEnd = T->arg_type_end();
727 A != AEnd; ++A) {
728 TRY_TO(TraverseType(*A));
729 }
730
731 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
732 EEnd = T->exception_end();
733 E != EEnd; ++E) {
734 TRY_TO(TraverseType(*E));
735 }
736 })
737
738 DEF_TRAVERSE_TYPE(UnresolvedUsingType, { })
739 DEF_TRAVERSE_TYPE(TypedefType, { })
740
741 DEF_TRAVERSE_TYPE(TypeOfExprType, {
742 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
743 })
744
745 DEF_TRAVERSE_TYPE(TypeOfType, {
746 TRY_TO(TraverseType(T->getUnderlyingType()));
747 })
748
749 DEF_TRAVERSE_TYPE(DecltypeType, {
750 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
751 })
752
753 DEF_TRAVERSE_TYPE(UnaryTransformType, {
754 TRY_TO(TraverseType(T->getBaseType()));
755 TRY_TO(TraverseType(T->getUnderlyingType()));
756 })
757
758 DEF_TRAVERSE_TYPE(AutoType, {
759 TRY_TO(TraverseType(T->getDeducedType()));
760 })
761
762 DEF_TRAVERSE_TYPE(RecordType, { })
763 DEF_TRAVERSE_TYPE(EnumType, { })
764 DEF_TRAVERSE_TYPE(TemplateTypeParmType, { })
765 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, { })
766 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, { })
767
768 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
769 TRY_TO(TraverseTemplateName(T->getTemplateName()));
770 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
771 })
772
773 DEF_TRAVERSE_TYPE(InjectedClassNameType, { })
774
775 DEF_TRAVERSE_TYPE(AttributedType, {
776 TRY_TO(TraverseType(T->getModifiedType()));
777 })
778
779 DEF_TRAVERSE_TYPE(ParenType, {
780 TRY_TO(TraverseType(T->getInnerType()));
781 })
782
783 DEF_TRAVERSE_TYPE(ElaboratedType, {
784 if (T->getQualifier()) {
785 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
786 }
787 TRY_TO(TraverseType(T->getNamedType()));
788 })
789
790 DEF_TRAVERSE_TYPE(DependentNameType, {
791 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
792 })
793
794 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
795 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
796 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
797 })
798
799 DEF_TRAVERSE_TYPE(PackExpansionType, {
800 TRY_TO(TraverseType(T->getPattern()));
801 })
802
803 DEF_TRAVERSE_TYPE(ObjCInterfaceType, { })
804
805 DEF_TRAVERSE_TYPE(ObjCObjectType, {
806 // We have to watch out here because an ObjCInterfaceType's base
807 // type is itself.
808 if (T->getBaseType().getTypePtr() != T)
809 TRY_TO(TraverseType(T->getBaseType()));
810 })
811
812 DEF_TRAVERSE_TYPE(ObjCObjectPointerType, {
813 TRY_TO(TraverseType(T->getPointeeType()));
814 })
815
816 #undef DEF_TRAVERSE_TYPE
817
818 // ----------------- TypeLoc traversal -----------------
819
820 // This macro makes available a variable TL, the passed-in TypeLoc.
821 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
822 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
823 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
824 // continue to work.
825 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
826 template<typename Derived> \
827 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
828 if (getDerived().shouldWalkTypesOfTypeLocs()) \
829 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE*>(TL.getTypePtr()))); \
830 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
831 { CODE; } \
832 return true; \
833 }
834
835 template<typename Derived>
TraverseQualifiedTypeLoc(QualifiedTypeLoc TL)836 bool RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(
837 QualifiedTypeLoc TL) {
838 // Move this over to the 'main' typeloc tree. Note that this is a
839 // move -- we pretend that we were really looking at the unqualified
840 // typeloc all along -- rather than a recursion, so we don't follow
841 // the normal CRTP plan of going through
842 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
843 // twice for the same type (once as a QualifiedTypeLoc version of
844 // the type, once as an UnqualifiedTypeLoc version of the type),
845 // which in effect means we'd call VisitTypeLoc twice with the
846 // 'same' type. This solves that problem, at the cost of never
847 // seeing the qualified version of the type (unless the client
848 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
849 // perfect solution. A perfect solution probably requires making
850 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
851 // wrapper around Type* -- rather than being its own class in the
852 // type hierarchy.
853 return TraverseTypeLoc(TL.getUnqualifiedLoc());
854 }
855
856 DEF_TRAVERSE_TYPELOC(BuiltinType, { })
857
858 // FIXME: ComplexTypeLoc is unfinished
859 DEF_TRAVERSE_TYPELOC(ComplexType, {
860 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
861 })
862
863 DEF_TRAVERSE_TYPELOC(PointerType, {
864 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
865 })
866
867 DEF_TRAVERSE_TYPELOC(BlockPointerType, {
868 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
869 })
870
871 DEF_TRAVERSE_TYPELOC(LValueReferenceType, {
872 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
873 })
874
875 DEF_TRAVERSE_TYPELOC(RValueReferenceType, {
876 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
877 })
878
879 // FIXME: location of base class?
880 // We traverse this in the type case as well, but how is it not reached through
881 // the pointee type?
882 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
883 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
884 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
885 })
886
887 template<typename Derived>
TraverseArrayTypeLocHelper(ArrayTypeLoc TL)888 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
889 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
890 TRY_TO(TraverseStmt(TL.getSizeExpr()));
891 return true;
892 }
893
894 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
895 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
896 return TraverseArrayTypeLocHelper(TL);
897 })
898
899 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
900 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
901 return TraverseArrayTypeLocHelper(TL);
902 })
903
904 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
905 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
906 return TraverseArrayTypeLocHelper(TL);
907 })
908
909 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
910 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
911 return TraverseArrayTypeLocHelper(TL);
912 })
913
914 // FIXME: order? why not size expr first?
915 // FIXME: base VectorTypeLoc is unfinished
916 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
917 if (TL.getTypePtr()->getSizeExpr())
918 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
919 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
920 })
921
922 // FIXME: VectorTypeLoc is unfinished
923 DEF_TRAVERSE_TYPELOC(VectorType, {
924 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
925 })
926
927 // FIXME: size and attributes
928 // FIXME: base VectorTypeLoc is unfinished
929 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
930 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
931 })
932
933 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, {
934 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
935 })
936
937 // FIXME: location of exception specifications (attributes?)
938 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
939 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
940
941 const FunctionProtoType *T = TL.getTypePtr();
942
943 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
944 if (TL.getArg(I)) {
945 TRY_TO(TraverseDecl(TL.getArg(I)));
946 } else if (I < T->getNumArgs()) {
947 TRY_TO(TraverseType(T->getArgType(I)));
948 }
949 }
950
951 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
952 EEnd = T->exception_end();
953 E != EEnd; ++E) {
954 TRY_TO(TraverseType(*E));
955 }
956 })
957
958 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, { })
959 DEF_TRAVERSE_TYPELOC(TypedefType, { })
960
961 DEF_TRAVERSE_TYPELOC(TypeOfExprType, {
962 TRY_TO(TraverseStmt(TL.getUnderlyingExpr()));
963 })
964
965 DEF_TRAVERSE_TYPELOC(TypeOfType, {
966 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
967 })
968
969 // FIXME: location of underlying expr
970 DEF_TRAVERSE_TYPELOC(DecltypeType, {
971 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
972 })
973
974 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
975 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
976 })
977
978 DEF_TRAVERSE_TYPELOC(AutoType, {
979 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
980 })
981
982 DEF_TRAVERSE_TYPELOC(RecordType, { })
983 DEF_TRAVERSE_TYPELOC(EnumType, { })
984 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, { })
985 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, { })
986 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, { })
987
988 // FIXME: use the loc for the template name?
989 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
990 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
991 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
992 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
993 }
994 })
995
996 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, { })
997
998 DEF_TRAVERSE_TYPELOC(ParenType, {
999 TRY_TO(TraverseTypeLoc(TL.getInnerLoc()));
1000 })
1001
1002 DEF_TRAVERSE_TYPELOC(AttributedType, {
1003 TRY_TO(TraverseTypeLoc(TL.getModifiedLoc()));
1004 })
1005
1006 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1007 if (TL.getQualifierLoc()) {
1008 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1009 }
1010 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1011 })
1012
1013 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1014 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1015 })
1016
1017 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1018 if (TL.getQualifierLoc()) {
1019 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1020 }
1021
1022 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1023 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1024 }
1025 })
1026
1027 DEF_TRAVERSE_TYPELOC(PackExpansionType, {
1028 TRY_TO(TraverseTypeLoc(TL.getPatternLoc()));
1029 })
1030
1031 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, { })
1032
1033 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1034 // We have to watch out here because an ObjCInterfaceType's base
1035 // type is itself.
1036 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1037 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1038 })
1039
1040 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, {
1041 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1042 })
1043
1044 #undef DEF_TRAVERSE_TYPELOC
1045
1046 // ----------------- Decl traversal -----------------
1047 //
1048 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1049 // the children that come from the DeclContext associated with it.
1050 // Therefore each Traverse* only needs to worry about children other
1051 // than those.
1052
1053 template<typename Derived>
TraverseDeclContextHelper(DeclContext * DC)1054 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1055 if (!DC)
1056 return true;
1057
1058 for (DeclContext::decl_iterator Child = DC->decls_begin(),
1059 ChildEnd = DC->decls_end();
1060 Child != ChildEnd; ++Child) {
1061 // BlockDecls are traversed through BlockExprs.
1062 if (!isa<BlockDecl>(*Child))
1063 TRY_TO(TraverseDecl(*Child));
1064 }
1065
1066 return true;
1067 }
1068
1069 // This macro makes available a variable D, the passed-in decl.
1070 #define DEF_TRAVERSE_DECL(DECL, CODE) \
1071 template<typename Derived> \
1072 bool RecursiveASTVisitor<Derived>::Traverse##DECL (DECL *D) { \
1073 TRY_TO(WalkUpFrom##DECL (D)); \
1074 { CODE; } \
1075 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1076 return true; \
1077 }
1078
1079 DEF_TRAVERSE_DECL(AccessSpecDecl, { })
1080
1081 DEF_TRAVERSE_DECL(BlockDecl, {
1082 TRY_TO(TraverseTypeLoc(D->getSignatureAsWritten()->getTypeLoc()));
1083 TRY_TO(TraverseStmt(D->getBody()));
1084 // This return statement makes sure the traversal of nodes in
1085 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1086 // is skipped - don't remove it.
1087 return true;
1088 })
1089
1090 DEF_TRAVERSE_DECL(FileScopeAsmDecl, {
1091 TRY_TO(TraverseStmt(D->getAsmString()));
1092 })
1093
1094 DEF_TRAVERSE_DECL(FriendDecl, {
1095 // Friend is either decl or a type.
1096 if (D->getFriendType())
1097 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1098 else
1099 TRY_TO(TraverseDecl(D->getFriendDecl()));
1100 })
1101
1102 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1103 if (D->getFriendType())
1104 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1105 else
1106 TRY_TO(TraverseDecl(D->getFriendDecl()));
1107 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1108 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1109 for (TemplateParameterList::iterator ITPL = TPL->begin(),
1110 ETPL = TPL->end();
1111 ITPL != ETPL; ++ITPL) {
1112 TRY_TO(TraverseDecl(*ITPL));
1113 }
1114 }
1115 })
1116
1117 DEF_TRAVERSE_DECL(LinkageSpecDecl, { })
1118
1119 DEF_TRAVERSE_DECL(ObjCClassDecl, {
1120 // FIXME: implement this
1121 })
1122
1123 DEF_TRAVERSE_DECL(ObjCForwardProtocolDecl, {
1124 // FIXME: implement this
1125 })
1126
1127 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {
1128 // FIXME: implement this
1129 })
1130
1131 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1132 TRY_TO(TraverseStmt(D->getAssertExpr()));
1133 TRY_TO(TraverseStmt(D->getMessage()));
1134 })
1135
1136 DEF_TRAVERSE_DECL(TranslationUnitDecl, {
1137 // Code in an unnamed namespace shows up automatically in
1138 // decls_begin()/decls_end(). Thus we don't need to recurse on
1139 // D->getAnonymousNamespace().
1140 })
1141
1142 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1143 // We shouldn't traverse an aliased namespace, since it will be
1144 // defined (and, therefore, traversed) somewhere else.
1145 //
1146 // This return statement makes sure the traversal of nodes in
1147 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1148 // is skipped - don't remove it.
1149 return true;
1150 })
1151
1152 DEF_TRAVERSE_DECL(LabelDecl, {
1153 // There is no code in a LabelDecl.
1154 })
1155
1156
1157 DEF_TRAVERSE_DECL(NamespaceDecl, {
1158 // Code in an unnamed namespace shows up automatically in
1159 // decls_begin()/decls_end(). Thus we don't need to recurse on
1160 // D->getAnonymousNamespace().
1161 })
1162
1163 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {
1164 // FIXME: implement
1165 })
1166
1167 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {
1168 // FIXME: implement
1169 })
1170
1171 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {
1172 // FIXME: implement
1173 })
1174
1175 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {
1176 // FIXME: implement
1177 })
1178
1179 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {
1180 // FIXME: implement
1181 })
1182
1183 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {
1184 // FIXME: implement
1185 })
1186
1187 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1188 if (D->getResultTypeSourceInfo()) {
1189 TRY_TO(TraverseTypeLoc(D->getResultTypeSourceInfo()->getTypeLoc()));
1190 }
1191 for (ObjCMethodDecl::param_iterator
1192 I = D->param_begin(), E = D->param_end(); I != E; ++I) {
1193 TRY_TO(TraverseDecl(*I));
1194 }
1195 if (D->isThisDeclarationADefinition()) {
1196 TRY_TO(TraverseStmt(D->getBody()));
1197 }
1198 return true;
1199 })
1200
1201 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1202 // FIXME: implement
1203 })
1204
1205 DEF_TRAVERSE_DECL(UsingDecl, {
1206 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1207 })
1208
1209 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1210 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1211 })
1212
1213 DEF_TRAVERSE_DECL(UsingShadowDecl, { })
1214
1215 // A helper method for TemplateDecl's children.
1216 template<typename Derived>
TraverseTemplateParameterListHelper(TemplateParameterList * TPL)1217 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1218 TemplateParameterList *TPL) {
1219 if (TPL) {
1220 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1221 I != E; ++I) {
1222 TRY_TO(TraverseDecl(*I));
1223 }
1224 }
1225 return true;
1226 }
1227
1228 // A helper method for traversing the implicit instantiations of a
1229 // class.
1230 template<typename Derived>
TraverseClassInstantiations(ClassTemplateDecl * D,Decl * Pattern)1231 bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations(
1232 ClassTemplateDecl* D, Decl *Pattern) {
1233 assert(isa<ClassTemplateDecl>(Pattern) ||
1234 isa<ClassTemplatePartialSpecializationDecl>(Pattern));
1235
1236 ClassTemplateDecl::spec_iterator end = D->spec_end();
1237 for (ClassTemplateDecl::spec_iterator it = D->spec_begin(); it != end; ++it) {
1238 ClassTemplateSpecializationDecl* SD = *it;
1239
1240 switch (SD->getSpecializationKind()) {
1241 // Visit the implicit instantiations with the requested pattern.
1242 case TSK_ImplicitInstantiation: {
1243 llvm::PointerUnion<ClassTemplateDecl *,
1244 ClassTemplatePartialSpecializationDecl *> U
1245 = SD->getInstantiatedFrom();
1246
1247 bool ShouldVisit;
1248 if (U.is<ClassTemplateDecl*>())
1249 ShouldVisit = (U.get<ClassTemplateDecl*>() == Pattern);
1250 else
1251 ShouldVisit
1252 = (U.get<ClassTemplatePartialSpecializationDecl*>() == Pattern);
1253
1254 if (ShouldVisit)
1255 TRY_TO(TraverseClassTemplateSpecializationDecl(SD));
1256 break;
1257 }
1258
1259 // We don't need to do anything on an explicit instantiation
1260 // or explicit specialization because there will be an explicit
1261 // node for it elsewhere.
1262 case TSK_ExplicitInstantiationDeclaration:
1263 case TSK_ExplicitInstantiationDefinition:
1264 case TSK_ExplicitSpecialization:
1265 break;
1266
1267 // We don't need to do anything for an uninstantiated
1268 // specialization.
1269 case TSK_Undeclared:
1270 break;
1271 }
1272 }
1273
1274 return true;
1275 }
1276
1277 DEF_TRAVERSE_DECL(ClassTemplateDecl, {
1278 CXXRecordDecl* TempDecl = D->getTemplatedDecl();
1279 TRY_TO(TraverseDecl(TempDecl));
1280 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1281
1282 // By default, we do not traverse the instantiations of
1283 // class templates since they do not apprear in the user code. The
1284 // following code optionally traverses them.
1285 if (getDerived().shouldVisitTemplateInstantiations()) {
1286 // If this is the definition of the primary template, visit
1287 // instantiations which were formed from this pattern.
1288 if (D->isThisDeclarationADefinition())
1289 TRY_TO(TraverseClassInstantiations(D, D));
1290 }
1291
1292 // Note that getInstantiatedFromMemberTemplate() is just a link
1293 // from a template instantiation back to the template from which
1294 // it was instantiated, and thus should not be traversed.
1295 })
1296
1297 // A helper method for traversing the instantiations of a
1298 // function while skipping its specializations.
1299 template<typename Derived>
TraverseFunctionInstantiations(FunctionTemplateDecl * D)1300 bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations(
1301 FunctionTemplateDecl* D) {
1302 FunctionTemplateDecl::spec_iterator end = D->spec_end();
1303 for (FunctionTemplateDecl::spec_iterator it = D->spec_begin(); it != end; ++it) {
1304 FunctionDecl* FD = *it;
1305 switch (FD->getTemplateSpecializationKind()) {
1306 case TSK_ImplicitInstantiation:
1307 // We don't know what kind of FunctionDecl this is.
1308 TRY_TO(TraverseDecl(FD));
1309 break;
1310
1311 // No need to visit explicit instantiations, we'll find the node
1312 // eventually.
1313 case TSK_ExplicitInstantiationDeclaration:
1314 case TSK_ExplicitInstantiationDefinition:
1315 break;
1316
1317 case TSK_Undeclared: // Declaration of the template definition.
1318 case TSK_ExplicitSpecialization:
1319 break;
1320 default:
1321 assert(false && "Unknown specialization kind.");
1322 }
1323 }
1324
1325 return true;
1326 }
1327
1328 DEF_TRAVERSE_DECL(FunctionTemplateDecl, {
1329 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1330 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1331
1332 // By default, we do not traverse the instantiations of
1333 // function templates since they do not apprear in the user code. The
1334 // following code optionally traverses them.
1335 if (getDerived().shouldVisitTemplateInstantiations()) {
1336 // Explicit function specializations will be traversed from the
1337 // context of their declaration. There is therefore no need to
1338 // traverse them for here.
1339 //
1340 // In addition, we only traverse the function instantiations when
1341 // the function template is a function template definition.
1342 if (D->isThisDeclarationADefinition()) {
1343 TRY_TO(TraverseFunctionInstantiations(D));
1344 }
1345 }
1346 })
1347
1348 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1349 // D is the "T" in something like
1350 // template <template <typename> class T> class container { };
1351 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1352 if (D->hasDefaultArgument()) {
1353 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1354 }
1355 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1356 })
1357
1358 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1359 // D is the "T" in something like "template<typename T> class vector;"
1360 if (D->getTypeForDecl())
1361 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1362 if (D->hasDefaultArgument())
1363 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1364 })
1365
1366 DEF_TRAVERSE_DECL(TypedefDecl, {
1367 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1368 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1369 // declaring the typedef, not something that was written in the
1370 // source.
1371 })
1372
1373 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1374 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1375 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1376 // declaring the type alias, not something that was written in the
1377 // source.
1378 })
1379
1380 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1381 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1382 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1383 })
1384
1385 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1386 // A dependent using declaration which was marked with 'typename'.
1387 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1388 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1389 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1390 // declaring the type, not something that was written in the
1391 // source.
1392 })
1393
1394 DEF_TRAVERSE_DECL(EnumDecl, {
1395 if (D->getTypeForDecl())
1396 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1397
1398 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1399 // The enumerators are already traversed by
1400 // decls_begin()/decls_end().
1401 })
1402
1403
1404 // Helper methods for RecordDecl and its children.
1405 template<typename Derived>
TraverseRecordHelper(RecordDecl * D)1406 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(
1407 RecordDecl *D) {
1408 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1409 // declaring the type, not something that was written in the source.
1410
1411 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1412 return true;
1413 }
1414
1415 template<typename Derived>
TraverseCXXRecordHelper(CXXRecordDecl * D)1416 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(
1417 CXXRecordDecl *D) {
1418 if (!TraverseRecordHelper(D))
1419 return false;
1420 if (D->hasDefinition()) {
1421 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1422 E = D->bases_end();
1423 I != E; ++I) {
1424 TRY_TO(TraverseTypeLoc(I->getTypeSourceInfo()->getTypeLoc()));
1425 }
1426 // We don't traverse the friends or the conversions, as they are
1427 // already in decls_begin()/decls_end().
1428 }
1429 return true;
1430 }
1431
1432 DEF_TRAVERSE_DECL(RecordDecl, {
1433 TRY_TO(TraverseRecordHelper(D));
1434 })
1435
1436 DEF_TRAVERSE_DECL(CXXRecordDecl, {
1437 TRY_TO(TraverseCXXRecordHelper(D));
1438 })
1439
1440 DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, {
1441 // For implicit instantiations ("set<int> x;"), we don't want to
1442 // recurse at all, since the instatiated class isn't written in
1443 // the source code anywhere. (Note the instatiated *type* --
1444 // set<int> -- is written, and will still get a callback of
1445 // TemplateSpecializationType). For explicit instantiations
1446 // ("template set<int>;"), we do need a callback, since this
1447 // is the only callback that's made for this instantiation.
1448 // We use getTypeAsWritten() to distinguish.
1449 if (TypeSourceInfo *TSI = D->getTypeAsWritten())
1450 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1451
1452 if (!getDerived().shouldVisitTemplateInstantiations() &&
1453 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1454 // Returning from here skips traversing the
1455 // declaration context of the ClassTemplateSpecializationDecl
1456 // (embedded in the DEF_TRAVERSE_DECL() macro)
1457 // which contains the instantiated members of the class.
1458 return true;
1459 })
1460
1461 template <typename Derived>
TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc * TAL,unsigned Count)1462 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1463 const TemplateArgumentLoc *TAL, unsigned Count) {
1464 for (unsigned I = 0; I < Count; ++I) {
1465 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1466 }
1467 return true;
1468 }
1469
1470 DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, {
1471 // The partial specialization.
1472 if (TemplateParameterList *TPL = D->getTemplateParameters()) {
1473 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1474 I != E; ++I) {
1475 TRY_TO(TraverseDecl(*I));
1476 }
1477 }
1478 // The args that remains unspecialized.
1479 TRY_TO(TraverseTemplateArgumentLocsHelper(
1480 D->getTemplateArgsAsWritten(), D->getNumTemplateArgsAsWritten()));
1481
1482 // Don't need the ClassTemplatePartialSpecializationHelper, even
1483 // though that's our parent class -- we already visit all the
1484 // template args here.
1485 TRY_TO(TraverseCXXRecordHelper(D));
1486
1487 // If we're visiting instantiations, visit the instantiations of
1488 // this template now.
1489 if (getDerived().shouldVisitTemplateInstantiations() &&
1490 D->isThisDeclarationADefinition())
1491 TRY_TO(TraverseClassInstantiations(D->getSpecializedTemplate(), D));
1492 })
1493
1494 DEF_TRAVERSE_DECL(EnumConstantDecl, {
1495 TRY_TO(TraverseStmt(D->getInitExpr()));
1496 })
1497
1498 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1499 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1500 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1501 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1502 })
1503
1504 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1505
1506 template<typename Derived>
TraverseDeclaratorHelper(DeclaratorDecl * D)1507 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1508 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1509 if (D->getTypeSourceInfo())
1510 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1511 else
1512 TRY_TO(TraverseType(D->getType()));
1513 return true;
1514 }
1515
1516 DEF_TRAVERSE_DECL(FieldDecl, {
1517 TRY_TO(TraverseDeclaratorHelper(D));
1518 if (D->isBitField())
1519 TRY_TO(TraverseStmt(D->getBitWidth()));
1520 })
1521
1522 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1523 TRY_TO(TraverseDeclaratorHelper(D));
1524 if (D->isBitField())
1525 TRY_TO(TraverseStmt(D->getBitWidth()));
1526 // FIXME: implement the rest.
1527 })
1528
1529 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1530 TRY_TO(TraverseDeclaratorHelper(D));
1531 if (D->isBitField())
1532 TRY_TO(TraverseStmt(D->getBitWidth()));
1533 // FIXME: implement the rest.
1534 })
1535
1536 template<typename Derived>
TraverseFunctionHelper(FunctionDecl * D)1537 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1538 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1539
1540 // If we're an explicit template specialization, iterate over the
1541 // template args that were explicitly specified. If we were doing
1542 // this in typing order, we'd do it between the return type and
1543 // the function args, but both are handled by the FunctionTypeLoc
1544 // above, so we have to choose one side. I've decided to do before.
1545 if (const FunctionTemplateSpecializationInfo *FTSI =
1546 D->getTemplateSpecializationInfo()) {
1547 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1548 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1549 // A specialization might not have explicit template arguments if it has
1550 // a templated return type and concrete arguments.
1551 if (const TemplateArgumentListInfo *TALI =
1552 FTSI->TemplateArgumentsAsWritten) {
1553 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getArgumentArray(),
1554 TALI->size()));
1555 }
1556 }
1557 }
1558
1559 // Visit the function type itself, which can be either
1560 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1561 // also covers the return type and the function parameters,
1562 // including exception specifications.
1563 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1564
1565 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1566 // Constructor initializers.
1567 for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
1568 E = Ctor->init_end();
1569 I != E; ++I) {
1570 TRY_TO(TraverseConstructorInitializer(*I));
1571 }
1572 }
1573
1574 if (D->isThisDeclarationADefinition()) {
1575 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1576 }
1577 return true;
1578 }
1579
1580 DEF_TRAVERSE_DECL(FunctionDecl, {
1581 // We skip decls_begin/decls_end, which are already covered by
1582 // TraverseFunctionHelper().
1583 return TraverseFunctionHelper(D);
1584 })
1585
1586 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1587 // We skip decls_begin/decls_end, which are already covered by
1588 // TraverseFunctionHelper().
1589 return TraverseFunctionHelper(D);
1590 })
1591
1592 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1593 // We skip decls_begin/decls_end, which are already covered by
1594 // TraverseFunctionHelper().
1595 return TraverseFunctionHelper(D);
1596 })
1597
1598 // CXXConversionDecl is the declaration of a type conversion operator.
1599 // It's not a cast expression.
1600 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1601 // We skip decls_begin/decls_end, which are already covered by
1602 // TraverseFunctionHelper().
1603 return TraverseFunctionHelper(D);
1604 })
1605
1606 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1607 // We skip decls_begin/decls_end, which are already covered by
1608 // TraverseFunctionHelper().
1609 return TraverseFunctionHelper(D);
1610 })
1611
1612 template<typename Derived>
TraverseVarHelper(VarDecl * D)1613 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1614 TRY_TO(TraverseDeclaratorHelper(D));
1615 TRY_TO(TraverseStmt(D->getInit()));
1616 return true;
1617 }
1618
1619 DEF_TRAVERSE_DECL(VarDecl, {
1620 TRY_TO(TraverseVarHelper(D));
1621 })
1622
1623 DEF_TRAVERSE_DECL(ImplicitParamDecl, {
1624 TRY_TO(TraverseVarHelper(D));
1625 })
1626
1627 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1628 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1629 TRY_TO(TraverseDeclaratorHelper(D));
1630 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1631 })
1632
1633 DEF_TRAVERSE_DECL(ParmVarDecl, {
1634 TRY_TO(TraverseVarHelper(D));
1635
1636 if (D->hasDefaultArg() &&
1637 D->hasUninstantiatedDefaultArg() &&
1638 !D->hasUnparsedDefaultArg())
1639 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1640
1641 if (D->hasDefaultArg() &&
1642 !D->hasUninstantiatedDefaultArg() &&
1643 !D->hasUnparsedDefaultArg())
1644 TRY_TO(TraverseStmt(D->getDefaultArg()));
1645 })
1646
1647 #undef DEF_TRAVERSE_DECL
1648
1649 // ----------------- Stmt traversal -----------------
1650 //
1651 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1652 // over the children defined in children() (every stmt defines these,
1653 // though sometimes the range is empty). Each individual Traverse*
1654 // method only needs to worry about children other than those. To see
1655 // what children() does for a given class, see, e.g.,
1656 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1657
1658 // This macro makes available a variable S, the passed-in stmt.
1659 #define DEF_TRAVERSE_STMT(STMT, CODE) \
1660 template<typename Derived> \
1661 bool RecursiveASTVisitor<Derived>::Traverse##STMT (STMT *S) { \
1662 TRY_TO(WalkUpFrom##STMT(S)); \
1663 { CODE; } \
1664 for (Stmt::child_range range = S->children(); range; ++range) { \
1665 TRY_TO(TraverseStmt(*range)); \
1666 } \
1667 return true; \
1668 }
1669
1670 DEF_TRAVERSE_STMT(AsmStmt, {
1671 TRY_TO(TraverseStmt(S->getAsmString()));
1672 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1673 TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1674 }
1675 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1676 TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1677 }
1678 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1679 TRY_TO(TraverseStmt(S->getClobber(I)));
1680 }
1681 // children() iterates over inputExpr and outputExpr.
1682 })
1683
1684 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1685 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1686 // children() iterates over the handler block.
1687 })
1688
1689 DEF_TRAVERSE_STMT(DeclStmt, {
1690 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
1691 I != E; ++I) {
1692 TRY_TO(TraverseDecl(*I));
1693 }
1694 // Suppress the default iteration over children() by
1695 // returning. Here's why: A DeclStmt looks like 'type var [=
1696 // initializer]'. The decls above already traverse over the
1697 // initializers, so we don't have to do it again (which
1698 // children() would do).
1699 return true;
1700 })
1701
1702
1703 // These non-expr stmts (most of them), do not need any action except
1704 // iterating over the children.
1705 DEF_TRAVERSE_STMT(BreakStmt, { })
1706 DEF_TRAVERSE_STMT(CXXTryStmt, { })
1707 DEF_TRAVERSE_STMT(CaseStmt, { })
1708 DEF_TRAVERSE_STMT(CompoundStmt, { })
1709 DEF_TRAVERSE_STMT(ContinueStmt, { })
1710 DEF_TRAVERSE_STMT(DefaultStmt, { })
1711 DEF_TRAVERSE_STMT(DoStmt, { })
1712 DEF_TRAVERSE_STMT(ForStmt, { })
1713 DEF_TRAVERSE_STMT(GotoStmt, { })
1714 DEF_TRAVERSE_STMT(IfStmt, { })
1715 DEF_TRAVERSE_STMT(IndirectGotoStmt, { })
1716 DEF_TRAVERSE_STMT(LabelStmt, { })
1717 DEF_TRAVERSE_STMT(NullStmt, { })
1718 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, { })
1719 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, { })
1720 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, { })
1721 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, { })
1722 DEF_TRAVERSE_STMT(ObjCAtTryStmt, { })
1723 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, { })
1724 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, { })
1725 DEF_TRAVERSE_STMT(CXXForRangeStmt, { })
1726 DEF_TRAVERSE_STMT(ReturnStmt, { })
1727 DEF_TRAVERSE_STMT(SwitchStmt, { })
1728 DEF_TRAVERSE_STMT(WhileStmt, { })
1729
1730
1731 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1732 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1733 if (S->hasExplicitTemplateArgs()) {
1734 TRY_TO(TraverseTemplateArgumentLocsHelper(
1735 S->getTemplateArgs(), S->getNumTemplateArgs()));
1736 }
1737 })
1738
1739 DEF_TRAVERSE_STMT(DeclRefExpr, {
1740 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1741 TRY_TO(TraverseTemplateArgumentLocsHelper(
1742 S->getTemplateArgs(), S->getNumTemplateArgs()));
1743 })
1744
1745 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1746 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1747 if (S->hasExplicitTemplateArgs()) {
1748 TRY_TO(TraverseTemplateArgumentLocsHelper(
1749 S->getExplicitTemplateArgs().getTemplateArgs(),
1750 S->getNumTemplateArgs()));
1751 }
1752 })
1753
1754 DEF_TRAVERSE_STMT(MemberExpr, {
1755 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1756 TRY_TO(TraverseTemplateArgumentLocsHelper(
1757 S->getTemplateArgs(), S->getNumTemplateArgs()));
1758 })
1759
1760 DEF_TRAVERSE_STMT(ImplicitCastExpr, {
1761 // We don't traverse the cast type, as it's not written in the
1762 // source code.
1763 })
1764
1765 DEF_TRAVERSE_STMT(CStyleCastExpr, {
1766 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1767 })
1768
1769 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
1770 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1771 })
1772
1773 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
1774 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1775 })
1776
1777 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
1778 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1779 })
1780
1781 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
1782 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1783 })
1784
1785 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
1786 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1787 })
1788
1789 // InitListExpr is a tricky one, because we want to do all our work on
1790 // the syntactic form of the listexpr, but this method takes the
1791 // semantic form by default. We can't use the macro helper because it
1792 // calls WalkUp*() on the semantic form, before our code can convert
1793 // to the syntactic form.
1794 template<typename Derived>
TraverseInitListExpr(InitListExpr * S)1795 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
1796 if (InitListExpr *Syn = S->getSyntacticForm())
1797 S = Syn;
1798 TRY_TO(WalkUpFromInitListExpr(S));
1799 // All we need are the default actions. FIXME: use a helper function.
1800 for (Stmt::child_range range = S->children(); range; ++range) {
1801 TRY_TO(TraverseStmt(*range));
1802 }
1803 return true;
1804 }
1805
1806 // GenericSelectionExpr is a special case because the types and expressions
1807 // are interleaved. We also need to watch out for null types (default
1808 // generic associations).
1809 template<typename Derived>
1810 bool RecursiveASTVisitor<Derived>::
TraverseGenericSelectionExpr(GenericSelectionExpr * S)1811 TraverseGenericSelectionExpr(GenericSelectionExpr *S) {
1812 TRY_TO(WalkUpFromGenericSelectionExpr(S));
1813 TRY_TO(TraverseStmt(S->getControllingExpr()));
1814 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
1815 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
1816 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
1817 TRY_TO(TraverseStmt(S->getAssocExpr(i)));
1818 }
1819 return true;
1820 }
1821
1822 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
1823 // This is called for code like 'return T()' where T is a built-in
1824 // (i.e. non-class) type.
1825 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1826 })
1827
1828 DEF_TRAVERSE_STMT(CXXNewExpr, {
1829 // The child-iterator will pick up the other arguments.
1830 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
1831 })
1832
1833 DEF_TRAVERSE_STMT(OffsetOfExpr, {
1834 // The child-iterator will pick up the expression representing
1835 // the field.
1836 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
1837 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
1838 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1839 })
1840
1841 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
1842 // The child-iterator will pick up the arg if it's an expression,
1843 // but not if it's a type.
1844 if (S->isArgumentType())
1845 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
1846 })
1847
1848 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
1849 // The child-iterator will pick up the arg if it's an expression,
1850 // but not if it's a type.
1851 if (S->isTypeOperand())
1852 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
1853 })
1854
1855 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
1856 // The child-iterator will pick up the arg if it's an expression,
1857 // but not if it's a type.
1858 if (S->isTypeOperand())
1859 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
1860 })
1861
1862 DEF_TRAVERSE_STMT(UnaryTypeTraitExpr, {
1863 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
1864 })
1865
1866 DEF_TRAVERSE_STMT(BinaryTypeTraitExpr, {
1867 TRY_TO(TraverseTypeLoc(S->getLhsTypeSourceInfo()->getTypeLoc()));
1868 TRY_TO(TraverseTypeLoc(S->getRhsTypeSourceInfo()->getTypeLoc()));
1869 })
1870
1871 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
1872 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
1873 })
1874
1875 DEF_TRAVERSE_STMT(ExpressionTraitExpr, {
1876 TRY_TO(TraverseStmt(S->getQueriedExpression()));
1877 })
1878
1879 DEF_TRAVERSE_STMT(VAArgExpr, {
1880 // The child-iterator will pick up the expression argument.
1881 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
1882 })
1883
1884 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
1885 // This is called for code like 'return T()' where T is a class type.
1886 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1887 })
1888
1889 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
1890 // This is called for code like 'T()', where T is a template argument.
1891 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1892 })
1893
1894 // These expressions all might take explicit template arguments.
1895 // We traverse those if so. FIXME: implement these.
1896 DEF_TRAVERSE_STMT(CXXConstructExpr, { })
1897 DEF_TRAVERSE_STMT(CallExpr, { })
1898 DEF_TRAVERSE_STMT(CXXMemberCallExpr, { })
1899
1900 // These exprs (most of them), do not need any action except iterating
1901 // over the children.
1902 DEF_TRAVERSE_STMT(AddrLabelExpr, { })
1903 DEF_TRAVERSE_STMT(ArraySubscriptExpr, { })
1904 DEF_TRAVERSE_STMT(BlockDeclRefExpr, { })
1905 DEF_TRAVERSE_STMT(BlockExpr, {
1906 TRY_TO(TraverseDecl(S->getBlockDecl()));
1907 return true; // no child statements to loop through.
1908 })
1909 DEF_TRAVERSE_STMT(ChooseExpr, { })
1910 DEF_TRAVERSE_STMT(CompoundLiteralExpr, { })
1911 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, { })
1912 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, { })
1913 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, { })
1914 DEF_TRAVERSE_STMT(CXXDeleteExpr, { })
1915 DEF_TRAVERSE_STMT(ExprWithCleanups, { })
1916 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, { })
1917 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
1918 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1919 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
1920 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
1921 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
1922 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
1923 })
1924 DEF_TRAVERSE_STMT(CXXThisExpr, { })
1925 DEF_TRAVERSE_STMT(CXXThrowExpr, { })
1926 DEF_TRAVERSE_STMT(DesignatedInitExpr, { })
1927 DEF_TRAVERSE_STMT(ExtVectorElementExpr, { })
1928 DEF_TRAVERSE_STMT(GNUNullExpr, { })
1929 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, { })
1930 DEF_TRAVERSE_STMT(ObjCEncodeExpr, { })
1931 DEF_TRAVERSE_STMT(ObjCIsaExpr, { })
1932 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, { })
1933 DEF_TRAVERSE_STMT(ObjCMessageExpr, { })
1934 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, { })
1935 DEF_TRAVERSE_STMT(ObjCProtocolExpr, { })
1936 DEF_TRAVERSE_STMT(ObjCSelectorExpr, { })
1937 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, { })
1938 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
1939 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1940 })
1941 DEF_TRAVERSE_STMT(ParenExpr, { })
1942 DEF_TRAVERSE_STMT(ParenListExpr, { })
1943 DEF_TRAVERSE_STMT(PredefinedExpr, { })
1944 DEF_TRAVERSE_STMT(ShuffleVectorExpr, { })
1945 DEF_TRAVERSE_STMT(StmtExpr, { })
1946 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
1947 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1948 if (S->hasExplicitTemplateArgs()) {
1949 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1950 S->getNumTemplateArgs()));
1951 }
1952 })
1953
1954 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
1955 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1956 if (S->hasExplicitTemplateArgs()) {
1957 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1958 S->getNumTemplateArgs()));
1959 }
1960 })
1961
1962 DEF_TRAVERSE_STMT(SEHTryStmt, {})
1963 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
1964 DEF_TRAVERSE_STMT(SEHFinallyStmt,{})
1965
1966 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, { })
1967 DEF_TRAVERSE_STMT(OpaqueValueExpr, { })
1968 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, { })
1969
1970 // These operators (all of them) do not need any action except
1971 // iterating over the children.
1972 DEF_TRAVERSE_STMT(BinaryConditionalOperator, { })
1973 DEF_TRAVERSE_STMT(ConditionalOperator, { })
1974 DEF_TRAVERSE_STMT(UnaryOperator, { })
1975 DEF_TRAVERSE_STMT(BinaryOperator, { })
1976 DEF_TRAVERSE_STMT(CompoundAssignOperator, { })
1977 DEF_TRAVERSE_STMT(CXXNoexceptExpr, { })
1978 DEF_TRAVERSE_STMT(PackExpansionExpr, { })
1979 DEF_TRAVERSE_STMT(SizeOfPackExpr, { })
1980 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, { })
1981 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, { })
1982 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, { })
1983
1984 // These literals (all of them) do not need any action.
1985 DEF_TRAVERSE_STMT(IntegerLiteral, { })
1986 DEF_TRAVERSE_STMT(CharacterLiteral, { })
1987 DEF_TRAVERSE_STMT(FloatingLiteral, { })
1988 DEF_TRAVERSE_STMT(ImaginaryLiteral, { })
1989 DEF_TRAVERSE_STMT(StringLiteral, { })
1990 DEF_TRAVERSE_STMT(ObjCStringLiteral, { })
1991
1992 // Traverse OpenCL: AsType, Convert.
1993 DEF_TRAVERSE_STMT(AsTypeExpr, { })
1994
1995 // FIXME: look at the following tricky-seeming exprs to see if we
1996 // need to recurse on anything. These are ones that have methods
1997 // returning decls or qualtypes or nestednamespecifier -- though I'm
1998 // not sure if they own them -- or just seemed very complicated, or
1999 // had lots of sub-types to explore.
2000 //
2001 // VisitOverloadExpr and its children: recurse on template args? etc?
2002
2003 // FIXME: go through all the stmts and exprs again, and see which of them
2004 // create new types, and recurse on the types (TypeLocs?) of those.
2005 // Candidates:
2006 //
2007 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2008 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2009 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2010 // Every class that has getQualifier.
2011
2012 #undef DEF_TRAVERSE_STMT
2013
2014 #undef TRY_TO
2015
2016 } // end namespace clang
2017
2018 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
2019