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_LIBCLANG_RECURSIVEASTVISITOR_H
15 #define LLVM_CLANG_LIBCLANG_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)
71 // A helper macro to implement short-circuiting when recursing. It
72 // invokes CALL_EXPR, which must be a method call, on the derived
73 // object (s.t. a user of RecursiveASTVisitor can override the method
75 #define TRY_TO(CALL_EXPR) \
76 do { if (!getDerived().CALL_EXPR) return false; } while (0)
78 /// \brief A class that does preorder depth-first traversal on the
79 /// entire Clang AST and visits each node.
81 /// This class performs three distinct tasks:
82 /// 1. traverse the AST (i.e. go to each node);
83 /// 2. at a given node, walk up the class hierarchy, starting from
84 /// the node's dynamic type, until the top-most class (e.g. Stmt,
85 /// Decl, or Type) is reached.
86 /// 3. given a (node, class) combination, where 'class' is some base
87 /// class of the dynamic type of 'node', call a user-overridable
88 /// function to actually visit the node.
90 /// These tasks are done by three groups of methods, respectively:
91 /// 1. TraverseDecl(Decl *x) does task #1. It is the entry point
92 /// for traversing an AST rooted at x. This method simply
93 /// dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo
94 /// is the dynamic type of *x, which calls WalkUpFromFoo(x) and
95 /// then recursively visits the child nodes of x.
96 /// TraverseStmt(Stmt *x) and TraverseType(QualType x) work
98 /// 2. WalkUpFromFoo(Foo *x) does task #2. It does not try to visit
99 /// any child node of x. Instead, it first calls WalkUpFromBar(x)
100 /// where Bar is the direct parent class of Foo (unless Foo has
101 /// no parent), and then calls VisitFoo(x) (see the next list item).
102 /// 3. VisitFoo(Foo *x) does task #3.
104 /// These three method groups are tiered (Traverse* > WalkUpFrom* >
105 /// Visit*). A method (e.g. Traverse*) may call methods from the same
106 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*).
107 /// It may not call methods from a higher tier.
109 /// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar
110 /// is Foo's super class) before calling VisitFoo(), the result is
111 /// that the Visit*() methods for a given node are called in the
112 /// top-down order (e.g. for a node of type NamedDecl, the order will
113 /// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()).
115 /// This scheme guarantees that all Visit*() calls for the same AST
116 /// node are grouped together. In other words, Visit*() methods for
117 /// different nodes are never interleaved.
119 /// Stmts are traversed internally using a data queue to avoid a stack overflow
120 /// with hugely nested ASTs.
122 /// Clients of this visitor should subclass the visitor (providing
123 /// themselves as the template argument, using the curiously recurring
124 /// template pattern) and override any of the Traverse*, WalkUpFrom*,
125 /// and Visit* methods for declarations, types, statements,
126 /// expressions, or other AST nodes where the visitor should customize
127 /// behavior. Most users only need to override Visit*. Advanced
128 /// users may override Traverse* and WalkUpFrom* to implement custom
129 /// traversal strategies. Returning false from one of these overridden
130 /// functions will abort the entire traversal.
132 /// By default, this visitor tries to visit every part of the explicit
133 /// source code exactly once. The default policy towards templates
134 /// is to descend into the 'pattern' class or function body, not any
135 /// explicit or implicit instantiations. Explicit specializations
136 /// are still visited, and the patterns of partial specializations
137 /// are visited separately. This behavior can be changed by
138 /// overriding shouldVisitTemplateInstantiations() in the derived class
139 /// to return true, in which case all known implicit and explicit
140 /// instantiations will be visited at the same time as the pattern
141 /// from which they were produced.
142 template<typename Derived>
143 class RecursiveASTVisitor {
145 /// \brief Return a reference to the derived class.
146 Derived &getDerived() { return *static_cast<Derived*>(this); }
148 /// \brief Return whether this visitor should recurse into
149 /// template instantiations.
150 bool shouldVisitTemplateInstantiations() const { return false; }
152 /// \brief Return whether this visitor should recurse into the types of
154 bool shouldWalkTypesOfTypeLocs() const { return true; }
156 /// \brief Recursively visit a statement or expression, by
157 /// dispatching to Traverse*() based on the argument's dynamic type.
159 /// \returns false if the visitation was terminated early, true
160 /// otherwise (including when the argument is NULL).
161 bool TraverseStmt(Stmt *S);
163 /// \brief Recursively visit a type, by dispatching to
164 /// Traverse*Type() based on the argument's getTypeClass() property.
166 /// \returns false if the visitation was terminated early, true
167 /// otherwise (including when the argument is a Null type).
168 bool TraverseType(QualType T);
170 /// \brief Recursively visit a type with location, by dispatching to
171 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
173 /// \returns false if the visitation was terminated early, true
174 /// otherwise (including when the argument is a Null type location).
175 bool TraverseTypeLoc(TypeLoc TL);
177 /// \brief Recursively visit a declaration, by dispatching to
178 /// Traverse*Decl() based on the argument's dynamic type.
180 /// \returns false if the visitation was terminated early, true
181 /// otherwise (including when the argument is NULL).
182 bool TraverseDecl(Decl *D);
184 /// \brief Recursively visit a C++ nested-name-specifier.
186 /// \returns false if the visitation was terminated early, true otherwise.
187 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
189 /// \brief Recursively visit a C++ nested-name-specifier with location
192 /// \returns false if the visitation was terminated early, true otherwise.
193 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
195 /// \brief Recursively visit a name with its location information.
197 /// \returns false if the visitation was terminated early, true otherwise.
198 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
200 /// \brief Recursively visit a template name and dispatch to the
201 /// appropriate method.
203 /// \returns false if the visitation was terminated early, true otherwise.
204 bool TraverseTemplateName(TemplateName Template);
206 /// \brief Recursively visit a template argument and dispatch to the
207 /// appropriate method for the argument type.
209 /// \returns false if the visitation was terminated early, true otherwise.
210 // FIXME: migrate callers to TemplateArgumentLoc instead.
211 bool TraverseTemplateArgument(const TemplateArgument &Arg);
213 /// \brief Recursively visit a template argument location and dispatch to the
214 /// appropriate method for the argument type.
216 /// \returns false if the visitation was terminated early, true otherwise.
217 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
219 /// \brief Recursively visit a set of template arguments.
220 /// This can be overridden by a subclass, but it's not expected that
221 /// will be needed -- this visitor always dispatches to another.
223 /// \returns false if the visitation was terminated early, true otherwise.
224 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
225 bool TraverseTemplateArguments(const TemplateArgument *Args,
228 /// \brief Recursively visit a constructor initializer. This
229 /// automatically dispatches to another visitor for the initializer
230 /// expression, but not for the name of the initializer, so may
231 /// be overridden for clients that need access to the name.
233 /// \returns false if the visitation was terminated early, true otherwise.
234 bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
236 /// \brief Recursively visit a lambda capture.
238 /// \returns false if the visitation was terminated early, true otherwise.
239 bool TraverseLambdaCapture(LambdaExpr::Capture C);
241 // ---- Methods on Stmts ----
243 // Declare Traverse*() for all concrete Stmt classes.
244 #define ABSTRACT_STMT(STMT)
245 #define STMT(CLASS, PARENT) \
246 bool Traverse##CLASS(CLASS *S);
247 #include "clang/AST/StmtNodes.inc"
248 // The above header #undefs ABSTRACT_STMT and STMT upon exit.
250 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
251 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
252 bool VisitStmt(Stmt *S) { return true; }
253 #define STMT(CLASS, PARENT) \
254 bool WalkUpFrom##CLASS(CLASS *S) { \
255 TRY_TO(WalkUpFrom##PARENT(S)); \
256 TRY_TO(Visit##CLASS(S)); \
259 bool Visit##CLASS(CLASS *S) { return true; }
260 #include "clang/AST/StmtNodes.inc"
262 // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary
263 // operator methods. Unary operators are not classes in themselves
264 // (they're all opcodes in UnaryOperator) but do have visitors.
265 #define OPERATOR(NAME) \
266 bool TraverseUnary##NAME(UnaryOperator *S) { \
267 TRY_TO(WalkUpFromUnary##NAME(S)); \
268 StmtQueueAction StmtQueue(*this); \
269 StmtQueue.queue(S->getSubExpr()); \
272 bool WalkUpFromUnary##NAME(UnaryOperator *S) { \
273 TRY_TO(WalkUpFromUnaryOperator(S)); \
274 TRY_TO(VisitUnary##NAME(S)); \
277 bool VisitUnary##NAME(UnaryOperator *S) { return true; }
282 // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary
283 // operator methods. Binary operators are not classes in themselves
284 // (they're all opcodes in BinaryOperator) but do have visitors.
285 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE) \
286 bool TraverseBin##NAME(BINOP_TYPE *S) { \
287 TRY_TO(WalkUpFromBin##NAME(S)); \
288 StmtQueueAction StmtQueue(*this); \
289 StmtQueue.queue(S->getLHS()); \
290 StmtQueue.queue(S->getRHS()); \
293 bool WalkUpFromBin##NAME(BINOP_TYPE *S) { \
294 TRY_TO(WalkUpFrom##BINOP_TYPE(S)); \
295 TRY_TO(VisitBin##NAME(S)); \
298 bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
300 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
304 // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound
305 // assignment methods. Compound assignment operators are not
306 // classes in themselves (they're all opcodes in
307 // CompoundAssignOperator) but do have visitors.
308 #define OPERATOR(NAME) \
309 GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator)
313 #undef GENERAL_BINOP_FALLBACK
315 // ---- Methods on Types ----
316 // FIXME: revamp to take TypeLoc's rather than Types.
318 // Declare Traverse*() for all concrete Type classes.
319 #define ABSTRACT_TYPE(CLASS, BASE)
320 #define TYPE(CLASS, BASE) \
321 bool Traverse##CLASS##Type(CLASS##Type *T);
322 #include "clang/AST/TypeNodes.def"
323 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
325 // Define WalkUpFrom*() and empty Visit*() for all Type classes.
326 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
327 bool VisitType(Type *T) { return true; }
328 #define TYPE(CLASS, BASE) \
329 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \
330 TRY_TO(WalkUpFrom##BASE(T)); \
331 TRY_TO(Visit##CLASS##Type(T)); \
334 bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
335 #include "clang/AST/TypeNodes.def"
337 // ---- Methods on TypeLocs ----
338 // FIXME: this currently just calls the matching Type methods
340 // Declare Traverse*() for all concrete Type classes.
341 #define ABSTRACT_TYPELOC(CLASS, BASE)
342 #define TYPELOC(CLASS, BASE) \
343 bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
344 #include "clang/AST/TypeLocNodes.def"
345 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
347 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
348 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
349 bool VisitTypeLoc(TypeLoc TL) { return true; }
351 // QualifiedTypeLoc and UnqualTypeLoc are not declared in
352 // TypeNodes.def and thus need to be handled specially.
353 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
354 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
356 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
357 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
358 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
360 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
362 // Note that BASE includes trailing 'Type' which CLASS doesn't.
363 #define TYPE(CLASS, BASE) \
364 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
365 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \
366 TRY_TO(Visit##CLASS##TypeLoc(TL)); \
369 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
370 #include "clang/AST/TypeNodes.def"
372 // ---- Methods on Decls ----
374 // Declare Traverse*() for all concrete Decl classes.
375 #define ABSTRACT_DECL(DECL)
376 #define DECL(CLASS, BASE) \
377 bool Traverse##CLASS##Decl(CLASS##Decl *D);
378 #include "clang/AST/DeclNodes.inc"
379 // The above header #undefs ABSTRACT_DECL and DECL upon exit.
381 // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
382 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
383 bool VisitDecl(Decl *D) { return true; }
384 #define DECL(CLASS, BASE) \
385 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \
386 TRY_TO(WalkUpFrom##BASE(D)); \
387 TRY_TO(Visit##CLASS##Decl(D)); \
390 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
391 #include "clang/AST/DeclNodes.inc"
394 // These are helper methods used by more than one Traverse* method.
395 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
396 bool TraverseClassInstantiations(ClassTemplateDecl *D);
397 bool TraverseFunctionInstantiations(FunctionTemplateDecl *D) ;
398 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
400 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
401 bool TraverseRecordHelper(RecordDecl *D);
402 bool TraverseCXXRecordHelper(CXXRecordDecl *D);
403 bool TraverseDeclaratorHelper(DeclaratorDecl *D);
404 bool TraverseDeclContextHelper(DeclContext *DC);
405 bool TraverseFunctionHelper(FunctionDecl *D);
406 bool TraverseVarHelper(VarDecl *D);
408 typedef SmallVector<Stmt *, 16> StmtsTy;
409 typedef SmallVector<StmtsTy *, 4> QueuesTy;
414 RecursiveASTVisitor &RAV;
416 NewQueueRAII(StmtsTy &queue, RecursiveASTVisitor &RAV) : RAV(RAV) {
417 RAV.Queues.push_back(&queue);
420 RAV.Queues.pop_back();
424 StmtsTy &getCurrentQueue() {
425 assert(!Queues.empty() && "base TraverseStmt was never called?");
426 return *Queues.back();
430 class StmtQueueAction {
433 explicit StmtQueueAction(RecursiveASTVisitor &RAV)
434 : CurrQueue(RAV.getCurrentQueue()) { }
436 void queue(Stmt *S) {
437 CurrQueue.push_back(S);
442 #define DISPATCH(NAME, CLASS, VAR) \
443 return getDerived().Traverse##NAME(static_cast<CLASS*>(VAR))
445 template<typename Derived>
446 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S) {
450 StmtsTy Queue, StmtsToEnqueu;
452 NewQueueRAII NQ(StmtsToEnqueu, *this);
454 while (!Queue.empty()) {
455 S = Queue.pop_back_val();
459 StmtsToEnqueu.clear();
461 #define DISPATCH_STMT(NAME, CLASS, VAR) \
462 TRY_TO(Traverse##NAME(static_cast<CLASS*>(VAR))); break
464 // If we have a binary expr, dispatch to the subcode of the binop. A smart
465 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
467 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
468 switch (BinOp->getOpcode()) {
469 #define OPERATOR(NAME) \
470 case BO_##NAME: DISPATCH_STMT(Bin##NAME, BinaryOperator, S);
476 #define OPERATOR(NAME) \
477 case BO_##NAME##Assign: \
478 DISPATCH_STMT(Bin##NAME##Assign, CompoundAssignOperator, S);
484 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
485 switch (UnOp->getOpcode()) {
486 #define OPERATOR(NAME) \
487 case UO_##NAME: DISPATCH_STMT(Unary##NAME, UnaryOperator, S);
495 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
496 switch (S->getStmtClass()) {
497 case Stmt::NoStmtClass: break;
498 #define ABSTRACT_STMT(STMT)
499 #define STMT(CLASS, PARENT) \
500 case Stmt::CLASS##Class: DISPATCH_STMT(CLASS, CLASS, S);
501 #include "clang/AST/StmtNodes.inc"
505 for (SmallVector<Stmt *, 8>::reverse_iterator
506 RI = StmtsToEnqueu.rbegin(),
507 RE = StmtsToEnqueu.rend(); RI != RE; ++RI)
508 Queue.push_back(*RI);
514 template<typename Derived>
515 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
519 switch (T->getTypeClass()) {
520 #define ABSTRACT_TYPE(CLASS, BASE)
521 #define TYPE(CLASS, BASE) \
522 case Type::CLASS: DISPATCH(CLASS##Type, CLASS##Type, \
523 const_cast<Type*>(T.getTypePtr()));
524 #include "clang/AST/TypeNodes.def"
530 template<typename Derived>
531 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
535 switch (TL.getTypeLocClass()) {
536 #define ABSTRACT_TYPELOC(CLASS, BASE)
537 #define TYPELOC(CLASS, BASE) \
538 case TypeLoc::CLASS: \
539 return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>());
540 #include "clang/AST/TypeLocNodes.def"
547 template<typename Derived>
548 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
552 // As a syntax visitor, we want to ignore declarations for
553 // implicitly-defined declarations (ones not typed explicitly by the
558 switch (D->getKind()) {
559 #define ABSTRACT_DECL(DECL)
560 #define DECL(CLASS, BASE) \
561 case Decl::CLASS: DISPATCH(CLASS##Decl, CLASS##Decl, D);
562 #include "clang/AST/DeclNodes.inc"
570 template<typename Derived>
571 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
572 NestedNameSpecifier *NNS) {
576 if (NNS->getPrefix())
577 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
579 switch (NNS->getKind()) {
580 case NestedNameSpecifier::Identifier:
581 case NestedNameSpecifier::Namespace:
582 case NestedNameSpecifier::NamespaceAlias:
583 case NestedNameSpecifier::Global:
586 case NestedNameSpecifier::TypeSpec:
587 case NestedNameSpecifier::TypeSpecWithTemplate:
588 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
594 template<typename Derived>
595 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
596 NestedNameSpecifierLoc NNS) {
600 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
601 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
603 switch (NNS.getNestedNameSpecifier()->getKind()) {
604 case NestedNameSpecifier::Identifier:
605 case NestedNameSpecifier::Namespace:
606 case NestedNameSpecifier::NamespaceAlias:
607 case NestedNameSpecifier::Global:
610 case NestedNameSpecifier::TypeSpec:
611 case NestedNameSpecifier::TypeSpecWithTemplate:
612 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
619 template<typename Derived>
620 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
621 DeclarationNameInfo NameInfo) {
622 switch (NameInfo.getName().getNameKind()) {
623 case DeclarationName::CXXConstructorName:
624 case DeclarationName::CXXDestructorName:
625 case DeclarationName::CXXConversionFunctionName:
626 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
627 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
631 case DeclarationName::Identifier:
632 case DeclarationName::ObjCZeroArgSelector:
633 case DeclarationName::ObjCOneArgSelector:
634 case DeclarationName::ObjCMultiArgSelector:
635 case DeclarationName::CXXOperatorName:
636 case DeclarationName::CXXLiteralOperatorName:
637 case DeclarationName::CXXUsingDirective:
644 template<typename Derived>
645 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
646 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
647 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
648 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
649 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
654 template<typename Derived>
655 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
656 const TemplateArgument &Arg) {
657 switch (Arg.getKind()) {
658 case TemplateArgument::Null:
659 case TemplateArgument::Declaration:
660 case TemplateArgument::Integral:
661 case TemplateArgument::NullPtr:
664 case TemplateArgument::Type:
665 return getDerived().TraverseType(Arg.getAsType());
667 case TemplateArgument::Template:
668 case TemplateArgument::TemplateExpansion:
669 return getDerived().TraverseTemplateName(
670 Arg.getAsTemplateOrTemplatePattern());
672 case TemplateArgument::Expression:
673 return getDerived().TraverseStmt(Arg.getAsExpr());
675 case TemplateArgument::Pack:
676 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
683 // FIXME: no template name location?
684 // FIXME: no source locations for a template argument pack?
685 template<typename Derived>
686 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
687 const TemplateArgumentLoc &ArgLoc) {
688 const TemplateArgument &Arg = ArgLoc.getArgument();
690 switch (Arg.getKind()) {
691 case TemplateArgument::Null:
692 case TemplateArgument::Declaration:
693 case TemplateArgument::Integral:
694 case TemplateArgument::NullPtr:
697 case TemplateArgument::Type: {
698 // FIXME: how can TSI ever be NULL?
699 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
700 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
702 return getDerived().TraverseType(Arg.getAsType());
705 case TemplateArgument::Template:
706 case TemplateArgument::TemplateExpansion:
707 if (ArgLoc.getTemplateQualifierLoc())
708 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
709 ArgLoc.getTemplateQualifierLoc()));
710 return getDerived().TraverseTemplateName(
711 Arg.getAsTemplateOrTemplatePattern());
713 case TemplateArgument::Expression:
714 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
716 case TemplateArgument::Pack:
717 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
724 template<typename Derived>
725 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
726 const TemplateArgument *Args,
728 for (unsigned I = 0; I != NumArgs; ++I) {
729 TRY_TO(TraverseTemplateArgument(Args[I]));
735 template<typename Derived>
736 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
737 CXXCtorInitializer *Init) {
738 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
739 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
741 if (Init->isWritten())
742 TRY_TO(TraverseStmt(Init->getInit()));
746 template<typename Derived>
747 bool RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr::Capture C){
751 // ----------------- Type traversal -----------------
753 // This macro makes available a variable T, the passed-in type.
754 #define DEF_TRAVERSE_TYPE(TYPE, CODE) \
755 template<typename Derived> \
756 bool RecursiveASTVisitor<Derived>::Traverse##TYPE (TYPE *T) { \
757 TRY_TO(WalkUpFrom##TYPE (T)); \
762 DEF_TRAVERSE_TYPE(BuiltinType, { })
764 DEF_TRAVERSE_TYPE(ComplexType, {
765 TRY_TO(TraverseType(T->getElementType()));
768 DEF_TRAVERSE_TYPE(PointerType, {
769 TRY_TO(TraverseType(T->getPointeeType()));
772 DEF_TRAVERSE_TYPE(BlockPointerType, {
773 TRY_TO(TraverseType(T->getPointeeType()));
776 DEF_TRAVERSE_TYPE(LValueReferenceType, {
777 TRY_TO(TraverseType(T->getPointeeType()));
780 DEF_TRAVERSE_TYPE(RValueReferenceType, {
781 TRY_TO(TraverseType(T->getPointeeType()));
784 DEF_TRAVERSE_TYPE(MemberPointerType, {
785 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
786 TRY_TO(TraverseType(T->getPointeeType()));
789 DEF_TRAVERSE_TYPE(ConstantArrayType, {
790 TRY_TO(TraverseType(T->getElementType()));
793 DEF_TRAVERSE_TYPE(IncompleteArrayType, {
794 TRY_TO(TraverseType(T->getElementType()));
797 DEF_TRAVERSE_TYPE(VariableArrayType, {
798 TRY_TO(TraverseType(T->getElementType()));
799 TRY_TO(TraverseStmt(T->getSizeExpr()));
802 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
803 TRY_TO(TraverseType(T->getElementType()));
804 if (T->getSizeExpr())
805 TRY_TO(TraverseStmt(T->getSizeExpr()));
808 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
809 if (T->getSizeExpr())
810 TRY_TO(TraverseStmt(T->getSizeExpr()));
811 TRY_TO(TraverseType(T->getElementType()));
814 DEF_TRAVERSE_TYPE(VectorType, {
815 TRY_TO(TraverseType(T->getElementType()));
818 DEF_TRAVERSE_TYPE(ExtVectorType, {
819 TRY_TO(TraverseType(T->getElementType()));
822 DEF_TRAVERSE_TYPE(FunctionNoProtoType, {
823 TRY_TO(TraverseType(T->getResultType()));
826 DEF_TRAVERSE_TYPE(FunctionProtoType, {
827 TRY_TO(TraverseType(T->getResultType()));
829 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
830 AEnd = T->arg_type_end();
832 TRY_TO(TraverseType(*A));
835 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
836 EEnd = T->exception_end();
838 TRY_TO(TraverseType(*E));
842 DEF_TRAVERSE_TYPE(UnresolvedUsingType, { })
843 DEF_TRAVERSE_TYPE(TypedefType, { })
845 DEF_TRAVERSE_TYPE(TypeOfExprType, {
846 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
849 DEF_TRAVERSE_TYPE(TypeOfType, {
850 TRY_TO(TraverseType(T->getUnderlyingType()));
853 DEF_TRAVERSE_TYPE(DecltypeType, {
854 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
857 DEF_TRAVERSE_TYPE(UnaryTransformType, {
858 TRY_TO(TraverseType(T->getBaseType()));
859 TRY_TO(TraverseType(T->getUnderlyingType()));
862 DEF_TRAVERSE_TYPE(AutoType, {
863 TRY_TO(TraverseType(T->getDeducedType()));
866 DEF_TRAVERSE_TYPE(RecordType, { })
867 DEF_TRAVERSE_TYPE(EnumType, { })
868 DEF_TRAVERSE_TYPE(TemplateTypeParmType, { })
869 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, { })
870 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, { })
872 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
873 TRY_TO(TraverseTemplateName(T->getTemplateName()));
874 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
877 DEF_TRAVERSE_TYPE(InjectedClassNameType, { })
879 DEF_TRAVERSE_TYPE(AttributedType, {
880 TRY_TO(TraverseType(T->getModifiedType()));
883 DEF_TRAVERSE_TYPE(ParenType, {
884 TRY_TO(TraverseType(T->getInnerType()));
887 DEF_TRAVERSE_TYPE(ElaboratedType, {
888 if (T->getQualifier()) {
889 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
891 TRY_TO(TraverseType(T->getNamedType()));
894 DEF_TRAVERSE_TYPE(DependentNameType, {
895 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
898 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
899 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
900 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
903 DEF_TRAVERSE_TYPE(PackExpansionType, {
904 TRY_TO(TraverseType(T->getPattern()));
907 DEF_TRAVERSE_TYPE(ObjCInterfaceType, { })
909 DEF_TRAVERSE_TYPE(ObjCObjectType, {
910 // We have to watch out here because an ObjCInterfaceType's base
912 if (T->getBaseType().getTypePtr() != T)
913 TRY_TO(TraverseType(T->getBaseType()));
916 DEF_TRAVERSE_TYPE(ObjCObjectPointerType, {
917 TRY_TO(TraverseType(T->getPointeeType()));
920 DEF_TRAVERSE_TYPE(AtomicType, {
921 TRY_TO(TraverseType(T->getValueType()));
924 #undef DEF_TRAVERSE_TYPE
926 // ----------------- TypeLoc traversal -----------------
928 // This macro makes available a variable TL, the passed-in TypeLoc.
929 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
930 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
931 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
933 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
934 template<typename Derived> \
935 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
936 if (getDerived().shouldWalkTypesOfTypeLocs()) \
937 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE*>(TL.getTypePtr()))); \
938 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
943 template<typename Derived>
944 bool RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(
945 QualifiedTypeLoc TL) {
946 // Move this over to the 'main' typeloc tree. Note that this is a
947 // move -- we pretend that we were really looking at the unqualified
948 // typeloc all along -- rather than a recursion, so we don't follow
949 // the normal CRTP plan of going through
950 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
951 // twice for the same type (once as a QualifiedTypeLoc version of
952 // the type, once as an UnqualifiedTypeLoc version of the type),
953 // which in effect means we'd call VisitTypeLoc twice with the
954 // 'same' type. This solves that problem, at the cost of never
955 // seeing the qualified version of the type (unless the client
956 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
957 // perfect solution. A perfect solution probably requires making
958 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
959 // wrapper around Type* -- rather than being its own class in the
961 return TraverseTypeLoc(TL.getUnqualifiedLoc());
964 DEF_TRAVERSE_TYPELOC(BuiltinType, { })
966 // FIXME: ComplexTypeLoc is unfinished
967 DEF_TRAVERSE_TYPELOC(ComplexType, {
968 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
971 DEF_TRAVERSE_TYPELOC(PointerType, {
972 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
975 DEF_TRAVERSE_TYPELOC(BlockPointerType, {
976 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
979 DEF_TRAVERSE_TYPELOC(LValueReferenceType, {
980 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
983 DEF_TRAVERSE_TYPELOC(RValueReferenceType, {
984 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
987 // FIXME: location of base class?
988 // We traverse this in the type case as well, but how is it not reached through
990 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
991 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
992 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
995 template<typename Derived>
996 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
997 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
998 TRY_TO(TraverseStmt(TL.getSizeExpr()));
1002 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1003 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1004 return TraverseArrayTypeLocHelper(TL);
1007 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1008 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1009 return TraverseArrayTypeLocHelper(TL);
1012 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1013 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1014 return TraverseArrayTypeLocHelper(TL);
1017 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1018 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1019 return TraverseArrayTypeLocHelper(TL);
1022 // FIXME: order? why not size expr first?
1023 // FIXME: base VectorTypeLoc is unfinished
1024 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1025 if (TL.getTypePtr()->getSizeExpr())
1026 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1027 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1030 // FIXME: VectorTypeLoc is unfinished
1031 DEF_TRAVERSE_TYPELOC(VectorType, {
1032 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1035 // FIXME: size and attributes
1036 // FIXME: base VectorTypeLoc is unfinished
1037 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1038 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1041 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, {
1042 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1045 // FIXME: location of exception specifications (attributes?)
1046 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1047 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1049 const FunctionProtoType *T = TL.getTypePtr();
1051 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1053 TRY_TO(TraverseDecl(TL.getArg(I)));
1054 } else if (I < T->getNumArgs()) {
1055 TRY_TO(TraverseType(T->getArgType(I)));
1059 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1060 EEnd = T->exception_end();
1062 TRY_TO(TraverseType(*E));
1066 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, { })
1067 DEF_TRAVERSE_TYPELOC(TypedefType, { })
1069 DEF_TRAVERSE_TYPELOC(TypeOfExprType, {
1070 TRY_TO(TraverseStmt(TL.getUnderlyingExpr()));
1073 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1074 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1077 // FIXME: location of underlying expr
1078 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1079 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1082 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1083 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1086 DEF_TRAVERSE_TYPELOC(AutoType, {
1087 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1090 DEF_TRAVERSE_TYPELOC(RecordType, { })
1091 DEF_TRAVERSE_TYPELOC(EnumType, { })
1092 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, { })
1093 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, { })
1094 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, { })
1096 // FIXME: use the loc for the template name?
1097 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1098 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1099 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1100 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1104 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, { })
1106 DEF_TRAVERSE_TYPELOC(ParenType, {
1107 TRY_TO(TraverseTypeLoc(TL.getInnerLoc()));
1110 DEF_TRAVERSE_TYPELOC(AttributedType, {
1111 TRY_TO(TraverseTypeLoc(TL.getModifiedLoc()));
1114 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1115 if (TL.getQualifierLoc()) {
1116 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1118 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1121 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1122 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1125 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1126 if (TL.getQualifierLoc()) {
1127 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1130 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1131 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1135 DEF_TRAVERSE_TYPELOC(PackExpansionType, {
1136 TRY_TO(TraverseTypeLoc(TL.getPatternLoc()));
1139 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, { })
1141 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1142 // We have to watch out here because an ObjCInterfaceType's base
1144 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1145 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1148 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, {
1149 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1152 DEF_TRAVERSE_TYPELOC(AtomicType, {
1153 TRY_TO(TraverseTypeLoc(TL.getValueLoc()));
1156 #undef DEF_TRAVERSE_TYPELOC
1158 // ----------------- Decl traversal -----------------
1160 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1161 // the children that come from the DeclContext associated with it.
1162 // Therefore each Traverse* only needs to worry about children other
1165 template<typename Derived>
1166 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1170 for (DeclContext::decl_iterator Child = DC->decls_begin(),
1171 ChildEnd = DC->decls_end();
1172 Child != ChildEnd; ++Child) {
1173 // BlockDecls and CapturedDecls are traversed through BlockExprs and
1174 // CapturedStmts respectively.
1175 if (!isa<BlockDecl>(*Child) && !isa<CapturedDecl>(*Child))
1176 TRY_TO(TraverseDecl(*Child));
1182 // This macro makes available a variable D, the passed-in decl.
1183 #define DEF_TRAVERSE_DECL(DECL, CODE) \
1184 template<typename Derived> \
1185 bool RecursiveASTVisitor<Derived>::Traverse##DECL (DECL *D) { \
1186 TRY_TO(WalkUpFrom##DECL (D)); \
1188 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1192 DEF_TRAVERSE_DECL(AccessSpecDecl, { })
1194 DEF_TRAVERSE_DECL(BlockDecl, {
1195 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1196 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1197 TRY_TO(TraverseStmt(D->getBody()));
1198 // This return statement makes sure the traversal of nodes in
1199 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1200 // is skipped - don't remove it.
1204 DEF_TRAVERSE_DECL(CapturedDecl, {
1205 TRY_TO(TraverseStmt(D->getBody()));
1206 // This return statement makes sure the traversal of nodes in
1207 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1208 // is skipped - don't remove it.
1212 DEF_TRAVERSE_DECL(EmptyDecl, { })
1214 DEF_TRAVERSE_DECL(FileScopeAsmDecl, {
1215 TRY_TO(TraverseStmt(D->getAsmString()));
1218 DEF_TRAVERSE_DECL(ImportDecl, { })
1220 DEF_TRAVERSE_DECL(FriendDecl, {
1221 // Friend is either decl or a type.
1222 if (D->getFriendType())
1223 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1225 TRY_TO(TraverseDecl(D->getFriendDecl()));
1228 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1229 if (D->getFriendType())
1230 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1232 TRY_TO(TraverseDecl(D->getFriendDecl()));
1233 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1234 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1235 for (TemplateParameterList::iterator ITPL = TPL->begin(),
1237 ITPL != ETPL; ++ITPL) {
1238 TRY_TO(TraverseDecl(*ITPL));
1243 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1244 TRY_TO(TraverseDecl(D->getSpecialization()));
1247 DEF_TRAVERSE_DECL(LinkageSpecDecl, { })
1249 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {
1250 // FIXME: implement this
1253 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1254 TRY_TO(TraverseStmt(D->getAssertExpr()));
1255 TRY_TO(TraverseStmt(D->getMessage()));
1258 DEF_TRAVERSE_DECL(TranslationUnitDecl, {
1259 // Code in an unnamed namespace shows up automatically in
1260 // decls_begin()/decls_end(). Thus we don't need to recurse on
1261 // D->getAnonymousNamespace().
1264 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1265 // We shouldn't traverse an aliased namespace, since it will be
1266 // defined (and, therefore, traversed) somewhere else.
1268 // This return statement makes sure the traversal of nodes in
1269 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1270 // is skipped - don't remove it.
1274 DEF_TRAVERSE_DECL(LabelDecl, {
1275 // There is no code in a LabelDecl.
1279 DEF_TRAVERSE_DECL(NamespaceDecl, {
1280 // Code in an unnamed namespace shows up automatically in
1281 // decls_begin()/decls_end(). Thus we don't need to recurse on
1282 // D->getAnonymousNamespace().
1285 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {
1289 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {
1293 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {
1297 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {
1301 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {
1305 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {
1309 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1310 if (D->getResultTypeSourceInfo()) {
1311 TRY_TO(TraverseTypeLoc(D->getResultTypeSourceInfo()->getTypeLoc()));
1313 for (ObjCMethodDecl::param_iterator
1314 I = D->param_begin(), E = D->param_end(); I != E; ++I) {
1315 TRY_TO(TraverseDecl(*I));
1317 if (D->isThisDeclarationADefinition()) {
1318 TRY_TO(TraverseStmt(D->getBody()));
1323 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1327 DEF_TRAVERSE_DECL(UsingDecl, {
1328 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1329 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1332 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1333 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1336 DEF_TRAVERSE_DECL(UsingShadowDecl, { })
1338 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1339 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1340 E = D->varlist_end();
1342 TRY_TO(TraverseStmt(*I));
1346 // A helper method for TemplateDecl's children.
1347 template<typename Derived>
1348 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1349 TemplateParameterList *TPL) {
1351 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1353 TRY_TO(TraverseDecl(*I));
1359 // A helper method for traversing the implicit instantiations of a
1361 template<typename Derived>
1362 bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations(
1363 ClassTemplateDecl *D) {
1364 ClassTemplateDecl::spec_iterator end = D->spec_end();
1365 for (ClassTemplateDecl::spec_iterator it = D->spec_begin(); it != end; ++it) {
1366 ClassTemplateSpecializationDecl* SD = *it;
1368 switch (SD->getSpecializationKind()) {
1369 // Visit the implicit instantiations with the requested pattern.
1370 case TSK_Undeclared:
1371 case TSK_ImplicitInstantiation:
1372 TRY_TO(TraverseDecl(SD));
1375 // We don't need to do anything on an explicit instantiation
1376 // or explicit specialization because there will be an explicit
1377 // node for it elsewhere.
1378 case TSK_ExplicitInstantiationDeclaration:
1379 case TSK_ExplicitInstantiationDefinition:
1380 case TSK_ExplicitSpecialization:
1388 DEF_TRAVERSE_DECL(ClassTemplateDecl, {
1389 CXXRecordDecl* TempDecl = D->getTemplatedDecl();
1390 TRY_TO(TraverseDecl(TempDecl));
1391 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1393 // By default, we do not traverse the instantiations of
1394 // class templates since they do not appear in the user code. The
1395 // following code optionally traverses them.
1397 // We only traverse the class instantiations when we see the canonical
1398 // declaration of the template, to ensure we only visit them once.
1399 if (getDerived().shouldVisitTemplateInstantiations() &&
1400 D == D->getCanonicalDecl())
1401 TRY_TO(TraverseClassInstantiations(D));
1403 // Note that getInstantiatedFromMemberTemplate() is just a link
1404 // from a template instantiation back to the template from which
1405 // it was instantiated, and thus should not be traversed.
1408 // A helper method for traversing the instantiations of a
1409 // function while skipping its specializations.
1410 template<typename Derived>
1411 bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations(
1412 FunctionTemplateDecl *D) {
1413 FunctionTemplateDecl::spec_iterator end = D->spec_end();
1414 for (FunctionTemplateDecl::spec_iterator it = D->spec_begin(); it != end;
1416 FunctionDecl* FD = *it;
1417 switch (FD->getTemplateSpecializationKind()) {
1418 case TSK_Undeclared:
1419 case TSK_ImplicitInstantiation:
1420 // We don't know what kind of FunctionDecl this is.
1421 TRY_TO(TraverseDecl(FD));
1424 // No need to visit explicit instantiations, we'll find the node
1426 case TSK_ExplicitInstantiationDeclaration:
1427 case TSK_ExplicitInstantiationDefinition:
1430 case TSK_ExplicitSpecialization:
1438 DEF_TRAVERSE_DECL(FunctionTemplateDecl, {
1439 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1440 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1442 // By default, we do not traverse the instantiations of
1443 // function templates since they do not appear in the user code. The
1444 // following code optionally traverses them.
1446 // We only traverse the function instantiations when we see the canonical
1447 // declaration of the template, to ensure we only visit them once.
1448 if (getDerived().shouldVisitTemplateInstantiations() &&
1449 D == D->getCanonicalDecl())
1450 TRY_TO(TraverseFunctionInstantiations(D));
1453 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1454 // D is the "T" in something like
1455 // template <template <typename> class T> class container { };
1456 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1457 if (D->hasDefaultArgument()) {
1458 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1460 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1463 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1464 // D is the "T" in something like "template<typename T> class vector;"
1465 if (D->getTypeForDecl())
1466 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1467 if (D->hasDefaultArgument())
1468 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1471 DEF_TRAVERSE_DECL(TypedefDecl, {
1472 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1473 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1474 // declaring the typedef, not something that was written in the
1478 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1479 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1480 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1481 // declaring the type alias, not something that was written in the
1485 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1486 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1487 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1490 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1491 // A dependent using declaration which was marked with 'typename'.
1492 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1493 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1494 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1495 // declaring the type, not something that was written in the
1499 DEF_TRAVERSE_DECL(EnumDecl, {
1500 if (D->getTypeForDecl())
1501 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1503 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1504 // The enumerators are already traversed by
1505 // decls_begin()/decls_end().
1509 // Helper methods for RecordDecl and its children.
1510 template<typename Derived>
1511 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(
1513 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1514 // declaring the type, not something that was written in the source.
1516 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1520 template<typename Derived>
1521 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(
1523 if (!TraverseRecordHelper(D))
1525 if (D->isCompleteDefinition()) {
1526 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1529 TRY_TO(TraverseTypeLoc(I->getTypeSourceInfo()->getTypeLoc()));
1531 // We don't traverse the friends or the conversions, as they are
1532 // already in decls_begin()/decls_end().
1537 DEF_TRAVERSE_DECL(RecordDecl, {
1538 TRY_TO(TraverseRecordHelper(D));
1541 DEF_TRAVERSE_DECL(CXXRecordDecl, {
1542 TRY_TO(TraverseCXXRecordHelper(D));
1545 DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, {
1546 // For implicit instantiations ("set<int> x;"), we don't want to
1547 // recurse at all, since the instatiated class isn't written in
1548 // the source code anywhere. (Note the instatiated *type* --
1549 // set<int> -- is written, and will still get a callback of
1550 // TemplateSpecializationType). For explicit instantiations
1551 // ("template set<int>;"), we do need a callback, since this
1552 // is the only callback that's made for this instantiation.
1553 // We use getTypeAsWritten() to distinguish.
1554 if (TypeSourceInfo *TSI = D->getTypeAsWritten())
1555 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1557 if (!getDerived().shouldVisitTemplateInstantiations() &&
1558 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1559 // Returning from here skips traversing the
1560 // declaration context of the ClassTemplateSpecializationDecl
1561 // (embedded in the DEF_TRAVERSE_DECL() macro)
1562 // which contains the instantiated members of the class.
1566 template <typename Derived>
1567 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1568 const TemplateArgumentLoc *TAL, unsigned Count) {
1569 for (unsigned I = 0; I < Count; ++I) {
1570 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1575 DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, {
1576 // The partial specialization.
1577 if (TemplateParameterList *TPL = D->getTemplateParameters()) {
1578 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1580 TRY_TO(TraverseDecl(*I));
1583 // The args that remains unspecialized.
1584 TRY_TO(TraverseTemplateArgumentLocsHelper(
1585 D->getTemplateArgsAsWritten(), D->getNumTemplateArgsAsWritten()));
1587 // Don't need the ClassTemplatePartialSpecializationHelper, even
1588 // though that's our parent class -- we already visit all the
1589 // template args here.
1590 TRY_TO(TraverseCXXRecordHelper(D));
1592 // Instantiations will have been visited with the primary template.
1595 DEF_TRAVERSE_DECL(EnumConstantDecl, {
1596 TRY_TO(TraverseStmt(D->getInitExpr()));
1599 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1600 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1601 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1602 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1603 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1606 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1608 template<typename Derived>
1609 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1610 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1611 if (D->getTypeSourceInfo())
1612 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1614 TRY_TO(TraverseType(D->getType()));
1618 DEF_TRAVERSE_DECL(FieldDecl, {
1619 TRY_TO(TraverseDeclaratorHelper(D));
1620 if (D->isBitField())
1621 TRY_TO(TraverseStmt(D->getBitWidth()));
1622 else if (D->hasInClassInitializer())
1623 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1626 DEF_TRAVERSE_DECL(MSPropertyDecl, {
1627 TRY_TO(TraverseDeclaratorHelper(D));
1630 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1631 TRY_TO(TraverseDeclaratorHelper(D));
1632 if (D->isBitField())
1633 TRY_TO(TraverseStmt(D->getBitWidth()));
1634 // FIXME: implement the rest.
1637 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1638 TRY_TO(TraverseDeclaratorHelper(D));
1639 if (D->isBitField())
1640 TRY_TO(TraverseStmt(D->getBitWidth()));
1641 // FIXME: implement the rest.
1644 template<typename Derived>
1645 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1646 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1647 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1649 // If we're an explicit template specialization, iterate over the
1650 // template args that were explicitly specified. If we were doing
1651 // this in typing order, we'd do it between the return type and
1652 // the function args, but both are handled by the FunctionTypeLoc
1653 // above, so we have to choose one side. I've decided to do before.
1654 if (const FunctionTemplateSpecializationInfo *FTSI =
1655 D->getTemplateSpecializationInfo()) {
1656 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1657 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1658 // A specialization might not have explicit template arguments if it has
1659 // a templated return type and concrete arguments.
1660 if (const ASTTemplateArgumentListInfo *TALI =
1661 FTSI->TemplateArgumentsAsWritten) {
1662 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1663 TALI->NumTemplateArgs));
1668 // Visit the function type itself, which can be either
1669 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1670 // also covers the return type and the function parameters,
1671 // including exception specifications.
1672 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1674 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1675 // Constructor initializers.
1676 for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
1677 E = Ctor->init_end();
1679 TRY_TO(TraverseConstructorInitializer(*I));
1683 if (D->isThisDeclarationADefinition()) {
1684 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1689 DEF_TRAVERSE_DECL(FunctionDecl, {
1690 // We skip decls_begin/decls_end, which are already covered by
1691 // TraverseFunctionHelper().
1692 return TraverseFunctionHelper(D);
1695 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1696 // We skip decls_begin/decls_end, which are already covered by
1697 // TraverseFunctionHelper().
1698 return TraverseFunctionHelper(D);
1701 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1702 // We skip decls_begin/decls_end, which are already covered by
1703 // TraverseFunctionHelper().
1704 return TraverseFunctionHelper(D);
1707 // CXXConversionDecl is the declaration of a type conversion operator.
1708 // It's not a cast expression.
1709 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1710 // We skip decls_begin/decls_end, which are already covered by
1711 // TraverseFunctionHelper().
1712 return TraverseFunctionHelper(D);
1715 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1716 // We skip decls_begin/decls_end, which are already covered by
1717 // TraverseFunctionHelper().
1718 return TraverseFunctionHelper(D);
1721 template<typename Derived>
1722 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1723 TRY_TO(TraverseDeclaratorHelper(D));
1724 // Default params are taken care of when we traverse the ParmVarDecl.
1725 if (!isa<ParmVarDecl>(D))
1726 TRY_TO(TraverseStmt(D->getInit()));
1730 DEF_TRAVERSE_DECL(VarDecl, {
1731 TRY_TO(TraverseVarHelper(D));
1734 DEF_TRAVERSE_DECL(ImplicitParamDecl, {
1735 TRY_TO(TraverseVarHelper(D));
1738 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1739 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1740 TRY_TO(TraverseDeclaratorHelper(D));
1741 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1744 DEF_TRAVERSE_DECL(ParmVarDecl, {
1745 TRY_TO(TraverseVarHelper(D));
1747 if (D->hasDefaultArg() &&
1748 D->hasUninstantiatedDefaultArg() &&
1749 !D->hasUnparsedDefaultArg())
1750 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1752 if (D->hasDefaultArg() &&
1753 !D->hasUninstantiatedDefaultArg() &&
1754 !D->hasUnparsedDefaultArg())
1755 TRY_TO(TraverseStmt(D->getDefaultArg()));
1758 #undef DEF_TRAVERSE_DECL
1760 // ----------------- Stmt traversal -----------------
1762 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1763 // over the children defined in children() (every stmt defines these,
1764 // though sometimes the range is empty). Each individual Traverse*
1765 // method only needs to worry about children other than those. To see
1766 // what children() does for a given class, see, e.g.,
1767 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1769 // This macro makes available a variable S, the passed-in stmt.
1770 #define DEF_TRAVERSE_STMT(STMT, CODE) \
1771 template<typename Derived> \
1772 bool RecursiveASTVisitor<Derived>::Traverse##STMT (STMT *S) { \
1773 TRY_TO(WalkUpFrom##STMT(S)); \
1774 StmtQueueAction StmtQueue(*this); \
1776 for (Stmt::child_range range = S->children(); range; ++range) { \
1777 StmtQueue.queue(*range); \
1782 DEF_TRAVERSE_STMT(GCCAsmStmt, {
1783 StmtQueue.queue(S->getAsmString());
1784 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1785 StmtQueue.queue(S->getInputConstraintLiteral(I));
1787 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1788 StmtQueue.queue(S->getOutputConstraintLiteral(I));
1790 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1791 StmtQueue.queue(S->getClobberStringLiteral(I));
1793 // children() iterates over inputExpr and outputExpr.
1796 DEF_TRAVERSE_STMT(MSAsmStmt, {
1797 // FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once
1798 // added this needs to be implemented.
1801 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1802 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1803 // children() iterates over the handler block.
1806 DEF_TRAVERSE_STMT(DeclStmt, {
1807 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
1809 TRY_TO(TraverseDecl(*I));
1811 // Suppress the default iteration over children() by
1812 // returning. Here's why: A DeclStmt looks like 'type var [=
1813 // initializer]'. The decls above already traverse over the
1814 // initializers, so we don't have to do it again (which
1815 // children() would do).
1820 // These non-expr stmts (most of them), do not need any action except
1821 // iterating over the children.
1822 DEF_TRAVERSE_STMT(BreakStmt, { })
1823 DEF_TRAVERSE_STMT(CXXTryStmt, { })
1824 DEF_TRAVERSE_STMT(CaseStmt, { })
1825 DEF_TRAVERSE_STMT(CompoundStmt, { })
1826 DEF_TRAVERSE_STMT(ContinueStmt, { })
1827 DEF_TRAVERSE_STMT(DefaultStmt, { })
1828 DEF_TRAVERSE_STMT(DoStmt, { })
1829 DEF_TRAVERSE_STMT(ForStmt, { })
1830 DEF_TRAVERSE_STMT(GotoStmt, { })
1831 DEF_TRAVERSE_STMT(IfStmt, { })
1832 DEF_TRAVERSE_STMT(IndirectGotoStmt, { })
1833 DEF_TRAVERSE_STMT(LabelStmt, { })
1834 DEF_TRAVERSE_STMT(AttributedStmt, { })
1835 DEF_TRAVERSE_STMT(NullStmt, { })
1836 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, { })
1837 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, { })
1838 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, { })
1839 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, { })
1840 DEF_TRAVERSE_STMT(ObjCAtTryStmt, { })
1841 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, { })
1842 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, { })
1843 DEF_TRAVERSE_STMT(CXXForRangeStmt, { })
1844 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1845 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1846 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1848 DEF_TRAVERSE_STMT(ReturnStmt, { })
1849 DEF_TRAVERSE_STMT(SwitchStmt, { })
1850 DEF_TRAVERSE_STMT(WhileStmt, { })
1852 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1853 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1854 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1855 if (S->hasExplicitTemplateArgs()) {
1856 TRY_TO(TraverseTemplateArgumentLocsHelper(
1857 S->getTemplateArgs(), S->getNumTemplateArgs()));
1861 DEF_TRAVERSE_STMT(DeclRefExpr, {
1862 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1863 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1864 TRY_TO(TraverseTemplateArgumentLocsHelper(
1865 S->getTemplateArgs(), S->getNumTemplateArgs()));
1868 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1869 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1870 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1871 if (S->hasExplicitTemplateArgs()) {
1872 TRY_TO(TraverseTemplateArgumentLocsHelper(
1873 S->getExplicitTemplateArgs().getTemplateArgs(),
1874 S->getNumTemplateArgs()));
1878 DEF_TRAVERSE_STMT(MemberExpr, {
1879 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1880 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1881 TRY_TO(TraverseTemplateArgumentLocsHelper(
1882 S->getTemplateArgs(), S->getNumTemplateArgs()));
1885 DEF_TRAVERSE_STMT(ImplicitCastExpr, {
1886 // We don't traverse the cast type, as it's not written in the
1890 DEF_TRAVERSE_STMT(CStyleCastExpr, {
1891 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1894 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
1895 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1898 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
1899 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1902 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
1903 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1906 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
1907 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1910 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
1911 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1914 // InitListExpr is a tricky one, because we want to do all our work on
1915 // the syntactic form of the listexpr, but this method takes the
1916 // semantic form by default. We can't use the macro helper because it
1917 // calls WalkUp*() on the semantic form, before our code can convert
1918 // to the syntactic form.
1919 template<typename Derived>
1920 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
1921 if (InitListExpr *Syn = S->getSyntacticForm())
1923 TRY_TO(WalkUpFromInitListExpr(S));
1924 StmtQueueAction StmtQueue(*this);
1925 // All we need are the default actions. FIXME: use a helper function.
1926 for (Stmt::child_range range = S->children(); range; ++range) {
1927 StmtQueue.queue(*range);
1932 // GenericSelectionExpr is a special case because the types and expressions
1933 // are interleaved. We also need to watch out for null types (default
1934 // generic associations).
1935 template<typename Derived>
1936 bool RecursiveASTVisitor<Derived>::
1937 TraverseGenericSelectionExpr(GenericSelectionExpr *S) {
1938 TRY_TO(WalkUpFromGenericSelectionExpr(S));
1939 StmtQueueAction StmtQueue(*this);
1940 StmtQueue.queue(S->getControllingExpr());
1941 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
1942 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
1943 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
1944 StmtQueue.queue(S->getAssocExpr(i));
1949 // PseudoObjectExpr is a special case because of the wierdness with
1950 // syntactic expressions and opaque values.
1951 template<typename Derived>
1952 bool RecursiveASTVisitor<Derived>::
1953 TraversePseudoObjectExpr(PseudoObjectExpr *S) {
1954 TRY_TO(WalkUpFromPseudoObjectExpr(S));
1955 StmtQueueAction StmtQueue(*this);
1956 StmtQueue.queue(S->getSyntacticForm());
1957 for (PseudoObjectExpr::semantics_iterator
1958 i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i) {
1960 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
1961 sub = OVE->getSourceExpr();
1962 StmtQueue.queue(sub);
1967 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
1968 // This is called for code like 'return T()' where T is a built-in
1969 // (i.e. non-class) type.
1970 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1973 DEF_TRAVERSE_STMT(CXXNewExpr, {
1974 // The child-iterator will pick up the other arguments.
1975 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
1978 DEF_TRAVERSE_STMT(OffsetOfExpr, {
1979 // The child-iterator will pick up the expression representing
1981 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
1982 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
1983 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1986 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
1987 // The child-iterator will pick up the arg if it's an expression,
1988 // but not if it's a type.
1989 if (S->isArgumentType())
1990 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
1993 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
1994 // The child-iterator will pick up the arg if it's an expression,
1995 // but not if it's a type.
1996 if (S->isTypeOperand())
1997 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2000 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2001 // The child-iterator will pick up the arg if it's an expression,
2002 // but not if it's a type.
2003 if (S->isTypeOperand())
2004 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2007 DEF_TRAVERSE_STMT(UnaryTypeTraitExpr, {
2008 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2011 DEF_TRAVERSE_STMT(BinaryTypeTraitExpr, {
2012 TRY_TO(TraverseTypeLoc(S->getLhsTypeSourceInfo()->getTypeLoc()));
2013 TRY_TO(TraverseTypeLoc(S->getRhsTypeSourceInfo()->getTypeLoc()));
2016 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2017 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2018 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2021 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2022 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2025 DEF_TRAVERSE_STMT(ExpressionTraitExpr, {
2026 StmtQueue.queue(S->getQueriedExpression());
2029 DEF_TRAVERSE_STMT(VAArgExpr, {
2030 // The child-iterator will pick up the expression argument.
2031 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2034 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2035 // This is called for code like 'return T()' where T is a class type.
2036 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2039 // Walk only the visible parts of lambda expressions.
2040 template<typename Derived>
2041 bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2042 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2043 CEnd = S->explicit_capture_end();
2045 TRY_TO(TraverseLambdaCapture(*C));
2048 if (S->hasExplicitParameters() || S->hasExplicitResultType()) {
2049 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2050 if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2051 // Visit the whole type.
2052 TRY_TO(TraverseTypeLoc(TL));
2053 } else if (FunctionProtoTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) {
2054 if (S->hasExplicitParameters()) {
2055 // Visit parameters.
2056 for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) {
2057 TRY_TO(TraverseDecl(Proto.getArg(I)));
2060 TRY_TO(TraverseTypeLoc(Proto.getResultLoc()));
2065 StmtQueueAction StmtQueue(*this);
2066 StmtQueue.queue(S->getBody());
2070 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2071 // This is called for code like 'T()', where T is a template argument.
2072 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2075 // These expressions all might take explicit template arguments.
2076 // We traverse those if so. FIXME: implement these.
2077 DEF_TRAVERSE_STMT(CXXConstructExpr, { })
2078 DEF_TRAVERSE_STMT(CallExpr, { })
2079 DEF_TRAVERSE_STMT(CXXMemberCallExpr, { })
2081 // These exprs (most of them), do not need any action except iterating
2082 // over the children.
2083 DEF_TRAVERSE_STMT(AddrLabelExpr, { })
2084 DEF_TRAVERSE_STMT(ArraySubscriptExpr, { })
2085 DEF_TRAVERSE_STMT(BlockExpr, {
2086 TRY_TO(TraverseDecl(S->getBlockDecl()));
2087 return true; // no child statements to loop through.
2089 DEF_TRAVERSE_STMT(ChooseExpr, { })
2090 DEF_TRAVERSE_STMT(CompoundLiteralExpr, { })
2091 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, { })
2092 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, { })
2093 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, { })
2094 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, { })
2095 DEF_TRAVERSE_STMT(CXXDeleteExpr, { })
2096 DEF_TRAVERSE_STMT(ExprWithCleanups, { })
2097 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, { })
2098 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2099 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2100 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2101 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2102 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2103 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2105 DEF_TRAVERSE_STMT(CXXThisExpr, { })
2106 DEF_TRAVERSE_STMT(CXXThrowExpr, { })
2107 DEF_TRAVERSE_STMT(UserDefinedLiteral, { })
2108 DEF_TRAVERSE_STMT(DesignatedInitExpr, { })
2109 DEF_TRAVERSE_STMT(ExtVectorElementExpr, { })
2110 DEF_TRAVERSE_STMT(GNUNullExpr, { })
2111 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, { })
2112 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, { })
2113 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2114 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2115 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2117 DEF_TRAVERSE_STMT(ObjCIsaExpr, { })
2118 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, { })
2119 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2120 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2121 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2123 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, { })
2124 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, { })
2125 DEF_TRAVERSE_STMT(ObjCProtocolExpr, { })
2126 DEF_TRAVERSE_STMT(ObjCSelectorExpr, { })
2127 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, { })
2128 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2129 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2131 DEF_TRAVERSE_STMT(ParenExpr, { })
2132 DEF_TRAVERSE_STMT(ParenListExpr, { })
2133 DEF_TRAVERSE_STMT(PredefinedExpr, { })
2134 DEF_TRAVERSE_STMT(ShuffleVectorExpr, { })
2135 DEF_TRAVERSE_STMT(StmtExpr, { })
2136 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2137 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2138 if (S->hasExplicitTemplateArgs()) {
2139 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2140 S->getNumTemplateArgs()));
2144 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2145 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2146 if (S->hasExplicitTemplateArgs()) {
2147 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2148 S->getNumTemplateArgs()));
2152 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {})
2153 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2154 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2155 DEF_TRAVERSE_STMT(SEHFinallyStmt,{})
2156 DEF_TRAVERSE_STMT(CapturedStmt, {
2157 TRY_TO(TraverseDecl(S->getCapturedDecl()));
2160 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, { })
2161 DEF_TRAVERSE_STMT(OpaqueValueExpr, { })
2162 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, { })
2164 // These operators (all of them) do not need any action except
2165 // iterating over the children.
2166 DEF_TRAVERSE_STMT(BinaryConditionalOperator, { })
2167 DEF_TRAVERSE_STMT(ConditionalOperator, { })
2168 DEF_TRAVERSE_STMT(UnaryOperator, { })
2169 DEF_TRAVERSE_STMT(BinaryOperator, { })
2170 DEF_TRAVERSE_STMT(CompoundAssignOperator, { })
2171 DEF_TRAVERSE_STMT(CXXNoexceptExpr, { })
2172 DEF_TRAVERSE_STMT(PackExpansionExpr, { })
2173 DEF_TRAVERSE_STMT(SizeOfPackExpr, { })
2174 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, { })
2175 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, { })
2176 DEF_TRAVERSE_STMT(FunctionParmPackExpr, { })
2177 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, { })
2178 DEF_TRAVERSE_STMT(AtomicExpr, { })
2180 // These literals (all of them) do not need any action.
2181 DEF_TRAVERSE_STMT(IntegerLiteral, { })
2182 DEF_TRAVERSE_STMT(CharacterLiteral, { })
2183 DEF_TRAVERSE_STMT(FloatingLiteral, { })
2184 DEF_TRAVERSE_STMT(ImaginaryLiteral, { })
2185 DEF_TRAVERSE_STMT(StringLiteral, { })
2186 DEF_TRAVERSE_STMT(ObjCStringLiteral, { })
2187 DEF_TRAVERSE_STMT(ObjCBoxedExpr, { })
2188 DEF_TRAVERSE_STMT(ObjCArrayLiteral, { })
2189 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, { })
2191 // Traverse OpenCL: AsType, Convert.
2192 DEF_TRAVERSE_STMT(AsTypeExpr, { })
2194 // FIXME: look at the following tricky-seeming exprs to see if we
2195 // need to recurse on anything. These are ones that have methods
2196 // returning decls or qualtypes or nestednamespecifier -- though I'm
2197 // not sure if they own them -- or just seemed very complicated, or
2198 // had lots of sub-types to explore.
2200 // VisitOverloadExpr and its children: recurse on template args? etc?
2202 // FIXME: go through all the stmts and exprs again, and see which of them
2203 // create new types, and recurse on the types (TypeLocs?) of those.
2206 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2207 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2208 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2209 // Every class that has getQualifier.
2211 #undef DEF_TRAVERSE_STMT
2215 } // end namespace cxindex
2216 } // end namespace clang
2218 #endif // LLVM_CLANG_LIBCLANG_RECURSIVEASTVISITOR_H