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