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 Recursively visit a statement or expression, by
152 /// dispatching to Traverse*() based on the argument's dynamic type.
154 /// \returns false if the visitation was terminated early, true
155 /// otherwise (including when the argument is NULL).
156 bool TraverseStmt(Stmt *S);
158 /// \brief Recursively visit a type, by dispatching to
159 /// Traverse*Type() based on the argument's getTypeClass() property.
161 /// \returns false if the visitation was terminated early, true
162 /// otherwise (including when the argument is a Null type).
163 bool TraverseType(QualType T);
165 /// \brief Recursively visit a type with location, by dispatching to
166 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
168 /// \returns false if the visitation was terminated early, true
169 /// otherwise (including when the argument is a Null type location).
170 bool TraverseTypeLoc(TypeLoc TL);
172 /// \brief Recursively visit a declaration, by dispatching to
173 /// Traverse*Decl() based on the argument's dynamic type.
175 /// \returns false if the visitation was terminated early, true
176 /// otherwise (including when the argument is NULL).
177 bool TraverseDecl(Decl *D);
179 /// \brief Recursively visit a C++ nested-name-specifier.
181 /// \returns false if the visitation was terminated early, true otherwise.
182 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
184 /// \brief Recursively visit a C++ nested-name-specifier with location
187 /// \returns false if the visitation was terminated early, true otherwise.
188 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
190 /// \brief Recursively visit a template name and dispatch to the
191 /// appropriate method.
193 /// \returns false if the visitation was terminated early, true otherwise.
194 bool TraverseTemplateName(TemplateName Template);
196 /// \brief Recursively visit a template argument and dispatch to the
197 /// appropriate method for the argument type.
199 /// \returns false if the visitation was terminated early, true otherwise.
200 // FIXME: migrate callers to TemplateArgumentLoc instead.
201 bool TraverseTemplateArgument(const TemplateArgument &Arg);
203 /// \brief Recursively visit a template argument location and dispatch to the
204 /// appropriate method for the argument type.
206 /// \returns false if the visitation was terminated early, true otherwise.
207 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
209 /// \brief Recursively visit a set of template arguments.
210 /// This can be overridden by a subclass, but it's not expected that
211 /// will be needed -- this visitor always dispatches to another.
213 /// \returns false if the visitation was terminated early, true otherwise.
214 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
215 bool TraverseTemplateArguments(const TemplateArgument *Args,
218 /// \brief Recursively visit a constructor initializer. This
219 /// automatically dispatches to another visitor for the initializer
220 /// expression, but not for the name of the initializer, so may
221 /// be overridden for clients that need access to the name.
223 /// \returns false if the visitation was terminated early, true otherwise.
224 bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
226 // ---- Methods on Stmts ----
228 // Declare Traverse*() for all concrete Stmt classes.
229 #define ABSTRACT_STMT(STMT)
230 #define STMT(CLASS, PARENT) \
231 bool Traverse##CLASS(CLASS *S);
232 #include "clang/AST/StmtNodes.inc"
233 // The above header #undefs ABSTRACT_STMT and STMT upon exit.
235 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
236 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
237 bool VisitStmt(Stmt *S) { return true; }
238 #define STMT(CLASS, PARENT) \
239 bool WalkUpFrom##CLASS(CLASS *S) { \
240 TRY_TO(WalkUpFrom##PARENT(S)); \
241 TRY_TO(Visit##CLASS(S)); \
244 bool Visit##CLASS(CLASS *S) { return true; }
245 #include "clang/AST/StmtNodes.inc"
247 // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary
248 // operator methods. Unary operators are not classes in themselves
249 // (they're all opcodes in UnaryOperator) but do have visitors.
250 #define OPERATOR(NAME) \
251 bool TraverseUnary##NAME(UnaryOperator *S) { \
252 TRY_TO(WalkUpFromUnary##NAME(S)); \
253 TRY_TO(TraverseStmt(S->getSubExpr())); \
256 bool WalkUpFromUnary##NAME(UnaryOperator *S) { \
257 TRY_TO(WalkUpFromUnaryOperator(S)); \
258 TRY_TO(VisitUnary##NAME(S)); \
261 bool VisitUnary##NAME(UnaryOperator *S) { return true; }
266 // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary
267 // operator methods. Binary operators are not classes in themselves
268 // (they're all opcodes in BinaryOperator) but do have visitors.
269 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE) \
270 bool TraverseBin##NAME(BINOP_TYPE *S) { \
271 TRY_TO(WalkUpFromBin##NAME(S)); \
272 TRY_TO(TraverseStmt(S->getLHS())); \
273 TRY_TO(TraverseStmt(S->getRHS())); \
276 bool WalkUpFromBin##NAME(BINOP_TYPE *S) { \
277 TRY_TO(WalkUpFrom##BINOP_TYPE(S)); \
278 TRY_TO(VisitBin##NAME(S)); \
281 bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
283 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
287 // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound
288 // assignment methods. Compound assignment operators are not
289 // classes in themselves (they're all opcodes in
290 // CompoundAssignOperator) but do have visitors.
291 #define OPERATOR(NAME) \
292 GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator)
296 #undef GENERAL_BINOP_FALLBACK
298 // ---- Methods on Types ----
299 // FIXME: revamp to take TypeLoc's rather than Types.
301 // Declare Traverse*() for all concrete Type classes.
302 #define ABSTRACT_TYPE(CLASS, BASE)
303 #define TYPE(CLASS, BASE) \
304 bool Traverse##CLASS##Type(CLASS##Type *T);
305 #include "clang/AST/TypeNodes.def"
306 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
308 // Define WalkUpFrom*() and empty Visit*() for all Type classes.
309 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
310 bool VisitType(Type *T) { return true; }
311 #define TYPE(CLASS, BASE) \
312 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \
313 TRY_TO(WalkUpFrom##BASE(T)); \
314 TRY_TO(Visit##CLASS##Type(T)); \
317 bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
318 #include "clang/AST/TypeNodes.def"
320 // ---- Methods on TypeLocs ----
321 // FIXME: this currently just calls the matching Type methods
323 // Declare Traverse*() for all concrete Type classes.
324 #define ABSTRACT_TYPELOC(CLASS, BASE)
325 #define TYPELOC(CLASS, BASE) \
326 bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
327 #include "clang/AST/TypeLocNodes.def"
328 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
330 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
331 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
332 bool VisitTypeLoc(TypeLoc TL) { return true; }
334 // QualifiedTypeLoc and UnqualTypeLoc are not declared in
335 // TypeNodes.def and thus need to be handled specially.
336 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
337 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
339 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
340 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
341 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
343 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
345 // Note that BASE includes trailing 'Type' which CLASS doesn't.
346 #define TYPE(CLASS, BASE) \
347 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
348 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \
349 TRY_TO(Visit##CLASS##TypeLoc(TL)); \
352 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
353 #include "clang/AST/TypeNodes.def"
355 // ---- Methods on Decls ----
357 // Declare Traverse*() for all concrete Decl classes.
358 #define ABSTRACT_DECL(DECL)
359 #define DECL(CLASS, BASE) \
360 bool Traverse##CLASS##Decl(CLASS##Decl *D);
361 #include "clang/AST/DeclNodes.inc"
362 // The above header #undefs ABSTRACT_DECL and DECL upon exit.
364 // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
365 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
366 bool VisitDecl(Decl *D) { return true; }
367 #define DECL(CLASS, BASE) \
368 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \
369 TRY_TO(WalkUpFrom##BASE(D)); \
370 TRY_TO(Visit##CLASS##Decl(D)); \
373 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
374 #include "clang/AST/DeclNodes.inc"
377 // These are helper methods used by more than one Traverse* method.
378 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
379 bool TraverseClassInstantiations(ClassTemplateDecl* D, Decl *Pattern);
380 bool TraverseFunctionInstantiations(FunctionTemplateDecl* D) ;
381 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
383 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
384 bool TraverseRecordHelper(RecordDecl *D);
385 bool TraverseCXXRecordHelper(CXXRecordDecl *D);
386 bool TraverseDeclaratorHelper(DeclaratorDecl *D);
387 bool TraverseDeclContextHelper(DeclContext *DC);
388 bool TraverseFunctionHelper(FunctionDecl *D);
389 bool TraverseVarHelper(VarDecl *D);
392 #define DISPATCH(NAME, CLASS, VAR) \
393 return getDerived().Traverse##NAME(static_cast<CLASS*>(VAR))
395 template<typename Derived>
396 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S) {
400 // If we have a binary expr, dispatch to the subcode of the binop. A smart
401 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
403 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
404 switch (BinOp->getOpcode()) {
405 #define OPERATOR(NAME) \
406 case BO_##NAME: DISPATCH(Bin##NAME, BinaryOperator, S);
412 #define OPERATOR(NAME) \
413 case BO_##NAME##Assign: \
414 DISPATCH(Bin##NAME##Assign, CompoundAssignOperator, S);
420 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
421 switch (UnOp->getOpcode()) {
422 #define OPERATOR(NAME) \
423 case UO_##NAME: DISPATCH(Unary##NAME, UnaryOperator, S);
431 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
432 switch (S->getStmtClass()) {
433 case Stmt::NoStmtClass: break;
434 #define ABSTRACT_STMT(STMT)
435 #define STMT(CLASS, PARENT) \
436 case Stmt::CLASS##Class: DISPATCH(CLASS, CLASS, S);
437 #include "clang/AST/StmtNodes.inc"
443 template<typename Derived>
444 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
448 switch (T->getTypeClass()) {
449 #define ABSTRACT_TYPE(CLASS, BASE)
450 #define TYPE(CLASS, BASE) \
451 case Type::CLASS: DISPATCH(CLASS##Type, CLASS##Type, \
452 const_cast<Type*>(T.getTypePtr()));
453 #include "clang/AST/TypeNodes.def"
459 template<typename Derived>
460 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
464 switch (TL.getTypeLocClass()) {
465 #define ABSTRACT_TYPELOC(CLASS, BASE)
466 #define TYPELOC(CLASS, BASE) \
467 case TypeLoc::CLASS: \
468 return getDerived().Traverse##CLASS##TypeLoc(*cast<CLASS##TypeLoc>(&TL));
469 #include "clang/AST/TypeLocNodes.def"
476 template<typename Derived>
477 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
481 // As a syntax visitor, we want to ignore declarations for
482 // implicitly-defined declarations (ones not typed explicitly by the
487 switch (D->getKind()) {
488 #define ABSTRACT_DECL(DECL)
489 #define DECL(CLASS, BASE) \
490 case Decl::CLASS: DISPATCH(CLASS##Decl, CLASS##Decl, D);
491 #include "clang/AST/DeclNodes.inc"
499 template<typename Derived>
500 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
501 NestedNameSpecifier *NNS) {
505 if (NNS->getPrefix())
506 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
508 switch (NNS->getKind()) {
509 case NestedNameSpecifier::Identifier:
510 case NestedNameSpecifier::Namespace:
511 case NestedNameSpecifier::NamespaceAlias:
512 case NestedNameSpecifier::Global:
515 case NestedNameSpecifier::TypeSpec:
516 case NestedNameSpecifier::TypeSpecWithTemplate:
517 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
523 template<typename Derived>
524 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
525 NestedNameSpecifierLoc NNS) {
529 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
530 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
532 switch (NNS.getNestedNameSpecifier()->getKind()) {
533 case NestedNameSpecifier::Identifier:
534 case NestedNameSpecifier::Namespace:
535 case NestedNameSpecifier::NamespaceAlias:
536 case NestedNameSpecifier::Global:
539 case NestedNameSpecifier::TypeSpec:
540 case NestedNameSpecifier::TypeSpecWithTemplate:
541 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
548 template<typename Derived>
549 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
550 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
551 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
552 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
553 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
558 template<typename Derived>
559 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
560 const TemplateArgument &Arg) {
561 switch (Arg.getKind()) {
562 case TemplateArgument::Null:
563 case TemplateArgument::Declaration:
564 case TemplateArgument::Integral:
567 case TemplateArgument::Type:
568 return getDerived().TraverseType(Arg.getAsType());
570 case TemplateArgument::Template:
571 case TemplateArgument::TemplateExpansion:
572 return getDerived().TraverseTemplateName(
573 Arg.getAsTemplateOrTemplatePattern());
575 case TemplateArgument::Expression:
576 return getDerived().TraverseStmt(Arg.getAsExpr());
578 case TemplateArgument::Pack:
579 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
586 // FIXME: no template name location?
587 // FIXME: no source locations for a template argument pack?
588 template<typename Derived>
589 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
590 const TemplateArgumentLoc &ArgLoc) {
591 const TemplateArgument &Arg = ArgLoc.getArgument();
593 switch (Arg.getKind()) {
594 case TemplateArgument::Null:
595 case TemplateArgument::Declaration:
596 case TemplateArgument::Integral:
599 case TemplateArgument::Type: {
600 // FIXME: how can TSI ever be NULL?
601 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
602 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
604 return getDerived().TraverseType(Arg.getAsType());
607 case TemplateArgument::Template:
608 case TemplateArgument::TemplateExpansion:
609 if (ArgLoc.getTemplateQualifierLoc())
610 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
611 ArgLoc.getTemplateQualifierLoc()));
612 return getDerived().TraverseTemplateName(
613 Arg.getAsTemplateOrTemplatePattern());
615 case TemplateArgument::Expression:
616 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
618 case TemplateArgument::Pack:
619 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
626 template<typename Derived>
627 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
628 const TemplateArgument *Args,
630 for (unsigned I = 0; I != NumArgs; ++I) {
631 TRY_TO(TraverseTemplateArgument(Args[I]));
637 template<typename Derived>
638 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
639 CXXCtorInitializer *Init) {
640 // FIXME: recurse on TypeLoc of the base initializer if isBaseInitializer()?
641 if (Init->isWritten())
642 TRY_TO(TraverseStmt(Init->getInit()));
647 // ----------------- Type traversal -----------------
649 // This macro makes available a variable T, the passed-in type.
650 #define DEF_TRAVERSE_TYPE(TYPE, CODE) \
651 template<typename Derived> \
652 bool RecursiveASTVisitor<Derived>::Traverse##TYPE (TYPE *T) { \
653 TRY_TO(WalkUpFrom##TYPE (T)); \
658 DEF_TRAVERSE_TYPE(BuiltinType, { })
660 DEF_TRAVERSE_TYPE(ComplexType, {
661 TRY_TO(TraverseType(T->getElementType()));
664 DEF_TRAVERSE_TYPE(PointerType, {
665 TRY_TO(TraverseType(T->getPointeeType()));
668 DEF_TRAVERSE_TYPE(BlockPointerType, {
669 TRY_TO(TraverseType(T->getPointeeType()));
672 DEF_TRAVERSE_TYPE(LValueReferenceType, {
673 TRY_TO(TraverseType(T->getPointeeType()));
676 DEF_TRAVERSE_TYPE(RValueReferenceType, {
677 TRY_TO(TraverseType(T->getPointeeType()));
680 DEF_TRAVERSE_TYPE(MemberPointerType, {
681 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
682 TRY_TO(TraverseType(T->getPointeeType()));
685 DEF_TRAVERSE_TYPE(ConstantArrayType, {
686 TRY_TO(TraverseType(T->getElementType()));
689 DEF_TRAVERSE_TYPE(IncompleteArrayType, {
690 TRY_TO(TraverseType(T->getElementType()));
693 DEF_TRAVERSE_TYPE(VariableArrayType, {
694 TRY_TO(TraverseType(T->getElementType()));
695 TRY_TO(TraverseStmt(T->getSizeExpr()));
698 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
699 TRY_TO(TraverseType(T->getElementType()));
700 if (T->getSizeExpr())
701 TRY_TO(TraverseStmt(T->getSizeExpr()));
704 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
705 if (T->getSizeExpr())
706 TRY_TO(TraverseStmt(T->getSizeExpr()));
707 TRY_TO(TraverseType(T->getElementType()));
710 DEF_TRAVERSE_TYPE(VectorType, {
711 TRY_TO(TraverseType(T->getElementType()));
714 DEF_TRAVERSE_TYPE(ExtVectorType, {
715 TRY_TO(TraverseType(T->getElementType()));
718 DEF_TRAVERSE_TYPE(FunctionNoProtoType, {
719 TRY_TO(TraverseType(T->getResultType()));
722 DEF_TRAVERSE_TYPE(FunctionProtoType, {
723 TRY_TO(TraverseType(T->getResultType()));
725 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
726 AEnd = T->arg_type_end();
728 TRY_TO(TraverseType(*A));
731 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
732 EEnd = T->exception_end();
734 TRY_TO(TraverseType(*E));
738 DEF_TRAVERSE_TYPE(UnresolvedUsingType, { })
739 DEF_TRAVERSE_TYPE(TypedefType, { })
741 DEF_TRAVERSE_TYPE(TypeOfExprType, {
742 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
745 DEF_TRAVERSE_TYPE(TypeOfType, {
746 TRY_TO(TraverseType(T->getUnderlyingType()));
749 DEF_TRAVERSE_TYPE(DecltypeType, {
750 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
753 DEF_TRAVERSE_TYPE(UnaryTransformType, {
754 TRY_TO(TraverseType(T->getBaseType()));
755 TRY_TO(TraverseType(T->getUnderlyingType()));
758 DEF_TRAVERSE_TYPE(AutoType, {
759 TRY_TO(TraverseType(T->getDeducedType()));
762 DEF_TRAVERSE_TYPE(RecordType, { })
763 DEF_TRAVERSE_TYPE(EnumType, { })
764 DEF_TRAVERSE_TYPE(TemplateTypeParmType, { })
765 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, { })
766 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, { })
768 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
769 TRY_TO(TraverseTemplateName(T->getTemplateName()));
770 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
773 DEF_TRAVERSE_TYPE(InjectedClassNameType, { })
775 DEF_TRAVERSE_TYPE(AttributedType, {
776 TRY_TO(TraverseType(T->getModifiedType()));
779 DEF_TRAVERSE_TYPE(ParenType, {
780 TRY_TO(TraverseType(T->getInnerType()));
783 DEF_TRAVERSE_TYPE(ElaboratedType, {
784 if (T->getQualifier()) {
785 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
787 TRY_TO(TraverseType(T->getNamedType()));
790 DEF_TRAVERSE_TYPE(DependentNameType, {
791 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
794 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
795 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
796 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
799 DEF_TRAVERSE_TYPE(PackExpansionType, {
800 TRY_TO(TraverseType(T->getPattern()));
803 DEF_TRAVERSE_TYPE(ObjCInterfaceType, { })
805 DEF_TRAVERSE_TYPE(ObjCObjectType, {
806 // We have to watch out here because an ObjCInterfaceType's base
808 if (T->getBaseType().getTypePtr() != T)
809 TRY_TO(TraverseType(T->getBaseType()));
812 DEF_TRAVERSE_TYPE(ObjCObjectPointerType, {
813 TRY_TO(TraverseType(T->getPointeeType()));
816 DEF_TRAVERSE_TYPE(AtomicType, {
817 TRY_TO(TraverseType(T->getValueType()));
820 #undef DEF_TRAVERSE_TYPE
822 // ----------------- TypeLoc traversal -----------------
824 // This macro makes available a variable TL, the passed-in TypeLoc.
825 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
826 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
827 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
829 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
830 template<typename Derived> \
831 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
832 if (getDerived().shouldWalkTypesOfTypeLocs()) \
833 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE*>(TL.getTypePtr()))); \
834 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
839 template<typename Derived>
840 bool RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(
841 QualifiedTypeLoc TL) {
842 // Move this over to the 'main' typeloc tree. Note that this is a
843 // move -- we pretend that we were really looking at the unqualified
844 // typeloc all along -- rather than a recursion, so we don't follow
845 // the normal CRTP plan of going through
846 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
847 // twice for the same type (once as a QualifiedTypeLoc version of
848 // the type, once as an UnqualifiedTypeLoc version of the type),
849 // which in effect means we'd call VisitTypeLoc twice with the
850 // 'same' type. This solves that problem, at the cost of never
851 // seeing the qualified version of the type (unless the client
852 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
853 // perfect solution. A perfect solution probably requires making
854 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
855 // wrapper around Type* -- rather than being its own class in the
857 return TraverseTypeLoc(TL.getUnqualifiedLoc());
860 DEF_TRAVERSE_TYPELOC(BuiltinType, { })
862 // FIXME: ComplexTypeLoc is unfinished
863 DEF_TRAVERSE_TYPELOC(ComplexType, {
864 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
867 DEF_TRAVERSE_TYPELOC(PointerType, {
868 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
871 DEF_TRAVERSE_TYPELOC(BlockPointerType, {
872 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
875 DEF_TRAVERSE_TYPELOC(LValueReferenceType, {
876 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
879 DEF_TRAVERSE_TYPELOC(RValueReferenceType, {
880 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
883 // FIXME: location of base class?
884 // We traverse this in the type case as well, but how is it not reached through
886 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
887 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
888 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
891 template<typename Derived>
892 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
893 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
894 TRY_TO(TraverseStmt(TL.getSizeExpr()));
898 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
899 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
900 return TraverseArrayTypeLocHelper(TL);
903 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
904 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
905 return TraverseArrayTypeLocHelper(TL);
908 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
909 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
910 return TraverseArrayTypeLocHelper(TL);
913 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
914 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
915 return TraverseArrayTypeLocHelper(TL);
918 // FIXME: order? why not size expr first?
919 // FIXME: base VectorTypeLoc is unfinished
920 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
921 if (TL.getTypePtr()->getSizeExpr())
922 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
923 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
926 // FIXME: VectorTypeLoc is unfinished
927 DEF_TRAVERSE_TYPELOC(VectorType, {
928 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
931 // FIXME: size and attributes
932 // FIXME: base VectorTypeLoc is unfinished
933 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
934 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
937 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, {
938 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
941 // FIXME: location of exception specifications (attributes?)
942 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
943 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
945 const FunctionProtoType *T = TL.getTypePtr();
947 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
949 TRY_TO(TraverseDecl(TL.getArg(I)));
950 } else if (I < T->getNumArgs()) {
951 TRY_TO(TraverseType(T->getArgType(I)));
955 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
956 EEnd = T->exception_end();
958 TRY_TO(TraverseType(*E));
962 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, { })
963 DEF_TRAVERSE_TYPELOC(TypedefType, { })
965 DEF_TRAVERSE_TYPELOC(TypeOfExprType, {
966 TRY_TO(TraverseStmt(TL.getUnderlyingExpr()));
969 DEF_TRAVERSE_TYPELOC(TypeOfType, {
970 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
973 // FIXME: location of underlying expr
974 DEF_TRAVERSE_TYPELOC(DecltypeType, {
975 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
978 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
979 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
982 DEF_TRAVERSE_TYPELOC(AutoType, {
983 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
986 DEF_TRAVERSE_TYPELOC(RecordType, { })
987 DEF_TRAVERSE_TYPELOC(EnumType, { })
988 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, { })
989 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, { })
990 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, { })
992 // FIXME: use the loc for the template name?
993 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
994 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
995 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
996 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1000 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, { })
1002 DEF_TRAVERSE_TYPELOC(ParenType, {
1003 TRY_TO(TraverseTypeLoc(TL.getInnerLoc()));
1006 DEF_TRAVERSE_TYPELOC(AttributedType, {
1007 TRY_TO(TraverseTypeLoc(TL.getModifiedLoc()));
1010 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1011 if (TL.getQualifierLoc()) {
1012 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1014 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1017 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1018 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1021 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1022 if (TL.getQualifierLoc()) {
1023 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1026 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1027 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1031 DEF_TRAVERSE_TYPELOC(PackExpansionType, {
1032 TRY_TO(TraverseTypeLoc(TL.getPatternLoc()));
1035 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, { })
1037 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1038 // We have to watch out here because an ObjCInterfaceType's base
1040 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1041 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1044 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, {
1045 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1048 DEF_TRAVERSE_TYPELOC(AtomicType, {
1049 TRY_TO(TraverseTypeLoc(TL.getValueLoc()));
1052 #undef DEF_TRAVERSE_TYPELOC
1054 // ----------------- Decl traversal -----------------
1056 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1057 // the children that come from the DeclContext associated with it.
1058 // Therefore each Traverse* only needs to worry about children other
1061 template<typename Derived>
1062 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1066 for (DeclContext::decl_iterator Child = DC->decls_begin(),
1067 ChildEnd = DC->decls_end();
1068 Child != ChildEnd; ++Child) {
1069 // BlockDecls are traversed through BlockExprs.
1070 if (!isa<BlockDecl>(*Child))
1071 TRY_TO(TraverseDecl(*Child));
1077 // This macro makes available a variable D, the passed-in decl.
1078 #define DEF_TRAVERSE_DECL(DECL, CODE) \
1079 template<typename Derived> \
1080 bool RecursiveASTVisitor<Derived>::Traverse##DECL (DECL *D) { \
1081 TRY_TO(WalkUpFrom##DECL (D)); \
1083 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1087 DEF_TRAVERSE_DECL(AccessSpecDecl, { })
1089 DEF_TRAVERSE_DECL(BlockDecl, {
1090 TRY_TO(TraverseTypeLoc(D->getSignatureAsWritten()->getTypeLoc()));
1091 TRY_TO(TraverseStmt(D->getBody()));
1092 // This return statement makes sure the traversal of nodes in
1093 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1094 // is skipped - don't remove it.
1098 DEF_TRAVERSE_DECL(FileScopeAsmDecl, {
1099 TRY_TO(TraverseStmt(D->getAsmString()));
1102 DEF_TRAVERSE_DECL(FriendDecl, {
1103 // Friend is either decl or a type.
1104 if (D->getFriendType())
1105 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1107 TRY_TO(TraverseDecl(D->getFriendDecl()));
1110 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1111 if (D->getFriendType())
1112 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1114 TRY_TO(TraverseDecl(D->getFriendDecl()));
1115 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1116 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1117 for (TemplateParameterList::iterator ITPL = TPL->begin(),
1119 ITPL != ETPL; ++ITPL) {
1120 TRY_TO(TraverseDecl(*ITPL));
1125 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1126 TRY_TO(TraverseDecl(D->getSpecialization()));
1129 DEF_TRAVERSE_DECL(LinkageSpecDecl, { })
1131 DEF_TRAVERSE_DECL(ObjCClassDecl, {
1132 // FIXME: implement this
1135 DEF_TRAVERSE_DECL(ObjCForwardProtocolDecl, {
1136 // FIXME: implement this
1139 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {
1140 // FIXME: implement this
1143 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1144 TRY_TO(TraverseStmt(D->getAssertExpr()));
1145 TRY_TO(TraverseStmt(D->getMessage()));
1148 DEF_TRAVERSE_DECL(TranslationUnitDecl, {
1149 // Code in an unnamed namespace shows up automatically in
1150 // decls_begin()/decls_end(). Thus we don't need to recurse on
1151 // D->getAnonymousNamespace().
1154 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1155 // We shouldn't traverse an aliased namespace, since it will be
1156 // defined (and, therefore, traversed) somewhere else.
1158 // This return statement makes sure the traversal of nodes in
1159 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1160 // is skipped - don't remove it.
1164 DEF_TRAVERSE_DECL(LabelDecl, {
1165 // There is no code in a LabelDecl.
1169 DEF_TRAVERSE_DECL(NamespaceDecl, {
1170 // Code in an unnamed namespace shows up automatically in
1171 // decls_begin()/decls_end(). Thus we don't need to recurse on
1172 // D->getAnonymousNamespace().
1175 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {
1179 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {
1183 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {
1187 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {
1191 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {
1195 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {
1199 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1200 if (D->getResultTypeSourceInfo()) {
1201 TRY_TO(TraverseTypeLoc(D->getResultTypeSourceInfo()->getTypeLoc()));
1203 for (ObjCMethodDecl::param_iterator
1204 I = D->param_begin(), E = D->param_end(); I != E; ++I) {
1205 TRY_TO(TraverseDecl(*I));
1207 if (D->isThisDeclarationADefinition()) {
1208 TRY_TO(TraverseStmt(D->getBody()));
1213 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1217 DEF_TRAVERSE_DECL(UsingDecl, {
1218 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1221 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1222 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1225 DEF_TRAVERSE_DECL(UsingShadowDecl, { })
1227 // A helper method for TemplateDecl's children.
1228 template<typename Derived>
1229 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1230 TemplateParameterList *TPL) {
1232 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1234 TRY_TO(TraverseDecl(*I));
1240 // A helper method for traversing the implicit instantiations of a
1242 template<typename Derived>
1243 bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations(
1244 ClassTemplateDecl* D, Decl *Pattern) {
1245 assert(isa<ClassTemplateDecl>(Pattern) ||
1246 isa<ClassTemplatePartialSpecializationDecl>(Pattern));
1248 ClassTemplateDecl::spec_iterator end = D->spec_end();
1249 for (ClassTemplateDecl::spec_iterator it = D->spec_begin(); it != end; ++it) {
1250 ClassTemplateSpecializationDecl* SD = *it;
1252 switch (SD->getSpecializationKind()) {
1253 // Visit the implicit instantiations with the requested pattern.
1254 case TSK_ImplicitInstantiation: {
1255 llvm::PointerUnion<ClassTemplateDecl *,
1256 ClassTemplatePartialSpecializationDecl *> U
1257 = SD->getInstantiatedFrom();
1260 if (U.is<ClassTemplateDecl*>())
1261 ShouldVisit = (U.get<ClassTemplateDecl*>() == Pattern);
1264 = (U.get<ClassTemplatePartialSpecializationDecl*>() == Pattern);
1267 TRY_TO(TraverseDecl(SD));
1271 // We don't need to do anything on an explicit instantiation
1272 // or explicit specialization because there will be an explicit
1273 // node for it elsewhere.
1274 case TSK_ExplicitInstantiationDeclaration:
1275 case TSK_ExplicitInstantiationDefinition:
1276 case TSK_ExplicitSpecialization:
1279 // We don't need to do anything for an uninstantiated
1281 case TSK_Undeclared:
1289 DEF_TRAVERSE_DECL(ClassTemplateDecl, {
1290 CXXRecordDecl* TempDecl = D->getTemplatedDecl();
1291 TRY_TO(TraverseDecl(TempDecl));
1292 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1294 // By default, we do not traverse the instantiations of
1295 // class templates since they do not appear in the user code. The
1296 // following code optionally traverses them.
1297 if (getDerived().shouldVisitTemplateInstantiations()) {
1298 // If this is the definition of the primary template, visit
1299 // instantiations which were formed from this pattern.
1300 if (D->isThisDeclarationADefinition())
1301 TRY_TO(TraverseClassInstantiations(D, D));
1304 // Note that getInstantiatedFromMemberTemplate() is just a link
1305 // from a template instantiation back to the template from which
1306 // it was instantiated, and thus should not be traversed.
1309 // A helper method for traversing the instantiations of a
1310 // function while skipping its specializations.
1311 template<typename Derived>
1312 bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations(
1313 FunctionTemplateDecl* D) {
1314 FunctionTemplateDecl::spec_iterator end = D->spec_end();
1315 for (FunctionTemplateDecl::spec_iterator it = D->spec_begin(); it != end; ++it) {
1316 FunctionDecl* FD = *it;
1317 switch (FD->getTemplateSpecializationKind()) {
1318 case TSK_ImplicitInstantiation:
1319 // We don't know what kind of FunctionDecl this is.
1320 TRY_TO(TraverseDecl(FD));
1323 // No need to visit explicit instantiations, we'll find the node
1325 case TSK_ExplicitInstantiationDeclaration:
1326 case TSK_ExplicitInstantiationDefinition:
1329 case TSK_Undeclared: // Declaration of the template definition.
1330 case TSK_ExplicitSpecialization:
1333 llvm_unreachable("Unknown specialization kind.");
1340 DEF_TRAVERSE_DECL(FunctionTemplateDecl, {
1341 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1342 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1344 // By default, we do not traverse the instantiations of
1345 // function templates since they do not apprear in the user code. The
1346 // following code optionally traverses them.
1347 if (getDerived().shouldVisitTemplateInstantiations()) {
1348 // Explicit function specializations will be traversed from the
1349 // context of their declaration. There is therefore no need to
1350 // traverse them for here.
1352 // In addition, we only traverse the function instantiations when
1353 // the function template is a function template definition.
1354 if (D->isThisDeclarationADefinition()) {
1355 TRY_TO(TraverseFunctionInstantiations(D));
1360 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1361 // D is the "T" in something like
1362 // template <template <typename> class T> class container { };
1363 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1364 if (D->hasDefaultArgument()) {
1365 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1367 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1370 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1371 // D is the "T" in something like "template<typename T> class vector;"
1372 if (D->getTypeForDecl())
1373 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1374 if (D->hasDefaultArgument())
1375 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1378 DEF_TRAVERSE_DECL(TypedefDecl, {
1379 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1380 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1381 // declaring the typedef, not something that was written in the
1385 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1386 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1387 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1388 // declaring the type alias, not something that was written in the
1392 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1393 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1394 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1397 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1398 // A dependent using declaration which was marked with 'typename'.
1399 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1400 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1401 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1402 // declaring the type, not something that was written in the
1406 DEF_TRAVERSE_DECL(EnumDecl, {
1407 if (D->getTypeForDecl())
1408 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1410 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1411 // The enumerators are already traversed by
1412 // decls_begin()/decls_end().
1416 // Helper methods for RecordDecl and its children.
1417 template<typename Derived>
1418 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(
1420 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1421 // declaring the type, not something that was written in the source.
1423 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1427 template<typename Derived>
1428 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(
1430 if (!TraverseRecordHelper(D))
1432 if (D->hasDefinition()) {
1433 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1436 TRY_TO(TraverseTypeLoc(I->getTypeSourceInfo()->getTypeLoc()));
1438 // We don't traverse the friends or the conversions, as they are
1439 // already in decls_begin()/decls_end().
1444 DEF_TRAVERSE_DECL(RecordDecl, {
1445 TRY_TO(TraverseRecordHelper(D));
1448 DEF_TRAVERSE_DECL(CXXRecordDecl, {
1449 TRY_TO(TraverseCXXRecordHelper(D));
1452 DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, {
1453 // For implicit instantiations ("set<int> x;"), we don't want to
1454 // recurse at all, since the instatiated class isn't written in
1455 // the source code anywhere. (Note the instatiated *type* --
1456 // set<int> -- is written, and will still get a callback of
1457 // TemplateSpecializationType). For explicit instantiations
1458 // ("template set<int>;"), we do need a callback, since this
1459 // is the only callback that's made for this instantiation.
1460 // We use getTypeAsWritten() to distinguish.
1461 if (TypeSourceInfo *TSI = D->getTypeAsWritten())
1462 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1464 if (!getDerived().shouldVisitTemplateInstantiations() &&
1465 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1466 // Returning from here skips traversing the
1467 // declaration context of the ClassTemplateSpecializationDecl
1468 // (embedded in the DEF_TRAVERSE_DECL() macro)
1469 // which contains the instantiated members of the class.
1473 template <typename Derived>
1474 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1475 const TemplateArgumentLoc *TAL, unsigned Count) {
1476 for (unsigned I = 0; I < Count; ++I) {
1477 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1482 DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, {
1483 // The partial specialization.
1484 if (TemplateParameterList *TPL = D->getTemplateParameters()) {
1485 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1487 TRY_TO(TraverseDecl(*I));
1490 // The args that remains unspecialized.
1491 TRY_TO(TraverseTemplateArgumentLocsHelper(
1492 D->getTemplateArgsAsWritten(), D->getNumTemplateArgsAsWritten()));
1494 // Don't need the ClassTemplatePartialSpecializationHelper, even
1495 // though that's our parent class -- we already visit all the
1496 // template args here.
1497 TRY_TO(TraverseCXXRecordHelper(D));
1499 // If we're visiting instantiations, visit the instantiations of
1500 // this template now.
1501 if (getDerived().shouldVisitTemplateInstantiations() &&
1502 D->isThisDeclarationADefinition())
1503 TRY_TO(TraverseClassInstantiations(D->getSpecializedTemplate(), D));
1506 DEF_TRAVERSE_DECL(EnumConstantDecl, {
1507 TRY_TO(TraverseStmt(D->getInitExpr()));
1510 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1511 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1512 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1513 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1516 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1518 template<typename Derived>
1519 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1520 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1521 if (D->getTypeSourceInfo())
1522 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1524 TRY_TO(TraverseType(D->getType()));
1528 DEF_TRAVERSE_DECL(FieldDecl, {
1529 TRY_TO(TraverseDeclaratorHelper(D));
1530 if (D->isBitField())
1531 TRY_TO(TraverseStmt(D->getBitWidth()));
1534 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1535 TRY_TO(TraverseDeclaratorHelper(D));
1536 if (D->isBitField())
1537 TRY_TO(TraverseStmt(D->getBitWidth()));
1538 // FIXME: implement the rest.
1541 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1542 TRY_TO(TraverseDeclaratorHelper(D));
1543 if (D->isBitField())
1544 TRY_TO(TraverseStmt(D->getBitWidth()));
1545 // FIXME: implement the rest.
1548 template<typename Derived>
1549 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1550 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1552 // If we're an explicit template specialization, iterate over the
1553 // template args that were explicitly specified. If we were doing
1554 // this in typing order, we'd do it between the return type and
1555 // the function args, but both are handled by the FunctionTypeLoc
1556 // above, so we have to choose one side. I've decided to do before.
1557 if (const FunctionTemplateSpecializationInfo *FTSI =
1558 D->getTemplateSpecializationInfo()) {
1559 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1560 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1561 // A specialization might not have explicit template arguments if it has
1562 // a templated return type and concrete arguments.
1563 if (const ASTTemplateArgumentListInfo *TALI =
1564 FTSI->TemplateArgumentsAsWritten) {
1565 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1566 TALI->NumTemplateArgs));
1571 // Visit the function type itself, which can be either
1572 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1573 // also covers the return type and the function parameters,
1574 // including exception specifications.
1575 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1577 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1578 // Constructor initializers.
1579 for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
1580 E = Ctor->init_end();
1582 TRY_TO(TraverseConstructorInitializer(*I));
1586 if (D->isThisDeclarationADefinition()) {
1587 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1592 DEF_TRAVERSE_DECL(FunctionDecl, {
1593 // We skip decls_begin/decls_end, which are already covered by
1594 // TraverseFunctionHelper().
1595 return TraverseFunctionHelper(D);
1598 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1599 // We skip decls_begin/decls_end, which are already covered by
1600 // TraverseFunctionHelper().
1601 return TraverseFunctionHelper(D);
1604 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1605 // We skip decls_begin/decls_end, which are already covered by
1606 // TraverseFunctionHelper().
1607 return TraverseFunctionHelper(D);
1610 // CXXConversionDecl is the declaration of a type conversion operator.
1611 // It's not a cast expression.
1612 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1613 // We skip decls_begin/decls_end, which are already covered by
1614 // TraverseFunctionHelper().
1615 return TraverseFunctionHelper(D);
1618 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1619 // We skip decls_begin/decls_end, which are already covered by
1620 // TraverseFunctionHelper().
1621 return TraverseFunctionHelper(D);
1624 template<typename Derived>
1625 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1626 TRY_TO(TraverseDeclaratorHelper(D));
1627 TRY_TO(TraverseStmt(D->getInit()));
1631 DEF_TRAVERSE_DECL(VarDecl, {
1632 TRY_TO(TraverseVarHelper(D));
1635 DEF_TRAVERSE_DECL(ImplicitParamDecl, {
1636 TRY_TO(TraverseVarHelper(D));
1639 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1640 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1641 TRY_TO(TraverseDeclaratorHelper(D));
1642 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1645 DEF_TRAVERSE_DECL(ParmVarDecl, {
1646 TRY_TO(TraverseVarHelper(D));
1648 if (D->hasDefaultArg() &&
1649 D->hasUninstantiatedDefaultArg() &&
1650 !D->hasUnparsedDefaultArg())
1651 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1653 if (D->hasDefaultArg() &&
1654 !D->hasUninstantiatedDefaultArg() &&
1655 !D->hasUnparsedDefaultArg())
1656 TRY_TO(TraverseStmt(D->getDefaultArg()));
1659 #undef DEF_TRAVERSE_DECL
1661 // ----------------- Stmt traversal -----------------
1663 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1664 // over the children defined in children() (every stmt defines these,
1665 // though sometimes the range is empty). Each individual Traverse*
1666 // method only needs to worry about children other than those. To see
1667 // what children() does for a given class, see, e.g.,
1668 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1670 // This macro makes available a variable S, the passed-in stmt.
1671 #define DEF_TRAVERSE_STMT(STMT, CODE) \
1672 template<typename Derived> \
1673 bool RecursiveASTVisitor<Derived>::Traverse##STMT (STMT *S) { \
1674 TRY_TO(WalkUpFrom##STMT(S)); \
1676 for (Stmt::child_range range = S->children(); range; ++range) { \
1677 TRY_TO(TraverseStmt(*range)); \
1682 DEF_TRAVERSE_STMT(AsmStmt, {
1683 TRY_TO(TraverseStmt(S->getAsmString()));
1684 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1685 TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1687 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1688 TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1690 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1691 TRY_TO(TraverseStmt(S->getClobber(I)));
1693 // children() iterates over inputExpr and outputExpr.
1696 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1697 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1698 // children() iterates over the handler block.
1701 DEF_TRAVERSE_STMT(DeclStmt, {
1702 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
1704 TRY_TO(TraverseDecl(*I));
1706 // Suppress the default iteration over children() by
1707 // returning. Here's why: A DeclStmt looks like 'type var [=
1708 // initializer]'. The decls above already traverse over the
1709 // initializers, so we don't have to do it again (which
1710 // children() would do).
1715 // These non-expr stmts (most of them), do not need any action except
1716 // iterating over the children.
1717 DEF_TRAVERSE_STMT(BreakStmt, { })
1718 DEF_TRAVERSE_STMT(CXXTryStmt, { })
1719 DEF_TRAVERSE_STMT(CaseStmt, { })
1720 DEF_TRAVERSE_STMT(CompoundStmt, { })
1721 DEF_TRAVERSE_STMT(ContinueStmt, { })
1722 DEF_TRAVERSE_STMT(DefaultStmt, { })
1723 DEF_TRAVERSE_STMT(DoStmt, { })
1724 DEF_TRAVERSE_STMT(ForStmt, { })
1725 DEF_TRAVERSE_STMT(GotoStmt, { })
1726 DEF_TRAVERSE_STMT(IfStmt, { })
1727 DEF_TRAVERSE_STMT(IndirectGotoStmt, { })
1728 DEF_TRAVERSE_STMT(LabelStmt, { })
1729 DEF_TRAVERSE_STMT(NullStmt, { })
1730 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, { })
1731 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, { })
1732 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, { })
1733 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, { })
1734 DEF_TRAVERSE_STMT(ObjCAtTryStmt, { })
1735 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, { })
1736 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, { })
1737 DEF_TRAVERSE_STMT(CXXForRangeStmt, { })
1738 DEF_TRAVERSE_STMT(ReturnStmt, { })
1739 DEF_TRAVERSE_STMT(SwitchStmt, { })
1740 DEF_TRAVERSE_STMT(WhileStmt, { })
1743 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1744 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1745 if (S->hasExplicitTemplateArgs()) {
1746 TRY_TO(TraverseTemplateArgumentLocsHelper(
1747 S->getTemplateArgs(), S->getNumTemplateArgs()));
1751 DEF_TRAVERSE_STMT(DeclRefExpr, {
1752 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1753 TRY_TO(TraverseTemplateArgumentLocsHelper(
1754 S->getTemplateArgs(), S->getNumTemplateArgs()));
1757 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1758 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1759 if (S->hasExplicitTemplateArgs()) {
1760 TRY_TO(TraverseTemplateArgumentLocsHelper(
1761 S->getExplicitTemplateArgs().getTemplateArgs(),
1762 S->getNumTemplateArgs()));
1766 DEF_TRAVERSE_STMT(MemberExpr, {
1767 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1768 TRY_TO(TraverseTemplateArgumentLocsHelper(
1769 S->getTemplateArgs(), S->getNumTemplateArgs()));
1772 DEF_TRAVERSE_STMT(ImplicitCastExpr, {
1773 // We don't traverse the cast type, as it's not written in the
1777 DEF_TRAVERSE_STMT(CStyleCastExpr, {
1778 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1781 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
1782 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1785 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
1786 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1789 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
1790 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1793 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
1794 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1797 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
1798 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1801 // InitListExpr is a tricky one, because we want to do all our work on
1802 // the syntactic form of the listexpr, but this method takes the
1803 // semantic form by default. We can't use the macro helper because it
1804 // calls WalkUp*() on the semantic form, before our code can convert
1805 // to the syntactic form.
1806 template<typename Derived>
1807 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
1808 if (InitListExpr *Syn = S->getSyntacticForm())
1810 TRY_TO(WalkUpFromInitListExpr(S));
1811 // All we need are the default actions. FIXME: use a helper function.
1812 for (Stmt::child_range range = S->children(); range; ++range) {
1813 TRY_TO(TraverseStmt(*range));
1818 // GenericSelectionExpr is a special case because the types and expressions
1819 // are interleaved. We also need to watch out for null types (default
1820 // generic associations).
1821 template<typename Derived>
1822 bool RecursiveASTVisitor<Derived>::
1823 TraverseGenericSelectionExpr(GenericSelectionExpr *S) {
1824 TRY_TO(WalkUpFromGenericSelectionExpr(S));
1825 TRY_TO(TraverseStmt(S->getControllingExpr()));
1826 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
1827 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
1828 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
1829 TRY_TO(TraverseStmt(S->getAssocExpr(i)));
1834 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
1835 // This is called for code like 'return T()' where T is a built-in
1836 // (i.e. non-class) type.
1837 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1840 DEF_TRAVERSE_STMT(CXXNewExpr, {
1841 // The child-iterator will pick up the other arguments.
1842 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
1845 DEF_TRAVERSE_STMT(OffsetOfExpr, {
1846 // The child-iterator will pick up the expression representing
1848 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
1849 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
1850 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1853 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
1854 // The child-iterator will pick up the arg if it's an expression,
1855 // but not if it's a type.
1856 if (S->isArgumentType())
1857 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
1860 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
1861 // The child-iterator will pick up the arg if it's an expression,
1862 // but not if it's a type.
1863 if (S->isTypeOperand())
1864 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
1867 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
1868 // The child-iterator will pick up the arg if it's an expression,
1869 // but not if it's a type.
1870 if (S->isTypeOperand())
1871 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
1874 DEF_TRAVERSE_STMT(UnaryTypeTraitExpr, {
1875 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
1878 DEF_TRAVERSE_STMT(BinaryTypeTraitExpr, {
1879 TRY_TO(TraverseTypeLoc(S->getLhsTypeSourceInfo()->getTypeLoc()));
1880 TRY_TO(TraverseTypeLoc(S->getRhsTypeSourceInfo()->getTypeLoc()));
1883 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
1884 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
1887 DEF_TRAVERSE_STMT(ExpressionTraitExpr, {
1888 TRY_TO(TraverseStmt(S->getQueriedExpression()));
1891 DEF_TRAVERSE_STMT(VAArgExpr, {
1892 // The child-iterator will pick up the expression argument.
1893 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
1896 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
1897 // This is called for code like 'return T()' where T is a class type.
1898 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1901 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
1902 // This is called for code like 'T()', where T is a template argument.
1903 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1906 // These expressions all might take explicit template arguments.
1907 // We traverse those if so. FIXME: implement these.
1908 DEF_TRAVERSE_STMT(CXXConstructExpr, { })
1909 DEF_TRAVERSE_STMT(CallExpr, { })
1910 DEF_TRAVERSE_STMT(CXXMemberCallExpr, { })
1912 // These exprs (most of them), do not need any action except iterating
1913 // over the children.
1914 DEF_TRAVERSE_STMT(AddrLabelExpr, { })
1915 DEF_TRAVERSE_STMT(ArraySubscriptExpr, { })
1916 DEF_TRAVERSE_STMT(BlockDeclRefExpr, { })
1917 DEF_TRAVERSE_STMT(BlockExpr, {
1918 TRY_TO(TraverseDecl(S->getBlockDecl()));
1919 return true; // no child statements to loop through.
1921 DEF_TRAVERSE_STMT(ChooseExpr, { })
1922 DEF_TRAVERSE_STMT(CompoundLiteralExpr, { })
1923 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, { })
1924 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, { })
1925 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, { })
1926 DEF_TRAVERSE_STMT(CXXDeleteExpr, { })
1927 DEF_TRAVERSE_STMT(ExprWithCleanups, { })
1928 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, { })
1929 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
1930 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1931 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
1932 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
1933 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
1934 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
1936 DEF_TRAVERSE_STMT(CXXThisExpr, { })
1937 DEF_TRAVERSE_STMT(CXXThrowExpr, { })
1938 DEF_TRAVERSE_STMT(DesignatedInitExpr, { })
1939 DEF_TRAVERSE_STMT(ExtVectorElementExpr, { })
1940 DEF_TRAVERSE_STMT(GNUNullExpr, { })
1941 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, { })
1942 DEF_TRAVERSE_STMT(ObjCEncodeExpr, { })
1943 DEF_TRAVERSE_STMT(ObjCIsaExpr, { })
1944 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, { })
1945 DEF_TRAVERSE_STMT(ObjCMessageExpr, { })
1946 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, { })
1947 DEF_TRAVERSE_STMT(ObjCProtocolExpr, { })
1948 DEF_TRAVERSE_STMT(ObjCSelectorExpr, { })
1949 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, { })
1950 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
1951 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1953 DEF_TRAVERSE_STMT(ParenExpr, { })
1954 DEF_TRAVERSE_STMT(ParenListExpr, { })
1955 DEF_TRAVERSE_STMT(PredefinedExpr, { })
1956 DEF_TRAVERSE_STMT(ShuffleVectorExpr, { })
1957 DEF_TRAVERSE_STMT(StmtExpr, { })
1958 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
1959 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1960 if (S->hasExplicitTemplateArgs()) {
1961 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1962 S->getNumTemplateArgs()));
1966 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
1967 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1968 if (S->hasExplicitTemplateArgs()) {
1969 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
1970 S->getNumTemplateArgs()));
1974 DEF_TRAVERSE_STMT(SEHTryStmt, {})
1975 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
1976 DEF_TRAVERSE_STMT(SEHFinallyStmt,{})
1978 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, { })
1979 DEF_TRAVERSE_STMT(OpaqueValueExpr, { })
1980 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, { })
1982 // These operators (all of them) do not need any action except
1983 // iterating over the children.
1984 DEF_TRAVERSE_STMT(BinaryConditionalOperator, { })
1985 DEF_TRAVERSE_STMT(ConditionalOperator, { })
1986 DEF_TRAVERSE_STMT(UnaryOperator, { })
1987 DEF_TRAVERSE_STMT(BinaryOperator, { })
1988 DEF_TRAVERSE_STMT(CompoundAssignOperator, { })
1989 DEF_TRAVERSE_STMT(CXXNoexceptExpr, { })
1990 DEF_TRAVERSE_STMT(PackExpansionExpr, { })
1991 DEF_TRAVERSE_STMT(SizeOfPackExpr, { })
1992 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, { })
1993 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, { })
1994 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, { })
1995 DEF_TRAVERSE_STMT(AtomicExpr, { })
1997 // These literals (all of them) do not need any action.
1998 DEF_TRAVERSE_STMT(IntegerLiteral, { })
1999 DEF_TRAVERSE_STMT(CharacterLiteral, { })
2000 DEF_TRAVERSE_STMT(FloatingLiteral, { })
2001 DEF_TRAVERSE_STMT(ImaginaryLiteral, { })
2002 DEF_TRAVERSE_STMT(StringLiteral, { })
2003 DEF_TRAVERSE_STMT(ObjCStringLiteral, { })
2005 // Traverse OpenCL: AsType, Convert.
2006 DEF_TRAVERSE_STMT(AsTypeExpr, { })
2008 // FIXME: look at the following tricky-seeming exprs to see if we
2009 // need to recurse on anything. These are ones that have methods
2010 // returning decls or qualtypes or nestednamespecifier -- though I'm
2011 // not sure if they own them -- or just seemed very complicated, or
2012 // had lots of sub-types to explore.
2014 // VisitOverloadExpr and its children: recurse on template args? etc?
2016 // FIXME: go through all the stmts and exprs again, and see which of them
2017 // create new types, and recurse on the types (TypeLocs?) of those.
2020 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2021 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2022 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2023 // Every class that has getQualifier.
2025 #undef DEF_TRAVERSE_STMT
2029 } // end namespace clang
2031 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H