1 //===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the RecursiveASTVisitor interface, which recursively
11 // traverses the entire AST.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
15 #define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
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"
34 // The following three macros are used for meta programming. The code
35 // using them is responsible for defining macro OPERATOR().
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) \
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) \
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) \
62 // All compound assign operators.
64 OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) \
65 OPERATOR(Shl) OPERATOR(Shr) OPERATOR(And) OPERATOR(Or) OPERATOR(Xor)
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
73 #define TRY_TO(CALL_EXPR) \
74 do { if (!getDerived().CALL_EXPR) return false; } while (0)
76 /// \brief A class that does preorder depth-first traversal on the
77 /// entire Clang AST and visits each node.
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.
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
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.
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.
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()).
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.
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.
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 {
140 /// \brief Return a reference to the derived class.
141 Derived &getDerived() { return *static_cast<Derived*>(this); }
143 /// \brief Return whether this visitor should recurse into
144 /// template instantiations.
145 bool shouldVisitTemplateInstantiations() const { return false; }
147 /// \brief Return whether this visitor should recurse into the types of
149 bool shouldWalkTypesOfTypeLocs() const { return true; }
151 /// \brief Return whether \param S should be traversed using data recursion
152 /// to avoid a stack overflow with extreme cases.
153 bool shouldUseDataRecursionFor(Stmt *S) const {
154 return isa<BinaryOperator>(S) || isa<UnaryOperator>(S) || isa<CaseStmt>(S);
157 /// \brief Recursively visit a statement or expression, by
158 /// dispatching to Traverse*() based on the argument's dynamic type.
160 /// \returns false if the visitation was terminated early, true
161 /// otherwise (including when the argument is NULL).
162 bool TraverseStmt(Stmt *S);
164 /// \brief Recursively visit a type, by dispatching to
165 /// Traverse*Type() based on the argument's getTypeClass() property.
167 /// \returns false if the visitation was terminated early, true
168 /// otherwise (including when the argument is a Null type).
169 bool TraverseType(QualType T);
171 /// \brief Recursively visit a type with location, by dispatching to
172 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
174 /// \returns false if the visitation was terminated early, true
175 /// otherwise (including when the argument is a Null type location).
176 bool TraverseTypeLoc(TypeLoc TL);
178 /// \brief Recursively visit a declaration, by dispatching to
179 /// Traverse*Decl() based on the argument's dynamic type.
181 /// \returns false if the visitation was terminated early, true
182 /// otherwise (including when the argument is NULL).
183 bool TraverseDecl(Decl *D);
185 /// \brief Recursively visit a C++ nested-name-specifier.
187 /// \returns false if the visitation was terminated early, true otherwise.
188 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
190 /// \brief Recursively visit a C++ nested-name-specifier with location
193 /// \returns false if the visitation was terminated early, true otherwise.
194 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
196 /// \brief Recursively visit a name with its location information.
198 /// \returns false if the visitation was terminated early, true otherwise.
199 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
201 /// \brief Recursively visit a template name and dispatch to the
202 /// appropriate method.
204 /// \returns false if the visitation was terminated early, true otherwise.
205 bool TraverseTemplateName(TemplateName Template);
207 /// \brief Recursively visit a template argument and dispatch to the
208 /// appropriate method for the argument type.
210 /// \returns false if the visitation was terminated early, true otherwise.
211 // FIXME: migrate callers to TemplateArgumentLoc instead.
212 bool TraverseTemplateArgument(const TemplateArgument &Arg);
214 /// \brief Recursively visit a template argument location and dispatch to the
215 /// appropriate method for the argument type.
217 /// \returns false if the visitation was terminated early, true otherwise.
218 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
220 /// \brief Recursively visit a set of template arguments.
221 /// This can be overridden by a subclass, but it's not expected that
222 /// will be needed -- this visitor always dispatches to another.
224 /// \returns false if the visitation was terminated early, true otherwise.
225 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
226 bool TraverseTemplateArguments(const TemplateArgument *Args,
229 /// \brief Recursively visit a constructor initializer. This
230 /// automatically dispatches to another visitor for the initializer
231 /// expression, but not for the name of the initializer, so may
232 /// be overridden for clients that need access to the name.
234 /// \returns false if the visitation was terminated early, true otherwise.
235 bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
237 /// \brief Recursively visit a lambda capture.
239 /// \returns false if the visitation was terminated early, true otherwise.
240 bool TraverseLambdaCapture(LambdaExpr::Capture C);
242 // ---- Methods on Stmts ----
244 // Declare Traverse*() for all concrete Stmt classes.
245 #define ABSTRACT_STMT(STMT)
246 #define STMT(CLASS, PARENT) \
247 bool Traverse##CLASS(CLASS *S);
248 #include "clang/AST/StmtNodes.inc"
249 // The above header #undefs ABSTRACT_STMT and STMT upon exit.
251 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
252 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
253 bool VisitStmt(Stmt *S) { return true; }
254 #define STMT(CLASS, PARENT) \
255 bool WalkUpFrom##CLASS(CLASS *S) { \
256 TRY_TO(WalkUpFrom##PARENT(S)); \
257 TRY_TO(Visit##CLASS(S)); \
260 bool Visit##CLASS(CLASS *S) { return true; }
261 #include "clang/AST/StmtNodes.inc"
263 // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary
264 // operator methods. Unary operators are not classes in themselves
265 // (they're all opcodes in UnaryOperator) but do have visitors.
266 #define OPERATOR(NAME) \
267 bool TraverseUnary##NAME(UnaryOperator *S) { \
268 TRY_TO(WalkUpFromUnary##NAME(S)); \
269 TRY_TO(TraverseStmt(S->getSubExpr())); \
272 bool WalkUpFromUnary##NAME(UnaryOperator *S) { \
273 TRY_TO(WalkUpFromUnaryOperator(S)); \
274 TRY_TO(VisitUnary##NAME(S)); \
277 bool VisitUnary##NAME(UnaryOperator *S) { return true; }
282 // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary
283 // operator methods. Binary operators are not classes in themselves
284 // (they're all opcodes in BinaryOperator) but do have visitors.
285 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE) \
286 bool TraverseBin##NAME(BINOP_TYPE *S) { \
287 TRY_TO(WalkUpFromBin##NAME(S)); \
288 TRY_TO(TraverseStmt(S->getLHS())); \
289 TRY_TO(TraverseStmt(S->getRHS())); \
292 bool WalkUpFromBin##NAME(BINOP_TYPE *S) { \
293 TRY_TO(WalkUpFrom##BINOP_TYPE(S)); \
294 TRY_TO(VisitBin##NAME(S)); \
297 bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
299 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
303 // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound
304 // assignment methods. Compound assignment operators are not
305 // classes in themselves (they're all opcodes in
306 // CompoundAssignOperator) but do have visitors.
307 #define OPERATOR(NAME) \
308 GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator)
312 #undef GENERAL_BINOP_FALLBACK
314 // ---- Methods on Types ----
315 // FIXME: revamp to take TypeLoc's rather than Types.
317 // Declare Traverse*() for all concrete Type classes.
318 #define ABSTRACT_TYPE(CLASS, BASE)
319 #define TYPE(CLASS, BASE) \
320 bool Traverse##CLASS##Type(CLASS##Type *T);
321 #include "clang/AST/TypeNodes.def"
322 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
324 // Define WalkUpFrom*() and empty Visit*() for all Type classes.
325 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
326 bool VisitType(Type *T) { return true; }
327 #define TYPE(CLASS, BASE) \
328 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \
329 TRY_TO(WalkUpFrom##BASE(T)); \
330 TRY_TO(Visit##CLASS##Type(T)); \
333 bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
334 #include "clang/AST/TypeNodes.def"
336 // ---- Methods on TypeLocs ----
337 // FIXME: this currently just calls the matching Type methods
339 // Declare Traverse*() for all concrete Type classes.
340 #define ABSTRACT_TYPELOC(CLASS, BASE)
341 #define TYPELOC(CLASS, BASE) \
342 bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
343 #include "clang/AST/TypeLocNodes.def"
344 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
346 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
347 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
348 bool VisitTypeLoc(TypeLoc TL) { return true; }
350 // QualifiedTypeLoc and UnqualTypeLoc are not declared in
351 // TypeNodes.def and thus need to be handled specially.
352 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
353 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
355 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
356 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
357 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
359 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
361 // Note that BASE includes trailing 'Type' which CLASS doesn't.
362 #define TYPE(CLASS, BASE) \
363 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
364 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \
365 TRY_TO(Visit##CLASS##TypeLoc(TL)); \
368 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
369 #include "clang/AST/TypeNodes.def"
371 // ---- Methods on Decls ----
373 // Declare Traverse*() for all concrete Decl classes.
374 #define ABSTRACT_DECL(DECL)
375 #define DECL(CLASS, BASE) \
376 bool Traverse##CLASS##Decl(CLASS##Decl *D);
377 #include "clang/AST/DeclNodes.inc"
378 // The above header #undefs ABSTRACT_DECL and DECL upon exit.
380 // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
381 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
382 bool VisitDecl(Decl *D) { return true; }
383 #define DECL(CLASS, BASE) \
384 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \
385 TRY_TO(WalkUpFrom##BASE(D)); \
386 TRY_TO(Visit##CLASS##Decl(D)); \
389 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
390 #include "clang/AST/DeclNodes.inc"
393 // These are helper methods used by more than one Traverse* method.
394 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
395 bool TraverseClassInstantiations(ClassTemplateDecl* D, Decl *Pattern);
396 bool TraverseFunctionInstantiations(FunctionTemplateDecl* D) ;
397 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
399 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
400 bool TraverseRecordHelper(RecordDecl *D);
401 bool TraverseCXXRecordHelper(CXXRecordDecl *D);
402 bool TraverseDeclaratorHelper(DeclaratorDecl *D);
403 bool TraverseDeclContextHelper(DeclContext *DC);
404 bool TraverseFunctionHelper(FunctionDecl *D);
405 bool TraverseVarHelper(VarDecl *D);
411 Stmt::child_iterator StmtIt;
413 EnqueueJob(Stmt *S) : S(S), StmtIt() {
414 if (Expr *E = dyn_cast_or_null<Expr>(S))
415 S = E->IgnoreParens();
418 bool dataTraverse(Stmt *S);
421 template<typename Derived>
422 bool RecursiveASTVisitor<Derived>::dataTraverse(Stmt *S) {
424 SmallVector<EnqueueJob, 16> Queue;
427 while (!Queue.empty()) {
428 EnqueueJob &job = Queue.back();
435 if (getDerived().shouldUseDataRecursionFor(CurrS)) {
436 if (job.StmtIt == Stmt::child_iterator()) {
437 if (!Walk(CurrS)) return false;
438 job.StmtIt = CurrS->child_begin();
443 if (job.StmtIt != CurrS->child_end())
444 Queue.push_back(*job.StmtIt);
451 TRY_TO(TraverseStmt(CurrS));
457 template<typename Derived>
458 bool RecursiveASTVisitor<Derived>::Walk(Stmt *S) {
460 #define DISPATCH_WALK(NAME, CLASS, VAR) \
461 return getDerived().WalkUpFrom##NAME(static_cast<CLASS*>(VAR));
463 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
464 switch (BinOp->getOpcode()) {
465 #define OPERATOR(NAME) \
466 case BO_##NAME: DISPATCH_WALK(Bin##NAME, BinaryOperator, S);
471 #define OPERATOR(NAME) \
472 case BO_##NAME##Assign: \
473 DISPATCH_WALK(Bin##NAME##Assign, CompoundAssignOperator, S);
478 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
479 switch (UnOp->getOpcode()) {
480 #define OPERATOR(NAME) \
481 case UO_##NAME: DISPATCH_WALK(Unary##NAME, UnaryOperator, S);
488 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
489 switch (S->getStmtClass()) {
490 case Stmt::NoStmtClass: break;
491 #define ABSTRACT_STMT(STMT)
492 #define STMT(CLASS, PARENT) \
493 case Stmt::CLASS##Class: DISPATCH_WALK(CLASS, CLASS, S);
494 #include "clang/AST/StmtNodes.inc"
502 #define DISPATCH(NAME, CLASS, VAR) \
503 return getDerived().Traverse##NAME(static_cast<CLASS*>(VAR))
505 template<typename Derived>
506 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S) {
510 if (getDerived().shouldUseDataRecursionFor(S))
511 return dataTraverse(S);
513 // If we have a binary expr, dispatch to the subcode of the binop. A smart
514 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
516 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
517 switch (BinOp->getOpcode()) {
518 #define OPERATOR(NAME) \
519 case BO_##NAME: DISPATCH(Bin##NAME, BinaryOperator, S);
525 #define OPERATOR(NAME) \
526 case BO_##NAME##Assign: \
527 DISPATCH(Bin##NAME##Assign, CompoundAssignOperator, S);
533 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
534 switch (UnOp->getOpcode()) {
535 #define OPERATOR(NAME) \
536 case UO_##NAME: DISPATCH(Unary##NAME, UnaryOperator, S);
544 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
545 switch (S->getStmtClass()) {
546 case Stmt::NoStmtClass: break;
547 #define ABSTRACT_STMT(STMT)
548 #define STMT(CLASS, PARENT) \
549 case Stmt::CLASS##Class: DISPATCH(CLASS, CLASS, S);
550 #include "clang/AST/StmtNodes.inc"
556 template<typename Derived>
557 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
561 switch (T->getTypeClass()) {
562 #define ABSTRACT_TYPE(CLASS, BASE)
563 #define TYPE(CLASS, BASE) \
564 case Type::CLASS: DISPATCH(CLASS##Type, CLASS##Type, \
565 const_cast<Type*>(T.getTypePtr()));
566 #include "clang/AST/TypeNodes.def"
572 template<typename Derived>
573 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
577 switch (TL.getTypeLocClass()) {
578 #define ABSTRACT_TYPELOC(CLASS, BASE)
579 #define TYPELOC(CLASS, BASE) \
580 case TypeLoc::CLASS: \
581 return getDerived().Traverse##CLASS##TypeLoc(*cast<CLASS##TypeLoc>(&TL));
582 #include "clang/AST/TypeLocNodes.def"
589 template<typename Derived>
590 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
594 // As a syntax visitor, we want to ignore declarations for
595 // implicitly-defined declarations (ones not typed explicitly by the
600 switch (D->getKind()) {
601 #define ABSTRACT_DECL(DECL)
602 #define DECL(CLASS, BASE) \
603 case Decl::CLASS: DISPATCH(CLASS##Decl, CLASS##Decl, D);
604 #include "clang/AST/DeclNodes.inc"
612 template<typename Derived>
613 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
614 NestedNameSpecifier *NNS) {
618 if (NNS->getPrefix())
619 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
621 switch (NNS->getKind()) {
622 case NestedNameSpecifier::Identifier:
623 case NestedNameSpecifier::Namespace:
624 case NestedNameSpecifier::NamespaceAlias:
625 case NestedNameSpecifier::Global:
628 case NestedNameSpecifier::TypeSpec:
629 case NestedNameSpecifier::TypeSpecWithTemplate:
630 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
636 template<typename Derived>
637 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
638 NestedNameSpecifierLoc NNS) {
642 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
643 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
645 switch (NNS.getNestedNameSpecifier()->getKind()) {
646 case NestedNameSpecifier::Identifier:
647 case NestedNameSpecifier::Namespace:
648 case NestedNameSpecifier::NamespaceAlias:
649 case NestedNameSpecifier::Global:
652 case NestedNameSpecifier::TypeSpec:
653 case NestedNameSpecifier::TypeSpecWithTemplate:
654 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
661 template<typename Derived>
662 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
663 DeclarationNameInfo NameInfo) {
664 switch (NameInfo.getName().getNameKind()) {
665 case DeclarationName::CXXConstructorName:
666 case DeclarationName::CXXDestructorName:
667 case DeclarationName::CXXConversionFunctionName:
668 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
669 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
673 case DeclarationName::Identifier:
674 case DeclarationName::ObjCZeroArgSelector:
675 case DeclarationName::ObjCOneArgSelector:
676 case DeclarationName::ObjCMultiArgSelector:
677 case DeclarationName::CXXOperatorName:
678 case DeclarationName::CXXLiteralOperatorName:
679 case DeclarationName::CXXUsingDirective:
686 template<typename Derived>
687 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
688 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
689 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
690 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
691 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
696 template<typename Derived>
697 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
698 const TemplateArgument &Arg) {
699 switch (Arg.getKind()) {
700 case TemplateArgument::Null:
701 case TemplateArgument::Declaration:
702 case TemplateArgument::Integral:
705 case TemplateArgument::Type:
706 return getDerived().TraverseType(Arg.getAsType());
708 case TemplateArgument::Template:
709 case TemplateArgument::TemplateExpansion:
710 return getDerived().TraverseTemplateName(
711 Arg.getAsTemplateOrTemplatePattern());
713 case TemplateArgument::Expression:
714 return getDerived().TraverseStmt(Arg.getAsExpr());
716 case TemplateArgument::Pack:
717 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
724 // FIXME: no template name location?
725 // FIXME: no source locations for a template argument pack?
726 template<typename Derived>
727 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
728 const TemplateArgumentLoc &ArgLoc) {
729 const TemplateArgument &Arg = ArgLoc.getArgument();
731 switch (Arg.getKind()) {
732 case TemplateArgument::Null:
733 case TemplateArgument::Declaration:
734 case TemplateArgument::Integral:
737 case TemplateArgument::Type: {
738 // FIXME: how can TSI ever be NULL?
739 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
740 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
742 return getDerived().TraverseType(Arg.getAsType());
745 case TemplateArgument::Template:
746 case TemplateArgument::TemplateExpansion:
747 if (ArgLoc.getTemplateQualifierLoc())
748 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
749 ArgLoc.getTemplateQualifierLoc()));
750 return getDerived().TraverseTemplateName(
751 Arg.getAsTemplateOrTemplatePattern());
753 case TemplateArgument::Expression:
754 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
756 case TemplateArgument::Pack:
757 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
764 template<typename Derived>
765 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
766 const TemplateArgument *Args,
768 for (unsigned I = 0; I != NumArgs; ++I) {
769 TRY_TO(TraverseTemplateArgument(Args[I]));
775 template<typename Derived>
776 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
777 CXXCtorInitializer *Init) {
778 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
779 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
781 if (Init->isWritten())
782 TRY_TO(TraverseStmt(Init->getInit()));
786 template<typename Derived>
787 bool RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr::Capture C){
791 // ----------------- Type traversal -----------------
793 // This macro makes available a variable T, the passed-in type.
794 #define DEF_TRAVERSE_TYPE(TYPE, CODE) \
795 template<typename Derived> \
796 bool RecursiveASTVisitor<Derived>::Traverse##TYPE (TYPE *T) { \
797 TRY_TO(WalkUpFrom##TYPE (T)); \
802 DEF_TRAVERSE_TYPE(BuiltinType, { })
804 DEF_TRAVERSE_TYPE(ComplexType, {
805 TRY_TO(TraverseType(T->getElementType()));
808 DEF_TRAVERSE_TYPE(PointerType, {
809 TRY_TO(TraverseType(T->getPointeeType()));
812 DEF_TRAVERSE_TYPE(BlockPointerType, {
813 TRY_TO(TraverseType(T->getPointeeType()));
816 DEF_TRAVERSE_TYPE(LValueReferenceType, {
817 TRY_TO(TraverseType(T->getPointeeType()));
820 DEF_TRAVERSE_TYPE(RValueReferenceType, {
821 TRY_TO(TraverseType(T->getPointeeType()));
824 DEF_TRAVERSE_TYPE(MemberPointerType, {
825 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
826 TRY_TO(TraverseType(T->getPointeeType()));
829 DEF_TRAVERSE_TYPE(ConstantArrayType, {
830 TRY_TO(TraverseType(T->getElementType()));
833 DEF_TRAVERSE_TYPE(IncompleteArrayType, {
834 TRY_TO(TraverseType(T->getElementType()));
837 DEF_TRAVERSE_TYPE(VariableArrayType, {
838 TRY_TO(TraverseType(T->getElementType()));
839 TRY_TO(TraverseStmt(T->getSizeExpr()));
842 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
843 TRY_TO(TraverseType(T->getElementType()));
844 if (T->getSizeExpr())
845 TRY_TO(TraverseStmt(T->getSizeExpr()));
848 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
849 if (T->getSizeExpr())
850 TRY_TO(TraverseStmt(T->getSizeExpr()));
851 TRY_TO(TraverseType(T->getElementType()));
854 DEF_TRAVERSE_TYPE(VectorType, {
855 TRY_TO(TraverseType(T->getElementType()));
858 DEF_TRAVERSE_TYPE(ExtVectorType, {
859 TRY_TO(TraverseType(T->getElementType()));
862 DEF_TRAVERSE_TYPE(FunctionNoProtoType, {
863 TRY_TO(TraverseType(T->getResultType()));
866 DEF_TRAVERSE_TYPE(FunctionProtoType, {
867 TRY_TO(TraverseType(T->getResultType()));
869 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
870 AEnd = T->arg_type_end();
872 TRY_TO(TraverseType(*A));
875 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
876 EEnd = T->exception_end();
878 TRY_TO(TraverseType(*E));
882 DEF_TRAVERSE_TYPE(UnresolvedUsingType, { })
883 DEF_TRAVERSE_TYPE(TypedefType, { })
885 DEF_TRAVERSE_TYPE(TypeOfExprType, {
886 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
889 DEF_TRAVERSE_TYPE(TypeOfType, {
890 TRY_TO(TraverseType(T->getUnderlyingType()));
893 DEF_TRAVERSE_TYPE(DecltypeType, {
894 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
897 DEF_TRAVERSE_TYPE(UnaryTransformType, {
898 TRY_TO(TraverseType(T->getBaseType()));
899 TRY_TO(TraverseType(T->getUnderlyingType()));
902 DEF_TRAVERSE_TYPE(AutoType, {
903 TRY_TO(TraverseType(T->getDeducedType()));
906 DEF_TRAVERSE_TYPE(RecordType, { })
907 DEF_TRAVERSE_TYPE(EnumType, { })
908 DEF_TRAVERSE_TYPE(TemplateTypeParmType, { })
909 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, { })
910 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, { })
912 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
913 TRY_TO(TraverseTemplateName(T->getTemplateName()));
914 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
917 DEF_TRAVERSE_TYPE(InjectedClassNameType, { })
919 DEF_TRAVERSE_TYPE(AttributedType, {
920 TRY_TO(TraverseType(T->getModifiedType()));
923 DEF_TRAVERSE_TYPE(ParenType, {
924 TRY_TO(TraverseType(T->getInnerType()));
927 DEF_TRAVERSE_TYPE(ElaboratedType, {
928 if (T->getQualifier()) {
929 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
931 TRY_TO(TraverseType(T->getNamedType()));
934 DEF_TRAVERSE_TYPE(DependentNameType, {
935 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
938 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
939 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
940 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
943 DEF_TRAVERSE_TYPE(PackExpansionType, {
944 TRY_TO(TraverseType(T->getPattern()));
947 DEF_TRAVERSE_TYPE(ObjCInterfaceType, { })
949 DEF_TRAVERSE_TYPE(ObjCObjectType, {
950 // We have to watch out here because an ObjCInterfaceType's base
952 if (T->getBaseType().getTypePtr() != T)
953 TRY_TO(TraverseType(T->getBaseType()));
956 DEF_TRAVERSE_TYPE(ObjCObjectPointerType, {
957 TRY_TO(TraverseType(T->getPointeeType()));
960 DEF_TRAVERSE_TYPE(AtomicType, {
961 TRY_TO(TraverseType(T->getValueType()));
964 #undef DEF_TRAVERSE_TYPE
966 // ----------------- TypeLoc traversal -----------------
968 // This macro makes available a variable TL, the passed-in TypeLoc.
969 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
970 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
971 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
973 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
974 template<typename Derived> \
975 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
976 if (getDerived().shouldWalkTypesOfTypeLocs()) \
977 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE*>(TL.getTypePtr()))); \
978 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
983 template<typename Derived>
984 bool RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(
985 QualifiedTypeLoc TL) {
986 // Move this over to the 'main' typeloc tree. Note that this is a
987 // move -- we pretend that we were really looking at the unqualified
988 // typeloc all along -- rather than a recursion, so we don't follow
989 // the normal CRTP plan of going through
990 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
991 // twice for the same type (once as a QualifiedTypeLoc version of
992 // the type, once as an UnqualifiedTypeLoc version of the type),
993 // which in effect means we'd call VisitTypeLoc twice with the
994 // 'same' type. This solves that problem, at the cost of never
995 // seeing the qualified version of the type (unless the client
996 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
997 // perfect solution. A perfect solution probably requires making
998 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
999 // wrapper around Type* -- rather than being its own class in the
1001 return TraverseTypeLoc(TL.getUnqualifiedLoc());
1004 DEF_TRAVERSE_TYPELOC(BuiltinType, { })
1006 // FIXME: ComplexTypeLoc is unfinished
1007 DEF_TRAVERSE_TYPELOC(ComplexType, {
1008 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1011 DEF_TRAVERSE_TYPELOC(PointerType, {
1012 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1015 DEF_TRAVERSE_TYPELOC(BlockPointerType, {
1016 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1019 DEF_TRAVERSE_TYPELOC(LValueReferenceType, {
1020 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1023 DEF_TRAVERSE_TYPELOC(RValueReferenceType, {
1024 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1027 // FIXME: location of base class?
1028 // We traverse this in the type case as well, but how is it not reached through
1029 // the pointee type?
1030 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1031 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1032 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1035 template<typename Derived>
1036 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1037 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1038 TRY_TO(TraverseStmt(TL.getSizeExpr()));
1042 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1043 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1044 return TraverseArrayTypeLocHelper(TL);
1047 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1048 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1049 return TraverseArrayTypeLocHelper(TL);
1052 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1053 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1054 return TraverseArrayTypeLocHelper(TL);
1057 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1058 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1059 return TraverseArrayTypeLocHelper(TL);
1062 // FIXME: order? why not size expr first?
1063 // FIXME: base VectorTypeLoc is unfinished
1064 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1065 if (TL.getTypePtr()->getSizeExpr())
1066 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1067 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1070 // FIXME: VectorTypeLoc is unfinished
1071 DEF_TRAVERSE_TYPELOC(VectorType, {
1072 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1075 // FIXME: size and attributes
1076 // FIXME: base VectorTypeLoc is unfinished
1077 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1078 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1081 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, {
1082 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1085 // FIXME: location of exception specifications (attributes?)
1086 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1087 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1089 const FunctionProtoType *T = TL.getTypePtr();
1091 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1093 TRY_TO(TraverseDecl(TL.getArg(I)));
1094 } else if (I < T->getNumArgs()) {
1095 TRY_TO(TraverseType(T->getArgType(I)));
1099 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1100 EEnd = T->exception_end();
1102 TRY_TO(TraverseType(*E));
1106 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, { })
1107 DEF_TRAVERSE_TYPELOC(TypedefType, { })
1109 DEF_TRAVERSE_TYPELOC(TypeOfExprType, {
1110 TRY_TO(TraverseStmt(TL.getUnderlyingExpr()));
1113 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1114 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1117 // FIXME: location of underlying expr
1118 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1119 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1122 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1123 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1126 DEF_TRAVERSE_TYPELOC(AutoType, {
1127 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1130 DEF_TRAVERSE_TYPELOC(RecordType, { })
1131 DEF_TRAVERSE_TYPELOC(EnumType, { })
1132 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, { })
1133 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, { })
1134 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, { })
1136 // FIXME: use the loc for the template name?
1137 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1138 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1139 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1140 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1144 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, { })
1146 DEF_TRAVERSE_TYPELOC(ParenType, {
1147 TRY_TO(TraverseTypeLoc(TL.getInnerLoc()));
1150 DEF_TRAVERSE_TYPELOC(AttributedType, {
1151 TRY_TO(TraverseTypeLoc(TL.getModifiedLoc()));
1154 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1155 if (TL.getQualifierLoc()) {
1156 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1158 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1161 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1162 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1165 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1166 if (TL.getQualifierLoc()) {
1167 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1170 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1171 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1175 DEF_TRAVERSE_TYPELOC(PackExpansionType, {
1176 TRY_TO(TraverseTypeLoc(TL.getPatternLoc()));
1179 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, { })
1181 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1182 // We have to watch out here because an ObjCInterfaceType's base
1184 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1185 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1188 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, {
1189 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1192 DEF_TRAVERSE_TYPELOC(AtomicType, {
1193 TRY_TO(TraverseTypeLoc(TL.getValueLoc()));
1196 #undef DEF_TRAVERSE_TYPELOC
1198 // ----------------- Decl traversal -----------------
1200 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1201 // the children that come from the DeclContext associated with it.
1202 // Therefore each Traverse* only needs to worry about children other
1205 template<typename Derived>
1206 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1210 for (DeclContext::decl_iterator Child = DC->decls_begin(),
1211 ChildEnd = DC->decls_end();
1212 Child != ChildEnd; ++Child) {
1213 // BlockDecls are traversed through BlockExprs.
1214 if (!isa<BlockDecl>(*Child))
1215 TRY_TO(TraverseDecl(*Child));
1221 // This macro makes available a variable D, the passed-in decl.
1222 #define DEF_TRAVERSE_DECL(DECL, CODE) \
1223 template<typename Derived> \
1224 bool RecursiveASTVisitor<Derived>::Traverse##DECL (DECL *D) { \
1225 TRY_TO(WalkUpFrom##DECL (D)); \
1227 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1231 DEF_TRAVERSE_DECL(AccessSpecDecl, { })
1233 DEF_TRAVERSE_DECL(BlockDecl, {
1234 TRY_TO(TraverseTypeLoc(D->getSignatureAsWritten()->getTypeLoc()));
1235 TRY_TO(TraverseStmt(D->getBody()));
1236 // This return statement makes sure the traversal of nodes in
1237 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1238 // is skipped - don't remove it.
1242 DEF_TRAVERSE_DECL(FileScopeAsmDecl, {
1243 TRY_TO(TraverseStmt(D->getAsmString()));
1246 DEF_TRAVERSE_DECL(ImportDecl, { })
1248 DEF_TRAVERSE_DECL(FriendDecl, {
1249 // Friend is either decl or a type.
1250 if (D->getFriendType())
1251 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1253 TRY_TO(TraverseDecl(D->getFriendDecl()));
1256 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1257 if (D->getFriendType())
1258 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1260 TRY_TO(TraverseDecl(D->getFriendDecl()));
1261 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1262 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1263 for (TemplateParameterList::iterator ITPL = TPL->begin(),
1265 ITPL != ETPL; ++ITPL) {
1266 TRY_TO(TraverseDecl(*ITPL));
1271 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1272 TRY_TO(TraverseDecl(D->getSpecialization()));
1275 DEF_TRAVERSE_DECL(LinkageSpecDecl, { })
1277 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {
1278 // FIXME: implement this
1281 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1282 TRY_TO(TraverseStmt(D->getAssertExpr()));
1283 TRY_TO(TraverseStmt(D->getMessage()));
1286 DEF_TRAVERSE_DECL(TranslationUnitDecl, {
1287 // Code in an unnamed namespace shows up automatically in
1288 // decls_begin()/decls_end(). Thus we don't need to recurse on
1289 // D->getAnonymousNamespace().
1292 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1293 // We shouldn't traverse an aliased namespace, since it will be
1294 // defined (and, therefore, traversed) somewhere else.
1296 // This return statement makes sure the traversal of nodes in
1297 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1298 // is skipped - don't remove it.
1302 DEF_TRAVERSE_DECL(LabelDecl, {
1303 // There is no code in a LabelDecl.
1307 DEF_TRAVERSE_DECL(NamespaceDecl, {
1308 // Code in an unnamed namespace shows up automatically in
1309 // decls_begin()/decls_end(). Thus we don't need to recurse on
1310 // D->getAnonymousNamespace().
1313 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {
1317 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {
1321 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {
1325 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {
1329 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {
1333 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {
1337 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1338 if (D->getResultTypeSourceInfo()) {
1339 TRY_TO(TraverseTypeLoc(D->getResultTypeSourceInfo()->getTypeLoc()));
1341 for (ObjCMethodDecl::param_iterator
1342 I = D->param_begin(), E = D->param_end(); I != E; ++I) {
1343 TRY_TO(TraverseDecl(*I));
1345 if (D->isThisDeclarationADefinition()) {
1346 TRY_TO(TraverseStmt(D->getBody()));
1351 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1355 DEF_TRAVERSE_DECL(UsingDecl, {
1356 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1357 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1360 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1361 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1364 DEF_TRAVERSE_DECL(UsingShadowDecl, { })
1366 // A helper method for TemplateDecl's children.
1367 template<typename Derived>
1368 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1369 TemplateParameterList *TPL) {
1371 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1373 TRY_TO(TraverseDecl(*I));
1379 // A helper method for traversing the implicit instantiations of a
1381 template<typename Derived>
1382 bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations(
1383 ClassTemplateDecl* D, Decl *Pattern) {
1384 assert(isa<ClassTemplateDecl>(Pattern) ||
1385 isa<ClassTemplatePartialSpecializationDecl>(Pattern));
1387 ClassTemplateDecl::spec_iterator end = D->spec_end();
1388 for (ClassTemplateDecl::spec_iterator it = D->spec_begin(); it != end; ++it) {
1389 ClassTemplateSpecializationDecl* SD = *it;
1391 switch (SD->getSpecializationKind()) {
1392 // Visit the implicit instantiations with the requested pattern.
1393 case TSK_ImplicitInstantiation: {
1394 llvm::PointerUnion<ClassTemplateDecl *,
1395 ClassTemplatePartialSpecializationDecl *> U
1396 = SD->getInstantiatedFrom();
1399 if (U.is<ClassTemplateDecl*>())
1400 ShouldVisit = (U.get<ClassTemplateDecl*>() == Pattern);
1403 = (U.get<ClassTemplatePartialSpecializationDecl*>() == Pattern);
1406 TRY_TO(TraverseDecl(SD));
1410 // We don't need to do anything on an explicit instantiation
1411 // or explicit specialization because there will be an explicit
1412 // node for it elsewhere.
1413 case TSK_ExplicitInstantiationDeclaration:
1414 case TSK_ExplicitInstantiationDefinition:
1415 case TSK_ExplicitSpecialization:
1418 // We don't need to do anything for an uninstantiated
1420 case TSK_Undeclared:
1428 DEF_TRAVERSE_DECL(ClassTemplateDecl, {
1429 CXXRecordDecl* TempDecl = D->getTemplatedDecl();
1430 TRY_TO(TraverseDecl(TempDecl));
1431 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1433 // By default, we do not traverse the instantiations of
1434 // class templates since they do not appear in the user code. The
1435 // following code optionally traverses them.
1436 if (getDerived().shouldVisitTemplateInstantiations()) {
1437 // If this is the definition of the primary template, visit
1438 // instantiations which were formed from this pattern.
1439 if (D->isThisDeclarationADefinition())
1440 TRY_TO(TraverseClassInstantiations(D, D));
1443 // Note that getInstantiatedFromMemberTemplate() is just a link
1444 // from a template instantiation back to the template from which
1445 // it was instantiated, and thus should not be traversed.
1448 // A helper method for traversing the instantiations of a
1449 // function while skipping its specializations.
1450 template<typename Derived>
1451 bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations(
1452 FunctionTemplateDecl* D) {
1453 FunctionTemplateDecl::spec_iterator end = D->spec_end();
1454 for (FunctionTemplateDecl::spec_iterator it = D->spec_begin(); it != end;
1456 FunctionDecl* FD = *it;
1457 switch (FD->getTemplateSpecializationKind()) {
1458 case TSK_ImplicitInstantiation:
1459 // We don't know what kind of FunctionDecl this is.
1460 TRY_TO(TraverseDecl(FD));
1463 // No need to visit explicit instantiations, we'll find the node
1465 case TSK_ExplicitInstantiationDeclaration:
1466 case TSK_ExplicitInstantiationDefinition:
1469 case TSK_Undeclared: // Declaration of the template definition.
1470 case TSK_ExplicitSpecialization:
1478 DEF_TRAVERSE_DECL(FunctionTemplateDecl, {
1479 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1480 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1482 // By default, we do not traverse the instantiations of
1483 // function templates since they do not apprear in the user code. The
1484 // following code optionally traverses them.
1485 if (getDerived().shouldVisitTemplateInstantiations()) {
1486 // Explicit function specializations will be traversed from the
1487 // context of their declaration. There is therefore no need to
1488 // traverse them for here.
1490 // In addition, we only traverse the function instantiations when
1491 // the function template is a function template definition.
1492 if (D->isThisDeclarationADefinition()) {
1493 TRY_TO(TraverseFunctionInstantiations(D));
1498 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1499 // D is the "T" in something like
1500 // template <template <typename> class T> class container { };
1501 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1502 if (D->hasDefaultArgument()) {
1503 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1505 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1508 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1509 // D is the "T" in something like "template<typename T> class vector;"
1510 if (D->getTypeForDecl())
1511 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1512 if (D->hasDefaultArgument())
1513 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1516 DEF_TRAVERSE_DECL(TypedefDecl, {
1517 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1518 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1519 // declaring the typedef, not something that was written in the
1523 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1524 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1525 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1526 // declaring the type alias, not something that was written in the
1530 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1531 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1532 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1535 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1536 // A dependent using declaration which was marked with 'typename'.
1537 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1538 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1539 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1540 // declaring the type, not something that was written in the
1544 DEF_TRAVERSE_DECL(EnumDecl, {
1545 if (D->getTypeForDecl())
1546 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1548 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1549 // The enumerators are already traversed by
1550 // decls_begin()/decls_end().
1554 // Helper methods for RecordDecl and its children.
1555 template<typename Derived>
1556 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(
1558 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1559 // declaring the type, not something that was written in the source.
1561 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1565 template<typename Derived>
1566 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(
1568 if (!TraverseRecordHelper(D))
1570 if (D->hasDefinition()) {
1571 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1574 TRY_TO(TraverseTypeLoc(I->getTypeSourceInfo()->getTypeLoc()));
1576 // We don't traverse the friends or the conversions, as they are
1577 // already in decls_begin()/decls_end().
1582 DEF_TRAVERSE_DECL(RecordDecl, {
1583 TRY_TO(TraverseRecordHelper(D));
1586 DEF_TRAVERSE_DECL(CXXRecordDecl, {
1587 TRY_TO(TraverseCXXRecordHelper(D));
1590 DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, {
1591 // For implicit instantiations ("set<int> x;"), we don't want to
1592 // recurse at all, since the instatiated class isn't written in
1593 // the source code anywhere. (Note the instatiated *type* --
1594 // set<int> -- is written, and will still get a callback of
1595 // TemplateSpecializationType). For explicit instantiations
1596 // ("template set<int>;"), we do need a callback, since this
1597 // is the only callback that's made for this instantiation.
1598 // We use getTypeAsWritten() to distinguish.
1599 if (TypeSourceInfo *TSI = D->getTypeAsWritten())
1600 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1602 if (!getDerived().shouldVisitTemplateInstantiations() &&
1603 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1604 // Returning from here skips traversing the
1605 // declaration context of the ClassTemplateSpecializationDecl
1606 // (embedded in the DEF_TRAVERSE_DECL() macro)
1607 // which contains the instantiated members of the class.
1611 template <typename Derived>
1612 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1613 const TemplateArgumentLoc *TAL, unsigned Count) {
1614 for (unsigned I = 0; I < Count; ++I) {
1615 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1620 DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, {
1621 // The partial specialization.
1622 if (TemplateParameterList *TPL = D->getTemplateParameters()) {
1623 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1625 TRY_TO(TraverseDecl(*I));
1628 // The args that remains unspecialized.
1629 TRY_TO(TraverseTemplateArgumentLocsHelper(
1630 D->getTemplateArgsAsWritten(), D->getNumTemplateArgsAsWritten()));
1632 // Don't need the ClassTemplatePartialSpecializationHelper, even
1633 // though that's our parent class -- we already visit all the
1634 // template args here.
1635 TRY_TO(TraverseCXXRecordHelper(D));
1637 // If we're visiting instantiations, visit the instantiations of
1638 // this template now.
1639 if (getDerived().shouldVisitTemplateInstantiations() &&
1640 D->isThisDeclarationADefinition())
1641 TRY_TO(TraverseClassInstantiations(D->getSpecializedTemplate(), D));
1644 DEF_TRAVERSE_DECL(EnumConstantDecl, {
1645 TRY_TO(TraverseStmt(D->getInitExpr()));
1648 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1649 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1650 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1651 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1652 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1655 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1657 template<typename Derived>
1658 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1659 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1660 if (D->getTypeSourceInfo())
1661 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1663 TRY_TO(TraverseType(D->getType()));
1667 DEF_TRAVERSE_DECL(FieldDecl, {
1668 TRY_TO(TraverseDeclaratorHelper(D));
1669 if (D->isBitField())
1670 TRY_TO(TraverseStmt(D->getBitWidth()));
1671 else if (D->hasInClassInitializer())
1672 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1675 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1676 TRY_TO(TraverseDeclaratorHelper(D));
1677 if (D->isBitField())
1678 TRY_TO(TraverseStmt(D->getBitWidth()));
1679 // FIXME: implement the rest.
1682 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1683 TRY_TO(TraverseDeclaratorHelper(D));
1684 if (D->isBitField())
1685 TRY_TO(TraverseStmt(D->getBitWidth()));
1686 // FIXME: implement the rest.
1689 template<typename Derived>
1690 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1691 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1692 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1694 // If we're an explicit template specialization, iterate over the
1695 // template args that were explicitly specified. If we were doing
1696 // this in typing order, we'd do it between the return type and
1697 // the function args, but both are handled by the FunctionTypeLoc
1698 // above, so we have to choose one side. I've decided to do before.
1699 if (const FunctionTemplateSpecializationInfo *FTSI =
1700 D->getTemplateSpecializationInfo()) {
1701 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1702 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1703 // A specialization might not have explicit template arguments if it has
1704 // a templated return type and concrete arguments.
1705 if (const ASTTemplateArgumentListInfo *TALI =
1706 FTSI->TemplateArgumentsAsWritten) {
1707 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1708 TALI->NumTemplateArgs));
1713 // Visit the function type itself, which can be either
1714 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1715 // also covers the return type and the function parameters,
1716 // including exception specifications.
1717 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1719 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1720 // Constructor initializers.
1721 for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
1722 E = Ctor->init_end();
1724 TRY_TO(TraverseConstructorInitializer(*I));
1728 if (D->isThisDeclarationADefinition()) {
1729 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1734 DEF_TRAVERSE_DECL(FunctionDecl, {
1735 // We skip decls_begin/decls_end, which are already covered by
1736 // TraverseFunctionHelper().
1737 return TraverseFunctionHelper(D);
1740 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1741 // We skip decls_begin/decls_end, which are already covered by
1742 // TraverseFunctionHelper().
1743 return TraverseFunctionHelper(D);
1746 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1747 // We skip decls_begin/decls_end, which are already covered by
1748 // TraverseFunctionHelper().
1749 return TraverseFunctionHelper(D);
1752 // CXXConversionDecl is the declaration of a type conversion operator.
1753 // It's not a cast expression.
1754 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1755 // We skip decls_begin/decls_end, which are already covered by
1756 // TraverseFunctionHelper().
1757 return TraverseFunctionHelper(D);
1760 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1761 // We skip decls_begin/decls_end, which are already covered by
1762 // TraverseFunctionHelper().
1763 return TraverseFunctionHelper(D);
1766 template<typename Derived>
1767 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1768 TRY_TO(TraverseDeclaratorHelper(D));
1769 // Default params are taken care of when we traverse the ParmVarDecl.
1770 if (!isa<ParmVarDecl>(D))
1771 TRY_TO(TraverseStmt(D->getInit()));
1775 DEF_TRAVERSE_DECL(VarDecl, {
1776 TRY_TO(TraverseVarHelper(D));
1779 DEF_TRAVERSE_DECL(ImplicitParamDecl, {
1780 TRY_TO(TraverseVarHelper(D));
1783 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1784 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1785 TRY_TO(TraverseDeclaratorHelper(D));
1786 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1789 DEF_TRAVERSE_DECL(ParmVarDecl, {
1790 TRY_TO(TraverseVarHelper(D));
1792 if (D->hasDefaultArg() &&
1793 D->hasUninstantiatedDefaultArg() &&
1794 !D->hasUnparsedDefaultArg())
1795 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1797 if (D->hasDefaultArg() &&
1798 !D->hasUninstantiatedDefaultArg() &&
1799 !D->hasUnparsedDefaultArg())
1800 TRY_TO(TraverseStmt(D->getDefaultArg()));
1803 #undef DEF_TRAVERSE_DECL
1805 // ----------------- Stmt traversal -----------------
1807 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1808 // over the children defined in children() (every stmt defines these,
1809 // though sometimes the range is empty). Each individual Traverse*
1810 // method only needs to worry about children other than those. To see
1811 // what children() does for a given class, see, e.g.,
1812 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1814 // This macro makes available a variable S, the passed-in stmt.
1815 #define DEF_TRAVERSE_STMT(STMT, CODE) \
1816 template<typename Derived> \
1817 bool RecursiveASTVisitor<Derived>::Traverse##STMT (STMT *S) { \
1818 TRY_TO(WalkUpFrom##STMT(S)); \
1820 for (Stmt::child_range range = S->children(); range; ++range) { \
1821 TRY_TO(TraverseStmt(*range)); \
1826 DEF_TRAVERSE_STMT(AsmStmt, {
1827 TRY_TO(TraverseStmt(S->getAsmString()));
1828 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1829 TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1831 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1832 TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1834 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1835 TRY_TO(TraverseStmt(S->getClobber(I)));
1837 // children() iterates over inputExpr and outputExpr.
1840 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1841 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1842 // children() iterates over the handler block.
1845 DEF_TRAVERSE_STMT(DeclStmt, {
1846 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
1848 TRY_TO(TraverseDecl(*I));
1850 // Suppress the default iteration over children() by
1851 // returning. Here's why: A DeclStmt looks like 'type var [=
1852 // initializer]'. The decls above already traverse over the
1853 // initializers, so we don't have to do it again (which
1854 // children() would do).
1859 // These non-expr stmts (most of them), do not need any action except
1860 // iterating over the children.
1861 DEF_TRAVERSE_STMT(BreakStmt, { })
1862 DEF_TRAVERSE_STMT(CXXTryStmt, { })
1863 DEF_TRAVERSE_STMT(CaseStmt, { })
1864 DEF_TRAVERSE_STMT(CompoundStmt, { })
1865 DEF_TRAVERSE_STMT(ContinueStmt, { })
1866 DEF_TRAVERSE_STMT(DefaultStmt, { })
1867 DEF_TRAVERSE_STMT(DoStmt, { })
1868 DEF_TRAVERSE_STMT(ForStmt, { })
1869 DEF_TRAVERSE_STMT(GotoStmt, { })
1870 DEF_TRAVERSE_STMT(IfStmt, { })
1871 DEF_TRAVERSE_STMT(IndirectGotoStmt, { })
1872 DEF_TRAVERSE_STMT(LabelStmt, { })
1873 DEF_TRAVERSE_STMT(AttributedStmt, { })
1874 DEF_TRAVERSE_STMT(NullStmt, { })
1875 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, { })
1876 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, { })
1877 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, { })
1878 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, { })
1879 DEF_TRAVERSE_STMT(ObjCAtTryStmt, { })
1880 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, { })
1881 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, { })
1882 DEF_TRAVERSE_STMT(CXXForRangeStmt, { })
1883 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1884 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1885 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1887 DEF_TRAVERSE_STMT(ReturnStmt, { })
1888 DEF_TRAVERSE_STMT(SwitchStmt, { })
1889 DEF_TRAVERSE_STMT(WhileStmt, { })
1892 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1893 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1894 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1895 if (S->hasExplicitTemplateArgs()) {
1896 TRY_TO(TraverseTemplateArgumentLocsHelper(
1897 S->getTemplateArgs(), S->getNumTemplateArgs()));
1901 DEF_TRAVERSE_STMT(DeclRefExpr, {
1902 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1903 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1904 TRY_TO(TraverseTemplateArgumentLocsHelper(
1905 S->getTemplateArgs(), S->getNumTemplateArgs()));
1908 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1909 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1910 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1911 if (S->hasExplicitTemplateArgs()) {
1912 TRY_TO(TraverseTemplateArgumentLocsHelper(
1913 S->getExplicitTemplateArgs().getTemplateArgs(),
1914 S->getNumTemplateArgs()));
1918 DEF_TRAVERSE_STMT(MemberExpr, {
1919 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1920 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1921 TRY_TO(TraverseTemplateArgumentLocsHelper(
1922 S->getTemplateArgs(), S->getNumTemplateArgs()));
1925 DEF_TRAVERSE_STMT(ImplicitCastExpr, {
1926 // We don't traverse the cast type, as it's not written in the
1930 DEF_TRAVERSE_STMT(CStyleCastExpr, {
1931 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1934 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
1935 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1938 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
1939 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1942 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
1943 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1946 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
1947 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1950 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
1951 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1954 // InitListExpr is a tricky one, because we want to do all our work on
1955 // the syntactic form of the listexpr, but this method takes the
1956 // semantic form by default. We can't use the macro helper because it
1957 // calls WalkUp*() on the semantic form, before our code can convert
1958 // to the syntactic form.
1959 template<typename Derived>
1960 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
1961 if (InitListExpr *Syn = S->getSyntacticForm())
1963 TRY_TO(WalkUpFromInitListExpr(S));
1964 // All we need are the default actions. FIXME: use a helper function.
1965 for (Stmt::child_range range = S->children(); range; ++range) {
1966 TRY_TO(TraverseStmt(*range));
1971 // GenericSelectionExpr is a special case because the types and expressions
1972 // are interleaved. We also need to watch out for null types (default
1973 // generic associations).
1974 template<typename Derived>
1975 bool RecursiveASTVisitor<Derived>::
1976 TraverseGenericSelectionExpr(GenericSelectionExpr *S) {
1977 TRY_TO(WalkUpFromGenericSelectionExpr(S));
1978 TRY_TO(TraverseStmt(S->getControllingExpr()));
1979 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
1980 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
1981 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
1982 TRY_TO(TraverseStmt(S->getAssocExpr(i)));
1987 // PseudoObjectExpr is a special case because of the wierdness with
1988 // syntactic expressions and opaque values.
1989 template<typename Derived>
1990 bool RecursiveASTVisitor<Derived>::
1991 TraversePseudoObjectExpr(PseudoObjectExpr *S) {
1992 TRY_TO(WalkUpFromPseudoObjectExpr(S));
1993 TRY_TO(TraverseStmt(S->getSyntacticForm()));
1994 for (PseudoObjectExpr::semantics_iterator
1995 i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i) {
1997 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
1998 sub = OVE->getSourceExpr();
1999 TRY_TO(TraverseStmt(sub));
2004 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2005 // This is called for code like 'return T()' where T is a built-in
2006 // (i.e. non-class) type.
2007 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2010 DEF_TRAVERSE_STMT(CXXNewExpr, {
2011 // The child-iterator will pick up the other arguments.
2012 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2015 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2016 // The child-iterator will pick up the expression representing
2018 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2019 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2020 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2023 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2024 // The child-iterator will pick up the arg if it's an expression,
2025 // but not if it's a type.
2026 if (S->isArgumentType())
2027 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2030 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2031 // The child-iterator will pick up the arg if it's an expression,
2032 // but not if it's a type.
2033 if (S->isTypeOperand())
2034 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2037 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2038 // The child-iterator will pick up the arg if it's an expression,
2039 // but not if it's a type.
2040 if (S->isTypeOperand())
2041 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2044 DEF_TRAVERSE_STMT(UnaryTypeTraitExpr, {
2045 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2048 DEF_TRAVERSE_STMT(BinaryTypeTraitExpr, {
2049 TRY_TO(TraverseTypeLoc(S->getLhsTypeSourceInfo()->getTypeLoc()));
2050 TRY_TO(TraverseTypeLoc(S->getRhsTypeSourceInfo()->getTypeLoc()));
2053 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2054 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2055 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2058 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2059 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2062 DEF_TRAVERSE_STMT(ExpressionTraitExpr, {
2063 TRY_TO(TraverseStmt(S->getQueriedExpression()));
2066 DEF_TRAVERSE_STMT(VAArgExpr, {
2067 // The child-iterator will pick up the expression argument.
2068 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2071 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2072 // This is called for code like 'return T()' where T is a class type.
2073 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2076 // Walk only the visible parts of lambda expressions.
2077 template<typename Derived>
2078 bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2079 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2080 CEnd = S->explicit_capture_end();
2082 TRY_TO(TraverseLambdaCapture(*C));
2085 if (S->hasExplicitParameters() || S->hasExplicitResultType()) {
2086 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2087 if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2088 // Visit the whole type.
2089 TRY_TO(TraverseTypeLoc(TL));
2090 } else if (isa<FunctionProtoTypeLoc>(TL)) {
2091 FunctionProtoTypeLoc Proto = cast<FunctionProtoTypeLoc>(TL);
2092 if (S->hasExplicitParameters()) {
2093 // Visit parameters.
2094 for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) {
2095 TRY_TO(TraverseDecl(Proto.getArg(I)));
2098 TRY_TO(TraverseTypeLoc(Proto.getResultLoc()));
2103 TRY_TO(TraverseStmt(S->getBody()));
2107 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2108 // This is called for code like 'T()', where T is a template argument.
2109 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2112 // These expressions all might take explicit template arguments.
2113 // We traverse those if so. FIXME: implement these.
2114 DEF_TRAVERSE_STMT(CXXConstructExpr, { })
2115 DEF_TRAVERSE_STMT(CallExpr, { })
2116 DEF_TRAVERSE_STMT(CXXMemberCallExpr, { })
2118 // These exprs (most of them), do not need any action except iterating
2119 // over the children.
2120 DEF_TRAVERSE_STMT(AddrLabelExpr, { })
2121 DEF_TRAVERSE_STMT(ArraySubscriptExpr, { })
2122 DEF_TRAVERSE_STMT(BlockExpr, {
2123 TRY_TO(TraverseDecl(S->getBlockDecl()));
2124 return true; // no child statements to loop through.
2126 DEF_TRAVERSE_STMT(ChooseExpr, { })
2127 DEF_TRAVERSE_STMT(CompoundLiteralExpr, { })
2128 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, { })
2129 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, { })
2130 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, { })
2131 DEF_TRAVERSE_STMT(CXXDeleteExpr, { })
2132 DEF_TRAVERSE_STMT(ExprWithCleanups, { })
2133 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, { })
2134 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2135 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2136 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2137 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2138 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2139 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2141 DEF_TRAVERSE_STMT(CXXThisExpr, { })
2142 DEF_TRAVERSE_STMT(CXXThrowExpr, { })
2143 DEF_TRAVERSE_STMT(UserDefinedLiteral, { })
2144 DEF_TRAVERSE_STMT(DesignatedInitExpr, { })
2145 DEF_TRAVERSE_STMT(ExtVectorElementExpr, { })
2146 DEF_TRAVERSE_STMT(GNUNullExpr, { })
2147 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, { })
2148 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, { })
2149 DEF_TRAVERSE_STMT(ObjCEncodeExpr, { })
2150 DEF_TRAVERSE_STMT(ObjCIsaExpr, { })
2151 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, { })
2152 DEF_TRAVERSE_STMT(ObjCMessageExpr, { })
2153 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, { })
2154 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, { })
2155 DEF_TRAVERSE_STMT(ObjCProtocolExpr, { })
2156 DEF_TRAVERSE_STMT(ObjCSelectorExpr, { })
2157 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, { })
2158 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2159 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2161 DEF_TRAVERSE_STMT(ParenExpr, { })
2162 DEF_TRAVERSE_STMT(ParenListExpr, { })
2163 DEF_TRAVERSE_STMT(PredefinedExpr, { })
2164 DEF_TRAVERSE_STMT(ShuffleVectorExpr, { })
2165 DEF_TRAVERSE_STMT(StmtExpr, { })
2166 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2167 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2168 if (S->hasExplicitTemplateArgs()) {
2169 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2170 S->getNumTemplateArgs()));
2174 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2175 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2176 if (S->hasExplicitTemplateArgs()) {
2177 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2178 S->getNumTemplateArgs()));
2182 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2183 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2184 DEF_TRAVERSE_STMT(SEHFinallyStmt,{})
2186 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, { })
2187 DEF_TRAVERSE_STMT(OpaqueValueExpr, { })
2188 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, { })
2190 // These operators (all of them) do not need any action except
2191 // iterating over the children.
2192 DEF_TRAVERSE_STMT(BinaryConditionalOperator, { })
2193 DEF_TRAVERSE_STMT(ConditionalOperator, { })
2194 DEF_TRAVERSE_STMT(UnaryOperator, { })
2195 DEF_TRAVERSE_STMT(BinaryOperator, { })
2196 DEF_TRAVERSE_STMT(CompoundAssignOperator, { })
2197 DEF_TRAVERSE_STMT(CXXNoexceptExpr, { })
2198 DEF_TRAVERSE_STMT(PackExpansionExpr, { })
2199 DEF_TRAVERSE_STMT(SizeOfPackExpr, { })
2200 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, { })
2201 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, { })
2202 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, { })
2203 DEF_TRAVERSE_STMT(AtomicExpr, { })
2205 // These literals (all of them) do not need any action.
2206 DEF_TRAVERSE_STMT(IntegerLiteral, { })
2207 DEF_TRAVERSE_STMT(CharacterLiteral, { })
2208 DEF_TRAVERSE_STMT(FloatingLiteral, { })
2209 DEF_TRAVERSE_STMT(ImaginaryLiteral, { })
2210 DEF_TRAVERSE_STMT(StringLiteral, { })
2211 DEF_TRAVERSE_STMT(ObjCStringLiteral, { })
2212 DEF_TRAVERSE_STMT(ObjCNumericLiteral, { })
2213 DEF_TRAVERSE_STMT(ObjCArrayLiteral, { })
2214 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, { })
2216 // Traverse OpenCL: AsType, Convert.
2217 DEF_TRAVERSE_STMT(AsTypeExpr, { })
2219 // FIXME: look at the following tricky-seeming exprs to see if we
2220 // need to recurse on anything. These are ones that have methods
2221 // returning decls or qualtypes or nestednamespecifier -- though I'm
2222 // not sure if they own them -- or just seemed very complicated, or
2223 // had lots of sub-types to explore.
2225 // VisitOverloadExpr and its children: recurse on template args? etc?
2227 // FIXME: go through all the stmts and exprs again, and see which of them
2228 // create new types, and recurse on the types (TypeLocs?) of those.
2231 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2232 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2233 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2234 // Every class that has getQualifier.
2236 #undef DEF_TRAVERSE_STMT
2240 } // end namespace clang
2242 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H