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