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 this visitor should recurse into implicit
152 /// code, e.g., implicit constructors and destructors.
153 bool shouldVisitImplicitCode() const { return false; }
155 /// \brief Return whether \param S should be traversed using data recursion
156 /// to avoid a stack overflow with extreme cases.
157 bool shouldUseDataRecursionFor(Stmt *S) const {
158 return isa<BinaryOperator>(S) || isa<UnaryOperator>(S) ||
159 isa<CaseStmt>(S) || isa<CXXOperatorCallExpr>(S);
162 /// \brief Recursively visit a statement or expression, by
163 /// dispatching to Traverse*() based on the argument's dynamic type.
165 /// \returns false if the visitation was terminated early, true
166 /// otherwise (including when the argument is NULL).
167 bool TraverseStmt(Stmt *S);
169 /// \brief Recursively visit a type, by dispatching to
170 /// Traverse*Type() based on the argument's getTypeClass() property.
172 /// \returns false if the visitation was terminated early, true
173 /// otherwise (including when the argument is a Null type).
174 bool TraverseType(QualType T);
176 /// \brief Recursively visit a type with location, by dispatching to
177 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
179 /// \returns false if the visitation was terminated early, true
180 /// otherwise (including when the argument is a Null type location).
181 bool TraverseTypeLoc(TypeLoc TL);
183 /// \brief Recursively visit a declaration, by dispatching to
184 /// Traverse*Decl() based on the argument's dynamic type.
186 /// \returns false if the visitation was terminated early, true
187 /// otherwise (including when the argument is NULL).
188 bool TraverseDecl(Decl *D);
190 /// \brief Recursively visit a C++ nested-name-specifier.
192 /// \returns false if the visitation was terminated early, true otherwise.
193 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
195 /// \brief Recursively visit a C++ nested-name-specifier with location
198 /// \returns false if the visitation was terminated early, true otherwise.
199 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
201 /// \brief Recursively visit a name with its location information.
203 /// \returns false if the visitation was terminated early, true otherwise.
204 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
206 /// \brief Recursively visit a template name and dispatch to the
207 /// appropriate method.
209 /// \returns false if the visitation was terminated early, true otherwise.
210 bool TraverseTemplateName(TemplateName Template);
212 /// \brief Recursively visit a template argument and dispatch to the
213 /// appropriate method for the argument type.
215 /// \returns false if the visitation was terminated early, true otherwise.
216 // FIXME: migrate callers to TemplateArgumentLoc instead.
217 bool TraverseTemplateArgument(const TemplateArgument &Arg);
219 /// \brief Recursively visit a template argument location and dispatch to the
220 /// appropriate method for the argument type.
222 /// \returns false if the visitation was terminated early, true otherwise.
223 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
225 /// \brief Recursively visit a set of template arguments.
226 /// This can be overridden by a subclass, but it's not expected that
227 /// will be needed -- this visitor always dispatches to another.
229 /// \returns false if the visitation was terminated early, true otherwise.
230 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
231 bool TraverseTemplateArguments(const TemplateArgument *Args,
234 /// \brief Recursively visit a constructor initializer. This
235 /// automatically dispatches to another visitor for the initializer
236 /// expression, but not for the name of the initializer, so may
237 /// be overridden for clients that need access to the name.
239 /// \returns false if the visitation was terminated early, true otherwise.
240 bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
242 /// \brief Recursively visit a lambda capture.
244 /// \returns false if the visitation was terminated early, true otherwise.
245 bool TraverseLambdaCapture(LambdaExpr::Capture C);
247 // ---- Methods on Stmts ----
249 // Declare Traverse*() for all concrete Stmt classes.
250 #define ABSTRACT_STMT(STMT)
251 #define STMT(CLASS, PARENT) \
252 bool Traverse##CLASS(CLASS *S);
253 #include "clang/AST/StmtNodes.inc"
254 // The above header #undefs ABSTRACT_STMT and STMT upon exit.
256 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
257 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
258 bool VisitStmt(Stmt *S) { return true; }
259 #define STMT(CLASS, PARENT) \
260 bool WalkUpFrom##CLASS(CLASS *S) { \
261 TRY_TO(WalkUpFrom##PARENT(S)); \
262 TRY_TO(Visit##CLASS(S)); \
265 bool Visit##CLASS(CLASS *S) { return true; }
266 #include "clang/AST/StmtNodes.inc"
268 // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary
269 // operator methods. Unary operators are not classes in themselves
270 // (they're all opcodes in UnaryOperator) but do have visitors.
271 #define OPERATOR(NAME) \
272 bool TraverseUnary##NAME(UnaryOperator *S) { \
273 TRY_TO(WalkUpFromUnary##NAME(S)); \
274 TRY_TO(TraverseStmt(S->getSubExpr())); \
277 bool WalkUpFromUnary##NAME(UnaryOperator *S) { \
278 TRY_TO(WalkUpFromUnaryOperator(S)); \
279 TRY_TO(VisitUnary##NAME(S)); \
282 bool VisitUnary##NAME(UnaryOperator *S) { return true; }
287 // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary
288 // operator methods. Binary operators are not classes in themselves
289 // (they're all opcodes in BinaryOperator) but do have visitors.
290 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE) \
291 bool TraverseBin##NAME(BINOP_TYPE *S) { \
292 TRY_TO(WalkUpFromBin##NAME(S)); \
293 TRY_TO(TraverseStmt(S->getLHS())); \
294 TRY_TO(TraverseStmt(S->getRHS())); \
297 bool WalkUpFromBin##NAME(BINOP_TYPE *S) { \
298 TRY_TO(WalkUpFrom##BINOP_TYPE(S)); \
299 TRY_TO(VisitBin##NAME(S)); \
302 bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
304 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
308 // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound
309 // assignment methods. Compound assignment operators are not
310 // classes in themselves (they're all opcodes in
311 // CompoundAssignOperator) but do have visitors.
312 #define OPERATOR(NAME) \
313 GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator)
317 #undef GENERAL_BINOP_FALLBACK
319 // ---- Methods on Types ----
320 // FIXME: revamp to take TypeLoc's rather than Types.
322 // Declare Traverse*() for all concrete Type classes.
323 #define ABSTRACT_TYPE(CLASS, BASE)
324 #define TYPE(CLASS, BASE) \
325 bool Traverse##CLASS##Type(CLASS##Type *T);
326 #include "clang/AST/TypeNodes.def"
327 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
329 // Define WalkUpFrom*() and empty Visit*() for all Type classes.
330 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
331 bool VisitType(Type *T) { return true; }
332 #define TYPE(CLASS, BASE) \
333 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \
334 TRY_TO(WalkUpFrom##BASE(T)); \
335 TRY_TO(Visit##CLASS##Type(T)); \
338 bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
339 #include "clang/AST/TypeNodes.def"
341 // ---- Methods on TypeLocs ----
342 // FIXME: this currently just calls the matching Type methods
344 // Declare Traverse*() for all concrete Type classes.
345 #define ABSTRACT_TYPELOC(CLASS, BASE)
346 #define TYPELOC(CLASS, BASE) \
347 bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
348 #include "clang/AST/TypeLocNodes.def"
349 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
351 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
352 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
353 bool VisitTypeLoc(TypeLoc TL) { return true; }
355 // QualifiedTypeLoc and UnqualTypeLoc are not declared in
356 // TypeNodes.def and thus need to be handled specially.
357 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
358 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
360 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
361 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
362 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
364 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
366 // Note that BASE includes trailing 'Type' which CLASS doesn't.
367 #define TYPE(CLASS, BASE) \
368 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
369 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \
370 TRY_TO(Visit##CLASS##TypeLoc(TL)); \
373 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
374 #include "clang/AST/TypeNodes.def"
376 // ---- Methods on Decls ----
378 // Declare Traverse*() for all concrete Decl classes.
379 #define ABSTRACT_DECL(DECL)
380 #define DECL(CLASS, BASE) \
381 bool Traverse##CLASS##Decl(CLASS##Decl *D);
382 #include "clang/AST/DeclNodes.inc"
383 // The above header #undefs ABSTRACT_DECL and DECL upon exit.
385 // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
386 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
387 bool VisitDecl(Decl *D) { return true; }
388 #define DECL(CLASS, BASE) \
389 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \
390 TRY_TO(WalkUpFrom##BASE(D)); \
391 TRY_TO(Visit##CLASS##Decl(D)); \
394 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
395 #include "clang/AST/DeclNodes.inc"
398 // These are helper methods used by more than one Traverse* method.
399 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
400 bool TraverseClassInstantiations(ClassTemplateDecl *D);
401 bool TraverseFunctionInstantiations(FunctionTemplateDecl *D) ;
402 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
404 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
405 bool TraverseRecordHelper(RecordDecl *D);
406 bool TraverseCXXRecordHelper(CXXRecordDecl *D);
407 bool TraverseDeclaratorHelper(DeclaratorDecl *D);
408 bool TraverseDeclContextHelper(DeclContext *DC);
409 bool TraverseFunctionHelper(FunctionDecl *D);
410 bool TraverseVarHelper(VarDecl *D);
414 Stmt::child_iterator StmtIt;
416 EnqueueJob(Stmt *S) : S(S), StmtIt() {}
418 bool dataTraverse(Stmt *S);
419 bool dataTraverseNode(Stmt *S, bool &EnqueueChildren);
422 template<typename Derived>
423 bool RecursiveASTVisitor<Derived>::dataTraverse(Stmt *S) {
425 SmallVector<EnqueueJob, 16> Queue;
428 while (!Queue.empty()) {
429 EnqueueJob &job = Queue.back();
436 if (getDerived().shouldUseDataRecursionFor(CurrS)) {
437 if (job.StmtIt == Stmt::child_iterator()) {
438 bool EnqueueChildren = true;
439 if (!dataTraverseNode(CurrS, EnqueueChildren)) return false;
440 if (!EnqueueChildren) {
444 job.StmtIt = CurrS->child_begin();
449 if (job.StmtIt != CurrS->child_end())
450 Queue.push_back(*job.StmtIt);
457 TRY_TO(TraverseStmt(CurrS));
463 template<typename Derived>
464 bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S,
465 bool &EnqueueChildren) {
467 // The cast for DISPATCH_WALK is needed for older versions of g++, but causes
468 // problems for MSVC. So we'll skip the cast entirely for MSVC.
469 #if defined(_MSC_VER)
470 #define GCC_CAST(CLASS)
472 #define GCC_CAST(CLASS) (bool (RecursiveASTVisitor::*)(CLASS*))
475 // Dispatch to the corresponding WalkUpFrom* function only if the derived
476 // class didn't override Traverse* (and thus the traversal is trivial).
477 #define DISPATCH_WALK(NAME, CLASS, VAR) \
478 if (&RecursiveASTVisitor::Traverse##NAME == \
479 GCC_CAST(CLASS)&Derived::Traverse##NAME) \
480 return getDerived().WalkUpFrom##NAME(static_cast<CLASS*>(VAR)); \
481 EnqueueChildren = false; \
482 return getDerived().Traverse##NAME(static_cast<CLASS*>(VAR));
484 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
485 switch (BinOp->getOpcode()) {
486 #define OPERATOR(NAME) \
487 case BO_##NAME: DISPATCH_WALK(Bin##NAME, BinaryOperator, S);
492 #define OPERATOR(NAME) \
493 case BO_##NAME##Assign: \
494 DISPATCH_WALK(Bin##NAME##Assign, CompoundAssignOperator, S);
499 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
500 switch (UnOp->getOpcode()) {
501 #define OPERATOR(NAME) \
502 case UO_##NAME: DISPATCH_WALK(Unary##NAME, UnaryOperator, S);
509 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
510 switch (S->getStmtClass()) {
511 case Stmt::NoStmtClass: break;
512 #define ABSTRACT_STMT(STMT)
513 #define STMT(CLASS, PARENT) \
514 case Stmt::CLASS##Class: DISPATCH_WALK(CLASS, CLASS, S);
515 #include "clang/AST/StmtNodes.inc"
524 #define DISPATCH(NAME, CLASS, VAR) \
525 return getDerived().Traverse##NAME(static_cast<CLASS*>(VAR))
527 template<typename Derived>
528 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S) {
532 if (getDerived().shouldUseDataRecursionFor(S))
533 return dataTraverse(S);
535 // If we have a binary expr, dispatch to the subcode of the binop. A smart
536 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
538 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
539 switch (BinOp->getOpcode()) {
540 #define OPERATOR(NAME) \
541 case BO_##NAME: DISPATCH(Bin##NAME, BinaryOperator, S);
547 #define OPERATOR(NAME) \
548 case BO_##NAME##Assign: \
549 DISPATCH(Bin##NAME##Assign, CompoundAssignOperator, S);
555 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
556 switch (UnOp->getOpcode()) {
557 #define OPERATOR(NAME) \
558 case UO_##NAME: DISPATCH(Unary##NAME, UnaryOperator, S);
566 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
567 switch (S->getStmtClass()) {
568 case Stmt::NoStmtClass: break;
569 #define ABSTRACT_STMT(STMT)
570 #define STMT(CLASS, PARENT) \
571 case Stmt::CLASS##Class: DISPATCH(CLASS, CLASS, S);
572 #include "clang/AST/StmtNodes.inc"
578 template<typename Derived>
579 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
583 switch (T->getTypeClass()) {
584 #define ABSTRACT_TYPE(CLASS, BASE)
585 #define TYPE(CLASS, BASE) \
586 case Type::CLASS: DISPATCH(CLASS##Type, CLASS##Type, \
587 const_cast<Type*>(T.getTypePtr()));
588 #include "clang/AST/TypeNodes.def"
594 template<typename Derived>
595 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
599 switch (TL.getTypeLocClass()) {
600 #define ABSTRACT_TYPELOC(CLASS, BASE)
601 #define TYPELOC(CLASS, BASE) \
602 case TypeLoc::CLASS: \
603 return getDerived().Traverse##CLASS##TypeLoc(*cast<CLASS##TypeLoc>(&TL));
604 #include "clang/AST/TypeLocNodes.def"
611 template<typename Derived>
612 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
616 // As a syntax visitor, by default we want to ignore declarations for
617 // implicit declarations (ones not typed explicitly by the user).
618 if (!getDerived().shouldVisitImplicitCode() && D->isImplicit())
621 switch (D->getKind()) {
622 #define ABSTRACT_DECL(DECL)
623 #define DECL(CLASS, BASE) \
624 case Decl::CLASS: DISPATCH(CLASS##Decl, CLASS##Decl, D);
625 #include "clang/AST/DeclNodes.inc"
633 template<typename Derived>
634 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
635 NestedNameSpecifier *NNS) {
639 if (NNS->getPrefix())
640 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
642 switch (NNS->getKind()) {
643 case NestedNameSpecifier::Identifier:
644 case NestedNameSpecifier::Namespace:
645 case NestedNameSpecifier::NamespaceAlias:
646 case NestedNameSpecifier::Global:
649 case NestedNameSpecifier::TypeSpec:
650 case NestedNameSpecifier::TypeSpecWithTemplate:
651 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
657 template<typename Derived>
658 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
659 NestedNameSpecifierLoc NNS) {
663 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
664 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
666 switch (NNS.getNestedNameSpecifier()->getKind()) {
667 case NestedNameSpecifier::Identifier:
668 case NestedNameSpecifier::Namespace:
669 case NestedNameSpecifier::NamespaceAlias:
670 case NestedNameSpecifier::Global:
673 case NestedNameSpecifier::TypeSpec:
674 case NestedNameSpecifier::TypeSpecWithTemplate:
675 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
682 template<typename Derived>
683 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
684 DeclarationNameInfo NameInfo) {
685 switch (NameInfo.getName().getNameKind()) {
686 case DeclarationName::CXXConstructorName:
687 case DeclarationName::CXXDestructorName:
688 case DeclarationName::CXXConversionFunctionName:
689 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
690 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
694 case DeclarationName::Identifier:
695 case DeclarationName::ObjCZeroArgSelector:
696 case DeclarationName::ObjCOneArgSelector:
697 case DeclarationName::ObjCMultiArgSelector:
698 case DeclarationName::CXXOperatorName:
699 case DeclarationName::CXXLiteralOperatorName:
700 case DeclarationName::CXXUsingDirective:
707 template<typename Derived>
708 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
709 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
710 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
711 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
712 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
717 template<typename Derived>
718 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
719 const TemplateArgument &Arg) {
720 switch (Arg.getKind()) {
721 case TemplateArgument::Null:
722 case TemplateArgument::Declaration:
723 case TemplateArgument::Integral:
724 case TemplateArgument::NullPtr:
727 case TemplateArgument::Type:
728 return getDerived().TraverseType(Arg.getAsType());
730 case TemplateArgument::Template:
731 case TemplateArgument::TemplateExpansion:
732 return getDerived().TraverseTemplateName(
733 Arg.getAsTemplateOrTemplatePattern());
735 case TemplateArgument::Expression:
736 return getDerived().TraverseStmt(Arg.getAsExpr());
738 case TemplateArgument::Pack:
739 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
746 // FIXME: no template name location?
747 // FIXME: no source locations for a template argument pack?
748 template<typename Derived>
749 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
750 const TemplateArgumentLoc &ArgLoc) {
751 const TemplateArgument &Arg = ArgLoc.getArgument();
753 switch (Arg.getKind()) {
754 case TemplateArgument::Null:
755 case TemplateArgument::Declaration:
756 case TemplateArgument::Integral:
757 case TemplateArgument::NullPtr:
760 case TemplateArgument::Type: {
761 // FIXME: how can TSI ever be NULL?
762 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
763 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
765 return getDerived().TraverseType(Arg.getAsType());
768 case TemplateArgument::Template:
769 case TemplateArgument::TemplateExpansion:
770 if (ArgLoc.getTemplateQualifierLoc())
771 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
772 ArgLoc.getTemplateQualifierLoc()));
773 return getDerived().TraverseTemplateName(
774 Arg.getAsTemplateOrTemplatePattern());
776 case TemplateArgument::Expression:
777 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
779 case TemplateArgument::Pack:
780 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
787 template<typename Derived>
788 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
789 const TemplateArgument *Args,
791 for (unsigned I = 0; I != NumArgs; ++I) {
792 TRY_TO(TraverseTemplateArgument(Args[I]));
798 template<typename Derived>
799 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
800 CXXCtorInitializer *Init) {
801 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
802 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
804 if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
805 TRY_TO(TraverseStmt(Init->getInit()));
809 template<typename Derived>
810 bool RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr::Capture C){
814 // ----------------- Type traversal -----------------
816 // This macro makes available a variable T, the passed-in type.
817 #define DEF_TRAVERSE_TYPE(TYPE, CODE) \
818 template<typename Derived> \
819 bool RecursiveASTVisitor<Derived>::Traverse##TYPE (TYPE *T) { \
820 TRY_TO(WalkUpFrom##TYPE (T)); \
825 DEF_TRAVERSE_TYPE(BuiltinType, { })
827 DEF_TRAVERSE_TYPE(ComplexType, {
828 TRY_TO(TraverseType(T->getElementType()));
831 DEF_TRAVERSE_TYPE(PointerType, {
832 TRY_TO(TraverseType(T->getPointeeType()));
835 DEF_TRAVERSE_TYPE(BlockPointerType, {
836 TRY_TO(TraverseType(T->getPointeeType()));
839 DEF_TRAVERSE_TYPE(LValueReferenceType, {
840 TRY_TO(TraverseType(T->getPointeeType()));
843 DEF_TRAVERSE_TYPE(RValueReferenceType, {
844 TRY_TO(TraverseType(T->getPointeeType()));
847 DEF_TRAVERSE_TYPE(MemberPointerType, {
848 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
849 TRY_TO(TraverseType(T->getPointeeType()));
852 DEF_TRAVERSE_TYPE(ConstantArrayType, {
853 TRY_TO(TraverseType(T->getElementType()));
856 DEF_TRAVERSE_TYPE(IncompleteArrayType, {
857 TRY_TO(TraverseType(T->getElementType()));
860 DEF_TRAVERSE_TYPE(VariableArrayType, {
861 TRY_TO(TraverseType(T->getElementType()));
862 TRY_TO(TraverseStmt(T->getSizeExpr()));
865 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
866 TRY_TO(TraverseType(T->getElementType()));
867 if (T->getSizeExpr())
868 TRY_TO(TraverseStmt(T->getSizeExpr()));
871 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
872 if (T->getSizeExpr())
873 TRY_TO(TraverseStmt(T->getSizeExpr()));
874 TRY_TO(TraverseType(T->getElementType()));
877 DEF_TRAVERSE_TYPE(VectorType, {
878 TRY_TO(TraverseType(T->getElementType()));
881 DEF_TRAVERSE_TYPE(ExtVectorType, {
882 TRY_TO(TraverseType(T->getElementType()));
885 DEF_TRAVERSE_TYPE(FunctionNoProtoType, {
886 TRY_TO(TraverseType(T->getResultType()));
889 DEF_TRAVERSE_TYPE(FunctionProtoType, {
890 TRY_TO(TraverseType(T->getResultType()));
892 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
893 AEnd = T->arg_type_end();
895 TRY_TO(TraverseType(*A));
898 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
899 EEnd = T->exception_end();
901 TRY_TO(TraverseType(*E));
905 DEF_TRAVERSE_TYPE(UnresolvedUsingType, { })
906 DEF_TRAVERSE_TYPE(TypedefType, { })
908 DEF_TRAVERSE_TYPE(TypeOfExprType, {
909 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
912 DEF_TRAVERSE_TYPE(TypeOfType, {
913 TRY_TO(TraverseType(T->getUnderlyingType()));
916 DEF_TRAVERSE_TYPE(DecltypeType, {
917 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
920 DEF_TRAVERSE_TYPE(UnaryTransformType, {
921 TRY_TO(TraverseType(T->getBaseType()));
922 TRY_TO(TraverseType(T->getUnderlyingType()));
925 DEF_TRAVERSE_TYPE(AutoType, {
926 TRY_TO(TraverseType(T->getDeducedType()));
929 DEF_TRAVERSE_TYPE(RecordType, { })
930 DEF_TRAVERSE_TYPE(EnumType, { })
931 DEF_TRAVERSE_TYPE(TemplateTypeParmType, { })
932 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, { })
933 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, { })
935 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
936 TRY_TO(TraverseTemplateName(T->getTemplateName()));
937 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
940 DEF_TRAVERSE_TYPE(InjectedClassNameType, { })
942 DEF_TRAVERSE_TYPE(AttributedType, {
943 TRY_TO(TraverseType(T->getModifiedType()));
946 DEF_TRAVERSE_TYPE(ParenType, {
947 TRY_TO(TraverseType(T->getInnerType()));
950 DEF_TRAVERSE_TYPE(ElaboratedType, {
951 if (T->getQualifier()) {
952 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
954 TRY_TO(TraverseType(T->getNamedType()));
957 DEF_TRAVERSE_TYPE(DependentNameType, {
958 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
961 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
962 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
963 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
966 DEF_TRAVERSE_TYPE(PackExpansionType, {
967 TRY_TO(TraverseType(T->getPattern()));
970 DEF_TRAVERSE_TYPE(ObjCInterfaceType, { })
972 DEF_TRAVERSE_TYPE(ObjCObjectType, {
973 // We have to watch out here because an ObjCInterfaceType's base
975 if (T->getBaseType().getTypePtr() != T)
976 TRY_TO(TraverseType(T->getBaseType()));
979 DEF_TRAVERSE_TYPE(ObjCObjectPointerType, {
980 TRY_TO(TraverseType(T->getPointeeType()));
983 DEF_TRAVERSE_TYPE(AtomicType, {
984 TRY_TO(TraverseType(T->getValueType()));
987 #undef DEF_TRAVERSE_TYPE
989 // ----------------- TypeLoc traversal -----------------
991 // This macro makes available a variable TL, the passed-in TypeLoc.
992 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
993 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
994 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
996 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
997 template<typename Derived> \
998 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
999 if (getDerived().shouldWalkTypesOfTypeLocs()) \
1000 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE*>(TL.getTypePtr()))); \
1001 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
1006 template<typename Derived>
1007 bool RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(
1008 QualifiedTypeLoc TL) {
1009 // Move this over to the 'main' typeloc tree. Note that this is a
1010 // move -- we pretend that we were really looking at the unqualified
1011 // typeloc all along -- rather than a recursion, so we don't follow
1012 // the normal CRTP plan of going through
1013 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
1014 // twice for the same type (once as a QualifiedTypeLoc version of
1015 // the type, once as an UnqualifiedTypeLoc version of the type),
1016 // which in effect means we'd call VisitTypeLoc twice with the
1017 // 'same' type. This solves that problem, at the cost of never
1018 // seeing the qualified version of the type (unless the client
1019 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
1020 // perfect solution. A perfect solution probably requires making
1021 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1022 // wrapper around Type* -- rather than being its own class in the
1024 return TraverseTypeLoc(TL.getUnqualifiedLoc());
1027 DEF_TRAVERSE_TYPELOC(BuiltinType, { })
1029 // FIXME: ComplexTypeLoc is unfinished
1030 DEF_TRAVERSE_TYPELOC(ComplexType, {
1031 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1034 DEF_TRAVERSE_TYPELOC(PointerType, {
1035 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1038 DEF_TRAVERSE_TYPELOC(BlockPointerType, {
1039 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1042 DEF_TRAVERSE_TYPELOC(LValueReferenceType, {
1043 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1046 DEF_TRAVERSE_TYPELOC(RValueReferenceType, {
1047 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1050 // FIXME: location of base class?
1051 // We traverse this in the type case as well, but how is it not reached through
1052 // the pointee type?
1053 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1054 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1055 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1058 template<typename Derived>
1059 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1060 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1061 TRY_TO(TraverseStmt(TL.getSizeExpr()));
1065 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1066 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1067 return TraverseArrayTypeLocHelper(TL);
1070 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1071 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1072 return TraverseArrayTypeLocHelper(TL);
1075 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1076 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1077 return TraverseArrayTypeLocHelper(TL);
1080 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1081 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1082 return TraverseArrayTypeLocHelper(TL);
1085 // FIXME: order? why not size expr first?
1086 // FIXME: base VectorTypeLoc is unfinished
1087 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1088 if (TL.getTypePtr()->getSizeExpr())
1089 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1090 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1093 // FIXME: VectorTypeLoc is unfinished
1094 DEF_TRAVERSE_TYPELOC(VectorType, {
1095 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1098 // FIXME: size and attributes
1099 // FIXME: base VectorTypeLoc is unfinished
1100 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1101 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1104 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, {
1105 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1108 // FIXME: location of exception specifications (attributes?)
1109 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1110 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1112 const FunctionProtoType *T = TL.getTypePtr();
1114 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1116 TRY_TO(TraverseDecl(TL.getArg(I)));
1117 } else if (I < T->getNumArgs()) {
1118 TRY_TO(TraverseType(T->getArgType(I)));
1122 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1123 EEnd = T->exception_end();
1125 TRY_TO(TraverseType(*E));
1129 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, { })
1130 DEF_TRAVERSE_TYPELOC(TypedefType, { })
1132 DEF_TRAVERSE_TYPELOC(TypeOfExprType, {
1133 TRY_TO(TraverseStmt(TL.getUnderlyingExpr()));
1136 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1137 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1140 // FIXME: location of underlying expr
1141 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1142 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1145 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1146 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1149 DEF_TRAVERSE_TYPELOC(AutoType, {
1150 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1153 DEF_TRAVERSE_TYPELOC(RecordType, { })
1154 DEF_TRAVERSE_TYPELOC(EnumType, { })
1155 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, { })
1156 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, { })
1157 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, { })
1159 // FIXME: use the loc for the template name?
1160 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1161 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1162 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1163 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1167 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, { })
1169 DEF_TRAVERSE_TYPELOC(ParenType, {
1170 TRY_TO(TraverseTypeLoc(TL.getInnerLoc()));
1173 DEF_TRAVERSE_TYPELOC(AttributedType, {
1174 TRY_TO(TraverseTypeLoc(TL.getModifiedLoc()));
1177 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1178 if (TL.getQualifierLoc()) {
1179 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1181 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1184 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1185 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1188 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1189 if (TL.getQualifierLoc()) {
1190 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1193 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1194 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1198 DEF_TRAVERSE_TYPELOC(PackExpansionType, {
1199 TRY_TO(TraverseTypeLoc(TL.getPatternLoc()));
1202 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, { })
1204 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1205 // We have to watch out here because an ObjCInterfaceType's base
1207 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1208 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1211 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, {
1212 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1215 DEF_TRAVERSE_TYPELOC(AtomicType, {
1216 TRY_TO(TraverseTypeLoc(TL.getValueLoc()));
1219 #undef DEF_TRAVERSE_TYPELOC
1221 // ----------------- Decl traversal -----------------
1223 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1224 // the children that come from the DeclContext associated with it.
1225 // Therefore each Traverse* only needs to worry about children other
1228 template<typename Derived>
1229 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1233 for (DeclContext::decl_iterator Child = DC->decls_begin(),
1234 ChildEnd = DC->decls_end();
1235 Child != ChildEnd; ++Child) {
1236 // BlockDecls are traversed through BlockExprs.
1237 if (!isa<BlockDecl>(*Child))
1238 TRY_TO(TraverseDecl(*Child));
1244 // This macro makes available a variable D, the passed-in decl.
1245 #define DEF_TRAVERSE_DECL(DECL, CODE) \
1246 template<typename Derived> \
1247 bool RecursiveASTVisitor<Derived>::Traverse##DECL (DECL *D) { \
1248 TRY_TO(WalkUpFrom##DECL (D)); \
1250 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1254 DEF_TRAVERSE_DECL(AccessSpecDecl, { })
1256 DEF_TRAVERSE_DECL(BlockDecl, {
1257 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1258 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1259 TRY_TO(TraverseStmt(D->getBody()));
1260 // This return statement makes sure the traversal of nodes in
1261 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1262 // is skipped - don't remove it.
1266 DEF_TRAVERSE_DECL(FileScopeAsmDecl, {
1267 TRY_TO(TraverseStmt(D->getAsmString()));
1270 DEF_TRAVERSE_DECL(ImportDecl, { })
1272 DEF_TRAVERSE_DECL(FriendDecl, {
1273 // Friend is either decl or a type.
1274 if (D->getFriendType())
1275 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1277 TRY_TO(TraverseDecl(D->getFriendDecl()));
1280 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1281 if (D->getFriendType())
1282 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1284 TRY_TO(TraverseDecl(D->getFriendDecl()));
1285 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1286 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1287 for (TemplateParameterList::iterator ITPL = TPL->begin(),
1289 ITPL != ETPL; ++ITPL) {
1290 TRY_TO(TraverseDecl(*ITPL));
1295 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1296 TRY_TO(TraverseDecl(D->getSpecialization()));
1298 if (D->hasExplicitTemplateArgs()) {
1299 const TemplateArgumentListInfo& args = D->templateArgs();
1300 TRY_TO(TraverseTemplateArgumentLocsHelper(
1301 args.getArgumentArray(), args.size()));
1305 DEF_TRAVERSE_DECL(LinkageSpecDecl, { })
1307 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {
1308 // FIXME: implement this
1311 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1312 TRY_TO(TraverseStmt(D->getAssertExpr()));
1313 TRY_TO(TraverseStmt(D->getMessage()));
1316 DEF_TRAVERSE_DECL(TranslationUnitDecl, {
1317 // Code in an unnamed namespace shows up automatically in
1318 // decls_begin()/decls_end(). Thus we don't need to recurse on
1319 // D->getAnonymousNamespace().
1322 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1323 // We shouldn't traverse an aliased namespace, since it will be
1324 // defined (and, therefore, traversed) somewhere else.
1326 // This return statement makes sure the traversal of nodes in
1327 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1328 // is skipped - don't remove it.
1332 DEF_TRAVERSE_DECL(LabelDecl, {
1333 // There is no code in a LabelDecl.
1337 DEF_TRAVERSE_DECL(NamespaceDecl, {
1338 // Code in an unnamed namespace shows up automatically in
1339 // decls_begin()/decls_end(). Thus we don't need to recurse on
1340 // D->getAnonymousNamespace().
1343 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {
1347 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {
1351 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {
1355 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {
1359 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {
1363 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {
1367 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1368 if (D->getResultTypeSourceInfo()) {
1369 TRY_TO(TraverseTypeLoc(D->getResultTypeSourceInfo()->getTypeLoc()));
1371 for (ObjCMethodDecl::param_iterator
1372 I = D->param_begin(), E = D->param_end(); I != E; ++I) {
1373 TRY_TO(TraverseDecl(*I));
1375 if (D->isThisDeclarationADefinition()) {
1376 TRY_TO(TraverseStmt(D->getBody()));
1381 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1385 DEF_TRAVERSE_DECL(UsingDecl, {
1386 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1387 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1390 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1391 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1394 DEF_TRAVERSE_DECL(UsingShadowDecl, { })
1396 // A helper method for TemplateDecl's children.
1397 template<typename Derived>
1398 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1399 TemplateParameterList *TPL) {
1401 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1403 TRY_TO(TraverseDecl(*I));
1409 // A helper method for traversing the implicit instantiations of a
1411 template<typename Derived>
1412 bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations(
1413 ClassTemplateDecl *D) {
1414 ClassTemplateDecl::spec_iterator end = D->spec_end();
1415 for (ClassTemplateDecl::spec_iterator it = D->spec_begin(); it != end; ++it) {
1416 ClassTemplateSpecializationDecl* SD = *it;
1418 switch (SD->getSpecializationKind()) {
1419 // Visit the implicit instantiations with the requested pattern.
1420 case TSK_Undeclared:
1421 case TSK_ImplicitInstantiation:
1422 TRY_TO(TraverseDecl(SD));
1425 // We don't need to do anything on an explicit instantiation
1426 // or explicit specialization because there will be an explicit
1427 // node for it elsewhere.
1428 case TSK_ExplicitInstantiationDeclaration:
1429 case TSK_ExplicitInstantiationDefinition:
1430 case TSK_ExplicitSpecialization:
1438 DEF_TRAVERSE_DECL(ClassTemplateDecl, {
1439 CXXRecordDecl* TempDecl = D->getTemplatedDecl();
1440 TRY_TO(TraverseDecl(TempDecl));
1441 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1443 // By default, we do not traverse the instantiations of
1444 // class templates since they do not appear in the user code. The
1445 // following code optionally traverses them.
1447 // We only traverse the class instantiations when we see the canonical
1448 // declaration of the template, to ensure we only visit them once.
1449 if (getDerived().shouldVisitTemplateInstantiations() &&
1450 D == D->getCanonicalDecl())
1451 TRY_TO(TraverseClassInstantiations(D));
1453 // Note that getInstantiatedFromMemberTemplate() is just a link
1454 // from a template instantiation back to the template from which
1455 // it was instantiated, and thus should not be traversed.
1458 // A helper method for traversing the instantiations of a
1459 // function while skipping its specializations.
1460 template<typename Derived>
1461 bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations(
1462 FunctionTemplateDecl *D) {
1463 FunctionTemplateDecl::spec_iterator end = D->spec_end();
1464 for (FunctionTemplateDecl::spec_iterator it = D->spec_begin(); it != end;
1466 FunctionDecl* FD = *it;
1467 switch (FD->getTemplateSpecializationKind()) {
1468 case TSK_Undeclared:
1469 case TSK_ImplicitInstantiation:
1470 // We don't know what kind of FunctionDecl this is.
1471 TRY_TO(TraverseDecl(FD));
1474 // FIXME: For now traverse explicit instantiations here. Change that
1475 // once they are represented as dedicated nodes in the AST.
1476 case TSK_ExplicitInstantiationDeclaration:
1477 case TSK_ExplicitInstantiationDefinition:
1478 TRY_TO(TraverseDecl(FD));
1481 case TSK_ExplicitSpecialization:
1489 DEF_TRAVERSE_DECL(FunctionTemplateDecl, {
1490 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1491 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1493 // By default, we do not traverse the instantiations of
1494 // function templates since they do not appear in the user code. The
1495 // following code optionally traverses them.
1497 // We only traverse the function instantiations when we see the canonical
1498 // declaration of the template, to ensure we only visit them once.
1499 if (getDerived().shouldVisitTemplateInstantiations() &&
1500 D == D->getCanonicalDecl())
1501 TRY_TO(TraverseFunctionInstantiations(D));
1504 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1505 // D is the "T" in something like
1506 // template <template <typename> class T> class container { };
1507 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1508 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1509 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1511 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1514 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1515 // D is the "T" in something like "template<typename T> class vector;"
1516 if (D->getTypeForDecl())
1517 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1518 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1519 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1522 DEF_TRAVERSE_DECL(TypedefDecl, {
1523 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1524 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1525 // declaring the typedef, not something that was written in the
1529 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1530 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1531 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1532 // declaring the type alias, not something that was written in the
1536 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1537 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1538 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1541 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1542 // A dependent using declaration which was marked with 'typename'.
1543 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1544 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1545 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1546 // declaring the type, not something that was written in the
1550 DEF_TRAVERSE_DECL(EnumDecl, {
1551 if (D->getTypeForDecl())
1552 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1554 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1555 // The enumerators are already traversed by
1556 // decls_begin()/decls_end().
1560 // Helper methods for RecordDecl and its children.
1561 template<typename Derived>
1562 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(
1564 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1565 // declaring the type, not something that was written in the source.
1567 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1571 template<typename Derived>
1572 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(
1574 if (!TraverseRecordHelper(D))
1576 if (D->isCompleteDefinition()) {
1577 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1580 TRY_TO(TraverseTypeLoc(I->getTypeSourceInfo()->getTypeLoc()));
1582 // We don't traverse the friends or the conversions, as they are
1583 // already in decls_begin()/decls_end().
1588 DEF_TRAVERSE_DECL(RecordDecl, {
1589 TRY_TO(TraverseRecordHelper(D));
1592 DEF_TRAVERSE_DECL(CXXRecordDecl, {
1593 TRY_TO(TraverseCXXRecordHelper(D));
1596 DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, {
1597 // For implicit instantiations ("set<int> x;"), we don't want to
1598 // recurse at all, since the instatiated class isn't written in
1599 // the source code anywhere. (Note the instatiated *type* --
1600 // set<int> -- is written, and will still get a callback of
1601 // TemplateSpecializationType). For explicit instantiations
1602 // ("template set<int>;"), we do need a callback, since this
1603 // is the only callback that's made for this instantiation.
1604 // We use getTypeAsWritten() to distinguish.
1605 if (TypeSourceInfo *TSI = D->getTypeAsWritten())
1606 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1608 if (!getDerived().shouldVisitTemplateInstantiations() &&
1609 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1610 // Returning from here skips traversing the
1611 // declaration context of the ClassTemplateSpecializationDecl
1612 // (embedded in the DEF_TRAVERSE_DECL() macro)
1613 // which contains the instantiated members of the class.
1617 template <typename Derived>
1618 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1619 const TemplateArgumentLoc *TAL, unsigned Count) {
1620 for (unsigned I = 0; I < Count; ++I) {
1621 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1626 DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, {
1627 // The partial specialization.
1628 if (TemplateParameterList *TPL = D->getTemplateParameters()) {
1629 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1631 TRY_TO(TraverseDecl(*I));
1634 // The args that remains unspecialized.
1635 TRY_TO(TraverseTemplateArgumentLocsHelper(
1636 D->getTemplateArgsAsWritten(), D->getNumTemplateArgsAsWritten()));
1638 // Don't need the ClassTemplatePartialSpecializationHelper, even
1639 // though that's our parent class -- we already visit all the
1640 // template args here.
1641 TRY_TO(TraverseCXXRecordHelper(D));
1643 // Instantiations will have been visited with the primary template.
1646 DEF_TRAVERSE_DECL(EnumConstantDecl, {
1647 TRY_TO(TraverseStmt(D->getInitExpr()));
1650 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1651 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1652 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1653 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1654 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1657 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1659 template<typename Derived>
1660 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1661 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1662 if (D->getTypeSourceInfo())
1663 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1665 TRY_TO(TraverseType(D->getType()));
1669 DEF_TRAVERSE_DECL(FieldDecl, {
1670 TRY_TO(TraverseDeclaratorHelper(D));
1671 if (D->isBitField())
1672 TRY_TO(TraverseStmt(D->getBitWidth()));
1673 else if (D->hasInClassInitializer())
1674 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1677 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1678 TRY_TO(TraverseDeclaratorHelper(D));
1679 if (D->isBitField())
1680 TRY_TO(TraverseStmt(D->getBitWidth()));
1681 // FIXME: implement the rest.
1684 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1685 TRY_TO(TraverseDeclaratorHelper(D));
1686 if (D->isBitField())
1687 TRY_TO(TraverseStmt(D->getBitWidth()));
1688 // FIXME: implement the rest.
1691 template<typename Derived>
1692 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1693 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1694 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1696 // If we're an explicit template specialization, iterate over the
1697 // template args that were explicitly specified. If we were doing
1698 // this in typing order, we'd do it between the return type and
1699 // the function args, but both are handled by the FunctionTypeLoc
1700 // above, so we have to choose one side. I've decided to do before.
1701 if (const FunctionTemplateSpecializationInfo *FTSI =
1702 D->getTemplateSpecializationInfo()) {
1703 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1704 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1705 // A specialization might not have explicit template arguments if it has
1706 // a templated return type and concrete arguments.
1707 if (const ASTTemplateArgumentListInfo *TALI =
1708 FTSI->TemplateArgumentsAsWritten) {
1709 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1710 TALI->NumTemplateArgs));
1715 // Visit the function type itself, which can be either
1716 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1717 // also covers the return type and the function parameters,
1718 // including exception specifications.
1719 if (clang::TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1720 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1723 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1724 // Constructor initializers.
1725 for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
1726 E = Ctor->init_end();
1728 TRY_TO(TraverseConstructorInitializer(*I));
1732 if (D->isThisDeclarationADefinition()) {
1733 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1738 DEF_TRAVERSE_DECL(FunctionDecl, {
1739 // We skip decls_begin/decls_end, which are already covered by
1740 // TraverseFunctionHelper().
1741 return TraverseFunctionHelper(D);
1744 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1745 // We skip decls_begin/decls_end, which are already covered by
1746 // TraverseFunctionHelper().
1747 return TraverseFunctionHelper(D);
1750 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1751 // We skip decls_begin/decls_end, which are already covered by
1752 // TraverseFunctionHelper().
1753 return TraverseFunctionHelper(D);
1756 // CXXConversionDecl is the declaration of a type conversion operator.
1757 // It's not a cast expression.
1758 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1759 // We skip decls_begin/decls_end, which are already covered by
1760 // TraverseFunctionHelper().
1761 return TraverseFunctionHelper(D);
1764 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1765 // We skip decls_begin/decls_end, which are already covered by
1766 // TraverseFunctionHelper().
1767 return TraverseFunctionHelper(D);
1770 template<typename Derived>
1771 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1772 TRY_TO(TraverseDeclaratorHelper(D));
1773 // Default params are taken care of when we traverse the ParmVarDecl.
1774 if (!isa<ParmVarDecl>(D) &&
1775 (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
1776 TRY_TO(TraverseStmt(D->getInit()));
1780 DEF_TRAVERSE_DECL(VarDecl, {
1781 TRY_TO(TraverseVarHelper(D));
1784 DEF_TRAVERSE_DECL(ImplicitParamDecl, {
1785 TRY_TO(TraverseVarHelper(D));
1788 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1789 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1790 TRY_TO(TraverseDeclaratorHelper(D));
1791 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1792 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1795 DEF_TRAVERSE_DECL(ParmVarDecl, {
1796 TRY_TO(TraverseVarHelper(D));
1798 if (D->hasDefaultArg() &&
1799 D->hasUninstantiatedDefaultArg() &&
1800 !D->hasUnparsedDefaultArg())
1801 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1803 if (D->hasDefaultArg() &&
1804 !D->hasUninstantiatedDefaultArg() &&
1805 !D->hasUnparsedDefaultArg())
1806 TRY_TO(TraverseStmt(D->getDefaultArg()));
1809 #undef DEF_TRAVERSE_DECL
1811 // ----------------- Stmt traversal -----------------
1813 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1814 // over the children defined in children() (every stmt defines these,
1815 // though sometimes the range is empty). Each individual Traverse*
1816 // method only needs to worry about children other than those. To see
1817 // what children() does for a given class, see, e.g.,
1818 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1820 // This macro makes available a variable S, the passed-in stmt.
1821 #define DEF_TRAVERSE_STMT(STMT, CODE) \
1822 template<typename Derived> \
1823 bool RecursiveASTVisitor<Derived>::Traverse##STMT (STMT *S) { \
1824 TRY_TO(WalkUpFrom##STMT(S)); \
1826 for (Stmt::child_range range = S->children(); range; ++range) { \
1827 TRY_TO(TraverseStmt(*range)); \
1832 DEF_TRAVERSE_STMT(GCCAsmStmt, {
1833 TRY_TO(TraverseStmt(S->getAsmString()));
1834 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1835 TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1837 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1838 TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1840 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1841 TRY_TO(TraverseStmt(S->getClobberStringLiteral(I)));
1843 // children() iterates over inputExpr and outputExpr.
1846 DEF_TRAVERSE_STMT(MSAsmStmt, {
1847 // FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once
1848 // added this needs to be implemented.
1851 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1852 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1853 // children() iterates over the handler block.
1856 DEF_TRAVERSE_STMT(DeclStmt, {
1857 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
1859 TRY_TO(TraverseDecl(*I));
1861 // Suppress the default iteration over children() by
1862 // returning. Here's why: A DeclStmt looks like 'type var [=
1863 // initializer]'. The decls above already traverse over the
1864 // initializers, so we don't have to do it again (which
1865 // children() would do).
1870 // These non-expr stmts (most of them), do not need any action except
1871 // iterating over the children.
1872 DEF_TRAVERSE_STMT(BreakStmt, { })
1873 DEF_TRAVERSE_STMT(CXXTryStmt, { })
1874 DEF_TRAVERSE_STMT(CaseStmt, { })
1875 DEF_TRAVERSE_STMT(CompoundStmt, { })
1876 DEF_TRAVERSE_STMT(ContinueStmt, { })
1877 DEF_TRAVERSE_STMT(DefaultStmt, { })
1878 DEF_TRAVERSE_STMT(DoStmt, { })
1879 DEF_TRAVERSE_STMT(ForStmt, { })
1880 DEF_TRAVERSE_STMT(GotoStmt, { })
1881 DEF_TRAVERSE_STMT(IfStmt, { })
1882 DEF_TRAVERSE_STMT(IndirectGotoStmt, { })
1883 DEF_TRAVERSE_STMT(LabelStmt, { })
1884 DEF_TRAVERSE_STMT(AttributedStmt, { })
1885 DEF_TRAVERSE_STMT(NullStmt, { })
1886 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, { })
1887 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, { })
1888 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, { })
1889 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, { })
1890 DEF_TRAVERSE_STMT(ObjCAtTryStmt, { })
1891 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, { })
1892 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, { })
1893 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
1894 if (!getDerived().shouldVisitImplicitCode()) {
1895 TRY_TO(TraverseStmt(S->getLoopVarStmt()));
1896 TRY_TO(TraverseStmt(S->getRangeInit()));
1897 TRY_TO(TraverseStmt(S->getBody()));
1898 // Visit everything else only if shouldVisitImplicitCode().
1902 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1903 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1904 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1906 DEF_TRAVERSE_STMT(ReturnStmt, { })
1907 DEF_TRAVERSE_STMT(SwitchStmt, { })
1908 DEF_TRAVERSE_STMT(WhileStmt, { })
1911 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1912 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1913 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1914 if (S->hasExplicitTemplateArgs()) {
1915 TRY_TO(TraverseTemplateArgumentLocsHelper(
1916 S->getTemplateArgs(), S->getNumTemplateArgs()));
1920 DEF_TRAVERSE_STMT(DeclRefExpr, {
1921 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1922 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1923 TRY_TO(TraverseTemplateArgumentLocsHelper(
1924 S->getTemplateArgs(), S->getNumTemplateArgs()));
1927 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1928 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1929 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1930 if (S->hasExplicitTemplateArgs()) {
1931 TRY_TO(TraverseTemplateArgumentLocsHelper(
1932 S->getExplicitTemplateArgs().getTemplateArgs(),
1933 S->getNumTemplateArgs()));
1937 DEF_TRAVERSE_STMT(MemberExpr, {
1938 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1939 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1940 TRY_TO(TraverseTemplateArgumentLocsHelper(
1941 S->getTemplateArgs(), S->getNumTemplateArgs()));
1944 DEF_TRAVERSE_STMT(ImplicitCastExpr, {
1945 // We don't traverse the cast type, as it's not written in the
1949 DEF_TRAVERSE_STMT(CStyleCastExpr, {
1950 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1953 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
1954 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1957 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
1958 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1961 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
1962 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1965 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
1966 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1969 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
1970 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1973 // InitListExpr is a tricky one, because we want to do all our work on
1974 // the syntactic form of the listexpr, but this method takes the
1975 // semantic form by default. We can't use the macro helper because it
1976 // calls WalkUp*() on the semantic form, before our code can convert
1977 // to the syntactic form.
1978 template<typename Derived>
1979 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
1980 if (InitListExpr *Syn = S->getSyntacticForm())
1982 TRY_TO(WalkUpFromInitListExpr(S));
1983 // All we need are the default actions. FIXME: use a helper function.
1984 for (Stmt::child_range range = S->children(); range; ++range) {
1985 TRY_TO(TraverseStmt(*range));
1990 // GenericSelectionExpr is a special case because the types and expressions
1991 // are interleaved. We also need to watch out for null types (default
1992 // generic associations).
1993 template<typename Derived>
1994 bool RecursiveASTVisitor<Derived>::
1995 TraverseGenericSelectionExpr(GenericSelectionExpr *S) {
1996 TRY_TO(WalkUpFromGenericSelectionExpr(S));
1997 TRY_TO(TraverseStmt(S->getControllingExpr()));
1998 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
1999 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2000 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2001 TRY_TO(TraverseStmt(S->getAssocExpr(i)));
2006 // PseudoObjectExpr is a special case because of the wierdness with
2007 // syntactic expressions and opaque values.
2008 template<typename Derived>
2009 bool RecursiveASTVisitor<Derived>::
2010 TraversePseudoObjectExpr(PseudoObjectExpr *S) {
2011 TRY_TO(WalkUpFromPseudoObjectExpr(S));
2012 TRY_TO(TraverseStmt(S->getSyntacticForm()));
2013 for (PseudoObjectExpr::semantics_iterator
2014 i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i) {
2016 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2017 sub = OVE->getSourceExpr();
2018 TRY_TO(TraverseStmt(sub));
2023 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2024 // This is called for code like 'return T()' where T is a built-in
2025 // (i.e. non-class) type.
2026 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2029 DEF_TRAVERSE_STMT(CXXNewExpr, {
2030 // The child-iterator will pick up the other arguments.
2031 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2034 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2035 // The child-iterator will pick up the expression representing
2037 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2038 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2039 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2042 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2043 // The child-iterator will pick up the arg if it's an expression,
2044 // but not if it's a type.
2045 if (S->isArgumentType())
2046 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2049 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2050 // The child-iterator will pick up the arg if it's an expression,
2051 // but not if it's a type.
2052 if (S->isTypeOperand())
2053 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2056 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2057 // The child-iterator will pick up the arg if it's an expression,
2058 // but not if it's a type.
2059 if (S->isTypeOperand())
2060 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2063 DEF_TRAVERSE_STMT(UnaryTypeTraitExpr, {
2064 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2067 DEF_TRAVERSE_STMT(BinaryTypeTraitExpr, {
2068 TRY_TO(TraverseTypeLoc(S->getLhsTypeSourceInfo()->getTypeLoc()));
2069 TRY_TO(TraverseTypeLoc(S->getRhsTypeSourceInfo()->getTypeLoc()));
2072 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2073 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2074 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2077 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2078 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2081 DEF_TRAVERSE_STMT(ExpressionTraitExpr, {
2082 TRY_TO(TraverseStmt(S->getQueriedExpression()));
2085 DEF_TRAVERSE_STMT(VAArgExpr, {
2086 // The child-iterator will pick up the expression argument.
2087 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2090 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2091 // This is called for code like 'return T()' where T is a class type.
2092 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2095 // Walk only the visible parts of lambda expressions.
2096 template<typename Derived>
2097 bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2098 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2099 CEnd = S->explicit_capture_end();
2101 TRY_TO(TraverseLambdaCapture(*C));
2104 if (S->hasExplicitParameters() || S->hasExplicitResultType()) {
2105 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2106 if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2107 // Visit the whole type.
2108 TRY_TO(TraverseTypeLoc(TL));
2109 } else if (isa<FunctionProtoTypeLoc>(TL)) {
2110 FunctionProtoTypeLoc Proto = cast<FunctionProtoTypeLoc>(TL);
2111 if (S->hasExplicitParameters()) {
2112 // Visit parameters.
2113 for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) {
2114 TRY_TO(TraverseDecl(Proto.getArg(I)));
2117 TRY_TO(TraverseTypeLoc(Proto.getResultLoc()));
2122 TRY_TO(TraverseStmt(S->getBody()));
2126 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2127 // This is called for code like 'T()', where T is a template argument.
2128 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2131 // These expressions all might take explicit template arguments.
2132 // We traverse those if so. FIXME: implement these.
2133 DEF_TRAVERSE_STMT(CXXConstructExpr, { })
2134 DEF_TRAVERSE_STMT(CallExpr, { })
2135 DEF_TRAVERSE_STMT(CXXMemberCallExpr, { })
2137 // These exprs (most of them), do not need any action except iterating
2138 // over the children.
2139 DEF_TRAVERSE_STMT(AddrLabelExpr, { })
2140 DEF_TRAVERSE_STMT(ArraySubscriptExpr, { })
2141 DEF_TRAVERSE_STMT(BlockExpr, {
2142 TRY_TO(TraverseDecl(S->getBlockDecl()));
2143 return true; // no child statements to loop through.
2145 DEF_TRAVERSE_STMT(ChooseExpr, { })
2146 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2147 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2149 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, { })
2150 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, { })
2151 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, { })
2152 DEF_TRAVERSE_STMT(CXXDeleteExpr, { })
2153 DEF_TRAVERSE_STMT(ExprWithCleanups, { })
2154 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, { })
2155 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2156 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2157 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2158 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2159 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2160 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2162 DEF_TRAVERSE_STMT(CXXThisExpr, { })
2163 DEF_TRAVERSE_STMT(CXXThrowExpr, { })
2164 DEF_TRAVERSE_STMT(UserDefinedLiteral, { })
2165 DEF_TRAVERSE_STMT(DesignatedInitExpr, { })
2166 DEF_TRAVERSE_STMT(ExtVectorElementExpr, { })
2167 DEF_TRAVERSE_STMT(GNUNullExpr, { })
2168 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, { })
2169 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, { })
2170 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2171 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2172 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2174 DEF_TRAVERSE_STMT(ObjCIsaExpr, { })
2175 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, { })
2176 DEF_TRAVERSE_STMT(ObjCMessageExpr, { })
2177 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, { })
2178 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, { })
2179 DEF_TRAVERSE_STMT(ObjCProtocolExpr, { })
2180 DEF_TRAVERSE_STMT(ObjCSelectorExpr, { })
2181 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, { })
2182 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2183 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2185 DEF_TRAVERSE_STMT(ParenExpr, { })
2186 DEF_TRAVERSE_STMT(ParenListExpr, { })
2187 DEF_TRAVERSE_STMT(PredefinedExpr, { })
2188 DEF_TRAVERSE_STMT(ShuffleVectorExpr, { })
2189 DEF_TRAVERSE_STMT(StmtExpr, { })
2190 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2191 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2192 if (S->hasExplicitTemplateArgs()) {
2193 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2194 S->getNumTemplateArgs()));
2198 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2199 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2200 if (S->hasExplicitTemplateArgs()) {
2201 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2202 S->getNumTemplateArgs()));
2206 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2207 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2208 DEF_TRAVERSE_STMT(SEHFinallyStmt,{})
2210 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, { })
2211 DEF_TRAVERSE_STMT(OpaqueValueExpr, { })
2212 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, { })
2214 // These operators (all of them) do not need any action except
2215 // iterating over the children.
2216 DEF_TRAVERSE_STMT(BinaryConditionalOperator, { })
2217 DEF_TRAVERSE_STMT(ConditionalOperator, { })
2218 DEF_TRAVERSE_STMT(UnaryOperator, { })
2219 DEF_TRAVERSE_STMT(BinaryOperator, { })
2220 DEF_TRAVERSE_STMT(CompoundAssignOperator, { })
2221 DEF_TRAVERSE_STMT(CXXNoexceptExpr, { })
2222 DEF_TRAVERSE_STMT(PackExpansionExpr, { })
2223 DEF_TRAVERSE_STMT(SizeOfPackExpr, { })
2224 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, { })
2225 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, { })
2226 DEF_TRAVERSE_STMT(FunctionParmPackExpr, { })
2227 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, { })
2228 DEF_TRAVERSE_STMT(AtomicExpr, { })
2230 // These literals (all of them) do not need any action.
2231 DEF_TRAVERSE_STMT(IntegerLiteral, { })
2232 DEF_TRAVERSE_STMT(CharacterLiteral, { })
2233 DEF_TRAVERSE_STMT(FloatingLiteral, { })
2234 DEF_TRAVERSE_STMT(ImaginaryLiteral, { })
2235 DEF_TRAVERSE_STMT(StringLiteral, { })
2236 DEF_TRAVERSE_STMT(ObjCStringLiteral, { })
2237 DEF_TRAVERSE_STMT(ObjCBoxedExpr, { })
2238 DEF_TRAVERSE_STMT(ObjCArrayLiteral, { })
2239 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, { })
2241 // Traverse OpenCL: AsType, Convert.
2242 DEF_TRAVERSE_STMT(AsTypeExpr, { })
2244 // FIXME: look at the following tricky-seeming exprs to see if we
2245 // need to recurse on anything. These are ones that have methods
2246 // returning decls or qualtypes or nestednamespecifier -- though I'm
2247 // not sure if they own them -- or just seemed very complicated, or
2248 // had lots of sub-types to explore.
2250 // VisitOverloadExpr and its children: recurse on template args? etc?
2252 // FIXME: go through all the stmts and exprs again, and see which of them
2253 // create new types, and recurse on the types (TypeLocs?) of those.
2256 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2257 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2258 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2259 // Every class that has getQualifier.
2261 #undef DEF_TRAVERSE_STMT
2265 } // end namespace clang
2267 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H