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/DeclOpenMP.h"
22 #include "clang/AST/DeclTemplate.h"
23 #include "clang/AST/Expr.h"
24 #include "clang/AST/ExprCXX.h"
25 #include "clang/AST/ExprObjC.h"
26 #include "clang/AST/NestedNameSpecifier.h"
27 #include "clang/AST/Stmt.h"
28 #include "clang/AST/StmtCXX.h"
29 #include "clang/AST/StmtObjC.h"
30 #include "clang/AST/TemplateBase.h"
31 #include "clang/AST/TemplateName.h"
32 #include "clang/AST/Type.h"
33 #include "clang/AST/TypeLoc.h"
35 // The following three macros are used for meta programming. The code
36 // using them is responsible for defining macro OPERATOR().
38 // All unary operators.
39 #define UNARYOP_LIST() \
40 OPERATOR(PostInc) OPERATOR(PostDec) \
41 OPERATOR(PreInc) OPERATOR(PreDec) \
42 OPERATOR(AddrOf) OPERATOR(Deref) \
43 OPERATOR(Plus) OPERATOR(Minus) \
44 OPERATOR(Not) OPERATOR(LNot) \
45 OPERATOR(Real) OPERATOR(Imag) \
48 // All binary operators (excluding compound assign operators).
49 #define BINOP_LIST() \
50 OPERATOR(PtrMemD) OPERATOR(PtrMemI) \
51 OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) \
52 OPERATOR(Add) OPERATOR(Sub) OPERATOR(Shl) \
55 OPERATOR(LT) OPERATOR(GT) OPERATOR(LE) \
56 OPERATOR(GE) OPERATOR(EQ) OPERATOR(NE) \
57 OPERATOR(And) OPERATOR(Xor) OPERATOR(Or) \
58 OPERATOR(LAnd) OPERATOR(LOr) \
63 // All compound assign operators.
65 OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) \
66 OPERATOR(Shl) OPERATOR(Shr) OPERATOR(And) OPERATOR(Or) OPERATOR(Xor)
70 // A helper macro to implement short-circuiting when recursing. It
71 // invokes CALL_EXPR, which must be a method call, on the derived
72 // object (s.t. a user of RecursiveASTVisitor can override the method
74 #define TRY_TO(CALL_EXPR) \
75 do { if (!getDerived().CALL_EXPR) return false; } while (0)
77 /// \brief A class that does preorder depth-first traversal on the
78 /// entire Clang AST and visits each node.
80 /// This class performs three distinct tasks:
81 /// 1. traverse the AST (i.e. go to each node);
82 /// 2. at a given node, walk up the class hierarchy, starting from
83 /// the node's dynamic type, until the top-most class (e.g. Stmt,
84 /// Decl, or Type) is reached.
85 /// 3. given a (node, class) combination, where 'class' is some base
86 /// class of the dynamic type of 'node', call a user-overridable
87 /// function to actually visit the node.
89 /// These tasks are done by three groups of methods, respectively:
90 /// 1. TraverseDecl(Decl *x) does task #1. It is the entry point
91 /// for traversing an AST rooted at x. This method simply
92 /// dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo
93 /// is the dynamic type of *x, which calls WalkUpFromFoo(x) and
94 /// then recursively visits the child nodes of x.
95 /// TraverseStmt(Stmt *x) and TraverseType(QualType x) work
97 /// 2. WalkUpFromFoo(Foo *x) does task #2. It does not try to visit
98 /// any child node of x. Instead, it first calls WalkUpFromBar(x)
99 /// where Bar is the direct parent class of Foo (unless Foo has
100 /// no parent), and then calls VisitFoo(x) (see the next list item).
101 /// 3. VisitFoo(Foo *x) does task #3.
103 /// These three method groups are tiered (Traverse* > WalkUpFrom* >
104 /// Visit*). A method (e.g. Traverse*) may call methods from the same
105 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*).
106 /// It may not call methods from a higher tier.
108 /// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar
109 /// is Foo's super class) before calling VisitFoo(), the result is
110 /// that the Visit*() methods for a given node are called in the
111 /// top-down order (e.g. for a node of type NamedDecl, the order will
112 /// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()).
114 /// This scheme guarantees that all Visit*() calls for the same AST
115 /// node are grouped together. In other words, Visit*() methods for
116 /// different nodes are never interleaved.
118 /// Clients of this visitor should subclass the visitor (providing
119 /// themselves as the template argument, using the curiously recurring
120 /// template pattern) and override any of the Traverse*, WalkUpFrom*,
121 /// and Visit* methods for declarations, types, statements,
122 /// expressions, or other AST nodes where the visitor should customize
123 /// behavior. Most users only need to override Visit*. Advanced
124 /// users may override Traverse* and WalkUpFrom* to implement custom
125 /// traversal strategies. Returning false from one of these overridden
126 /// functions will abort the entire traversal.
128 /// By default, this visitor tries to visit every part of the explicit
129 /// source code exactly once. The default policy towards templates
130 /// is to descend into the 'pattern' class or function body, not any
131 /// explicit or implicit instantiations. Explicit specializations
132 /// are still visited, and the patterns of partial specializations
133 /// are visited separately. This behavior can be changed by
134 /// overriding shouldVisitTemplateInstantiations() in the derived class
135 /// to return true, in which case all known implicit and explicit
136 /// instantiations will be visited at the same time as the pattern
137 /// from which they were produced.
138 template<typename Derived>
139 class RecursiveASTVisitor {
141 /// \brief Return a reference to the derived class.
142 Derived &getDerived() { return *static_cast<Derived*>(this); }
144 /// \brief Return whether this visitor should recurse into
145 /// template instantiations.
146 bool shouldVisitTemplateInstantiations() const { return false; }
148 /// \brief Return whether this visitor should recurse into the types of
150 bool shouldWalkTypesOfTypeLocs() const { return true; }
152 /// \brief Return whether this visitor should recurse into implicit
153 /// code, e.g., implicit constructors and destructors.
154 bool shouldVisitImplicitCode() const { return false; }
156 /// \brief Return whether \param S should be traversed using data recursion
157 /// to avoid a stack overflow with extreme cases.
158 bool shouldUseDataRecursionFor(Stmt *S) const {
159 return isa<BinaryOperator>(S) || isa<UnaryOperator>(S) ||
160 isa<CaseStmt>(S) || isa<CXXOperatorCallExpr>(S);
163 /// \brief Recursively visit a statement or expression, by
164 /// dispatching to Traverse*() based on the argument's dynamic type.
166 /// \returns false if the visitation was terminated early, true
167 /// otherwise (including when the argument is NULL).
168 bool TraverseStmt(Stmt *S);
170 /// \brief Recursively visit a type, by dispatching to
171 /// Traverse*Type() based on the argument's getTypeClass() property.
173 /// \returns false if the visitation was terminated early, true
174 /// otherwise (including when the argument is a Null type).
175 bool TraverseType(QualType T);
177 /// \brief Recursively visit a type with location, by dispatching to
178 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
180 /// \returns false if the visitation was terminated early, true
181 /// otherwise (including when the argument is a Null type location).
182 bool TraverseTypeLoc(TypeLoc TL);
184 /// \brief Recursively visit a declaration, by dispatching to
185 /// Traverse*Decl() based on the argument's dynamic type.
187 /// \returns false if the visitation was terminated early, true
188 /// otherwise (including when the argument is NULL).
189 bool TraverseDecl(Decl *D);
191 /// \brief Recursively visit a C++ nested-name-specifier.
193 /// \returns false if the visitation was terminated early, true otherwise.
194 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
196 /// \brief Recursively visit a C++ nested-name-specifier with location
199 /// \returns false if the visitation was terminated early, true otherwise.
200 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
202 /// \brief Recursively visit a name with its location information.
204 /// \returns false if the visitation was terminated early, true otherwise.
205 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
207 /// \brief Recursively visit a template name and dispatch to the
208 /// appropriate method.
210 /// \returns false if the visitation was terminated early, true otherwise.
211 bool TraverseTemplateName(TemplateName Template);
213 /// \brief Recursively visit a template argument and dispatch to the
214 /// appropriate method for the argument type.
216 /// \returns false if the visitation was terminated early, true otherwise.
217 // FIXME: migrate callers to TemplateArgumentLoc instead.
218 bool TraverseTemplateArgument(const TemplateArgument &Arg);
220 /// \brief Recursively visit a template argument location and dispatch to the
221 /// appropriate method for the argument type.
223 /// \returns false if the visitation was terminated early, true otherwise.
224 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
226 /// \brief Recursively visit a set of template arguments.
227 /// This can be overridden by a subclass, but it's not expected that
228 /// will be needed -- this visitor always dispatches to another.
230 /// \returns false if the visitation was terminated early, true otherwise.
231 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
232 bool TraverseTemplateArguments(const TemplateArgument *Args,
235 /// \brief Recursively visit a constructor initializer. This
236 /// automatically dispatches to another visitor for the initializer
237 /// expression, but not for the name of the initializer, so may
238 /// be overridden for clients that need access to the name.
240 /// \returns false if the visitation was terminated early, true otherwise.
241 bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
243 /// \brief Recursively visit a lambda capture.
245 /// \returns false if the visitation was terminated early, true otherwise.
246 bool TraverseLambdaCapture(LambdaExpr::Capture C);
248 // ---- Methods on Stmts ----
250 // Declare Traverse*() for all concrete Stmt classes.
251 #define ABSTRACT_STMT(STMT)
252 #define STMT(CLASS, PARENT) \
253 bool Traverse##CLASS(CLASS *S);
254 #include "clang/AST/StmtNodes.inc"
255 // The above header #undefs ABSTRACT_STMT and STMT upon exit.
257 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
258 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
259 bool VisitStmt(Stmt *S) { return true; }
260 #define STMT(CLASS, PARENT) \
261 bool WalkUpFrom##CLASS(CLASS *S) { \
262 TRY_TO(WalkUpFrom##PARENT(S)); \
263 TRY_TO(Visit##CLASS(S)); \
266 bool Visit##CLASS(CLASS *S) { return true; }
267 #include "clang/AST/StmtNodes.inc"
269 // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary
270 // operator methods. Unary operators are not classes in themselves
271 // (they're all opcodes in UnaryOperator) but do have visitors.
272 #define OPERATOR(NAME) \
273 bool TraverseUnary##NAME(UnaryOperator *S) { \
274 TRY_TO(WalkUpFromUnary##NAME(S)); \
275 TRY_TO(TraverseStmt(S->getSubExpr())); \
278 bool WalkUpFromUnary##NAME(UnaryOperator *S) { \
279 TRY_TO(WalkUpFromUnaryOperator(S)); \
280 TRY_TO(VisitUnary##NAME(S)); \
283 bool VisitUnary##NAME(UnaryOperator *S) { return true; }
288 // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary
289 // operator methods. Binary operators are not classes in themselves
290 // (they're all opcodes in BinaryOperator) but do have visitors.
291 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE) \
292 bool TraverseBin##NAME(BINOP_TYPE *S) { \
293 TRY_TO(WalkUpFromBin##NAME(S)); \
294 TRY_TO(TraverseStmt(S->getLHS())); \
295 TRY_TO(TraverseStmt(S->getRHS())); \
298 bool WalkUpFromBin##NAME(BINOP_TYPE *S) { \
299 TRY_TO(WalkUpFrom##BINOP_TYPE(S)); \
300 TRY_TO(VisitBin##NAME(S)); \
303 bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
305 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
309 // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound
310 // assignment methods. Compound assignment operators are not
311 // classes in themselves (they're all opcodes in
312 // CompoundAssignOperator) but do have visitors.
313 #define OPERATOR(NAME) \
314 GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator)
318 #undef GENERAL_BINOP_FALLBACK
320 // ---- Methods on Types ----
321 // FIXME: revamp to take TypeLoc's rather than Types.
323 // Declare Traverse*() for all concrete Type classes.
324 #define ABSTRACT_TYPE(CLASS, BASE)
325 #define TYPE(CLASS, BASE) \
326 bool Traverse##CLASS##Type(CLASS##Type *T);
327 #include "clang/AST/TypeNodes.def"
328 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
330 // Define WalkUpFrom*() and empty Visit*() for all Type classes.
331 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
332 bool VisitType(Type *T) { return true; }
333 #define TYPE(CLASS, BASE) \
334 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \
335 TRY_TO(WalkUpFrom##BASE(T)); \
336 TRY_TO(Visit##CLASS##Type(T)); \
339 bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
340 #include "clang/AST/TypeNodes.def"
342 // ---- Methods on TypeLocs ----
343 // FIXME: this currently just calls the matching Type methods
345 // Declare Traverse*() for all concrete Type classes.
346 #define ABSTRACT_TYPELOC(CLASS, BASE)
347 #define TYPELOC(CLASS, BASE) \
348 bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
349 #include "clang/AST/TypeLocNodes.def"
350 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
352 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
353 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
354 bool VisitTypeLoc(TypeLoc TL) { return true; }
356 // QualifiedTypeLoc and UnqualTypeLoc are not declared in
357 // TypeNodes.def and thus need to be handled specially.
358 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
359 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
361 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
362 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
363 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
365 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
367 // Note that BASE includes trailing 'Type' which CLASS doesn't.
368 #define TYPE(CLASS, BASE) \
369 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
370 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \
371 TRY_TO(Visit##CLASS##TypeLoc(TL)); \
374 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
375 #include "clang/AST/TypeNodes.def"
377 // ---- Methods on Decls ----
379 // Declare Traverse*() for all concrete Decl classes.
380 #define ABSTRACT_DECL(DECL)
381 #define DECL(CLASS, BASE) \
382 bool Traverse##CLASS##Decl(CLASS##Decl *D);
383 #include "clang/AST/DeclNodes.inc"
384 // The above header #undefs ABSTRACT_DECL and DECL upon exit.
386 // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
387 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
388 bool VisitDecl(Decl *D) { return true; }
389 #define DECL(CLASS, BASE) \
390 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \
391 TRY_TO(WalkUpFrom##BASE(D)); \
392 TRY_TO(Visit##CLASS##Decl(D)); \
395 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
396 #include "clang/AST/DeclNodes.inc"
399 // These are helper methods used by more than one Traverse* method.
400 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
401 bool TraverseClassInstantiations(ClassTemplateDecl *D);
402 bool TraverseFunctionInstantiations(FunctionTemplateDecl *D) ;
403 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
405 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
406 bool TraverseRecordHelper(RecordDecl *D);
407 bool TraverseCXXRecordHelper(CXXRecordDecl *D);
408 bool TraverseDeclaratorHelper(DeclaratorDecl *D);
409 bool TraverseDeclContextHelper(DeclContext *DC);
410 bool TraverseFunctionHelper(FunctionDecl *D);
411 bool TraverseVarHelper(VarDecl *D);
415 Stmt::child_iterator StmtIt;
417 EnqueueJob(Stmt *S) : S(S), StmtIt() {}
419 bool dataTraverse(Stmt *S);
420 bool dataTraverseNode(Stmt *S, bool &EnqueueChildren);
423 template<typename Derived>
424 bool RecursiveASTVisitor<Derived>::dataTraverse(Stmt *S) {
426 SmallVector<EnqueueJob, 16> Queue;
429 while (!Queue.empty()) {
430 EnqueueJob &job = Queue.back();
437 if (getDerived().shouldUseDataRecursionFor(CurrS)) {
438 if (job.StmtIt == Stmt::child_iterator()) {
439 bool EnqueueChildren = true;
440 if (!dataTraverseNode(CurrS, EnqueueChildren)) return false;
441 if (!EnqueueChildren) {
445 job.StmtIt = CurrS->child_begin();
450 if (job.StmtIt != CurrS->child_end())
451 Queue.push_back(*job.StmtIt);
458 TRY_TO(TraverseStmt(CurrS));
464 template<typename Derived>
465 bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S,
466 bool &EnqueueChildren) {
468 // Dispatch to the corresponding WalkUpFrom* function only if the derived
469 // class didn't override Traverse* (and thus the traversal is trivial).
470 #define DISPATCH_WALK(NAME, CLASS, VAR) \
472 bool (Derived::*DerivedFn)(CLASS*) = &Derived::Traverse##NAME; \
473 bool (Derived::*BaseFn)(CLASS*) = &RecursiveASTVisitor::Traverse##NAME; \
474 if (DerivedFn == BaseFn) \
475 return getDerived().WalkUpFrom##NAME(static_cast<CLASS*>(VAR)); \
477 EnqueueChildren = false; \
478 return getDerived().Traverse##NAME(static_cast<CLASS*>(VAR));
480 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
481 switch (BinOp->getOpcode()) {
482 #define OPERATOR(NAME) \
483 case BO_##NAME: DISPATCH_WALK(Bin##NAME, BinaryOperator, S);
488 #define OPERATOR(NAME) \
489 case BO_##NAME##Assign: \
490 DISPATCH_WALK(Bin##NAME##Assign, CompoundAssignOperator, S);
495 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
496 switch (UnOp->getOpcode()) {
497 #define OPERATOR(NAME) \
498 case UO_##NAME: DISPATCH_WALK(Unary##NAME, UnaryOperator, S);
505 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
506 switch (S->getStmtClass()) {
507 case Stmt::NoStmtClass: break;
508 #define ABSTRACT_STMT(STMT)
509 #define STMT(CLASS, PARENT) \
510 case Stmt::CLASS##Class: DISPATCH_WALK(CLASS, CLASS, S);
511 #include "clang/AST/StmtNodes.inc"
519 #define DISPATCH(NAME, CLASS, VAR) \
520 return getDerived().Traverse##NAME(static_cast<CLASS*>(VAR))
522 template<typename Derived>
523 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S) {
527 if (getDerived().shouldUseDataRecursionFor(S))
528 return dataTraverse(S);
530 // If we have a binary expr, dispatch to the subcode of the binop. A smart
531 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
533 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
534 switch (BinOp->getOpcode()) {
535 #define OPERATOR(NAME) \
536 case BO_##NAME: DISPATCH(Bin##NAME, BinaryOperator, S);
542 #define OPERATOR(NAME) \
543 case BO_##NAME##Assign: \
544 DISPATCH(Bin##NAME##Assign, CompoundAssignOperator, S);
550 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
551 switch (UnOp->getOpcode()) {
552 #define OPERATOR(NAME) \
553 case UO_##NAME: DISPATCH(Unary##NAME, UnaryOperator, S);
561 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
562 switch (S->getStmtClass()) {
563 case Stmt::NoStmtClass: break;
564 #define ABSTRACT_STMT(STMT)
565 #define STMT(CLASS, PARENT) \
566 case Stmt::CLASS##Class: DISPATCH(CLASS, CLASS, S);
567 #include "clang/AST/StmtNodes.inc"
573 template<typename Derived>
574 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
578 switch (T->getTypeClass()) {
579 #define ABSTRACT_TYPE(CLASS, BASE)
580 #define TYPE(CLASS, BASE) \
581 case Type::CLASS: DISPATCH(CLASS##Type, CLASS##Type, \
582 const_cast<Type*>(T.getTypePtr()));
583 #include "clang/AST/TypeNodes.def"
589 template<typename Derived>
590 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
594 switch (TL.getTypeLocClass()) {
595 #define ABSTRACT_TYPELOC(CLASS, BASE)
596 #define TYPELOC(CLASS, BASE) \
597 case TypeLoc::CLASS: \
598 return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>());
599 #include "clang/AST/TypeLocNodes.def"
606 template<typename Derived>
607 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
611 // As a syntax visitor, by default we want to ignore declarations for
612 // implicit declarations (ones not typed explicitly by the user).
613 if (!getDerived().shouldVisitImplicitCode() && D->isImplicit())
616 switch (D->getKind()) {
617 #define ABSTRACT_DECL(DECL)
618 #define DECL(CLASS, BASE) \
619 case Decl::CLASS: DISPATCH(CLASS##Decl, CLASS##Decl, D);
620 #include "clang/AST/DeclNodes.inc"
628 template<typename Derived>
629 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
630 NestedNameSpecifier *NNS) {
634 if (NNS->getPrefix())
635 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
637 switch (NNS->getKind()) {
638 case NestedNameSpecifier::Identifier:
639 case NestedNameSpecifier::Namespace:
640 case NestedNameSpecifier::NamespaceAlias:
641 case NestedNameSpecifier::Global:
644 case NestedNameSpecifier::TypeSpec:
645 case NestedNameSpecifier::TypeSpecWithTemplate:
646 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
652 template<typename Derived>
653 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
654 NestedNameSpecifierLoc NNS) {
658 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
659 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
661 switch (NNS.getNestedNameSpecifier()->getKind()) {
662 case NestedNameSpecifier::Identifier:
663 case NestedNameSpecifier::Namespace:
664 case NestedNameSpecifier::NamespaceAlias:
665 case NestedNameSpecifier::Global:
668 case NestedNameSpecifier::TypeSpec:
669 case NestedNameSpecifier::TypeSpecWithTemplate:
670 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
677 template<typename Derived>
678 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
679 DeclarationNameInfo NameInfo) {
680 switch (NameInfo.getName().getNameKind()) {
681 case DeclarationName::CXXConstructorName:
682 case DeclarationName::CXXDestructorName:
683 case DeclarationName::CXXConversionFunctionName:
684 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
685 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
689 case DeclarationName::Identifier:
690 case DeclarationName::ObjCZeroArgSelector:
691 case DeclarationName::ObjCOneArgSelector:
692 case DeclarationName::ObjCMultiArgSelector:
693 case DeclarationName::CXXOperatorName:
694 case DeclarationName::CXXLiteralOperatorName:
695 case DeclarationName::CXXUsingDirective:
702 template<typename Derived>
703 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
704 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
705 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
706 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
707 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
712 template<typename Derived>
713 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
714 const TemplateArgument &Arg) {
715 switch (Arg.getKind()) {
716 case TemplateArgument::Null:
717 case TemplateArgument::Declaration:
718 case TemplateArgument::Integral:
719 case TemplateArgument::NullPtr:
722 case TemplateArgument::Type:
723 return getDerived().TraverseType(Arg.getAsType());
725 case TemplateArgument::Template:
726 case TemplateArgument::TemplateExpansion:
727 return getDerived().TraverseTemplateName(
728 Arg.getAsTemplateOrTemplatePattern());
730 case TemplateArgument::Expression:
731 return getDerived().TraverseStmt(Arg.getAsExpr());
733 case TemplateArgument::Pack:
734 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
741 // FIXME: no template name location?
742 // FIXME: no source locations for a template argument pack?
743 template<typename Derived>
744 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
745 const TemplateArgumentLoc &ArgLoc) {
746 const TemplateArgument &Arg = ArgLoc.getArgument();
748 switch (Arg.getKind()) {
749 case TemplateArgument::Null:
750 case TemplateArgument::Declaration:
751 case TemplateArgument::Integral:
752 case TemplateArgument::NullPtr:
755 case TemplateArgument::Type: {
756 // FIXME: how can TSI ever be NULL?
757 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
758 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
760 return getDerived().TraverseType(Arg.getAsType());
763 case TemplateArgument::Template:
764 case TemplateArgument::TemplateExpansion:
765 if (ArgLoc.getTemplateQualifierLoc())
766 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
767 ArgLoc.getTemplateQualifierLoc()));
768 return getDerived().TraverseTemplateName(
769 Arg.getAsTemplateOrTemplatePattern());
771 case TemplateArgument::Expression:
772 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
774 case TemplateArgument::Pack:
775 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
782 template<typename Derived>
783 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
784 const TemplateArgument *Args,
786 for (unsigned I = 0; I != NumArgs; ++I) {
787 TRY_TO(TraverseTemplateArgument(Args[I]));
793 template<typename Derived>
794 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
795 CXXCtorInitializer *Init) {
796 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
797 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
799 if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
800 TRY_TO(TraverseStmt(Init->getInit()));
804 template<typename Derived>
805 bool RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr::Capture C){
809 // ----------------- Type traversal -----------------
811 // This macro makes available a variable T, the passed-in type.
812 #define DEF_TRAVERSE_TYPE(TYPE, CODE) \
813 template<typename Derived> \
814 bool RecursiveASTVisitor<Derived>::Traverse##TYPE (TYPE *T) { \
815 TRY_TO(WalkUpFrom##TYPE (T)); \
820 DEF_TRAVERSE_TYPE(BuiltinType, { })
822 DEF_TRAVERSE_TYPE(ComplexType, {
823 TRY_TO(TraverseType(T->getElementType()));
826 DEF_TRAVERSE_TYPE(PointerType, {
827 TRY_TO(TraverseType(T->getPointeeType()));
830 DEF_TRAVERSE_TYPE(BlockPointerType, {
831 TRY_TO(TraverseType(T->getPointeeType()));
834 DEF_TRAVERSE_TYPE(LValueReferenceType, {
835 TRY_TO(TraverseType(T->getPointeeType()));
838 DEF_TRAVERSE_TYPE(RValueReferenceType, {
839 TRY_TO(TraverseType(T->getPointeeType()));
842 DEF_TRAVERSE_TYPE(MemberPointerType, {
843 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
844 TRY_TO(TraverseType(T->getPointeeType()));
847 DEF_TRAVERSE_TYPE(ConstantArrayType, {
848 TRY_TO(TraverseType(T->getElementType()));
851 DEF_TRAVERSE_TYPE(IncompleteArrayType, {
852 TRY_TO(TraverseType(T->getElementType()));
855 DEF_TRAVERSE_TYPE(VariableArrayType, {
856 TRY_TO(TraverseType(T->getElementType()));
857 TRY_TO(TraverseStmt(T->getSizeExpr()));
860 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
861 TRY_TO(TraverseType(T->getElementType()));
862 if (T->getSizeExpr())
863 TRY_TO(TraverseStmt(T->getSizeExpr()));
866 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
867 if (T->getSizeExpr())
868 TRY_TO(TraverseStmt(T->getSizeExpr()));
869 TRY_TO(TraverseType(T->getElementType()));
872 DEF_TRAVERSE_TYPE(VectorType, {
873 TRY_TO(TraverseType(T->getElementType()));
876 DEF_TRAVERSE_TYPE(ExtVectorType, {
877 TRY_TO(TraverseType(T->getElementType()));
880 DEF_TRAVERSE_TYPE(FunctionNoProtoType, {
881 TRY_TO(TraverseType(T->getResultType()));
884 DEF_TRAVERSE_TYPE(FunctionProtoType, {
885 TRY_TO(TraverseType(T->getResultType()));
887 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
888 AEnd = T->arg_type_end();
890 TRY_TO(TraverseType(*A));
893 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
894 EEnd = T->exception_end();
896 TRY_TO(TraverseType(*E));
900 DEF_TRAVERSE_TYPE(UnresolvedUsingType, { })
901 DEF_TRAVERSE_TYPE(TypedefType, { })
903 DEF_TRAVERSE_TYPE(TypeOfExprType, {
904 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
907 DEF_TRAVERSE_TYPE(TypeOfType, {
908 TRY_TO(TraverseType(T->getUnderlyingType()));
911 DEF_TRAVERSE_TYPE(DecltypeType, {
912 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
915 DEF_TRAVERSE_TYPE(UnaryTransformType, {
916 TRY_TO(TraverseType(T->getBaseType()));
917 TRY_TO(TraverseType(T->getUnderlyingType()));
920 DEF_TRAVERSE_TYPE(AutoType, {
921 TRY_TO(TraverseType(T->getDeducedType()));
924 DEF_TRAVERSE_TYPE(RecordType, { })
925 DEF_TRAVERSE_TYPE(EnumType, { })
926 DEF_TRAVERSE_TYPE(TemplateTypeParmType, { })
927 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, { })
928 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, { })
930 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
931 TRY_TO(TraverseTemplateName(T->getTemplateName()));
932 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
935 DEF_TRAVERSE_TYPE(InjectedClassNameType, { })
937 DEF_TRAVERSE_TYPE(AttributedType, {
938 TRY_TO(TraverseType(T->getModifiedType()));
941 DEF_TRAVERSE_TYPE(ParenType, {
942 TRY_TO(TraverseType(T->getInnerType()));
945 DEF_TRAVERSE_TYPE(ElaboratedType, {
946 if (T->getQualifier()) {
947 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
949 TRY_TO(TraverseType(T->getNamedType()));
952 DEF_TRAVERSE_TYPE(DependentNameType, {
953 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
956 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
957 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
958 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
961 DEF_TRAVERSE_TYPE(PackExpansionType, {
962 TRY_TO(TraverseType(T->getPattern()));
965 DEF_TRAVERSE_TYPE(ObjCInterfaceType, { })
967 DEF_TRAVERSE_TYPE(ObjCObjectType, {
968 // We have to watch out here because an ObjCInterfaceType's base
970 if (T->getBaseType().getTypePtr() != T)
971 TRY_TO(TraverseType(T->getBaseType()));
974 DEF_TRAVERSE_TYPE(ObjCObjectPointerType, {
975 TRY_TO(TraverseType(T->getPointeeType()));
978 DEF_TRAVERSE_TYPE(AtomicType, {
979 TRY_TO(TraverseType(T->getValueType()));
982 #undef DEF_TRAVERSE_TYPE
984 // ----------------- TypeLoc traversal -----------------
986 // This macro makes available a variable TL, the passed-in TypeLoc.
987 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
988 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
989 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
991 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
992 template<typename Derived> \
993 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
994 if (getDerived().shouldWalkTypesOfTypeLocs()) \
995 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE*>(TL.getTypePtr()))); \
996 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
1001 template<typename Derived>
1002 bool RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(
1003 QualifiedTypeLoc TL) {
1004 // Move this over to the 'main' typeloc tree. Note that this is a
1005 // move -- we pretend that we were really looking at the unqualified
1006 // typeloc all along -- rather than a recursion, so we don't follow
1007 // the normal CRTP plan of going through
1008 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
1009 // twice for the same type (once as a QualifiedTypeLoc version of
1010 // the type, once as an UnqualifiedTypeLoc version of the type),
1011 // which in effect means we'd call VisitTypeLoc twice with the
1012 // 'same' type. This solves that problem, at the cost of never
1013 // seeing the qualified version of the type (unless the client
1014 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
1015 // perfect solution. A perfect solution probably requires making
1016 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1017 // wrapper around Type* -- rather than being its own class in the
1019 return TraverseTypeLoc(TL.getUnqualifiedLoc());
1022 DEF_TRAVERSE_TYPELOC(BuiltinType, { })
1024 // FIXME: ComplexTypeLoc is unfinished
1025 DEF_TRAVERSE_TYPELOC(ComplexType, {
1026 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1029 DEF_TRAVERSE_TYPELOC(PointerType, {
1030 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1033 DEF_TRAVERSE_TYPELOC(BlockPointerType, {
1034 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1037 DEF_TRAVERSE_TYPELOC(LValueReferenceType, {
1038 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1041 DEF_TRAVERSE_TYPELOC(RValueReferenceType, {
1042 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1045 // FIXME: location of base class?
1046 // We traverse this in the type case as well, but how is it not reached through
1047 // the pointee type?
1048 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1049 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1050 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1053 template<typename Derived>
1054 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1055 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1056 TRY_TO(TraverseStmt(TL.getSizeExpr()));
1060 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1061 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1062 return TraverseArrayTypeLocHelper(TL);
1065 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1066 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1067 return TraverseArrayTypeLocHelper(TL);
1070 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1071 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1072 return TraverseArrayTypeLocHelper(TL);
1075 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1076 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1077 return TraverseArrayTypeLocHelper(TL);
1080 // FIXME: order? why not size expr first?
1081 // FIXME: base VectorTypeLoc is unfinished
1082 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1083 if (TL.getTypePtr()->getSizeExpr())
1084 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1085 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1088 // FIXME: VectorTypeLoc is unfinished
1089 DEF_TRAVERSE_TYPELOC(VectorType, {
1090 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1093 // FIXME: size and attributes
1094 // FIXME: base VectorTypeLoc is unfinished
1095 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1096 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1099 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, {
1100 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1103 // FIXME: location of exception specifications (attributes?)
1104 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1105 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1107 const FunctionProtoType *T = TL.getTypePtr();
1109 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1111 TRY_TO(TraverseDecl(TL.getArg(I)));
1112 } else if (I < T->getNumArgs()) {
1113 TRY_TO(TraverseType(T->getArgType(I)));
1117 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1118 EEnd = T->exception_end();
1120 TRY_TO(TraverseType(*E));
1124 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, { })
1125 DEF_TRAVERSE_TYPELOC(TypedefType, { })
1127 DEF_TRAVERSE_TYPELOC(TypeOfExprType, {
1128 TRY_TO(TraverseStmt(TL.getUnderlyingExpr()));
1131 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1132 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1135 // FIXME: location of underlying expr
1136 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1137 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1140 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1141 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1144 DEF_TRAVERSE_TYPELOC(AutoType, {
1145 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1148 DEF_TRAVERSE_TYPELOC(RecordType, { })
1149 DEF_TRAVERSE_TYPELOC(EnumType, { })
1150 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, { })
1151 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, { })
1152 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, { })
1154 // FIXME: use the loc for the template name?
1155 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1156 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1157 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1158 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1162 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, { })
1164 DEF_TRAVERSE_TYPELOC(ParenType, {
1165 TRY_TO(TraverseTypeLoc(TL.getInnerLoc()));
1168 DEF_TRAVERSE_TYPELOC(AttributedType, {
1169 TRY_TO(TraverseTypeLoc(TL.getModifiedLoc()));
1172 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1173 if (TL.getQualifierLoc()) {
1174 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1176 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1179 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1180 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1183 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1184 if (TL.getQualifierLoc()) {
1185 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1188 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1189 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1193 DEF_TRAVERSE_TYPELOC(PackExpansionType, {
1194 TRY_TO(TraverseTypeLoc(TL.getPatternLoc()));
1197 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, { })
1199 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1200 // We have to watch out here because an ObjCInterfaceType's base
1202 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1203 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1206 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, {
1207 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1210 DEF_TRAVERSE_TYPELOC(AtomicType, {
1211 TRY_TO(TraverseTypeLoc(TL.getValueLoc()));
1214 #undef DEF_TRAVERSE_TYPELOC
1216 // ----------------- Decl traversal -----------------
1218 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1219 // the children that come from the DeclContext associated with it.
1220 // Therefore each Traverse* only needs to worry about children other
1223 template<typename Derived>
1224 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1228 for (DeclContext::decl_iterator Child = DC->decls_begin(),
1229 ChildEnd = DC->decls_end();
1230 Child != ChildEnd; ++Child) {
1231 // BlockDecls and CapturedDecls are traversed through BlockExprs and
1232 // CapturedStmts respectively.
1233 if (!isa<BlockDecl>(*Child) && !isa<CapturedDecl>(*Child))
1234 TRY_TO(TraverseDecl(*Child));
1240 // This macro makes available a variable D, the passed-in decl.
1241 #define DEF_TRAVERSE_DECL(DECL, CODE) \
1242 template<typename Derived> \
1243 bool RecursiveASTVisitor<Derived>::Traverse##DECL (DECL *D) { \
1244 TRY_TO(WalkUpFrom##DECL (D)); \
1246 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1250 DEF_TRAVERSE_DECL(AccessSpecDecl, { })
1252 DEF_TRAVERSE_DECL(BlockDecl, {
1253 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1254 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1255 TRY_TO(TraverseStmt(D->getBody()));
1256 // This return statement makes sure the traversal of nodes in
1257 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1258 // is skipped - don't remove it.
1262 DEF_TRAVERSE_DECL(CapturedDecl, {
1263 TRY_TO(TraverseStmt(D->getBody()));
1264 // This return statement makes sure the traversal of nodes in
1265 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1266 // is skipped - don't remove it.
1270 DEF_TRAVERSE_DECL(EmptyDecl, { })
1272 DEF_TRAVERSE_DECL(FileScopeAsmDecl, {
1273 TRY_TO(TraverseStmt(D->getAsmString()));
1276 DEF_TRAVERSE_DECL(ImportDecl, { })
1278 DEF_TRAVERSE_DECL(FriendDecl, {
1279 // Friend is either decl or a type.
1280 if (D->getFriendType())
1281 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1283 TRY_TO(TraverseDecl(D->getFriendDecl()));
1286 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1287 if (D->getFriendType())
1288 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1290 TRY_TO(TraverseDecl(D->getFriendDecl()));
1291 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1292 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1293 for (TemplateParameterList::iterator ITPL = TPL->begin(),
1295 ITPL != ETPL; ++ITPL) {
1296 TRY_TO(TraverseDecl(*ITPL));
1301 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1302 TRY_TO(TraverseDecl(D->getSpecialization()));
1304 if (D->hasExplicitTemplateArgs()) {
1305 const TemplateArgumentListInfo& args = D->templateArgs();
1306 TRY_TO(TraverseTemplateArgumentLocsHelper(
1307 args.getArgumentArray(), args.size()));
1311 DEF_TRAVERSE_DECL(LinkageSpecDecl, { })
1313 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {
1314 // FIXME: implement this
1317 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1318 TRY_TO(TraverseStmt(D->getAssertExpr()));
1319 TRY_TO(TraverseStmt(D->getMessage()));
1322 DEF_TRAVERSE_DECL(TranslationUnitDecl, {
1323 // Code in an unnamed namespace shows up automatically in
1324 // decls_begin()/decls_end(). Thus we don't need to recurse on
1325 // D->getAnonymousNamespace().
1328 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1329 // We shouldn't traverse an aliased namespace, since it will be
1330 // defined (and, therefore, traversed) somewhere else.
1332 // This return statement makes sure the traversal of nodes in
1333 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1334 // is skipped - don't remove it.
1338 DEF_TRAVERSE_DECL(LabelDecl, {
1339 // There is no code in a LabelDecl.
1343 DEF_TRAVERSE_DECL(NamespaceDecl, {
1344 // Code in an unnamed namespace shows up automatically in
1345 // decls_begin()/decls_end(). Thus we don't need to recurse on
1346 // D->getAnonymousNamespace().
1349 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {
1353 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {
1357 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {
1361 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {
1365 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {
1369 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {
1373 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1374 if (D->getResultTypeSourceInfo()) {
1375 TRY_TO(TraverseTypeLoc(D->getResultTypeSourceInfo()->getTypeLoc()));
1377 for (ObjCMethodDecl::param_iterator
1378 I = D->param_begin(), E = D->param_end(); I != E; ++I) {
1379 TRY_TO(TraverseDecl(*I));
1381 if (D->isThisDeclarationADefinition()) {
1382 TRY_TO(TraverseStmt(D->getBody()));
1387 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1391 DEF_TRAVERSE_DECL(UsingDecl, {
1392 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1393 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1396 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1397 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1400 DEF_TRAVERSE_DECL(UsingShadowDecl, { })
1402 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1403 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1404 E = D->varlist_end();
1406 TRY_TO(TraverseStmt(*I));
1410 // A helper method for TemplateDecl's children.
1411 template<typename Derived>
1412 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1413 TemplateParameterList *TPL) {
1415 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1417 TRY_TO(TraverseDecl(*I));
1423 // A helper method for traversing the implicit instantiations of a
1425 template<typename Derived>
1426 bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations(
1427 ClassTemplateDecl *D) {
1428 ClassTemplateDecl::spec_iterator end = D->spec_end();
1429 for (ClassTemplateDecl::spec_iterator it = D->spec_begin(); it != end; ++it) {
1430 ClassTemplateSpecializationDecl* SD = *it;
1432 switch (SD->getSpecializationKind()) {
1433 // Visit the implicit instantiations with the requested pattern.
1434 case TSK_Undeclared:
1435 case TSK_ImplicitInstantiation:
1436 TRY_TO(TraverseDecl(SD));
1439 // We don't need to do anything on an explicit instantiation
1440 // or explicit specialization because there will be an explicit
1441 // node for it elsewhere.
1442 case TSK_ExplicitInstantiationDeclaration:
1443 case TSK_ExplicitInstantiationDefinition:
1444 case TSK_ExplicitSpecialization:
1452 DEF_TRAVERSE_DECL(ClassTemplateDecl, {
1453 CXXRecordDecl* TempDecl = D->getTemplatedDecl();
1454 TRY_TO(TraverseDecl(TempDecl));
1455 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1457 // By default, we do not traverse the instantiations of
1458 // class templates since they do not appear in the user code. The
1459 // following code optionally traverses them.
1461 // We only traverse the class instantiations when we see the canonical
1462 // declaration of the template, to ensure we only visit them once.
1463 if (getDerived().shouldVisitTemplateInstantiations() &&
1464 D == D->getCanonicalDecl())
1465 TRY_TO(TraverseClassInstantiations(D));
1467 // Note that getInstantiatedFromMemberTemplate() is just a link
1468 // from a template instantiation back to the template from which
1469 // it was instantiated, and thus should not be traversed.
1472 // A helper method for traversing the instantiations of a
1473 // function while skipping its specializations.
1474 template<typename Derived>
1475 bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations(
1476 FunctionTemplateDecl *D) {
1477 FunctionTemplateDecl::spec_iterator end = D->spec_end();
1478 for (FunctionTemplateDecl::spec_iterator it = D->spec_begin(); it != end;
1480 FunctionDecl* FD = *it;
1481 switch (FD->getTemplateSpecializationKind()) {
1482 case TSK_Undeclared:
1483 case TSK_ImplicitInstantiation:
1484 // We don't know what kind of FunctionDecl this is.
1485 TRY_TO(TraverseDecl(FD));
1488 // FIXME: For now traverse explicit instantiations here. Change that
1489 // once they are represented as dedicated nodes in the AST.
1490 case TSK_ExplicitInstantiationDeclaration:
1491 case TSK_ExplicitInstantiationDefinition:
1492 TRY_TO(TraverseDecl(FD));
1495 case TSK_ExplicitSpecialization:
1503 DEF_TRAVERSE_DECL(FunctionTemplateDecl, {
1504 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1505 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1507 // By default, we do not traverse the instantiations of
1508 // function templates since they do not appear in the user code. The
1509 // following code optionally traverses them.
1511 // We only traverse the function instantiations when we see the canonical
1512 // declaration of the template, to ensure we only visit them once.
1513 if (getDerived().shouldVisitTemplateInstantiations() &&
1514 D == D->getCanonicalDecl())
1515 TRY_TO(TraverseFunctionInstantiations(D));
1518 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1519 // D is the "T" in something like
1520 // template <template <typename> class T> class container { };
1521 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1522 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1523 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1525 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1528 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1529 // D is the "T" in something like "template<typename T> class vector;"
1530 if (D->getTypeForDecl())
1531 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1532 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1533 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1536 DEF_TRAVERSE_DECL(TypedefDecl, {
1537 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1538 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1539 // declaring the typedef, not something that was written in the
1543 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1544 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1545 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1546 // declaring the type alias, not something that was written in the
1550 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1551 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1552 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1555 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1556 // A dependent using declaration which was marked with 'typename'.
1557 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1558 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1559 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1560 // declaring the type, not something that was written in the
1564 DEF_TRAVERSE_DECL(EnumDecl, {
1565 if (D->getTypeForDecl())
1566 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1568 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1569 // The enumerators are already traversed by
1570 // decls_begin()/decls_end().
1574 // Helper methods for RecordDecl and its children.
1575 template<typename Derived>
1576 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(
1578 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1579 // declaring the type, not something that was written in the source.
1581 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1585 template<typename Derived>
1586 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(
1588 if (!TraverseRecordHelper(D))
1590 if (D->isCompleteDefinition()) {
1591 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1594 TRY_TO(TraverseTypeLoc(I->getTypeSourceInfo()->getTypeLoc()));
1596 // We don't traverse the friends or the conversions, as they are
1597 // already in decls_begin()/decls_end().
1602 DEF_TRAVERSE_DECL(RecordDecl, {
1603 TRY_TO(TraverseRecordHelper(D));
1606 DEF_TRAVERSE_DECL(CXXRecordDecl, {
1607 TRY_TO(TraverseCXXRecordHelper(D));
1610 DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, {
1611 // For implicit instantiations ("set<int> x;"), we don't want to
1612 // recurse at all, since the instatiated class isn't written in
1613 // the source code anywhere. (Note the instatiated *type* --
1614 // set<int> -- is written, and will still get a callback of
1615 // TemplateSpecializationType). For explicit instantiations
1616 // ("template set<int>;"), we do need a callback, since this
1617 // is the only callback that's made for this instantiation.
1618 // We use getTypeAsWritten() to distinguish.
1619 if (TypeSourceInfo *TSI = D->getTypeAsWritten())
1620 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1622 if (!getDerived().shouldVisitTemplateInstantiations() &&
1623 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1624 // Returning from here skips traversing the
1625 // declaration context of the ClassTemplateSpecializationDecl
1626 // (embedded in the DEF_TRAVERSE_DECL() macro)
1627 // which contains the instantiated members of the class.
1631 template <typename Derived>
1632 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1633 const TemplateArgumentLoc *TAL, unsigned Count) {
1634 for (unsigned I = 0; I < Count; ++I) {
1635 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1640 DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, {
1641 // The partial specialization.
1642 if (TemplateParameterList *TPL = D->getTemplateParameters()) {
1643 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1645 TRY_TO(TraverseDecl(*I));
1648 // The args that remains unspecialized.
1649 TRY_TO(TraverseTemplateArgumentLocsHelper(
1650 D->getTemplateArgsAsWritten(), D->getNumTemplateArgsAsWritten()));
1652 // Don't need the ClassTemplatePartialSpecializationHelper, even
1653 // though that's our parent class -- we already visit all the
1654 // template args here.
1655 TRY_TO(TraverseCXXRecordHelper(D));
1657 // Instantiations will have been visited with the primary template.
1660 DEF_TRAVERSE_DECL(EnumConstantDecl, {
1661 TRY_TO(TraverseStmt(D->getInitExpr()));
1664 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1665 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1666 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1667 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1668 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1671 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1673 template<typename Derived>
1674 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1675 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1676 if (D->getTypeSourceInfo())
1677 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1679 TRY_TO(TraverseType(D->getType()));
1683 DEF_TRAVERSE_DECL(MSPropertyDecl, {
1684 TRY_TO(TraverseDeclaratorHelper(D));
1687 DEF_TRAVERSE_DECL(FieldDecl, {
1688 TRY_TO(TraverseDeclaratorHelper(D));
1689 if (D->isBitField())
1690 TRY_TO(TraverseStmt(D->getBitWidth()));
1691 else if (D->hasInClassInitializer())
1692 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1695 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1696 TRY_TO(TraverseDeclaratorHelper(D));
1697 if (D->isBitField())
1698 TRY_TO(TraverseStmt(D->getBitWidth()));
1699 // FIXME: implement the rest.
1702 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1703 TRY_TO(TraverseDeclaratorHelper(D));
1704 if (D->isBitField())
1705 TRY_TO(TraverseStmt(D->getBitWidth()));
1706 // FIXME: implement the rest.
1709 template<typename Derived>
1710 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1711 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1712 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1714 // If we're an explicit template specialization, iterate over the
1715 // template args that were explicitly specified. If we were doing
1716 // this in typing order, we'd do it between the return type and
1717 // the function args, but both are handled by the FunctionTypeLoc
1718 // above, so we have to choose one side. I've decided to do before.
1719 if (const FunctionTemplateSpecializationInfo *FTSI =
1720 D->getTemplateSpecializationInfo()) {
1721 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1722 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1723 // A specialization might not have explicit template arguments if it has
1724 // a templated return type and concrete arguments.
1725 if (const ASTTemplateArgumentListInfo *TALI =
1726 FTSI->TemplateArgumentsAsWritten) {
1727 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1728 TALI->NumTemplateArgs));
1733 // Visit the function type itself, which can be either
1734 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1735 // also covers the return type and the function parameters,
1736 // including exception specifications.
1737 if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1738 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1741 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1742 // Constructor initializers.
1743 for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
1744 E = Ctor->init_end();
1746 TRY_TO(TraverseConstructorInitializer(*I));
1750 if (D->isThisDeclarationADefinition()) {
1751 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1756 DEF_TRAVERSE_DECL(FunctionDecl, {
1757 // We skip decls_begin/decls_end, which are already covered by
1758 // TraverseFunctionHelper().
1759 return TraverseFunctionHelper(D);
1762 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1763 // We skip decls_begin/decls_end, which are already covered by
1764 // TraverseFunctionHelper().
1765 return TraverseFunctionHelper(D);
1768 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1769 // We skip decls_begin/decls_end, which are already covered by
1770 // TraverseFunctionHelper().
1771 return TraverseFunctionHelper(D);
1774 // CXXConversionDecl is the declaration of a type conversion operator.
1775 // It's not a cast expression.
1776 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1777 // We skip decls_begin/decls_end, which are already covered by
1778 // TraverseFunctionHelper().
1779 return TraverseFunctionHelper(D);
1782 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1783 // We skip decls_begin/decls_end, which are already covered by
1784 // TraverseFunctionHelper().
1785 return TraverseFunctionHelper(D);
1788 template<typename Derived>
1789 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1790 TRY_TO(TraverseDeclaratorHelper(D));
1791 // Default params are taken care of when we traverse the ParmVarDecl.
1792 if (!isa<ParmVarDecl>(D) &&
1793 (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
1794 TRY_TO(TraverseStmt(D->getInit()));
1798 DEF_TRAVERSE_DECL(VarDecl, {
1799 TRY_TO(TraverseVarHelper(D));
1802 DEF_TRAVERSE_DECL(ImplicitParamDecl, {
1803 TRY_TO(TraverseVarHelper(D));
1806 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1807 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1808 TRY_TO(TraverseDeclaratorHelper(D));
1809 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1810 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1813 DEF_TRAVERSE_DECL(ParmVarDecl, {
1814 TRY_TO(TraverseVarHelper(D));
1816 if (D->hasDefaultArg() &&
1817 D->hasUninstantiatedDefaultArg() &&
1818 !D->hasUnparsedDefaultArg())
1819 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1821 if (D->hasDefaultArg() &&
1822 !D->hasUninstantiatedDefaultArg() &&
1823 !D->hasUnparsedDefaultArg())
1824 TRY_TO(TraverseStmt(D->getDefaultArg()));
1827 #undef DEF_TRAVERSE_DECL
1829 // ----------------- Stmt traversal -----------------
1831 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1832 // over the children defined in children() (every stmt defines these,
1833 // though sometimes the range is empty). Each individual Traverse*
1834 // method only needs to worry about children other than those. To see
1835 // what children() does for a given class, see, e.g.,
1836 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1838 // This macro makes available a variable S, the passed-in stmt.
1839 #define DEF_TRAVERSE_STMT(STMT, CODE) \
1840 template<typename Derived> \
1841 bool RecursiveASTVisitor<Derived>::Traverse##STMT (STMT *S) { \
1842 TRY_TO(WalkUpFrom##STMT(S)); \
1844 for (Stmt::child_range range = S->children(); range; ++range) { \
1845 TRY_TO(TraverseStmt(*range)); \
1850 DEF_TRAVERSE_STMT(GCCAsmStmt, {
1851 TRY_TO(TraverseStmt(S->getAsmString()));
1852 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1853 TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1855 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1856 TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1858 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1859 TRY_TO(TraverseStmt(S->getClobberStringLiteral(I)));
1861 // children() iterates over inputExpr and outputExpr.
1864 DEF_TRAVERSE_STMT(MSAsmStmt, {
1865 // FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once
1866 // added this needs to be implemented.
1869 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1870 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1871 // children() iterates over the handler block.
1874 DEF_TRAVERSE_STMT(DeclStmt, {
1875 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
1877 TRY_TO(TraverseDecl(*I));
1879 // Suppress the default iteration over children() by
1880 // returning. Here's why: A DeclStmt looks like 'type var [=
1881 // initializer]'. The decls above already traverse over the
1882 // initializers, so we don't have to do it again (which
1883 // children() would do).
1888 // These non-expr stmts (most of them), do not need any action except
1889 // iterating over the children.
1890 DEF_TRAVERSE_STMT(BreakStmt, { })
1891 DEF_TRAVERSE_STMT(CXXTryStmt, { })
1892 DEF_TRAVERSE_STMT(CaseStmt, { })
1893 DEF_TRAVERSE_STMT(CompoundStmt, { })
1894 DEF_TRAVERSE_STMT(ContinueStmt, { })
1895 DEF_TRAVERSE_STMT(DefaultStmt, { })
1896 DEF_TRAVERSE_STMT(DoStmt, { })
1897 DEF_TRAVERSE_STMT(ForStmt, { })
1898 DEF_TRAVERSE_STMT(GotoStmt, { })
1899 DEF_TRAVERSE_STMT(IfStmt, { })
1900 DEF_TRAVERSE_STMT(IndirectGotoStmt, { })
1901 DEF_TRAVERSE_STMT(LabelStmt, { })
1902 DEF_TRAVERSE_STMT(AttributedStmt, { })
1903 DEF_TRAVERSE_STMT(NullStmt, { })
1904 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, { })
1905 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, { })
1906 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, { })
1907 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, { })
1908 DEF_TRAVERSE_STMT(ObjCAtTryStmt, { })
1909 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, { })
1910 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, { })
1911 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
1912 if (!getDerived().shouldVisitImplicitCode()) {
1913 TRY_TO(TraverseStmt(S->getLoopVarStmt()));
1914 TRY_TO(TraverseStmt(S->getRangeInit()));
1915 TRY_TO(TraverseStmt(S->getBody()));
1916 // Visit everything else only if shouldVisitImplicitCode().
1920 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1921 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1922 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1924 DEF_TRAVERSE_STMT(ReturnStmt, { })
1925 DEF_TRAVERSE_STMT(SwitchStmt, { })
1926 DEF_TRAVERSE_STMT(WhileStmt, { })
1929 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1930 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1931 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1932 if (S->hasExplicitTemplateArgs()) {
1933 TRY_TO(TraverseTemplateArgumentLocsHelper(
1934 S->getTemplateArgs(), S->getNumTemplateArgs()));
1938 DEF_TRAVERSE_STMT(DeclRefExpr, {
1939 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1940 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1941 TRY_TO(TraverseTemplateArgumentLocsHelper(
1942 S->getTemplateArgs(), S->getNumTemplateArgs()));
1945 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1946 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1947 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1948 if (S->hasExplicitTemplateArgs()) {
1949 TRY_TO(TraverseTemplateArgumentLocsHelper(
1950 S->getExplicitTemplateArgs().getTemplateArgs(),
1951 S->getNumTemplateArgs()));
1955 DEF_TRAVERSE_STMT(MemberExpr, {
1956 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1957 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1958 TRY_TO(TraverseTemplateArgumentLocsHelper(
1959 S->getTemplateArgs(), S->getNumTemplateArgs()));
1962 DEF_TRAVERSE_STMT(ImplicitCastExpr, {
1963 // We don't traverse the cast type, as it's not written in the
1967 DEF_TRAVERSE_STMT(CStyleCastExpr, {
1968 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1971 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
1972 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1975 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
1976 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1979 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
1980 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1983 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
1984 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1987 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
1988 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1991 // InitListExpr is a tricky one, because we want to do all our work on
1992 // the syntactic form of the listexpr, but this method takes the
1993 // semantic form by default. We can't use the macro helper because it
1994 // calls WalkUp*() on the semantic form, before our code can convert
1995 // to the syntactic form.
1996 template<typename Derived>
1997 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
1998 if (InitListExpr *Syn = S->getSyntacticForm())
2000 TRY_TO(WalkUpFromInitListExpr(S));
2001 // All we need are the default actions. FIXME: use a helper function.
2002 for (Stmt::child_range range = S->children(); range; ++range) {
2003 TRY_TO(TraverseStmt(*range));
2008 // GenericSelectionExpr is a special case because the types and expressions
2009 // are interleaved. We also need to watch out for null types (default
2010 // generic associations).
2011 template<typename Derived>
2012 bool RecursiveASTVisitor<Derived>::
2013 TraverseGenericSelectionExpr(GenericSelectionExpr *S) {
2014 TRY_TO(WalkUpFromGenericSelectionExpr(S));
2015 TRY_TO(TraverseStmt(S->getControllingExpr()));
2016 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
2017 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2018 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2019 TRY_TO(TraverseStmt(S->getAssocExpr(i)));
2024 // PseudoObjectExpr is a special case because of the wierdness with
2025 // syntactic expressions and opaque values.
2026 template<typename Derived>
2027 bool RecursiveASTVisitor<Derived>::
2028 TraversePseudoObjectExpr(PseudoObjectExpr *S) {
2029 TRY_TO(WalkUpFromPseudoObjectExpr(S));
2030 TRY_TO(TraverseStmt(S->getSyntacticForm()));
2031 for (PseudoObjectExpr::semantics_iterator
2032 i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i) {
2034 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2035 sub = OVE->getSourceExpr();
2036 TRY_TO(TraverseStmt(sub));
2041 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2042 // This is called for code like 'return T()' where T is a built-in
2043 // (i.e. non-class) type.
2044 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2047 DEF_TRAVERSE_STMT(CXXNewExpr, {
2048 // The child-iterator will pick up the other arguments.
2049 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2052 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2053 // The child-iterator will pick up the expression representing
2055 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2056 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2057 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2060 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2061 // The child-iterator will pick up the arg if it's an expression,
2062 // but not if it's a type.
2063 if (S->isArgumentType())
2064 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2067 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2068 // The child-iterator will pick up the arg if it's an expression,
2069 // but not if it's a type.
2070 if (S->isTypeOperand())
2071 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2074 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2075 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2078 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2079 // The child-iterator will pick up the arg if it's an expression,
2080 // but not if it's a type.
2081 if (S->isTypeOperand())
2082 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2085 DEF_TRAVERSE_STMT(UnaryTypeTraitExpr, {
2086 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2089 DEF_TRAVERSE_STMT(BinaryTypeTraitExpr, {
2090 TRY_TO(TraverseTypeLoc(S->getLhsTypeSourceInfo()->getTypeLoc()));
2091 TRY_TO(TraverseTypeLoc(S->getRhsTypeSourceInfo()->getTypeLoc()));
2094 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2095 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2096 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2099 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2100 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2103 DEF_TRAVERSE_STMT(ExpressionTraitExpr, {
2104 TRY_TO(TraverseStmt(S->getQueriedExpression()));
2107 DEF_TRAVERSE_STMT(VAArgExpr, {
2108 // The child-iterator will pick up the expression argument.
2109 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2112 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2113 // This is called for code like 'return T()' where T is a class type.
2114 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2117 // Walk only the visible parts of lambda expressions.
2118 template<typename Derived>
2119 bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2120 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2121 CEnd = S->explicit_capture_end();
2123 TRY_TO(TraverseLambdaCapture(*C));
2126 if (S->hasExplicitParameters() || S->hasExplicitResultType()) {
2127 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2128 if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2129 // Visit the whole type.
2130 TRY_TO(TraverseTypeLoc(TL));
2131 } else if (FunctionProtoTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) {
2132 if (S->hasExplicitParameters()) {
2133 // Visit parameters.
2134 for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) {
2135 TRY_TO(TraverseDecl(Proto.getArg(I)));
2138 TRY_TO(TraverseTypeLoc(Proto.getResultLoc()));
2143 TRY_TO(TraverseStmt(S->getBody()));
2147 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2148 // This is called for code like 'T()', where T is a template argument.
2149 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2152 // These expressions all might take explicit template arguments.
2153 // We traverse those if so. FIXME: implement these.
2154 DEF_TRAVERSE_STMT(CXXConstructExpr, { })
2155 DEF_TRAVERSE_STMT(CallExpr, { })
2156 DEF_TRAVERSE_STMT(CXXMemberCallExpr, { })
2158 // These exprs (most of them), do not need any action except iterating
2159 // over the children.
2160 DEF_TRAVERSE_STMT(AddrLabelExpr, { })
2161 DEF_TRAVERSE_STMT(ArraySubscriptExpr, { })
2162 DEF_TRAVERSE_STMT(BlockExpr, {
2163 TRY_TO(TraverseDecl(S->getBlockDecl()));
2164 return true; // no child statements to loop through.
2166 DEF_TRAVERSE_STMT(ChooseExpr, { })
2167 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2168 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2170 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, { })
2171 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, { })
2172 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, { })
2173 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, { })
2174 DEF_TRAVERSE_STMT(CXXDeleteExpr, { })
2175 DEF_TRAVERSE_STMT(ExprWithCleanups, { })
2176 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, { })
2177 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2178 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2179 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2180 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2181 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2182 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2184 DEF_TRAVERSE_STMT(CXXThisExpr, { })
2185 DEF_TRAVERSE_STMT(CXXThrowExpr, { })
2186 DEF_TRAVERSE_STMT(UserDefinedLiteral, { })
2187 DEF_TRAVERSE_STMT(DesignatedInitExpr, { })
2188 DEF_TRAVERSE_STMT(ExtVectorElementExpr, { })
2189 DEF_TRAVERSE_STMT(GNUNullExpr, { })
2190 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, { })
2191 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, { })
2192 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2193 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2194 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2196 DEF_TRAVERSE_STMT(ObjCIsaExpr, { })
2197 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, { })
2198 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2199 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2200 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2202 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, { })
2203 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, { })
2204 DEF_TRAVERSE_STMT(ObjCProtocolExpr, { })
2205 DEF_TRAVERSE_STMT(ObjCSelectorExpr, { })
2206 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, { })
2207 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2208 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2210 DEF_TRAVERSE_STMT(ParenExpr, { })
2211 DEF_TRAVERSE_STMT(ParenListExpr, { })
2212 DEF_TRAVERSE_STMT(PredefinedExpr, { })
2213 DEF_TRAVERSE_STMT(ShuffleVectorExpr, { })
2214 DEF_TRAVERSE_STMT(StmtExpr, { })
2215 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2216 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2217 if (S->hasExplicitTemplateArgs()) {
2218 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2219 S->getNumTemplateArgs()));
2223 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2224 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2225 if (S->hasExplicitTemplateArgs()) {
2226 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2227 S->getNumTemplateArgs()));
2231 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2232 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2233 DEF_TRAVERSE_STMT(SEHFinallyStmt,{})
2234 DEF_TRAVERSE_STMT(CapturedStmt, {
2235 TRY_TO(TraverseDecl(S->getCapturedDecl()));
2238 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, { })
2239 DEF_TRAVERSE_STMT(OpaqueValueExpr, { })
2240 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, { })
2242 // These operators (all of them) do not need any action except
2243 // iterating over the children.
2244 DEF_TRAVERSE_STMT(BinaryConditionalOperator, { })
2245 DEF_TRAVERSE_STMT(ConditionalOperator, { })
2246 DEF_TRAVERSE_STMT(UnaryOperator, { })
2247 DEF_TRAVERSE_STMT(BinaryOperator, { })
2248 DEF_TRAVERSE_STMT(CompoundAssignOperator, { })
2249 DEF_TRAVERSE_STMT(CXXNoexceptExpr, { })
2250 DEF_TRAVERSE_STMT(PackExpansionExpr, { })
2251 DEF_TRAVERSE_STMT(SizeOfPackExpr, { })
2252 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, { })
2253 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, { })
2254 DEF_TRAVERSE_STMT(FunctionParmPackExpr, { })
2255 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, { })
2256 DEF_TRAVERSE_STMT(AtomicExpr, { })
2258 // These literals (all of them) do not need any action.
2259 DEF_TRAVERSE_STMT(IntegerLiteral, { })
2260 DEF_TRAVERSE_STMT(CharacterLiteral, { })
2261 DEF_TRAVERSE_STMT(FloatingLiteral, { })
2262 DEF_TRAVERSE_STMT(ImaginaryLiteral, { })
2263 DEF_TRAVERSE_STMT(StringLiteral, { })
2264 DEF_TRAVERSE_STMT(ObjCStringLiteral, { })
2265 DEF_TRAVERSE_STMT(ObjCBoxedExpr, { })
2266 DEF_TRAVERSE_STMT(ObjCArrayLiteral, { })
2267 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, { })
2269 // Traverse OpenCL: AsType, Convert.
2270 DEF_TRAVERSE_STMT(AsTypeExpr, { })
2272 // FIXME: look at the following tricky-seeming exprs to see if we
2273 // need to recurse on anything. These are ones that have methods
2274 // returning decls or qualtypes or nestednamespecifier -- though I'm
2275 // not sure if they own them -- or just seemed very complicated, or
2276 // had lots of sub-types to explore.
2278 // VisitOverloadExpr and its children: recurse on template args? etc?
2280 // FIXME: go through all the stmts and exprs again, and see which of them
2281 // create new types, and recurse on the types (TypeLocs?) of those.
2284 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2285 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2286 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2287 // Every class that has getQualifier.
2289 #undef DEF_TRAVERSE_STMT
2293 } // end namespace clang
2295 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H