]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/clang/include/clang/AST/RecursiveASTVisitor.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / tools / clang / include / clang / AST / RecursiveASTVisitor.h
1 //===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the RecursiveASTVisitor interface, which recursively
11 //  traverses the entire AST.
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
15 #define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
16
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"
34
35 // The following three macros are used for meta programming.  The code
36 // using them is responsible for defining macro OPERATOR().
37
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)            \
46   OPERATOR(Extension)
47
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)        \
53   OPERATOR(Shr)                                       \
54                                                       \
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)                       \
59                                                       \
60   OPERATOR(Assign)                                    \
61   OPERATOR(Comma)
62
63 // All compound assign operators.
64 #define CAO_LIST()                                                      \
65   OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) \
66   OPERATOR(Shl) OPERATOR(Shr) OPERATOR(And) OPERATOR(Or)  OPERATOR(Xor)
67
68 namespace clang {
69
70 // A helper macro to implement short-circuiting when recursing.  It
71 // invokes CALL_EXPR, which must be a method call, on the derived
72 // object (s.t. a user of RecursiveASTVisitor can override the method
73 // in CALL_EXPR).
74 #define TRY_TO(CALL_EXPR) \
75   do { if (!getDerived().CALL_EXPR) return false; } while (0)
76
77 /// \brief A class that does preorder depth-first traversal on the
78 /// entire Clang AST and visits each node.
79 ///
80 /// This class performs three distinct tasks:
81 ///   1. traverse the AST (i.e. go to each node);
82 ///   2. at a given node, walk up the class hierarchy, starting from
83 ///      the node's dynamic type, until the top-most class (e.g. Stmt,
84 ///      Decl, or Type) is reached.
85 ///   3. given a (node, class) combination, where 'class' is some base
86 ///      class of the dynamic type of 'node', call a user-overridable
87 ///      function to actually visit the node.
88 ///
89 /// These tasks are done by three groups of methods, respectively:
90 ///   1. TraverseDecl(Decl *x) does task #1.  It is the entry point
91 ///      for traversing an AST rooted at x.  This method simply
92 ///      dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo
93 ///      is the dynamic type of *x, which calls WalkUpFromFoo(x) and
94 ///      then recursively visits the child nodes of x.
95 ///      TraverseStmt(Stmt *x) and TraverseType(QualType x) work
96 ///      similarly.
97 ///   2. WalkUpFromFoo(Foo *x) does task #2.  It does not try to visit
98 ///      any child node of x.  Instead, it first calls WalkUpFromBar(x)
99 ///      where Bar is the direct parent class of Foo (unless Foo has
100 ///      no parent), and then calls VisitFoo(x) (see the next list item).
101 ///   3. VisitFoo(Foo *x) does task #3.
102 ///
103 /// These three method groups are tiered (Traverse* > WalkUpFrom* >
104 /// Visit*).  A method (e.g. Traverse*) may call methods from the same
105 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*).
106 /// It may not call methods from a higher tier.
107 ///
108 /// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar
109 /// is Foo's super class) before calling VisitFoo(), the result is
110 /// that the Visit*() methods for a given node are called in the
111 /// top-down order (e.g. for a node of type NamedDecl, the order will
112 /// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()).
113 ///
114 /// This scheme guarantees that all Visit*() calls for the same AST
115 /// node are grouped together.  In other words, Visit*() methods for
116 /// different nodes are never interleaved.
117 ///
118 /// Clients of this visitor should subclass the visitor (providing
119 /// themselves as the template argument, using the curiously recurring
120 /// template pattern) and override any of the Traverse*, WalkUpFrom*,
121 /// and Visit* methods for declarations, types, statements,
122 /// expressions, or other AST nodes where the visitor should customize
123 /// behavior.  Most users only need to override Visit*.  Advanced
124 /// users may override Traverse* and WalkUpFrom* to implement custom
125 /// traversal strategies.  Returning false from one of these overridden
126 /// functions will abort the entire traversal.
127 ///
128 /// By default, this visitor tries to visit every part of the explicit
129 /// source code exactly once.  The default policy towards templates
130 /// is to descend into the 'pattern' class or function body, not any
131 /// explicit or implicit instantiations.  Explicit specializations
132 /// are still visited, and the patterns of partial specializations
133 /// are visited separately.  This behavior can be changed by
134 /// overriding shouldVisitTemplateInstantiations() in the derived class
135 /// to return true, in which case all known implicit and explicit
136 /// instantiations will be visited at the same time as the pattern
137 /// from which they were produced.
138 template<typename Derived>
139 class RecursiveASTVisitor {
140 public:
141   /// \brief Return a reference to the derived class.
142   Derived &getDerived() { return *static_cast<Derived*>(this); }
143
144   /// \brief Return whether this visitor should recurse into
145   /// template instantiations.
146   bool shouldVisitTemplateInstantiations() const { return false; }
147
148   /// \brief Return whether this visitor should recurse into the types of
149   /// TypeLocs.
150   bool shouldWalkTypesOfTypeLocs() const { return true; }
151
152   /// \brief Return whether this visitor should recurse into implicit
153   /// code, e.g., implicit constructors and destructors.
154   bool shouldVisitImplicitCode() const { return false; }
155
156   /// \brief Return whether \param S should be traversed using data recursion
157   /// to avoid a stack overflow with extreme cases.
158   bool shouldUseDataRecursionFor(Stmt *S) const {
159     return isa<BinaryOperator>(S) || isa<UnaryOperator>(S) ||
160            isa<CaseStmt>(S) || isa<CXXOperatorCallExpr>(S);
161   }
162
163   /// \brief Recursively visit a statement or expression, by
164   /// dispatching to Traverse*() based on the argument's dynamic type.
165   ///
166   /// \returns false if the visitation was terminated early, true
167   /// otherwise (including when the argument is NULL).
168   bool TraverseStmt(Stmt *S);
169
170   /// \brief Recursively visit a type, by dispatching to
171   /// Traverse*Type() based on the argument's getTypeClass() property.
172   ///
173   /// \returns false if the visitation was terminated early, true
174   /// otherwise (including when the argument is a Null type).
175   bool TraverseType(QualType T);
176
177   /// \brief Recursively visit a type with location, by dispatching to
178   /// Traverse*TypeLoc() based on the argument type's getTypeClass() property.
179   ///
180   /// \returns false if the visitation was terminated early, true
181   /// otherwise (including when the argument is a Null type location).
182   bool TraverseTypeLoc(TypeLoc TL);
183
184   /// \brief Recursively visit a declaration, by dispatching to
185   /// Traverse*Decl() based on the argument's dynamic type.
186   ///
187   /// \returns false if the visitation was terminated early, true
188   /// otherwise (including when the argument is NULL).
189   bool TraverseDecl(Decl *D);
190
191   /// \brief Recursively visit a C++ nested-name-specifier.
192   ///
193   /// \returns false if the visitation was terminated early, true otherwise.
194   bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS);
195
196   /// \brief Recursively visit a C++ nested-name-specifier with location
197   /// information.
198   ///
199   /// \returns false if the visitation was terminated early, true otherwise.
200   bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
201
202   /// \brief Recursively visit a name with its location information.
203   ///
204   /// \returns false if the visitation was terminated early, true otherwise.
205   bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo);
206
207   /// \brief Recursively visit a template name and dispatch to the
208   /// appropriate method.
209   ///
210   /// \returns false if the visitation was terminated early, true otherwise.
211   bool TraverseTemplateName(TemplateName Template);
212
213   /// \brief Recursively visit a template argument and dispatch to the
214   /// appropriate method for the argument type.
215   ///
216   /// \returns false if the visitation was terminated early, true otherwise.
217   // FIXME: migrate callers to TemplateArgumentLoc instead.
218   bool TraverseTemplateArgument(const TemplateArgument &Arg);
219
220   /// \brief Recursively visit a template argument location and dispatch to the
221   /// appropriate method for the argument type.
222   ///
223   /// \returns false if the visitation was terminated early, true otherwise.
224   bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc);
225
226   /// \brief Recursively visit a set of template arguments.
227   /// This can be overridden by a subclass, but it's not expected that
228   /// will be needed -- this visitor always dispatches to another.
229   ///
230   /// \returns false if the visitation was terminated early, true otherwise.
231   // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead.
232   bool TraverseTemplateArguments(const TemplateArgument *Args,
233                                  unsigned NumArgs);
234
235   /// \brief Recursively visit a constructor initializer.  This
236   /// automatically dispatches to another visitor for the initializer
237   /// expression, but not for the name of the initializer, so may
238   /// be overridden for clients that need access to the name.
239   ///
240   /// \returns false if the visitation was terminated early, true otherwise.
241   bool TraverseConstructorInitializer(CXXCtorInitializer *Init);
242
243   /// \brief Recursively visit a lambda capture.
244   ///
245   /// \returns false if the visitation was terminated early, true otherwise.
246   bool TraverseLambdaCapture(LambdaExpr::Capture C);
247   
248   // ---- Methods on Stmts ----
249
250   // Declare Traverse*() for all concrete Stmt classes.
251 #define ABSTRACT_STMT(STMT)
252 #define STMT(CLASS, PARENT)                                     \
253   bool Traverse##CLASS(CLASS *S);
254 #include "clang/AST/StmtNodes.inc"
255   // The above header #undefs ABSTRACT_STMT and STMT upon exit.
256
257   // Define WalkUpFrom*() and empty Visit*() for all Stmt classes.
258   bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); }
259   bool VisitStmt(Stmt *S) { return true; }
260 #define STMT(CLASS, PARENT)                                     \
261   bool WalkUpFrom##CLASS(CLASS *S) {                            \
262     TRY_TO(WalkUpFrom##PARENT(S));                              \
263     TRY_TO(Visit##CLASS(S));                                    \
264     return true;                                                \
265   }                                                             \
266   bool Visit##CLASS(CLASS *S) { return true; }
267 #include "clang/AST/StmtNodes.inc"
268
269   // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary
270   // operator methods.  Unary operators are not classes in themselves
271   // (they're all opcodes in UnaryOperator) but do have visitors.
272 #define OPERATOR(NAME)                                           \
273   bool TraverseUnary##NAME(UnaryOperator *S) {                  \
274     TRY_TO(WalkUpFromUnary##NAME(S));                           \
275     TRY_TO(TraverseStmt(S->getSubExpr()));                      \
276     return true;                                                \
277   }                                                             \
278   bool WalkUpFromUnary##NAME(UnaryOperator *S) {                \
279     TRY_TO(WalkUpFromUnaryOperator(S));                         \
280     TRY_TO(VisitUnary##NAME(S));                                \
281     return true;                                                \
282   }                                                             \
283   bool VisitUnary##NAME(UnaryOperator *S) { return true; }
284
285   UNARYOP_LIST()
286 #undef OPERATOR
287
288   // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary
289   // operator methods.  Binary operators are not classes in themselves
290   // (they're all opcodes in BinaryOperator) but do have visitors.
291 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE)                \
292   bool TraverseBin##NAME(BINOP_TYPE *S) {                       \
293     TRY_TO(WalkUpFromBin##NAME(S));                             \
294     TRY_TO(TraverseStmt(S->getLHS()));                          \
295     TRY_TO(TraverseStmt(S->getRHS()));                          \
296     return true;                                                \
297   }                                                             \
298   bool WalkUpFromBin##NAME(BINOP_TYPE *S) {                     \
299     TRY_TO(WalkUpFrom##BINOP_TYPE(S));                          \
300     TRY_TO(VisitBin##NAME(S));                                  \
301     return true;                                                \
302   }                                                             \
303   bool VisitBin##NAME(BINOP_TYPE *S) { return true; }
304
305 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator)
306   BINOP_LIST()
307 #undef OPERATOR
308
309   // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound
310   // assignment methods.  Compound assignment operators are not
311   // classes in themselves (they're all opcodes in
312   // CompoundAssignOperator) but do have visitors.
313 #define OPERATOR(NAME) \
314   GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator)
315
316   CAO_LIST()
317 #undef OPERATOR
318 #undef GENERAL_BINOP_FALLBACK
319
320   // ---- Methods on Types ----
321   // FIXME: revamp to take TypeLoc's rather than Types.
322
323   // Declare Traverse*() for all concrete Type classes.
324 #define ABSTRACT_TYPE(CLASS, BASE)
325 #define TYPE(CLASS, BASE) \
326   bool Traverse##CLASS##Type(CLASS##Type *T);
327 #include "clang/AST/TypeNodes.def"
328   // The above header #undefs ABSTRACT_TYPE and TYPE upon exit.
329
330   // Define WalkUpFrom*() and empty Visit*() for all Type classes.
331   bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); }
332   bool VisitType(Type *T) { return true; }
333 #define TYPE(CLASS, BASE)                                       \
334   bool WalkUpFrom##CLASS##Type(CLASS##Type *T) {                \
335     TRY_TO(WalkUpFrom##BASE(T));                                \
336     TRY_TO(Visit##CLASS##Type(T));                              \
337     return true;                                                \
338   }                                                             \
339   bool Visit##CLASS##Type(CLASS##Type *T) { return true; }
340 #include "clang/AST/TypeNodes.def"
341
342   // ---- Methods on TypeLocs ----
343   // FIXME: this currently just calls the matching Type methods
344
345   // Declare Traverse*() for all concrete Type classes.
346 #define ABSTRACT_TYPELOC(CLASS, BASE)
347 #define TYPELOC(CLASS, BASE) \
348   bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL);
349 #include "clang/AST/TypeLocNodes.def"
350   // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit.
351
352   // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes.
353   bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); }
354   bool VisitTypeLoc(TypeLoc TL) { return true; }
355
356   // QualifiedTypeLoc and UnqualTypeLoc are not declared in
357   // TypeNodes.def and thus need to be handled specially.
358   bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) {
359     return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
360   }
361   bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; }
362   bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) {
363     return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc());
364   }
365   bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; }
366
367   // Note that BASE includes trailing 'Type' which CLASS doesn't.
368 #define TYPE(CLASS, BASE)                                       \
369   bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) {          \
370     TRY_TO(WalkUpFrom##BASE##Loc(TL));                          \
371     TRY_TO(Visit##CLASS##TypeLoc(TL));                          \
372     return true;                                                \
373   }                                                             \
374   bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; }
375 #include "clang/AST/TypeNodes.def"
376
377   // ---- Methods on Decls ----
378
379   // Declare Traverse*() for all concrete Decl classes.
380 #define ABSTRACT_DECL(DECL)
381 #define DECL(CLASS, BASE) \
382   bool Traverse##CLASS##Decl(CLASS##Decl *D);
383 #include "clang/AST/DeclNodes.inc"
384   // The above header #undefs ABSTRACT_DECL and DECL upon exit.
385
386   // Define WalkUpFrom*() and empty Visit*() for all Decl classes.
387   bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); }
388   bool VisitDecl(Decl *D) { return true; }
389 #define DECL(CLASS, BASE)                                       \
390   bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) {                \
391     TRY_TO(WalkUpFrom##BASE(D));                                \
392     TRY_TO(Visit##CLASS##Decl(D));                              \
393     return true;                                                \
394   }                                                             \
395   bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; }
396 #include "clang/AST/DeclNodes.inc"
397
398 private:
399   // These are helper methods used by more than one Traverse* method.
400   bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL);
401   bool TraverseClassInstantiations(ClassTemplateDecl *D);
402   bool TraverseFunctionInstantiations(FunctionTemplateDecl *D) ;
403   bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL,
404                                           unsigned Count);
405   bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL);
406   bool TraverseRecordHelper(RecordDecl *D);
407   bool TraverseCXXRecordHelper(CXXRecordDecl *D);
408   bool TraverseDeclaratorHelper(DeclaratorDecl *D);
409   bool TraverseDeclContextHelper(DeclContext *DC);
410   bool TraverseFunctionHelper(FunctionDecl *D);
411   bool TraverseVarHelper(VarDecl *D);
412
413   struct EnqueueJob {
414     Stmt *S;
415     Stmt::child_iterator StmtIt;
416
417     EnqueueJob(Stmt *S) : S(S), StmtIt() {}
418   };
419   bool dataTraverse(Stmt *S);
420   bool dataTraverseNode(Stmt *S, bool &EnqueueChildren);
421 };
422
423 template<typename Derived>
424 bool RecursiveASTVisitor<Derived>::dataTraverse(Stmt *S) {
425
426   SmallVector<EnqueueJob, 16> Queue;
427   Queue.push_back(S);
428
429   while (!Queue.empty()) {
430     EnqueueJob &job = Queue.back();
431     Stmt *CurrS = job.S;
432     if (!CurrS) {
433       Queue.pop_back();
434       continue;
435     }
436
437     if (getDerived().shouldUseDataRecursionFor(CurrS)) {
438       if (job.StmtIt == Stmt::child_iterator()) {
439         bool EnqueueChildren = true;
440         if (!dataTraverseNode(CurrS, EnqueueChildren)) return false;
441         if (!EnqueueChildren) {
442           Queue.pop_back();
443           continue;
444         }
445         job.StmtIt = CurrS->child_begin();
446       } else {
447         ++job.StmtIt;
448       }
449
450       if (job.StmtIt != CurrS->child_end())
451         Queue.push_back(*job.StmtIt);
452       else
453         Queue.pop_back();
454       continue;
455     }
456
457     Queue.pop_back();
458     TRY_TO(TraverseStmt(CurrS));
459   }
460
461   return true;
462 }
463
464 template<typename Derived>
465 bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S,
466                                                     bool &EnqueueChildren) {
467
468   // Dispatch to the corresponding WalkUpFrom* function only if the derived
469   // class didn't override Traverse* (and thus the traversal is trivial).
470 #define DISPATCH_WALK(NAME, CLASS, VAR) \
471   { \
472     bool (Derived::*DerivedFn)(CLASS*) = &Derived::Traverse##NAME; \
473     bool (Derived::*BaseFn)(CLASS*) = &RecursiveASTVisitor::Traverse##NAME; \
474     if (DerivedFn == BaseFn) \
475       return getDerived().WalkUpFrom##NAME(static_cast<CLASS*>(VAR)); \
476   } \
477   EnqueueChildren = false; \
478   return getDerived().Traverse##NAME(static_cast<CLASS*>(VAR));
479
480   if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
481     switch (BinOp->getOpcode()) {
482 #define OPERATOR(NAME) \
483     case BO_##NAME: DISPATCH_WALK(Bin##NAME, BinaryOperator, S);
484
485     BINOP_LIST()
486 #undef OPERATOR
487
488 #define OPERATOR(NAME)                                          \
489     case BO_##NAME##Assign:                          \
490     DISPATCH_WALK(Bin##NAME##Assign, CompoundAssignOperator, S);
491
492     CAO_LIST()
493 #undef OPERATOR
494     }
495   } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
496     switch (UnOp->getOpcode()) {
497 #define OPERATOR(NAME)                                                  \
498     case UO_##NAME: DISPATCH_WALK(Unary##NAME, UnaryOperator, S);
499
500     UNARYOP_LIST()
501 #undef OPERATOR
502     }
503   }
504
505   // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
506   switch (S->getStmtClass()) {
507   case Stmt::NoStmtClass: break;
508 #define ABSTRACT_STMT(STMT)
509 #define STMT(CLASS, PARENT) \
510   case Stmt::CLASS##Class: DISPATCH_WALK(CLASS, CLASS, S);
511 #include "clang/AST/StmtNodes.inc"
512   }
513
514 #undef DISPATCH_WALK
515
516   return true;
517 }
518
519 #define DISPATCH(NAME, CLASS, VAR) \
520   return getDerived().Traverse##NAME(static_cast<CLASS*>(VAR))
521
522 template<typename Derived>
523 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S) {
524   if (!S)
525     return true;
526
527   if (getDerived().shouldUseDataRecursionFor(S))
528     return dataTraverse(S);
529
530   // If we have a binary expr, dispatch to the subcode of the binop.  A smart
531   // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
532   // below.
533   if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
534     switch (BinOp->getOpcode()) {
535 #define OPERATOR(NAME) \
536     case BO_##NAME: DISPATCH(Bin##NAME, BinaryOperator, S);
537
538     BINOP_LIST()
539 #undef OPERATOR
540 #undef BINOP_LIST
541
542 #define OPERATOR(NAME)                                          \
543     case BO_##NAME##Assign:                          \
544       DISPATCH(Bin##NAME##Assign, CompoundAssignOperator, S);
545
546     CAO_LIST()
547 #undef OPERATOR
548 #undef CAO_LIST
549     }
550   } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
551     switch (UnOp->getOpcode()) {
552 #define OPERATOR(NAME)                                                  \
553     case UO_##NAME: DISPATCH(Unary##NAME, UnaryOperator, S);
554
555     UNARYOP_LIST()
556 #undef OPERATOR
557 #undef UNARYOP_LIST
558     }
559   }
560
561   // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt.
562   switch (S->getStmtClass()) {
563   case Stmt::NoStmtClass: break;
564 #define ABSTRACT_STMT(STMT)
565 #define STMT(CLASS, PARENT) \
566   case Stmt::CLASS##Class: DISPATCH(CLASS, CLASS, S);
567 #include "clang/AST/StmtNodes.inc"
568   }
569
570   return true;
571 }
572
573 template<typename Derived>
574 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
575   if (T.isNull())
576     return true;
577
578   switch (T->getTypeClass()) {
579 #define ABSTRACT_TYPE(CLASS, BASE)
580 #define TYPE(CLASS, BASE) \
581   case Type::CLASS: DISPATCH(CLASS##Type, CLASS##Type, \
582                              const_cast<Type*>(T.getTypePtr()));
583 #include "clang/AST/TypeNodes.def"
584   }
585
586   return true;
587 }
588
589 template<typename Derived>
590 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) {
591   if (TL.isNull())
592     return true;
593
594   switch (TL.getTypeLocClass()) {
595 #define ABSTRACT_TYPELOC(CLASS, BASE)
596 #define TYPELOC(CLASS, BASE) \
597   case TypeLoc::CLASS: \
598     return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>());
599 #include "clang/AST/TypeLocNodes.def"
600   }
601
602   return true;
603 }
604
605
606 template<typename Derived>
607 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
608   if (!D)
609     return true;
610
611   // As a syntax visitor, by default we want to ignore declarations for
612   // implicit declarations (ones not typed explicitly by the user).
613   if (!getDerived().shouldVisitImplicitCode() && D->isImplicit())
614     return true;
615
616   switch (D->getKind()) {
617 #define ABSTRACT_DECL(DECL)
618 #define DECL(CLASS, BASE) \
619   case Decl::CLASS: DISPATCH(CLASS##Decl, CLASS##Decl, D);
620 #include "clang/AST/DeclNodes.inc"
621  }
622
623   return true;
624 }
625
626 #undef DISPATCH
627
628 template<typename Derived>
629 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier(
630                                                     NestedNameSpecifier *NNS) {
631   if (!NNS)
632     return true;
633
634   if (NNS->getPrefix())
635     TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix()));
636
637   switch (NNS->getKind()) {
638   case NestedNameSpecifier::Identifier:
639   case NestedNameSpecifier::Namespace:
640   case NestedNameSpecifier::NamespaceAlias:
641   case NestedNameSpecifier::Global:
642     return true;
643
644   case NestedNameSpecifier::TypeSpec:
645   case NestedNameSpecifier::TypeSpecWithTemplate:
646     TRY_TO(TraverseType(QualType(NNS->getAsType(), 0)));
647   }
648
649   return true;
650 }
651
652 template<typename Derived>
653 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc(
654                                                   NestedNameSpecifierLoc NNS) {
655   if (!NNS)
656     return true;
657
658    if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
659      TRY_TO(TraverseNestedNameSpecifierLoc(Prefix));
660
661   switch (NNS.getNestedNameSpecifier()->getKind()) {
662   case NestedNameSpecifier::Identifier:
663   case NestedNameSpecifier::Namespace:
664   case NestedNameSpecifier::NamespaceAlias:
665   case NestedNameSpecifier::Global:
666     return true;
667
668   case NestedNameSpecifier::TypeSpec:
669   case NestedNameSpecifier::TypeSpecWithTemplate:
670     TRY_TO(TraverseTypeLoc(NNS.getTypeLoc()));
671     break;
672   }
673
674   return true;
675 }
676
677 template<typename Derived>
678 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
679                                                  DeclarationNameInfo NameInfo) {
680   switch (NameInfo.getName().getNameKind()) {
681   case DeclarationName::CXXConstructorName:
682   case DeclarationName::CXXDestructorName:
683   case DeclarationName::CXXConversionFunctionName:
684     if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
685       TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc()));
686
687     break;
688
689   case DeclarationName::Identifier:
690   case DeclarationName::ObjCZeroArgSelector:
691   case DeclarationName::ObjCOneArgSelector:
692   case DeclarationName::ObjCMultiArgSelector:
693   case DeclarationName::CXXOperatorName:
694   case DeclarationName::CXXLiteralOperatorName:
695   case DeclarationName::CXXUsingDirective:
696     break;
697   }
698
699   return true;
700 }
701
702 template<typename Derived>
703 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
704   if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
705     TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
706   else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
707     TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
708
709   return true;
710 }
711
712 template<typename Derived>
713 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument(
714                                                 const TemplateArgument &Arg) {
715   switch (Arg.getKind()) {
716   case TemplateArgument::Null:
717   case TemplateArgument::Declaration:
718   case TemplateArgument::Integral:
719   case TemplateArgument::NullPtr:
720     return true;
721
722   case TemplateArgument::Type:
723     return getDerived().TraverseType(Arg.getAsType());
724
725   case TemplateArgument::Template:
726   case TemplateArgument::TemplateExpansion:
727     return getDerived().TraverseTemplateName(
728                                           Arg.getAsTemplateOrTemplatePattern());
729
730   case TemplateArgument::Expression:
731     return getDerived().TraverseStmt(Arg.getAsExpr());
732
733   case TemplateArgument::Pack:
734     return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
735                                                   Arg.pack_size());
736   }
737
738   return true;
739 }
740
741 // FIXME: no template name location?
742 // FIXME: no source locations for a template argument pack?
743 template<typename Derived>
744 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
745                                            const TemplateArgumentLoc &ArgLoc) {
746   const TemplateArgument &Arg = ArgLoc.getArgument();
747
748   switch (Arg.getKind()) {
749   case TemplateArgument::Null:
750   case TemplateArgument::Declaration:
751   case TemplateArgument::Integral:
752   case TemplateArgument::NullPtr:
753     return true;
754
755   case TemplateArgument::Type: {
756     // FIXME: how can TSI ever be NULL?
757     if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
758       return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
759     else
760       return getDerived().TraverseType(Arg.getAsType());
761   }
762
763   case TemplateArgument::Template:
764   case TemplateArgument::TemplateExpansion:
765     if (ArgLoc.getTemplateQualifierLoc())
766       TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
767                                             ArgLoc.getTemplateQualifierLoc()));
768     return getDerived().TraverseTemplateName(
769                                          Arg.getAsTemplateOrTemplatePattern());
770
771   case TemplateArgument::Expression:
772     return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
773
774   case TemplateArgument::Pack:
775     return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
776                                                   Arg.pack_size());
777   }
778
779   return true;
780 }
781
782 template<typename Derived>
783 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
784                                                   const TemplateArgument *Args,
785                                                             unsigned NumArgs) {
786   for (unsigned I = 0; I != NumArgs; ++I) {
787     TRY_TO(TraverseTemplateArgument(Args[I]));
788   }
789
790   return true;
791 }
792
793 template<typename Derived>
794 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
795                                                      CXXCtorInitializer *Init) {
796   if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
797     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
798
799   if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
800     TRY_TO(TraverseStmt(Init->getInit()));
801   return true;
802 }
803
804 template<typename Derived>
805 bool RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr::Capture C){
806   return true;
807 }
808
809 // ----------------- Type traversal -----------------
810
811 // This macro makes available a variable T, the passed-in type.
812 #define DEF_TRAVERSE_TYPE(TYPE, CODE)                     \
813   template<typename Derived>                                           \
814   bool RecursiveASTVisitor<Derived>::Traverse##TYPE (TYPE *T) {        \
815     TRY_TO(WalkUpFrom##TYPE (T));                                      \
816     { CODE; }                                                          \
817     return true;                                                       \
818   }
819
820 DEF_TRAVERSE_TYPE(BuiltinType, { })
821
822 DEF_TRAVERSE_TYPE(ComplexType, {
823     TRY_TO(TraverseType(T->getElementType()));
824   })
825
826 DEF_TRAVERSE_TYPE(PointerType, {
827     TRY_TO(TraverseType(T->getPointeeType()));
828   })
829
830 DEF_TRAVERSE_TYPE(BlockPointerType, {
831     TRY_TO(TraverseType(T->getPointeeType()));
832   })
833
834 DEF_TRAVERSE_TYPE(LValueReferenceType, {
835     TRY_TO(TraverseType(T->getPointeeType()));
836   })
837
838 DEF_TRAVERSE_TYPE(RValueReferenceType, {
839     TRY_TO(TraverseType(T->getPointeeType()));
840   })
841
842 DEF_TRAVERSE_TYPE(MemberPointerType, {
843     TRY_TO(TraverseType(QualType(T->getClass(), 0)));
844     TRY_TO(TraverseType(T->getPointeeType()));
845   })
846
847 DEF_TRAVERSE_TYPE(ConstantArrayType, {
848     TRY_TO(TraverseType(T->getElementType()));
849   })
850
851 DEF_TRAVERSE_TYPE(IncompleteArrayType, {
852     TRY_TO(TraverseType(T->getElementType()));
853   })
854
855 DEF_TRAVERSE_TYPE(VariableArrayType, {
856     TRY_TO(TraverseType(T->getElementType()));
857     TRY_TO(TraverseStmt(T->getSizeExpr()));
858   })
859
860 DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
861     TRY_TO(TraverseType(T->getElementType()));
862     if (T->getSizeExpr())
863       TRY_TO(TraverseStmt(T->getSizeExpr()));
864   })
865
866 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
867     if (T->getSizeExpr())
868       TRY_TO(TraverseStmt(T->getSizeExpr()));
869     TRY_TO(TraverseType(T->getElementType()));
870   })
871
872 DEF_TRAVERSE_TYPE(VectorType, {
873     TRY_TO(TraverseType(T->getElementType()));
874   })
875
876 DEF_TRAVERSE_TYPE(ExtVectorType, {
877     TRY_TO(TraverseType(T->getElementType()));
878   })
879
880 DEF_TRAVERSE_TYPE(FunctionNoProtoType, {
881     TRY_TO(TraverseType(T->getResultType()));
882   })
883
884 DEF_TRAVERSE_TYPE(FunctionProtoType, {
885     TRY_TO(TraverseType(T->getResultType()));
886
887     for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
888                                            AEnd = T->arg_type_end();
889          A != AEnd; ++A) {
890       TRY_TO(TraverseType(*A));
891     }
892
893     for (FunctionProtoType::exception_iterator E = T->exception_begin(),
894                                             EEnd = T->exception_end();
895          E != EEnd; ++E) {
896       TRY_TO(TraverseType(*E));
897     }
898   })
899
900 DEF_TRAVERSE_TYPE(UnresolvedUsingType, { })
901 DEF_TRAVERSE_TYPE(TypedefType, { })
902
903 DEF_TRAVERSE_TYPE(TypeOfExprType, {
904     TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
905   })
906
907 DEF_TRAVERSE_TYPE(TypeOfType, {
908     TRY_TO(TraverseType(T->getUnderlyingType()));
909   })
910
911 DEF_TRAVERSE_TYPE(DecltypeType, {
912     TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
913   })
914
915 DEF_TRAVERSE_TYPE(UnaryTransformType, {
916     TRY_TO(TraverseType(T->getBaseType()));
917     TRY_TO(TraverseType(T->getUnderlyingType()));
918     })
919
920 DEF_TRAVERSE_TYPE(AutoType, {
921     TRY_TO(TraverseType(T->getDeducedType()));
922   })
923
924 DEF_TRAVERSE_TYPE(RecordType, { })
925 DEF_TRAVERSE_TYPE(EnumType, { })
926 DEF_TRAVERSE_TYPE(TemplateTypeParmType, { })
927 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, { })
928 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, { })
929
930 DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
931     TRY_TO(TraverseTemplateName(T->getTemplateName()));
932     TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
933   })
934
935 DEF_TRAVERSE_TYPE(InjectedClassNameType, { })
936
937 DEF_TRAVERSE_TYPE(AttributedType, {
938     TRY_TO(TraverseType(T->getModifiedType()));
939   })
940
941 DEF_TRAVERSE_TYPE(ParenType, {
942     TRY_TO(TraverseType(T->getInnerType()));
943   })
944
945 DEF_TRAVERSE_TYPE(ElaboratedType, {
946     if (T->getQualifier()) {
947       TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
948     }
949     TRY_TO(TraverseType(T->getNamedType()));
950   })
951
952 DEF_TRAVERSE_TYPE(DependentNameType, {
953     TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
954   })
955
956 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
957     TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
958     TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
959   })
960
961 DEF_TRAVERSE_TYPE(PackExpansionType, {
962     TRY_TO(TraverseType(T->getPattern()));
963   })
964
965 DEF_TRAVERSE_TYPE(ObjCInterfaceType, { })
966
967 DEF_TRAVERSE_TYPE(ObjCObjectType, {
968     // We have to watch out here because an ObjCInterfaceType's base
969     // type is itself.
970     if (T->getBaseType().getTypePtr() != T)
971       TRY_TO(TraverseType(T->getBaseType()));
972   })
973
974 DEF_TRAVERSE_TYPE(ObjCObjectPointerType, {
975     TRY_TO(TraverseType(T->getPointeeType()));
976   })
977
978 DEF_TRAVERSE_TYPE(AtomicType, {
979     TRY_TO(TraverseType(T->getValueType()));
980   })
981
982 #undef DEF_TRAVERSE_TYPE
983
984 // ----------------- TypeLoc traversal -----------------
985
986 // This macro makes available a variable TL, the passed-in TypeLoc.
987 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
988 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing
989 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
990 // continue to work.
991 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE)                                \
992   template<typename Derived>                                            \
993   bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
994     if (getDerived().shouldWalkTypesOfTypeLocs())                       \
995       TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE*>(TL.getTypePtr())));     \
996     TRY_TO(WalkUpFrom##TYPE##Loc(TL));                                  \
997     { CODE; }                                                           \
998     return true;                                                        \
999   }
1000
1001 template<typename Derived>
1002 bool RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(
1003     QualifiedTypeLoc TL) {
1004   // Move this over to the 'main' typeloc tree.  Note that this is a
1005   // move -- we pretend that we were really looking at the unqualified
1006   // typeloc all along -- rather than a recursion, so we don't follow
1007   // the normal CRTP plan of going through
1008   // getDerived().TraverseTypeLoc.  If we did, we'd be traversing
1009   // twice for the same type (once as a QualifiedTypeLoc version of
1010   // the type, once as an UnqualifiedTypeLoc version of the type),
1011   // which in effect means we'd call VisitTypeLoc twice with the
1012   // 'same' type.  This solves that problem, at the cost of never
1013   // seeing the qualified version of the type (unless the client
1014   // subclasses TraverseQualifiedTypeLoc themselves).  It's not a
1015   // perfect solution.  A perfect solution probably requires making
1016   // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1017   // wrapper around Type* -- rather than being its own class in the
1018   // type hierarchy.
1019   return TraverseTypeLoc(TL.getUnqualifiedLoc());
1020 }
1021
1022 DEF_TRAVERSE_TYPELOC(BuiltinType, { })
1023
1024 // FIXME: ComplexTypeLoc is unfinished
1025 DEF_TRAVERSE_TYPELOC(ComplexType, {
1026     TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1027   })
1028
1029 DEF_TRAVERSE_TYPELOC(PointerType, {
1030     TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1031   })
1032
1033 DEF_TRAVERSE_TYPELOC(BlockPointerType, {
1034     TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1035   })
1036
1037 DEF_TRAVERSE_TYPELOC(LValueReferenceType, {
1038     TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1039   })
1040
1041 DEF_TRAVERSE_TYPELOC(RValueReferenceType, {
1042     TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1043   })
1044
1045 // FIXME: location of base class?
1046 // We traverse this in the type case as well, but how is it not reached through
1047 // the pointee type?
1048 DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1049     TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1050     TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1051   })
1052
1053 template<typename Derived>
1054 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1055   // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1056   TRY_TO(TraverseStmt(TL.getSizeExpr()));
1057   return true;
1058 }
1059
1060 DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1061     TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1062     return TraverseArrayTypeLocHelper(TL);
1063   })
1064
1065 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1066     TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1067     return TraverseArrayTypeLocHelper(TL);
1068   })
1069
1070 DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1071     TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1072     return TraverseArrayTypeLocHelper(TL);
1073   })
1074
1075 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1076     TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1077     return TraverseArrayTypeLocHelper(TL);
1078   })
1079
1080 // FIXME: order? why not size expr first?
1081 // FIXME: base VectorTypeLoc is unfinished
1082 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1083     if (TL.getTypePtr()->getSizeExpr())
1084       TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1085     TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1086   })
1087
1088 // FIXME: VectorTypeLoc is unfinished
1089 DEF_TRAVERSE_TYPELOC(VectorType, {
1090     TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1091   })
1092
1093 // FIXME: size and attributes
1094 // FIXME: base VectorTypeLoc is unfinished
1095 DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1096     TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1097   })
1098
1099 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, {
1100     TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1101   })
1102
1103 // FIXME: location of exception specifications (attributes?)
1104 DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1105     TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1106
1107     const FunctionProtoType *T = TL.getTypePtr();
1108
1109     for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1110       if (TL.getArg(I)) {
1111         TRY_TO(TraverseDecl(TL.getArg(I)));
1112       } else if (I < T->getNumArgs()) {
1113         TRY_TO(TraverseType(T->getArgType(I)));
1114       }
1115     }
1116
1117     for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1118                                             EEnd = T->exception_end();
1119          E != EEnd; ++E) {
1120       TRY_TO(TraverseType(*E));
1121     }
1122   })
1123
1124 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, { })
1125 DEF_TRAVERSE_TYPELOC(TypedefType, { })
1126
1127 DEF_TRAVERSE_TYPELOC(TypeOfExprType, {
1128     TRY_TO(TraverseStmt(TL.getUnderlyingExpr()));
1129   })
1130
1131 DEF_TRAVERSE_TYPELOC(TypeOfType, {
1132     TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1133   })
1134
1135 // FIXME: location of underlying expr
1136 DEF_TRAVERSE_TYPELOC(DecltypeType, {
1137     TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1138   })
1139
1140 DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1141     TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1142   })
1143
1144 DEF_TRAVERSE_TYPELOC(AutoType, {
1145     TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1146   })
1147
1148 DEF_TRAVERSE_TYPELOC(RecordType, { })
1149 DEF_TRAVERSE_TYPELOC(EnumType, { })
1150 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, { })
1151 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, { })
1152 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, { })
1153
1154 // FIXME: use the loc for the template name?
1155 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1156     TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1157     for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1158       TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1159     }
1160   })
1161
1162 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, { })
1163
1164 DEF_TRAVERSE_TYPELOC(ParenType, {
1165     TRY_TO(TraverseTypeLoc(TL.getInnerLoc()));
1166   })
1167
1168 DEF_TRAVERSE_TYPELOC(AttributedType, {
1169     TRY_TO(TraverseTypeLoc(TL.getModifiedLoc()));
1170   })
1171
1172 DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1173     if (TL.getQualifierLoc()) {
1174       TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1175     }
1176     TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1177   })
1178
1179 DEF_TRAVERSE_TYPELOC(DependentNameType, {
1180     TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1181   })
1182
1183 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1184     if (TL.getQualifierLoc()) {
1185       TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1186     }
1187
1188     for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1189       TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1190     }
1191   })
1192
1193 DEF_TRAVERSE_TYPELOC(PackExpansionType, {
1194     TRY_TO(TraverseTypeLoc(TL.getPatternLoc()));
1195   })
1196
1197 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, { })
1198
1199 DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1200     // We have to watch out here because an ObjCInterfaceType's base
1201     // type is itself.
1202     if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1203       TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1204   })
1205
1206 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, {
1207     TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1208   })
1209
1210 DEF_TRAVERSE_TYPELOC(AtomicType, {
1211     TRY_TO(TraverseTypeLoc(TL.getValueLoc()));
1212   })
1213
1214 #undef DEF_TRAVERSE_TYPELOC
1215
1216 // ----------------- Decl traversal -----------------
1217 //
1218 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1219 // the children that come from the DeclContext associated with it.
1220 // Therefore each Traverse* only needs to worry about children other
1221 // than those.
1222
1223 template<typename Derived>
1224 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1225   if (!DC)
1226     return true;
1227
1228   for (DeclContext::decl_iterator Child = DC->decls_begin(),
1229            ChildEnd = DC->decls_end();
1230        Child != ChildEnd; ++Child) {
1231     // BlockDecls and CapturedDecls are traversed through BlockExprs and
1232     // CapturedStmts respectively.
1233     if (!isa<BlockDecl>(*Child) && !isa<CapturedDecl>(*Child))
1234       TRY_TO(TraverseDecl(*Child));
1235   }
1236
1237   return true;
1238 }
1239
1240 // This macro makes available a variable D, the passed-in decl.
1241 #define DEF_TRAVERSE_DECL(DECL, CODE)                           \
1242 template<typename Derived>                                      \
1243 bool RecursiveASTVisitor<Derived>::Traverse##DECL (DECL *D) {   \
1244   TRY_TO(WalkUpFrom##DECL (D));                                 \
1245   { CODE; }                                                     \
1246   TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D)));  \
1247   return true;                                                  \
1248 }
1249
1250 DEF_TRAVERSE_DECL(AccessSpecDecl, { })
1251
1252 DEF_TRAVERSE_DECL(BlockDecl, {
1253     if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1254       TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1255     TRY_TO(TraverseStmt(D->getBody()));
1256     // This return statement makes sure the traversal of nodes in
1257     // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1258     // is skipped - don't remove it.
1259     return true;
1260   })
1261
1262 DEF_TRAVERSE_DECL(CapturedDecl, {
1263     TRY_TO(TraverseStmt(D->getBody()));
1264     // This return statement makes sure the traversal of nodes in
1265     // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1266     // is skipped - don't remove it.
1267     return true;
1268   })
1269
1270 DEF_TRAVERSE_DECL(EmptyDecl, { })
1271
1272 DEF_TRAVERSE_DECL(FileScopeAsmDecl, {
1273     TRY_TO(TraverseStmt(D->getAsmString()));
1274   })
1275
1276 DEF_TRAVERSE_DECL(ImportDecl, { })
1277
1278 DEF_TRAVERSE_DECL(FriendDecl, {
1279     // Friend is either decl or a type.
1280     if (D->getFriendType())
1281       TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1282     else
1283       TRY_TO(TraverseDecl(D->getFriendDecl()));
1284   })
1285
1286 DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1287     if (D->getFriendType())
1288       TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1289     else
1290       TRY_TO(TraverseDecl(D->getFriendDecl()));
1291     for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1292       TemplateParameterList *TPL = D->getTemplateParameterList(I);
1293       for (TemplateParameterList::iterator ITPL = TPL->begin(),
1294                                            ETPL = TPL->end();
1295            ITPL != ETPL; ++ITPL) {
1296         TRY_TO(TraverseDecl(*ITPL));
1297       }
1298     }
1299   })
1300
1301 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1302     TRY_TO(TraverseDecl(D->getSpecialization()));
1303
1304     if (D->hasExplicitTemplateArgs()) {
1305       const TemplateArgumentListInfo& args = D->templateArgs();
1306       TRY_TO(TraverseTemplateArgumentLocsHelper(
1307           args.getArgumentArray(), args.size()));
1308     }
1309  })
1310
1311 DEF_TRAVERSE_DECL(LinkageSpecDecl, { })
1312
1313 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {
1314     // FIXME: implement this
1315   })
1316
1317 DEF_TRAVERSE_DECL(StaticAssertDecl, {
1318     TRY_TO(TraverseStmt(D->getAssertExpr()));
1319     TRY_TO(TraverseStmt(D->getMessage()));
1320   })
1321
1322 DEF_TRAVERSE_DECL(TranslationUnitDecl, {
1323     // Code in an unnamed namespace shows up automatically in
1324     // decls_begin()/decls_end().  Thus we don't need to recurse on
1325     // D->getAnonymousNamespace().
1326   })
1327
1328 DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1329     // We shouldn't traverse an aliased namespace, since it will be
1330     // defined (and, therefore, traversed) somewhere else.
1331     //
1332     // This return statement makes sure the traversal of nodes in
1333     // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1334     // is skipped - don't remove it.
1335     return true;
1336   })
1337
1338 DEF_TRAVERSE_DECL(LabelDecl, {
1339   // There is no code in a LabelDecl.
1340 })
1341
1342
1343 DEF_TRAVERSE_DECL(NamespaceDecl, {
1344     // Code in an unnamed namespace shows up automatically in
1345     // decls_begin()/decls_end().  Thus we don't need to recurse on
1346     // D->getAnonymousNamespace().
1347   })
1348
1349 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {
1350     // FIXME: implement
1351   })
1352
1353 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {
1354     // FIXME: implement
1355   })
1356
1357 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {
1358     // FIXME: implement
1359   })
1360
1361 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {
1362     // FIXME: implement
1363   })
1364
1365 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {
1366     // FIXME: implement
1367   })
1368
1369 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {
1370     // FIXME: implement
1371   })
1372
1373 DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1374     if (D->getResultTypeSourceInfo()) {
1375       TRY_TO(TraverseTypeLoc(D->getResultTypeSourceInfo()->getTypeLoc()));
1376     }
1377     for (ObjCMethodDecl::param_iterator
1378            I = D->param_begin(), E = D->param_end(); I != E; ++I) {
1379       TRY_TO(TraverseDecl(*I));
1380     }
1381     if (D->isThisDeclarationADefinition()) {
1382       TRY_TO(TraverseStmt(D->getBody()));
1383     }
1384     return true;
1385   })
1386
1387 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1388     // FIXME: implement
1389   })
1390
1391 DEF_TRAVERSE_DECL(UsingDecl, {
1392     TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1393     TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1394   })
1395
1396 DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1397     TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1398   })
1399
1400 DEF_TRAVERSE_DECL(UsingShadowDecl, { })
1401
1402 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1403     for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1404                                                 E = D->varlist_end();
1405          I != E; ++I) {
1406       TRY_TO(TraverseStmt(*I));
1407     }
1408   })
1409
1410 // A helper method for TemplateDecl's children.
1411 template<typename Derived>
1412 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1413     TemplateParameterList *TPL) {
1414   if (TPL) {
1415     for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1416          I != E; ++I) {
1417       TRY_TO(TraverseDecl(*I));
1418     }
1419   }
1420   return true;
1421 }
1422
1423 // A helper method for traversing the implicit instantiations of a
1424 // class template.
1425 template<typename Derived>
1426 bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations(
1427     ClassTemplateDecl *D) {
1428   ClassTemplateDecl::spec_iterator end = D->spec_end();
1429   for (ClassTemplateDecl::spec_iterator it = D->spec_begin(); it != end; ++it) {
1430     ClassTemplateSpecializationDecl* SD = *it;
1431
1432     switch (SD->getSpecializationKind()) {
1433     // Visit the implicit instantiations with the requested pattern.
1434     case TSK_Undeclared:
1435     case TSK_ImplicitInstantiation:
1436       TRY_TO(TraverseDecl(SD));
1437       break;
1438
1439     // We don't need to do anything on an explicit instantiation
1440     // or explicit specialization because there will be an explicit
1441     // node for it elsewhere.
1442     case TSK_ExplicitInstantiationDeclaration:
1443     case TSK_ExplicitInstantiationDefinition:
1444     case TSK_ExplicitSpecialization:
1445       break;
1446     }
1447   }
1448
1449   return true;
1450 }
1451
1452 DEF_TRAVERSE_DECL(ClassTemplateDecl, {
1453     CXXRecordDecl* TempDecl = D->getTemplatedDecl();
1454     TRY_TO(TraverseDecl(TempDecl));
1455     TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1456
1457     // By default, we do not traverse the instantiations of
1458     // class templates since they do not appear in the user code. The
1459     // following code optionally traverses them.
1460     //
1461     // We only traverse the class instantiations when we see the canonical
1462     // declaration of the template, to ensure we only visit them once.
1463     if (getDerived().shouldVisitTemplateInstantiations() &&
1464         D == D->getCanonicalDecl())
1465       TRY_TO(TraverseClassInstantiations(D));
1466
1467     // Note that getInstantiatedFromMemberTemplate() is just a link
1468     // from a template instantiation back to the template from which
1469     // it was instantiated, and thus should not be traversed.
1470   })
1471
1472 // A helper method for traversing the instantiations of a
1473 // function while skipping its specializations.
1474 template<typename Derived>
1475 bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations(
1476     FunctionTemplateDecl *D) {
1477   FunctionTemplateDecl::spec_iterator end = D->spec_end();
1478   for (FunctionTemplateDecl::spec_iterator it = D->spec_begin(); it != end;
1479        ++it) {
1480     FunctionDecl* FD = *it;
1481     switch (FD->getTemplateSpecializationKind()) {
1482     case TSK_Undeclared:
1483     case TSK_ImplicitInstantiation:
1484       // We don't know what kind of FunctionDecl this is.
1485       TRY_TO(TraverseDecl(FD));
1486       break;
1487
1488     // FIXME: For now traverse explicit instantiations here. Change that
1489     // once they are represented as dedicated nodes in the AST.
1490     case TSK_ExplicitInstantiationDeclaration:
1491     case TSK_ExplicitInstantiationDefinition:
1492       TRY_TO(TraverseDecl(FD));
1493       break;
1494
1495     case TSK_ExplicitSpecialization:
1496       break;
1497     }
1498   }
1499
1500   return true;
1501 }
1502
1503 DEF_TRAVERSE_DECL(FunctionTemplateDecl, {
1504     TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1505     TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1506
1507     // By default, we do not traverse the instantiations of
1508     // function templates since they do not appear in the user code. The
1509     // following code optionally traverses them.
1510     //
1511     // We only traverse the function instantiations when we see the canonical
1512     // declaration of the template, to ensure we only visit them once.
1513     if (getDerived().shouldVisitTemplateInstantiations() &&
1514         D == D->getCanonicalDecl())
1515       TRY_TO(TraverseFunctionInstantiations(D));
1516   })
1517
1518 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1519     // D is the "T" in something like
1520     //   template <template <typename> class T> class container { };
1521     TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1522     if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1523       TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1524     }
1525     TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1526   })
1527
1528 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1529     // D is the "T" in something like "template<typename T> class vector;"
1530     if (D->getTypeForDecl())
1531       TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1532     if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1533       TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1534   })
1535
1536 DEF_TRAVERSE_DECL(TypedefDecl, {
1537     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1538     // We shouldn't traverse D->getTypeForDecl(); it's a result of
1539     // declaring the typedef, not something that was written in the
1540     // source.
1541   })
1542
1543 DEF_TRAVERSE_DECL(TypeAliasDecl, {
1544     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1545     // We shouldn't traverse D->getTypeForDecl(); it's a result of
1546     // declaring the type alias, not something that was written in the
1547     // source.
1548   })
1549
1550 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1551     TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1552     TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1553   })
1554
1555 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1556     // A dependent using declaration which was marked with 'typename'.
1557     //   template<class T> class A : public B<T> { using typename B<T>::foo; };
1558     TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1559     // We shouldn't traverse D->getTypeForDecl(); it's a result of
1560     // declaring the type, not something that was written in the
1561     // source.
1562   })
1563
1564 DEF_TRAVERSE_DECL(EnumDecl, {
1565     if (D->getTypeForDecl())
1566       TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1567
1568     TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1569     // The enumerators are already traversed by
1570     // decls_begin()/decls_end().
1571   })
1572
1573
1574 // Helper methods for RecordDecl and its children.
1575 template<typename Derived>
1576 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(
1577     RecordDecl *D) {
1578   // We shouldn't traverse D->getTypeForDecl(); it's a result of
1579   // declaring the type, not something that was written in the source.
1580
1581   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1582   return true;
1583 }
1584
1585 template<typename Derived>
1586 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(
1587     CXXRecordDecl *D) {
1588   if (!TraverseRecordHelper(D))
1589     return false;
1590   if (D->isCompleteDefinition()) {
1591     for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1592                                             E = D->bases_end();
1593          I != E; ++I) {
1594       TRY_TO(TraverseTypeLoc(I->getTypeSourceInfo()->getTypeLoc()));
1595     }
1596     // We don't traverse the friends or the conversions, as they are
1597     // already in decls_begin()/decls_end().
1598   }
1599   return true;
1600 }
1601
1602 DEF_TRAVERSE_DECL(RecordDecl, {
1603     TRY_TO(TraverseRecordHelper(D));
1604   })
1605
1606 DEF_TRAVERSE_DECL(CXXRecordDecl, {
1607     TRY_TO(TraverseCXXRecordHelper(D));
1608   })
1609
1610 DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, {
1611     // For implicit instantiations ("set<int> x;"), we don't want to
1612     // recurse at all, since the instatiated class isn't written in
1613     // the source code anywhere.  (Note the instatiated *type* --
1614     // set<int> -- is written, and will still get a callback of
1615     // TemplateSpecializationType).  For explicit instantiations
1616     // ("template set<int>;"), we do need a callback, since this
1617     // is the only callback that's made for this instantiation.
1618     // We use getTypeAsWritten() to distinguish.
1619     if (TypeSourceInfo *TSI = D->getTypeAsWritten())
1620       TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1621
1622     if (!getDerived().shouldVisitTemplateInstantiations() &&
1623         D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1624       // Returning from here skips traversing the
1625       // declaration context of the ClassTemplateSpecializationDecl
1626       // (embedded in the DEF_TRAVERSE_DECL() macro)
1627       // which contains the instantiated members of the class.
1628       return true;
1629   })
1630
1631 template <typename Derived>
1632 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1633     const TemplateArgumentLoc *TAL, unsigned Count) {
1634   for (unsigned I = 0; I < Count; ++I) {
1635     TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1636   }
1637   return true;
1638 }
1639
1640 DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, {
1641     // The partial specialization.
1642     if (TemplateParameterList *TPL = D->getTemplateParameters()) {
1643       for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1644            I != E; ++I) {
1645         TRY_TO(TraverseDecl(*I));
1646       }
1647     }
1648     // The args that remains unspecialized.
1649     TRY_TO(TraverseTemplateArgumentLocsHelper(
1650         D->getTemplateArgsAsWritten(), D->getNumTemplateArgsAsWritten()));
1651
1652     // Don't need the ClassTemplatePartialSpecializationHelper, even
1653     // though that's our parent class -- we already visit all the
1654     // template args here.
1655     TRY_TO(TraverseCXXRecordHelper(D));
1656
1657     // Instantiations will have been visited with the primary template.
1658   })
1659
1660 DEF_TRAVERSE_DECL(EnumConstantDecl, {
1661     TRY_TO(TraverseStmt(D->getInitExpr()));
1662   })
1663
1664 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1665     // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1666     //    template <class T> Class A : public Base<T> { using Base<T>::foo; };
1667     TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1668     TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1669   })
1670
1671 DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1672
1673 template<typename Derived>
1674 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1675   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1676   if (D->getTypeSourceInfo())
1677     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1678   else
1679     TRY_TO(TraverseType(D->getType()));
1680   return true;
1681 }
1682
1683 DEF_TRAVERSE_DECL(MSPropertyDecl, {
1684     TRY_TO(TraverseDeclaratorHelper(D));
1685   })
1686
1687 DEF_TRAVERSE_DECL(FieldDecl, {
1688     TRY_TO(TraverseDeclaratorHelper(D));
1689     if (D->isBitField())
1690       TRY_TO(TraverseStmt(D->getBitWidth()));
1691     else if (D->hasInClassInitializer())
1692       TRY_TO(TraverseStmt(D->getInClassInitializer()));
1693   })
1694
1695 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1696     TRY_TO(TraverseDeclaratorHelper(D));
1697     if (D->isBitField())
1698       TRY_TO(TraverseStmt(D->getBitWidth()));
1699     // FIXME: implement the rest.
1700   })
1701
1702 DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1703     TRY_TO(TraverseDeclaratorHelper(D));
1704     if (D->isBitField())
1705       TRY_TO(TraverseStmt(D->getBitWidth()));
1706     // FIXME: implement the rest.
1707   })
1708
1709 template<typename Derived>
1710 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1711   TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1712   TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1713
1714   // If we're an explicit template specialization, iterate over the
1715   // template args that were explicitly specified.  If we were doing
1716   // this in typing order, we'd do it between the return type and
1717   // the function args, but both are handled by the FunctionTypeLoc
1718   // above, so we have to choose one side.  I've decided to do before.
1719   if (const FunctionTemplateSpecializationInfo *FTSI =
1720       D->getTemplateSpecializationInfo()) {
1721     if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1722         FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1723       // A specialization might not have explicit template arguments if it has
1724       // a templated return type and concrete arguments.
1725       if (const ASTTemplateArgumentListInfo *TALI =
1726           FTSI->TemplateArgumentsAsWritten) {
1727         TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1728                                                   TALI->NumTemplateArgs));
1729       }
1730     }
1731   }
1732
1733   // Visit the function type itself, which can be either
1734   // FunctionNoProtoType or FunctionProtoType, or a typedef.  This
1735   // also covers the return type and the function parameters,
1736   // including exception specifications.
1737   if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1738     TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1739   }
1740
1741   if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1742     // Constructor initializers.
1743     for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
1744                                            E = Ctor->init_end();
1745          I != E; ++I) {
1746       TRY_TO(TraverseConstructorInitializer(*I));
1747     }
1748   }
1749
1750   if (D->isThisDeclarationADefinition()) {
1751     TRY_TO(TraverseStmt(D->getBody()));  // Function body.
1752   }
1753   return true;
1754 }
1755
1756 DEF_TRAVERSE_DECL(FunctionDecl, {
1757     // We skip decls_begin/decls_end, which are already covered by
1758     // TraverseFunctionHelper().
1759     return TraverseFunctionHelper(D);
1760   })
1761
1762 DEF_TRAVERSE_DECL(CXXMethodDecl, {
1763     // We skip decls_begin/decls_end, which are already covered by
1764     // TraverseFunctionHelper().
1765     return TraverseFunctionHelper(D);
1766   })
1767
1768 DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1769     // We skip decls_begin/decls_end, which are already covered by
1770     // TraverseFunctionHelper().
1771     return TraverseFunctionHelper(D);
1772   })
1773
1774 // CXXConversionDecl is the declaration of a type conversion operator.
1775 // It's not a cast expression.
1776 DEF_TRAVERSE_DECL(CXXConversionDecl, {
1777     // We skip decls_begin/decls_end, which are already covered by
1778     // TraverseFunctionHelper().
1779     return TraverseFunctionHelper(D);
1780   })
1781
1782 DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1783     // We skip decls_begin/decls_end, which are already covered by
1784     // TraverseFunctionHelper().
1785     return TraverseFunctionHelper(D);
1786   })
1787
1788 template<typename Derived>
1789 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1790   TRY_TO(TraverseDeclaratorHelper(D));
1791   // Default params are taken care of when we traverse the ParmVarDecl.
1792   if (!isa<ParmVarDecl>(D) &&
1793       (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
1794     TRY_TO(TraverseStmt(D->getInit()));
1795   return true;
1796 }
1797
1798 DEF_TRAVERSE_DECL(VarDecl, {
1799     TRY_TO(TraverseVarHelper(D));
1800   })
1801
1802 DEF_TRAVERSE_DECL(ImplicitParamDecl, {
1803     TRY_TO(TraverseVarHelper(D));
1804   })
1805
1806 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1807     // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1808     TRY_TO(TraverseDeclaratorHelper(D));
1809     if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1810       TRY_TO(TraverseStmt(D->getDefaultArgument()));
1811   })
1812
1813 DEF_TRAVERSE_DECL(ParmVarDecl, {
1814     TRY_TO(TraverseVarHelper(D));
1815
1816     if (D->hasDefaultArg() &&
1817         D->hasUninstantiatedDefaultArg() &&
1818         !D->hasUnparsedDefaultArg())
1819       TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1820
1821     if (D->hasDefaultArg() &&
1822         !D->hasUninstantiatedDefaultArg() &&
1823         !D->hasUnparsedDefaultArg())
1824       TRY_TO(TraverseStmt(D->getDefaultArg()));
1825   })
1826
1827 #undef DEF_TRAVERSE_DECL
1828
1829 // ----------------- Stmt traversal -----------------
1830 //
1831 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1832 // over the children defined in children() (every stmt defines these,
1833 // though sometimes the range is empty).  Each individual Traverse*
1834 // method only needs to worry about children other than those.  To see
1835 // what children() does for a given class, see, e.g.,
1836 //   http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1837
1838 // This macro makes available a variable S, the passed-in stmt.
1839 #define DEF_TRAVERSE_STMT(STMT, CODE)                                   \
1840 template<typename Derived>                                              \
1841 bool RecursiveASTVisitor<Derived>::Traverse##STMT (STMT *S) {           \
1842   TRY_TO(WalkUpFrom##STMT(S));                                          \
1843   { CODE; }                                                             \
1844   for (Stmt::child_range range = S->children(); range; ++range) {       \
1845     TRY_TO(TraverseStmt(*range));                                       \
1846   }                                                                     \
1847   return true;                                                          \
1848 }
1849
1850 DEF_TRAVERSE_STMT(GCCAsmStmt, {
1851     TRY_TO(TraverseStmt(S->getAsmString()));
1852     for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1853       TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1854     }
1855     for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1856       TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1857     }
1858     for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1859       TRY_TO(TraverseStmt(S->getClobberStringLiteral(I)));
1860     }
1861     // children() iterates over inputExpr and outputExpr.
1862   })
1863
1864 DEF_TRAVERSE_STMT(MSAsmStmt, { 
1865     // FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc.  Once
1866     // added this needs to be implemented.
1867   })
1868
1869 DEF_TRAVERSE_STMT(CXXCatchStmt, {
1870     TRY_TO(TraverseDecl(S->getExceptionDecl()));
1871     // children() iterates over the handler block.
1872   })
1873
1874 DEF_TRAVERSE_STMT(DeclStmt, {
1875     for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
1876          I != E; ++I) {
1877       TRY_TO(TraverseDecl(*I));
1878     }
1879     // Suppress the default iteration over children() by
1880     // returning.  Here's why: A DeclStmt looks like 'type var [=
1881     // initializer]'.  The decls above already traverse over the
1882     // initializers, so we don't have to do it again (which
1883     // children() would do).
1884     return true;
1885   })
1886
1887
1888 // These non-expr stmts (most of them), do not need any action except
1889 // iterating over the children.
1890 DEF_TRAVERSE_STMT(BreakStmt, { })
1891 DEF_TRAVERSE_STMT(CXXTryStmt, { })
1892 DEF_TRAVERSE_STMT(CaseStmt, { })
1893 DEF_TRAVERSE_STMT(CompoundStmt, { })
1894 DEF_TRAVERSE_STMT(ContinueStmt, { })
1895 DEF_TRAVERSE_STMT(DefaultStmt, { })
1896 DEF_TRAVERSE_STMT(DoStmt, { })
1897 DEF_TRAVERSE_STMT(ForStmt, { })
1898 DEF_TRAVERSE_STMT(GotoStmt, { })
1899 DEF_TRAVERSE_STMT(IfStmt, { })
1900 DEF_TRAVERSE_STMT(IndirectGotoStmt, { })
1901 DEF_TRAVERSE_STMT(LabelStmt, { })
1902 DEF_TRAVERSE_STMT(AttributedStmt, { })
1903 DEF_TRAVERSE_STMT(NullStmt, { })
1904 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, { })
1905 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, { })
1906 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, { })
1907 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, { })
1908 DEF_TRAVERSE_STMT(ObjCAtTryStmt, { })
1909 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, { })
1910 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, { })
1911 DEF_TRAVERSE_STMT(CXXForRangeStmt, {
1912   if (!getDerived().shouldVisitImplicitCode()) {
1913     TRY_TO(TraverseStmt(S->getLoopVarStmt()));
1914     TRY_TO(TraverseStmt(S->getRangeInit()));
1915     TRY_TO(TraverseStmt(S->getBody()));
1916     // Visit everything else only if shouldVisitImplicitCode().
1917     return true;
1918   }
1919 })
1920 DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1921     TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1922     TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1923 })
1924 DEF_TRAVERSE_STMT(ReturnStmt, { })
1925 DEF_TRAVERSE_STMT(SwitchStmt, { })
1926 DEF_TRAVERSE_STMT(WhileStmt, { })
1927
1928
1929 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1930     TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1931     TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1932     if (S->hasExplicitTemplateArgs()) {
1933       TRY_TO(TraverseTemplateArgumentLocsHelper(
1934           S->getTemplateArgs(), S->getNumTemplateArgs()));
1935     }
1936   })
1937
1938 DEF_TRAVERSE_STMT(DeclRefExpr, {
1939     TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1940     TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1941     TRY_TO(TraverseTemplateArgumentLocsHelper(
1942         S->getTemplateArgs(), S->getNumTemplateArgs()));
1943   })
1944
1945 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1946     TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1947     TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1948     if (S->hasExplicitTemplateArgs()) {
1949       TRY_TO(TraverseTemplateArgumentLocsHelper(
1950           S->getExplicitTemplateArgs().getTemplateArgs(),
1951           S->getNumTemplateArgs()));
1952     }
1953   })
1954
1955 DEF_TRAVERSE_STMT(MemberExpr, {
1956     TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1957     TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1958     TRY_TO(TraverseTemplateArgumentLocsHelper(
1959         S->getTemplateArgs(), S->getNumTemplateArgs()));
1960   })
1961
1962 DEF_TRAVERSE_STMT(ImplicitCastExpr, {
1963     // We don't traverse the cast type, as it's not written in the
1964     // source code.
1965   })
1966
1967 DEF_TRAVERSE_STMT(CStyleCastExpr, {
1968     TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1969   })
1970
1971 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
1972     TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1973   })
1974
1975 DEF_TRAVERSE_STMT(CXXConstCastExpr, {
1976     TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1977   })
1978
1979 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
1980     TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1981   })
1982
1983 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
1984     TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1985   })
1986
1987 DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
1988     TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1989   })
1990
1991 // InitListExpr is a tricky one, because we want to do all our work on
1992 // the syntactic form of the listexpr, but this method takes the
1993 // semantic form by default.  We can't use the macro helper because it
1994 // calls WalkUp*() on the semantic form, before our code can convert
1995 // to the syntactic form.
1996 template<typename Derived>
1997 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
1998   if (InitListExpr *Syn = S->getSyntacticForm())
1999     S = Syn;
2000   TRY_TO(WalkUpFromInitListExpr(S));
2001   // All we need are the default actions.  FIXME: use a helper function.
2002   for (Stmt::child_range range = S->children(); range; ++range) {
2003     TRY_TO(TraverseStmt(*range));
2004   }
2005   return true;
2006 }
2007
2008 // GenericSelectionExpr is a special case because the types and expressions
2009 // are interleaved.  We also need to watch out for null types (default
2010 // generic associations).
2011 template<typename Derived>
2012 bool RecursiveASTVisitor<Derived>::
2013 TraverseGenericSelectionExpr(GenericSelectionExpr *S) {
2014   TRY_TO(WalkUpFromGenericSelectionExpr(S));
2015   TRY_TO(TraverseStmt(S->getControllingExpr()));
2016   for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
2017     if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2018       TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2019     TRY_TO(TraverseStmt(S->getAssocExpr(i)));
2020   }
2021   return true;
2022 }
2023
2024 // PseudoObjectExpr is a special case because of the wierdness with
2025 // syntactic expressions and opaque values.
2026 template<typename Derived>
2027 bool RecursiveASTVisitor<Derived>::
2028 TraversePseudoObjectExpr(PseudoObjectExpr *S) {
2029   TRY_TO(WalkUpFromPseudoObjectExpr(S));
2030   TRY_TO(TraverseStmt(S->getSyntacticForm()));
2031   for (PseudoObjectExpr::semantics_iterator
2032          i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i) {
2033     Expr *sub = *i;
2034     if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2035       sub = OVE->getSourceExpr();
2036     TRY_TO(TraverseStmt(sub));
2037   }
2038   return true;
2039 }
2040
2041 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2042     // This is called for code like 'return T()' where T is a built-in
2043     // (i.e. non-class) type.
2044     TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2045   })
2046
2047 DEF_TRAVERSE_STMT(CXXNewExpr, {
2048   // The child-iterator will pick up the other arguments.
2049   TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2050   })
2051
2052 DEF_TRAVERSE_STMT(OffsetOfExpr, {
2053     // The child-iterator will pick up the expression representing
2054     // the field.
2055     // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2056     // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2057     TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2058   })
2059
2060 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2061     // The child-iterator will pick up the arg if it's an expression,
2062     // but not if it's a type.
2063     if (S->isArgumentType())
2064       TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2065   })
2066
2067 DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2068     // The child-iterator will pick up the arg if it's an expression,
2069     // but not if it's a type.
2070     if (S->isTypeOperand())
2071       TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2072   })
2073
2074 DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2075   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2076 })
2077
2078 DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2079     // The child-iterator will pick up the arg if it's an expression,
2080     // but not if it's a type.
2081     if (S->isTypeOperand())
2082       TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2083   })
2084
2085 DEF_TRAVERSE_STMT(UnaryTypeTraitExpr, {
2086     TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2087   })
2088
2089 DEF_TRAVERSE_STMT(BinaryTypeTraitExpr, {
2090     TRY_TO(TraverseTypeLoc(S->getLhsTypeSourceInfo()->getTypeLoc()));
2091     TRY_TO(TraverseTypeLoc(S->getRhsTypeSourceInfo()->getTypeLoc()));
2092   })
2093
2094 DEF_TRAVERSE_STMT(TypeTraitExpr, {
2095   for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2096     TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2097 })
2098
2099 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2100     TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2101   })
2102
2103 DEF_TRAVERSE_STMT(ExpressionTraitExpr, {
2104     TRY_TO(TraverseStmt(S->getQueriedExpression()));
2105   })
2106
2107 DEF_TRAVERSE_STMT(VAArgExpr, {
2108     // The child-iterator will pick up the expression argument.
2109     TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2110   })
2111
2112 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2113     // This is called for code like 'return T()' where T is a class type.
2114     TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2115   })
2116
2117 // Walk only the visible parts of lambda expressions.  
2118 template<typename Derived>
2119 bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2120   for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2121                                  CEnd = S->explicit_capture_end();
2122        C != CEnd; ++C) {
2123     TRY_TO(TraverseLambdaCapture(*C));
2124   }
2125
2126   if (S->hasExplicitParameters() || S->hasExplicitResultType()) {
2127     TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2128     if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2129       // Visit the whole type.
2130       TRY_TO(TraverseTypeLoc(TL));
2131     } else if (FunctionProtoTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) {
2132       if (S->hasExplicitParameters()) {
2133         // Visit parameters.
2134         for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) {
2135           TRY_TO(TraverseDecl(Proto.getArg(I)));
2136         }
2137       } else {
2138         TRY_TO(TraverseTypeLoc(Proto.getResultLoc()));
2139       }        
2140     }
2141   }
2142
2143   TRY_TO(TraverseStmt(S->getBody()));
2144   return true;
2145 }
2146
2147 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2148     // This is called for code like 'T()', where T is a template argument.
2149     TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2150   })
2151
2152 // These expressions all might take explicit template arguments.
2153 // We traverse those if so.  FIXME: implement these.
2154 DEF_TRAVERSE_STMT(CXXConstructExpr, { })
2155 DEF_TRAVERSE_STMT(CallExpr, { })
2156 DEF_TRAVERSE_STMT(CXXMemberCallExpr, { })
2157
2158 // These exprs (most of them), do not need any action except iterating
2159 // over the children.
2160 DEF_TRAVERSE_STMT(AddrLabelExpr, { })
2161 DEF_TRAVERSE_STMT(ArraySubscriptExpr, { })
2162 DEF_TRAVERSE_STMT(BlockExpr, {
2163   TRY_TO(TraverseDecl(S->getBlockDecl()));
2164   return true; // no child statements to loop through.
2165 })
2166 DEF_TRAVERSE_STMT(ChooseExpr, { })
2167 DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2168   TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2169 })
2170 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, { })
2171 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, { })
2172 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, { })
2173 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, { })
2174 DEF_TRAVERSE_STMT(CXXDeleteExpr, { })
2175 DEF_TRAVERSE_STMT(ExprWithCleanups, { })
2176 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, { })
2177 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2178   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2179   if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2180     TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2181   if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2182     TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2183 })
2184 DEF_TRAVERSE_STMT(CXXThisExpr, { })
2185 DEF_TRAVERSE_STMT(CXXThrowExpr, { })
2186 DEF_TRAVERSE_STMT(UserDefinedLiteral, { })
2187 DEF_TRAVERSE_STMT(DesignatedInitExpr, { })
2188 DEF_TRAVERSE_STMT(ExtVectorElementExpr, { })
2189 DEF_TRAVERSE_STMT(GNUNullExpr, { })
2190 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, { })
2191 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, { })
2192 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2193   if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2194     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2195 })
2196 DEF_TRAVERSE_STMT(ObjCIsaExpr, { })
2197 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, { })
2198 DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2199   if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2200     TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2201 })
2202 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, { })
2203 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, { })
2204 DEF_TRAVERSE_STMT(ObjCProtocolExpr, { })
2205 DEF_TRAVERSE_STMT(ObjCSelectorExpr, { })
2206 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, { })
2207 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2208   TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2209 })
2210 DEF_TRAVERSE_STMT(ParenExpr, { })
2211 DEF_TRAVERSE_STMT(ParenListExpr, { })
2212 DEF_TRAVERSE_STMT(PredefinedExpr, { })
2213 DEF_TRAVERSE_STMT(ShuffleVectorExpr, { })
2214 DEF_TRAVERSE_STMT(StmtExpr, { })
2215 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2216   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2217   if (S->hasExplicitTemplateArgs()) {
2218     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2219                                               S->getNumTemplateArgs()));
2220   }
2221 })
2222
2223 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2224   TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2225   if (S->hasExplicitTemplateArgs()) {
2226     TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2227                                               S->getNumTemplateArgs()));
2228   }
2229 })
2230
2231 DEF_TRAVERSE_STMT(SEHTryStmt, {})
2232 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2233 DEF_TRAVERSE_STMT(SEHFinallyStmt,{})
2234 DEF_TRAVERSE_STMT(CapturedStmt, {
2235   TRY_TO(TraverseDecl(S->getCapturedDecl()));
2236 })
2237
2238 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, { })
2239 DEF_TRAVERSE_STMT(OpaqueValueExpr, { })
2240 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, { })
2241
2242 // These operators (all of them) do not need any action except
2243 // iterating over the children.
2244 DEF_TRAVERSE_STMT(BinaryConditionalOperator, { })
2245 DEF_TRAVERSE_STMT(ConditionalOperator, { })
2246 DEF_TRAVERSE_STMT(UnaryOperator, { })
2247 DEF_TRAVERSE_STMT(BinaryOperator, { })
2248 DEF_TRAVERSE_STMT(CompoundAssignOperator, { })
2249 DEF_TRAVERSE_STMT(CXXNoexceptExpr, { })
2250 DEF_TRAVERSE_STMT(PackExpansionExpr, { })
2251 DEF_TRAVERSE_STMT(SizeOfPackExpr, { })
2252 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, { })
2253 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, { })
2254 DEF_TRAVERSE_STMT(FunctionParmPackExpr, { })
2255 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, { })
2256 DEF_TRAVERSE_STMT(AtomicExpr, { })
2257
2258 // These literals (all of them) do not need any action.
2259 DEF_TRAVERSE_STMT(IntegerLiteral, { })
2260 DEF_TRAVERSE_STMT(CharacterLiteral, { })
2261 DEF_TRAVERSE_STMT(FloatingLiteral, { })
2262 DEF_TRAVERSE_STMT(ImaginaryLiteral, { })
2263 DEF_TRAVERSE_STMT(StringLiteral, { })
2264 DEF_TRAVERSE_STMT(ObjCStringLiteral, { })
2265 DEF_TRAVERSE_STMT(ObjCBoxedExpr, { })
2266 DEF_TRAVERSE_STMT(ObjCArrayLiteral, { })
2267 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, { })
2268   
2269 // Traverse OpenCL: AsType, Convert.
2270 DEF_TRAVERSE_STMT(AsTypeExpr, { })
2271
2272 // FIXME: look at the following tricky-seeming exprs to see if we
2273 // need to recurse on anything.  These are ones that have methods
2274 // returning decls or qualtypes or nestednamespecifier -- though I'm
2275 // not sure if they own them -- or just seemed very complicated, or
2276 // had lots of sub-types to explore.
2277 //
2278 // VisitOverloadExpr and its children: recurse on template args? etc?
2279
2280 // FIXME: go through all the stmts and exprs again, and see which of them
2281 // create new types, and recurse on the types (TypeLocs?) of those.
2282 // Candidates:
2283 //
2284 //    http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2285 //    http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2286 //    http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2287 //    Every class that has getQualifier.
2288
2289 #undef DEF_TRAVERSE_STMT
2290
2291 #undef TRY_TO
2292
2293 } // end namespace clang
2294
2295 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H