RecursiveASTVisitor.h revision 89faf86c6e23ed62519dfe369f8ea408df1cb02e
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 *LE, const 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(
806    LambdaExpr *LE, const LambdaExpr::Capture *C) {
807  if (C->isInitCapture())
808    TRY_TO(TraverseStmt(LE->getInitCaptureInit(C)));
809  return true;
810}
811
812// ----------------- Type traversal -----------------
813
814// This macro makes available a variable T, the passed-in type.
815#define DEF_TRAVERSE_TYPE(TYPE, CODE)                     \
816  template<typename Derived>                                           \
817  bool RecursiveASTVisitor<Derived>::Traverse##TYPE (TYPE *T) {        \
818    TRY_TO(WalkUpFrom##TYPE (T));                                      \
819    { CODE; }                                                          \
820    return true;                                                       \
821  }
822
823DEF_TRAVERSE_TYPE(BuiltinType, { })
824
825DEF_TRAVERSE_TYPE(ComplexType, {
826    TRY_TO(TraverseType(T->getElementType()));
827  })
828
829DEF_TRAVERSE_TYPE(PointerType, {
830    TRY_TO(TraverseType(T->getPointeeType()));
831  })
832
833DEF_TRAVERSE_TYPE(BlockPointerType, {
834    TRY_TO(TraverseType(T->getPointeeType()));
835  })
836
837DEF_TRAVERSE_TYPE(LValueReferenceType, {
838    TRY_TO(TraverseType(T->getPointeeType()));
839  })
840
841DEF_TRAVERSE_TYPE(RValueReferenceType, {
842    TRY_TO(TraverseType(T->getPointeeType()));
843  })
844
845DEF_TRAVERSE_TYPE(MemberPointerType, {
846    TRY_TO(TraverseType(QualType(T->getClass(), 0)));
847    TRY_TO(TraverseType(T->getPointeeType()));
848  })
849
850DEF_TRAVERSE_TYPE(DecayedType, {
851    TRY_TO(TraverseType(T->getOriginalType()));
852  })
853
854DEF_TRAVERSE_TYPE(ConstantArrayType, {
855    TRY_TO(TraverseType(T->getElementType()));
856  })
857
858DEF_TRAVERSE_TYPE(IncompleteArrayType, {
859    TRY_TO(TraverseType(T->getElementType()));
860  })
861
862DEF_TRAVERSE_TYPE(VariableArrayType, {
863    TRY_TO(TraverseType(T->getElementType()));
864    TRY_TO(TraverseStmt(T->getSizeExpr()));
865  })
866
867DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
868    TRY_TO(TraverseType(T->getElementType()));
869    if (T->getSizeExpr())
870      TRY_TO(TraverseStmt(T->getSizeExpr()));
871  })
872
873DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
874    if (T->getSizeExpr())
875      TRY_TO(TraverseStmt(T->getSizeExpr()));
876    TRY_TO(TraverseType(T->getElementType()));
877  })
878
879DEF_TRAVERSE_TYPE(VectorType, {
880    TRY_TO(TraverseType(T->getElementType()));
881  })
882
883DEF_TRAVERSE_TYPE(ExtVectorType, {
884    TRY_TO(TraverseType(T->getElementType()));
885  })
886
887DEF_TRAVERSE_TYPE(FunctionNoProtoType, {
888    TRY_TO(TraverseType(T->getResultType()));
889  })
890
891DEF_TRAVERSE_TYPE(FunctionProtoType, {
892    TRY_TO(TraverseType(T->getResultType()));
893
894    for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
895                                           AEnd = T->arg_type_end();
896         A != AEnd; ++A) {
897      TRY_TO(TraverseType(*A));
898    }
899
900    for (FunctionProtoType::exception_iterator E = T->exception_begin(),
901                                            EEnd = T->exception_end();
902         E != EEnd; ++E) {
903      TRY_TO(TraverseType(*E));
904    }
905  })
906
907DEF_TRAVERSE_TYPE(UnresolvedUsingType, { })
908DEF_TRAVERSE_TYPE(TypedefType, { })
909
910DEF_TRAVERSE_TYPE(TypeOfExprType, {
911    TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
912  })
913
914DEF_TRAVERSE_TYPE(TypeOfType, {
915    TRY_TO(TraverseType(T->getUnderlyingType()));
916  })
917
918DEF_TRAVERSE_TYPE(DecltypeType, {
919    TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
920  })
921
922DEF_TRAVERSE_TYPE(UnaryTransformType, {
923    TRY_TO(TraverseType(T->getBaseType()));
924    TRY_TO(TraverseType(T->getUnderlyingType()));
925    })
926
927DEF_TRAVERSE_TYPE(AutoType, {
928    TRY_TO(TraverseType(T->getDeducedType()));
929  })
930
931DEF_TRAVERSE_TYPE(RecordType, { })
932DEF_TRAVERSE_TYPE(EnumType, { })
933DEF_TRAVERSE_TYPE(TemplateTypeParmType, { })
934DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, { })
935DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, { })
936
937DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
938    TRY_TO(TraverseTemplateName(T->getTemplateName()));
939    TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
940  })
941
942DEF_TRAVERSE_TYPE(InjectedClassNameType, { })
943
944DEF_TRAVERSE_TYPE(AttributedType, {
945    TRY_TO(TraverseType(T->getModifiedType()));
946  })
947
948DEF_TRAVERSE_TYPE(ParenType, {
949    TRY_TO(TraverseType(T->getInnerType()));
950  })
951
952DEF_TRAVERSE_TYPE(ElaboratedType, {
953    if (T->getQualifier()) {
954      TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
955    }
956    TRY_TO(TraverseType(T->getNamedType()));
957  })
958
959DEF_TRAVERSE_TYPE(DependentNameType, {
960    TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
961  })
962
963DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
964    TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
965    TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
966  })
967
968DEF_TRAVERSE_TYPE(PackExpansionType, {
969    TRY_TO(TraverseType(T->getPattern()));
970  })
971
972DEF_TRAVERSE_TYPE(ObjCInterfaceType, { })
973
974DEF_TRAVERSE_TYPE(ObjCObjectType, {
975    // We have to watch out here because an ObjCInterfaceType's base
976    // type is itself.
977    if (T->getBaseType().getTypePtr() != T)
978      TRY_TO(TraverseType(T->getBaseType()));
979  })
980
981DEF_TRAVERSE_TYPE(ObjCObjectPointerType, {
982    TRY_TO(TraverseType(T->getPointeeType()));
983  })
984
985DEF_TRAVERSE_TYPE(AtomicType, {
986    TRY_TO(TraverseType(T->getValueType()));
987  })
988
989#undef DEF_TRAVERSE_TYPE
990
991// ----------------- TypeLoc traversal -----------------
992
993// This macro makes available a variable TL, the passed-in TypeLoc.
994// If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
995// in addition to WalkUpFrom* for the TypeLoc itself, such that existing
996// clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
997// continue to work.
998#define DEF_TRAVERSE_TYPELOC(TYPE, CODE)                                \
999  template<typename Derived>                                            \
1000  bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
1001    if (getDerived().shouldWalkTypesOfTypeLocs())                       \
1002      TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE*>(TL.getTypePtr())));     \
1003    TRY_TO(WalkUpFrom##TYPE##Loc(TL));                                  \
1004    { CODE; }                                                           \
1005    return true;                                                        \
1006  }
1007
1008template<typename Derived>
1009bool RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(
1010    QualifiedTypeLoc TL) {
1011  // Move this over to the 'main' typeloc tree.  Note that this is a
1012  // move -- we pretend that we were really looking at the unqualified
1013  // typeloc all along -- rather than a recursion, so we don't follow
1014  // the normal CRTP plan of going through
1015  // getDerived().TraverseTypeLoc.  If we did, we'd be traversing
1016  // twice for the same type (once as a QualifiedTypeLoc version of
1017  // the type, once as an UnqualifiedTypeLoc version of the type),
1018  // which in effect means we'd call VisitTypeLoc twice with the
1019  // 'same' type.  This solves that problem, at the cost of never
1020  // seeing the qualified version of the type (unless the client
1021  // subclasses TraverseQualifiedTypeLoc themselves).  It's not a
1022  // perfect solution.  A perfect solution probably requires making
1023  // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
1024  // wrapper around Type* -- rather than being its own class in the
1025  // type hierarchy.
1026  return TraverseTypeLoc(TL.getUnqualifiedLoc());
1027}
1028
1029DEF_TRAVERSE_TYPELOC(BuiltinType, { })
1030
1031// FIXME: ComplexTypeLoc is unfinished
1032DEF_TRAVERSE_TYPELOC(ComplexType, {
1033    TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1034  })
1035
1036DEF_TRAVERSE_TYPELOC(PointerType, {
1037    TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1038  })
1039
1040DEF_TRAVERSE_TYPELOC(BlockPointerType, {
1041    TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1042  })
1043
1044DEF_TRAVERSE_TYPELOC(LValueReferenceType, {
1045    TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1046  })
1047
1048DEF_TRAVERSE_TYPELOC(RValueReferenceType, {
1049    TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1050  })
1051
1052// FIXME: location of base class?
1053// We traverse this in the type case as well, but how is it not reached through
1054// the pointee type?
1055DEF_TRAVERSE_TYPELOC(MemberPointerType, {
1056    TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
1057    TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1058  })
1059
1060DEF_TRAVERSE_TYPELOC(DecayedType, {
1061    TRY_TO(TraverseTypeLoc(TL.getOriginalLoc()));
1062  })
1063
1064template<typename Derived>
1065bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
1066  // This isn't available for ArrayType, but is for the ArrayTypeLoc.
1067  TRY_TO(TraverseStmt(TL.getSizeExpr()));
1068  return true;
1069}
1070
1071DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1072    TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1073    return TraverseArrayTypeLocHelper(TL);
1074  })
1075
1076DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1077    TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1078    return TraverseArrayTypeLocHelper(TL);
1079  })
1080
1081DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1082    TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1083    return TraverseArrayTypeLocHelper(TL);
1084  })
1085
1086DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1087    TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1088    return TraverseArrayTypeLocHelper(TL);
1089  })
1090
1091// FIXME: order? why not size expr first?
1092// FIXME: base VectorTypeLoc is unfinished
1093DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1094    if (TL.getTypePtr()->getSizeExpr())
1095      TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1096    TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1097  })
1098
1099// FIXME: VectorTypeLoc is unfinished
1100DEF_TRAVERSE_TYPELOC(VectorType, {
1101    TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1102  })
1103
1104// FIXME: size and attributes
1105// FIXME: base VectorTypeLoc is unfinished
1106DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1107    TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1108  })
1109
1110DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, {
1111    TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1112  })
1113
1114// FIXME: location of exception specifications (attributes?)
1115DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1116    TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1117
1118    const FunctionProtoType *T = TL.getTypePtr();
1119
1120    for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1121      if (TL.getArg(I)) {
1122        TRY_TO(TraverseDecl(TL.getArg(I)));
1123      } else if (I < T->getNumArgs()) {
1124        TRY_TO(TraverseType(T->getArgType(I)));
1125      }
1126    }
1127
1128    for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1129                                            EEnd = T->exception_end();
1130         E != EEnd; ++E) {
1131      TRY_TO(TraverseType(*E));
1132    }
1133  })
1134
1135DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, { })
1136DEF_TRAVERSE_TYPELOC(TypedefType, { })
1137
1138DEF_TRAVERSE_TYPELOC(TypeOfExprType, {
1139    TRY_TO(TraverseStmt(TL.getUnderlyingExpr()));
1140  })
1141
1142DEF_TRAVERSE_TYPELOC(TypeOfType, {
1143    TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1144  })
1145
1146// FIXME: location of underlying expr
1147DEF_TRAVERSE_TYPELOC(DecltypeType, {
1148    TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1149  })
1150
1151DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1152    TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1153  })
1154
1155DEF_TRAVERSE_TYPELOC(AutoType, {
1156    TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1157  })
1158
1159DEF_TRAVERSE_TYPELOC(RecordType, { })
1160DEF_TRAVERSE_TYPELOC(EnumType, { })
1161DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, { })
1162DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, { })
1163DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, { })
1164
1165// FIXME: use the loc for the template name?
1166DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1167    TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1168    for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1169      TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1170    }
1171  })
1172
1173DEF_TRAVERSE_TYPELOC(InjectedClassNameType, { })
1174
1175DEF_TRAVERSE_TYPELOC(ParenType, {
1176    TRY_TO(TraverseTypeLoc(TL.getInnerLoc()));
1177  })
1178
1179DEF_TRAVERSE_TYPELOC(AttributedType, {
1180    TRY_TO(TraverseTypeLoc(TL.getModifiedLoc()));
1181  })
1182
1183DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1184    if (TL.getQualifierLoc()) {
1185      TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1186    }
1187    TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1188  })
1189
1190DEF_TRAVERSE_TYPELOC(DependentNameType, {
1191    TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1192  })
1193
1194DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1195    if (TL.getQualifierLoc()) {
1196      TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1197    }
1198
1199    for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1200      TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1201    }
1202  })
1203
1204DEF_TRAVERSE_TYPELOC(PackExpansionType, {
1205    TRY_TO(TraverseTypeLoc(TL.getPatternLoc()));
1206  })
1207
1208DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, { })
1209
1210DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1211    // We have to watch out here because an ObjCInterfaceType's base
1212    // type is itself.
1213    if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1214      TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1215  })
1216
1217DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, {
1218    TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1219  })
1220
1221DEF_TRAVERSE_TYPELOC(AtomicType, {
1222    TRY_TO(TraverseTypeLoc(TL.getValueLoc()));
1223  })
1224
1225#undef DEF_TRAVERSE_TYPELOC
1226
1227// ----------------- Decl traversal -----------------
1228//
1229// For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1230// the children that come from the DeclContext associated with it.
1231// Therefore each Traverse* only needs to worry about children other
1232// than those.
1233
1234template<typename Derived>
1235bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1236  if (!DC)
1237    return true;
1238
1239  for (DeclContext::decl_iterator Child = DC->decls_begin(),
1240           ChildEnd = DC->decls_end();
1241       Child != ChildEnd; ++Child) {
1242    // BlockDecls and CapturedDecls are traversed through BlockExprs and
1243    // CapturedStmts respectively.
1244    if (!isa<BlockDecl>(*Child) && !isa<CapturedDecl>(*Child))
1245      TRY_TO(TraverseDecl(*Child));
1246  }
1247
1248  return true;
1249}
1250
1251// This macro makes available a variable D, the passed-in decl.
1252#define DEF_TRAVERSE_DECL(DECL, CODE)                           \
1253template<typename Derived>                                      \
1254bool RecursiveASTVisitor<Derived>::Traverse##DECL (DECL *D) {   \
1255  TRY_TO(WalkUpFrom##DECL (D));                                 \
1256  { CODE; }                                                     \
1257  TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D)));  \
1258  return true;                                                  \
1259}
1260
1261DEF_TRAVERSE_DECL(AccessSpecDecl, { })
1262
1263DEF_TRAVERSE_DECL(BlockDecl, {
1264    if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1265      TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
1266    TRY_TO(TraverseStmt(D->getBody()));
1267    // This return statement makes sure the traversal of nodes in
1268    // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1269    // is skipped - don't remove it.
1270    return true;
1271  })
1272
1273DEF_TRAVERSE_DECL(CapturedDecl, {
1274    TRY_TO(TraverseStmt(D->getBody()));
1275    // This return statement makes sure the traversal of nodes in
1276    // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1277    // is skipped - don't remove it.
1278    return true;
1279  })
1280
1281DEF_TRAVERSE_DECL(EmptyDecl, { })
1282
1283DEF_TRAVERSE_DECL(FileScopeAsmDecl, {
1284    TRY_TO(TraverseStmt(D->getAsmString()));
1285  })
1286
1287DEF_TRAVERSE_DECL(ImportDecl, { })
1288
1289DEF_TRAVERSE_DECL(FriendDecl, {
1290    // Friend is either decl or a type.
1291    if (D->getFriendType())
1292      TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1293    else
1294      TRY_TO(TraverseDecl(D->getFriendDecl()));
1295  })
1296
1297DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1298    if (D->getFriendType())
1299      TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1300    else
1301      TRY_TO(TraverseDecl(D->getFriendDecl()));
1302    for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1303      TemplateParameterList *TPL = D->getTemplateParameterList(I);
1304      for (TemplateParameterList::iterator ITPL = TPL->begin(),
1305                                           ETPL = TPL->end();
1306           ITPL != ETPL; ++ITPL) {
1307        TRY_TO(TraverseDecl(*ITPL));
1308      }
1309    }
1310  })
1311
1312DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1313    TRY_TO(TraverseDecl(D->getSpecialization()));
1314
1315    if (D->hasExplicitTemplateArgs()) {
1316      const TemplateArgumentListInfo& args = D->templateArgs();
1317      TRY_TO(TraverseTemplateArgumentLocsHelper(
1318          args.getArgumentArray(), args.size()));
1319    }
1320 })
1321
1322DEF_TRAVERSE_DECL(LinkageSpecDecl, { })
1323
1324DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {
1325    // FIXME: implement this
1326  })
1327
1328DEF_TRAVERSE_DECL(StaticAssertDecl, {
1329    TRY_TO(TraverseStmt(D->getAssertExpr()));
1330    TRY_TO(TraverseStmt(D->getMessage()));
1331  })
1332
1333DEF_TRAVERSE_DECL(TranslationUnitDecl, {
1334    // Code in an unnamed namespace shows up automatically in
1335    // decls_begin()/decls_end().  Thus we don't need to recurse on
1336    // D->getAnonymousNamespace().
1337  })
1338
1339DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1340    // We shouldn't traverse an aliased namespace, since it will be
1341    // defined (and, therefore, traversed) somewhere else.
1342    //
1343    // This return statement makes sure the traversal of nodes in
1344    // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1345    // is skipped - don't remove it.
1346    return true;
1347  })
1348
1349DEF_TRAVERSE_DECL(LabelDecl, {
1350  // There is no code in a LabelDecl.
1351})
1352
1353
1354DEF_TRAVERSE_DECL(NamespaceDecl, {
1355    // Code in an unnamed namespace shows up automatically in
1356    // decls_begin()/decls_end().  Thus we don't need to recurse on
1357    // D->getAnonymousNamespace().
1358  })
1359
1360DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {
1361    // FIXME: implement
1362  })
1363
1364DEF_TRAVERSE_DECL(ObjCCategoryDecl, {
1365    // FIXME: implement
1366  })
1367
1368DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {
1369    // FIXME: implement
1370  })
1371
1372DEF_TRAVERSE_DECL(ObjCImplementationDecl, {
1373    // FIXME: implement
1374  })
1375
1376DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {
1377    // FIXME: implement
1378  })
1379
1380DEF_TRAVERSE_DECL(ObjCProtocolDecl, {
1381    // FIXME: implement
1382  })
1383
1384DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1385    if (D->getResultTypeSourceInfo()) {
1386      TRY_TO(TraverseTypeLoc(D->getResultTypeSourceInfo()->getTypeLoc()));
1387    }
1388    for (ObjCMethodDecl::param_iterator
1389           I = D->param_begin(), E = D->param_end(); I != E; ++I) {
1390      TRY_TO(TraverseDecl(*I));
1391    }
1392    if (D->isThisDeclarationADefinition()) {
1393      TRY_TO(TraverseStmt(D->getBody()));
1394    }
1395    return true;
1396  })
1397
1398DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1399    // FIXME: implement
1400  })
1401
1402DEF_TRAVERSE_DECL(UsingDecl, {
1403    TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1404    TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1405  })
1406
1407DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1408    TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1409  })
1410
1411DEF_TRAVERSE_DECL(UsingShadowDecl, { })
1412
1413DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, {
1414    for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1415                                                E = D->varlist_end();
1416         I != E; ++I) {
1417      TRY_TO(TraverseStmt(*I));
1418    }
1419  })
1420
1421// A helper method for TemplateDecl's children.
1422template<typename Derived>
1423bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1424    TemplateParameterList *TPL) {
1425  if (TPL) {
1426    for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1427         I != E; ++I) {
1428      TRY_TO(TraverseDecl(*I));
1429    }
1430  }
1431  return true;
1432}
1433
1434// A helper method for traversing the implicit instantiations of a
1435// class template.
1436template<typename Derived>
1437bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations(
1438    ClassTemplateDecl *D) {
1439  ClassTemplateDecl::spec_iterator end = D->spec_end();
1440  for (ClassTemplateDecl::spec_iterator it = D->spec_begin(); it != end; ++it) {
1441    ClassTemplateSpecializationDecl* SD = *it;
1442
1443    switch (SD->getSpecializationKind()) {
1444    // Visit the implicit instantiations with the requested pattern.
1445    case TSK_Undeclared:
1446    case TSK_ImplicitInstantiation:
1447      TRY_TO(TraverseDecl(SD));
1448      break;
1449
1450    // We don't need to do anything on an explicit instantiation
1451    // or explicit specialization because there will be an explicit
1452    // node for it elsewhere.
1453    case TSK_ExplicitInstantiationDeclaration:
1454    case TSK_ExplicitInstantiationDefinition:
1455    case TSK_ExplicitSpecialization:
1456      break;
1457    }
1458  }
1459
1460  return true;
1461}
1462
1463DEF_TRAVERSE_DECL(ClassTemplateDecl, {
1464    CXXRecordDecl* TempDecl = D->getTemplatedDecl();
1465    TRY_TO(TraverseDecl(TempDecl));
1466    TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1467
1468    // By default, we do not traverse the instantiations of
1469    // class templates since they do not appear in the user code. The
1470    // following code optionally traverses them.
1471    //
1472    // We only traverse the class instantiations when we see the canonical
1473    // declaration of the template, to ensure we only visit them once.
1474    if (getDerived().shouldVisitTemplateInstantiations() &&
1475        D == D->getCanonicalDecl())
1476      TRY_TO(TraverseClassInstantiations(D));
1477
1478    // Note that getInstantiatedFromMemberTemplate() is just a link
1479    // from a template instantiation back to the template from which
1480    // it was instantiated, and thus should not be traversed.
1481  })
1482
1483// A helper method for traversing the instantiations of a
1484// function while skipping its specializations.
1485template<typename Derived>
1486bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations(
1487    FunctionTemplateDecl *D) {
1488  FunctionTemplateDecl::spec_iterator end = D->spec_end();
1489  for (FunctionTemplateDecl::spec_iterator it = D->spec_begin(); it != end;
1490       ++it) {
1491    FunctionDecl* FD = *it;
1492    switch (FD->getTemplateSpecializationKind()) {
1493    case TSK_Undeclared:
1494    case TSK_ImplicitInstantiation:
1495      // We don't know what kind of FunctionDecl this is.
1496      TRY_TO(TraverseDecl(FD));
1497      break;
1498
1499    // FIXME: For now traverse explicit instantiations here. Change that
1500    // once they are represented as dedicated nodes in the AST.
1501    case TSK_ExplicitInstantiationDeclaration:
1502    case TSK_ExplicitInstantiationDefinition:
1503      TRY_TO(TraverseDecl(FD));
1504      break;
1505
1506    case TSK_ExplicitSpecialization:
1507      break;
1508    }
1509  }
1510
1511  return true;
1512}
1513
1514DEF_TRAVERSE_DECL(FunctionTemplateDecl, {
1515    TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1516    TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1517
1518    // By default, we do not traverse the instantiations of
1519    // function templates since they do not appear in the user code. The
1520    // following code optionally traverses them.
1521    //
1522    // We only traverse the function instantiations when we see the canonical
1523    // declaration of the template, to ensure we only visit them once.
1524    if (getDerived().shouldVisitTemplateInstantiations() &&
1525        D == D->getCanonicalDecl())
1526      TRY_TO(TraverseFunctionInstantiations(D));
1527  })
1528
1529DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1530    // D is the "T" in something like
1531    //   template <template <typename> class T> class container { };
1532    TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1533    if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
1534      TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1535    }
1536    TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1537  })
1538
1539DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1540    // D is the "T" in something like "template<typename T> class vector;"
1541    if (D->getTypeForDecl())
1542      TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1543    if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1544      TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1545  })
1546
1547DEF_TRAVERSE_DECL(TypedefDecl, {
1548    TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1549    // We shouldn't traverse D->getTypeForDecl(); it's a result of
1550    // declaring the typedef, not something that was written in the
1551    // source.
1552  })
1553
1554DEF_TRAVERSE_DECL(TypeAliasDecl, {
1555    TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1556    // We shouldn't traverse D->getTypeForDecl(); it's a result of
1557    // declaring the type alias, not something that was written in the
1558    // source.
1559  })
1560
1561DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1562    TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1563    TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1564  })
1565
1566DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1567    // A dependent using declaration which was marked with 'typename'.
1568    //   template<class T> class A : public B<T> { using typename B<T>::foo; };
1569    TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1570    // We shouldn't traverse D->getTypeForDecl(); it's a result of
1571    // declaring the type, not something that was written in the
1572    // source.
1573  })
1574
1575DEF_TRAVERSE_DECL(EnumDecl, {
1576    if (D->getTypeForDecl())
1577      TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1578
1579    TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1580    // The enumerators are already traversed by
1581    // decls_begin()/decls_end().
1582  })
1583
1584
1585// Helper methods for RecordDecl and its children.
1586template<typename Derived>
1587bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(
1588    RecordDecl *D) {
1589  // We shouldn't traverse D->getTypeForDecl(); it's a result of
1590  // declaring the type, not something that was written in the source.
1591
1592  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1593  return true;
1594}
1595
1596template<typename Derived>
1597bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(
1598    CXXRecordDecl *D) {
1599  if (!TraverseRecordHelper(D))
1600    return false;
1601  if (D->isCompleteDefinition()) {
1602    for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1603                                            E = D->bases_end();
1604         I != E; ++I) {
1605      TRY_TO(TraverseTypeLoc(I->getTypeSourceInfo()->getTypeLoc()));
1606    }
1607    // We don't traverse the friends or the conversions, as they are
1608    // already in decls_begin()/decls_end().
1609  }
1610  return true;
1611}
1612
1613DEF_TRAVERSE_DECL(RecordDecl, {
1614    TRY_TO(TraverseRecordHelper(D));
1615  })
1616
1617DEF_TRAVERSE_DECL(CXXRecordDecl, {
1618    TRY_TO(TraverseCXXRecordHelper(D));
1619  })
1620
1621DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, {
1622    // For implicit instantiations ("set<int> x;"), we don't want to
1623    // recurse at all, since the instatiated class isn't written in
1624    // the source code anywhere.  (Note the instatiated *type* --
1625    // set<int> -- is written, and will still get a callback of
1626    // TemplateSpecializationType).  For explicit instantiations
1627    // ("template set<int>;"), we do need a callback, since this
1628    // is the only callback that's made for this instantiation.
1629    // We use getTypeAsWritten() to distinguish.
1630    if (TypeSourceInfo *TSI = D->getTypeAsWritten())
1631      TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1632
1633    if (!getDerived().shouldVisitTemplateInstantiations() &&
1634        D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1635      // Returning from here skips traversing the
1636      // declaration context of the ClassTemplateSpecializationDecl
1637      // (embedded in the DEF_TRAVERSE_DECL() macro)
1638      // which contains the instantiated members of the class.
1639      return true;
1640  })
1641
1642template <typename Derived>
1643bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1644    const TemplateArgumentLoc *TAL, unsigned Count) {
1645  for (unsigned I = 0; I < Count; ++I) {
1646    TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1647  }
1648  return true;
1649}
1650
1651DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, {
1652    // The partial specialization.
1653    if (TemplateParameterList *TPL = D->getTemplateParameters()) {
1654      for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1655           I != E; ++I) {
1656        TRY_TO(TraverseDecl(*I));
1657      }
1658    }
1659    // The args that remains unspecialized.
1660    TRY_TO(TraverseTemplateArgumentLocsHelper(
1661        D->getTemplateArgsAsWritten(), D->getNumTemplateArgsAsWritten()));
1662
1663    // Don't need the ClassTemplatePartialSpecializationHelper, even
1664    // though that's our parent class -- we already visit all the
1665    // template args here.
1666    TRY_TO(TraverseCXXRecordHelper(D));
1667
1668    // Instantiations will have been visited with the primary template.
1669  })
1670
1671DEF_TRAVERSE_DECL(EnumConstantDecl, {
1672    TRY_TO(TraverseStmt(D->getInitExpr()));
1673  })
1674
1675DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1676    // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1677    //    template <class T> Class A : public Base<T> { using Base<T>::foo; };
1678    TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1679    TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1680  })
1681
1682DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1683
1684template<typename Derived>
1685bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1686  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1687  if (D->getTypeSourceInfo())
1688    TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1689  else
1690    TRY_TO(TraverseType(D->getType()));
1691  return true;
1692}
1693
1694DEF_TRAVERSE_DECL(MSPropertyDecl, {
1695    TRY_TO(TraverseDeclaratorHelper(D));
1696  })
1697
1698DEF_TRAVERSE_DECL(FieldDecl, {
1699    TRY_TO(TraverseDeclaratorHelper(D));
1700    if (D->isBitField())
1701      TRY_TO(TraverseStmt(D->getBitWidth()));
1702    else if (D->hasInClassInitializer())
1703      TRY_TO(TraverseStmt(D->getInClassInitializer()));
1704  })
1705
1706DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1707    TRY_TO(TraverseDeclaratorHelper(D));
1708    if (D->isBitField())
1709      TRY_TO(TraverseStmt(D->getBitWidth()));
1710    // FIXME: implement the rest.
1711  })
1712
1713DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1714    TRY_TO(TraverseDeclaratorHelper(D));
1715    if (D->isBitField())
1716      TRY_TO(TraverseStmt(D->getBitWidth()));
1717    // FIXME: implement the rest.
1718  })
1719
1720template<typename Derived>
1721bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1722  TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1723  TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1724
1725  // If we're an explicit template specialization, iterate over the
1726  // template args that were explicitly specified.  If we were doing
1727  // this in typing order, we'd do it between the return type and
1728  // the function args, but both are handled by the FunctionTypeLoc
1729  // above, so we have to choose one side.  I've decided to do before.
1730  if (const FunctionTemplateSpecializationInfo *FTSI =
1731      D->getTemplateSpecializationInfo()) {
1732    if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1733        FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1734      // A specialization might not have explicit template arguments if it has
1735      // a templated return type and concrete arguments.
1736      if (const ASTTemplateArgumentListInfo *TALI =
1737          FTSI->TemplateArgumentsAsWritten) {
1738        TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1739                                                  TALI->NumTemplateArgs));
1740      }
1741    }
1742  }
1743
1744  // Visit the function type itself, which can be either
1745  // FunctionNoProtoType or FunctionProtoType, or a typedef.  This
1746  // also covers the return type and the function parameters,
1747  // including exception specifications.
1748  if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) {
1749    TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1750  }
1751
1752  if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1753    // Constructor initializers.
1754    for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
1755                                           E = Ctor->init_end();
1756         I != E; ++I) {
1757      TRY_TO(TraverseConstructorInitializer(*I));
1758    }
1759  }
1760
1761  if (D->isThisDeclarationADefinition()) {
1762    TRY_TO(TraverseStmt(D->getBody()));  // Function body.
1763  }
1764  return true;
1765}
1766
1767DEF_TRAVERSE_DECL(FunctionDecl, {
1768    // We skip decls_begin/decls_end, which are already covered by
1769    // TraverseFunctionHelper().
1770    return TraverseFunctionHelper(D);
1771  })
1772
1773DEF_TRAVERSE_DECL(CXXMethodDecl, {
1774    // We skip decls_begin/decls_end, which are already covered by
1775    // TraverseFunctionHelper().
1776    return TraverseFunctionHelper(D);
1777  })
1778
1779DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1780    // We skip decls_begin/decls_end, which are already covered by
1781    // TraverseFunctionHelper().
1782    return TraverseFunctionHelper(D);
1783  })
1784
1785// CXXConversionDecl is the declaration of a type conversion operator.
1786// It's not a cast expression.
1787DEF_TRAVERSE_DECL(CXXConversionDecl, {
1788    // We skip decls_begin/decls_end, which are already covered by
1789    // TraverseFunctionHelper().
1790    return TraverseFunctionHelper(D);
1791  })
1792
1793DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1794    // We skip decls_begin/decls_end, which are already covered by
1795    // TraverseFunctionHelper().
1796    return TraverseFunctionHelper(D);
1797  })
1798
1799template<typename Derived>
1800bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1801  TRY_TO(TraverseDeclaratorHelper(D));
1802  // Default params are taken care of when we traverse the ParmVarDecl.
1803  if (!isa<ParmVarDecl>(D) &&
1804      (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode()))
1805    TRY_TO(TraverseStmt(D->getInit()));
1806  return true;
1807}
1808
1809DEF_TRAVERSE_DECL(VarDecl, {
1810    TRY_TO(TraverseVarHelper(D));
1811  })
1812
1813DEF_TRAVERSE_DECL(ImplicitParamDecl, {
1814    TRY_TO(TraverseVarHelper(D));
1815  })
1816
1817DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1818    // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1819    TRY_TO(TraverseDeclaratorHelper(D));
1820    if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
1821      TRY_TO(TraverseStmt(D->getDefaultArgument()));
1822  })
1823
1824DEF_TRAVERSE_DECL(ParmVarDecl, {
1825    TRY_TO(TraverseVarHelper(D));
1826
1827    if (D->hasDefaultArg() &&
1828        D->hasUninstantiatedDefaultArg() &&
1829        !D->hasUnparsedDefaultArg())
1830      TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1831
1832    if (D->hasDefaultArg() &&
1833        !D->hasUninstantiatedDefaultArg() &&
1834        !D->hasUnparsedDefaultArg())
1835      TRY_TO(TraverseStmt(D->getDefaultArg()));
1836  })
1837
1838#undef DEF_TRAVERSE_DECL
1839
1840// ----------------- Stmt traversal -----------------
1841//
1842// For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1843// over the children defined in children() (every stmt defines these,
1844// though sometimes the range is empty).  Each individual Traverse*
1845// method only needs to worry about children other than those.  To see
1846// what children() does for a given class, see, e.g.,
1847//   http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1848
1849// This macro makes available a variable S, the passed-in stmt.
1850#define DEF_TRAVERSE_STMT(STMT, CODE)                                   \
1851template<typename Derived>                                              \
1852bool RecursiveASTVisitor<Derived>::Traverse##STMT (STMT *S) {           \
1853  TRY_TO(WalkUpFrom##STMT(S));                                          \
1854  { CODE; }                                                             \
1855  for (Stmt::child_range range = S->children(); range; ++range) {       \
1856    TRY_TO(TraverseStmt(*range));                                       \
1857  }                                                                     \
1858  return true;                                                          \
1859}
1860
1861DEF_TRAVERSE_STMT(GCCAsmStmt, {
1862    TRY_TO(TraverseStmt(S->getAsmString()));
1863    for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
1864      TRY_TO(TraverseStmt(S->getInputConstraintLiteral(I)));
1865    }
1866    for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
1867      TRY_TO(TraverseStmt(S->getOutputConstraintLiteral(I)));
1868    }
1869    for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
1870      TRY_TO(TraverseStmt(S->getClobberStringLiteral(I)));
1871    }
1872    // children() iterates over inputExpr and outputExpr.
1873  })
1874
1875DEF_TRAVERSE_STMT(MSAsmStmt, {
1876    // FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc.  Once
1877    // added this needs to be implemented.
1878  })
1879
1880DEF_TRAVERSE_STMT(CXXCatchStmt, {
1881    TRY_TO(TraverseDecl(S->getExceptionDecl()));
1882    // children() iterates over the handler block.
1883  })
1884
1885DEF_TRAVERSE_STMT(DeclStmt, {
1886    for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
1887         I != E; ++I) {
1888      TRY_TO(TraverseDecl(*I));
1889    }
1890    // Suppress the default iteration over children() by
1891    // returning.  Here's why: A DeclStmt looks like 'type var [=
1892    // initializer]'.  The decls above already traverse over the
1893    // initializers, so we don't have to do it again (which
1894    // children() would do).
1895    return true;
1896  })
1897
1898
1899// These non-expr stmts (most of them), do not need any action except
1900// iterating over the children.
1901DEF_TRAVERSE_STMT(BreakStmt, { })
1902DEF_TRAVERSE_STMT(CXXTryStmt, { })
1903DEF_TRAVERSE_STMT(CaseStmt, { })
1904DEF_TRAVERSE_STMT(CompoundStmt, { })
1905DEF_TRAVERSE_STMT(ContinueStmt, { })
1906DEF_TRAVERSE_STMT(DefaultStmt, { })
1907DEF_TRAVERSE_STMT(DoStmt, { })
1908DEF_TRAVERSE_STMT(ForStmt, { })
1909DEF_TRAVERSE_STMT(GotoStmt, { })
1910DEF_TRAVERSE_STMT(IfStmt, { })
1911DEF_TRAVERSE_STMT(IndirectGotoStmt, { })
1912DEF_TRAVERSE_STMT(LabelStmt, { })
1913DEF_TRAVERSE_STMT(AttributedStmt, { })
1914DEF_TRAVERSE_STMT(NullStmt, { })
1915DEF_TRAVERSE_STMT(ObjCAtCatchStmt, { })
1916DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, { })
1917DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, { })
1918DEF_TRAVERSE_STMT(ObjCAtThrowStmt, { })
1919DEF_TRAVERSE_STMT(ObjCAtTryStmt, { })
1920DEF_TRAVERSE_STMT(ObjCForCollectionStmt, { })
1921DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, { })
1922DEF_TRAVERSE_STMT(CXXForRangeStmt, {
1923  if (!getDerived().shouldVisitImplicitCode()) {
1924    TRY_TO(TraverseStmt(S->getLoopVarStmt()));
1925    TRY_TO(TraverseStmt(S->getRangeInit()));
1926    TRY_TO(TraverseStmt(S->getBody()));
1927    // Visit everything else only if shouldVisitImplicitCode().
1928    return true;
1929  }
1930})
1931DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1932    TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1933    TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1934})
1935DEF_TRAVERSE_STMT(ReturnStmt, { })
1936DEF_TRAVERSE_STMT(SwitchStmt, { })
1937DEF_TRAVERSE_STMT(WhileStmt, { })
1938
1939
1940DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1941    TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1942    TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1943    if (S->hasExplicitTemplateArgs()) {
1944      TRY_TO(TraverseTemplateArgumentLocsHelper(
1945          S->getTemplateArgs(), S->getNumTemplateArgs()));
1946    }
1947  })
1948
1949DEF_TRAVERSE_STMT(DeclRefExpr, {
1950    TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1951    TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1952    TRY_TO(TraverseTemplateArgumentLocsHelper(
1953        S->getTemplateArgs(), S->getNumTemplateArgs()));
1954  })
1955
1956DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1957    TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1958    TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1959    if (S->hasExplicitTemplateArgs()) {
1960      TRY_TO(TraverseTemplateArgumentLocsHelper(
1961          S->getExplicitTemplateArgs().getTemplateArgs(),
1962          S->getNumTemplateArgs()));
1963    }
1964  })
1965
1966DEF_TRAVERSE_STMT(MemberExpr, {
1967    TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1968    TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1969    TRY_TO(TraverseTemplateArgumentLocsHelper(
1970        S->getTemplateArgs(), S->getNumTemplateArgs()));
1971  })
1972
1973DEF_TRAVERSE_STMT(ImplicitCastExpr, {
1974    // We don't traverse the cast type, as it's not written in the
1975    // source code.
1976  })
1977
1978DEF_TRAVERSE_STMT(CStyleCastExpr, {
1979    TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1980  })
1981
1982DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
1983    TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1984  })
1985
1986DEF_TRAVERSE_STMT(CXXConstCastExpr, {
1987    TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1988  })
1989
1990DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
1991    TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1992  })
1993
1994DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
1995    TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1996  })
1997
1998DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
1999    TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2000  })
2001
2002// InitListExpr is a tricky one, because we want to do all our work on
2003// the syntactic form of the listexpr, but this method takes the
2004// semantic form by default.  We can't use the macro helper because it
2005// calls WalkUp*() on the semantic form, before our code can convert
2006// to the syntactic form.
2007template<typename Derived>
2008bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
2009  if (InitListExpr *Syn = S->getSyntacticForm())
2010    S = Syn;
2011  TRY_TO(WalkUpFromInitListExpr(S));
2012  // All we need are the default actions.  FIXME: use a helper function.
2013  for (Stmt::child_range range = S->children(); range; ++range) {
2014    TRY_TO(TraverseStmt(*range));
2015  }
2016  return true;
2017}
2018
2019// GenericSelectionExpr is a special case because the types and expressions
2020// are interleaved.  We also need to watch out for null types (default
2021// generic associations).
2022template<typename Derived>
2023bool RecursiveASTVisitor<Derived>::
2024TraverseGenericSelectionExpr(GenericSelectionExpr *S) {
2025  TRY_TO(WalkUpFromGenericSelectionExpr(S));
2026  TRY_TO(TraverseStmt(S->getControllingExpr()));
2027  for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
2028    if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
2029      TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
2030    TRY_TO(TraverseStmt(S->getAssocExpr(i)));
2031  }
2032  return true;
2033}
2034
2035// PseudoObjectExpr is a special case because of the wierdness with
2036// syntactic expressions and opaque values.
2037template<typename Derived>
2038bool RecursiveASTVisitor<Derived>::
2039TraversePseudoObjectExpr(PseudoObjectExpr *S) {
2040  TRY_TO(WalkUpFromPseudoObjectExpr(S));
2041  TRY_TO(TraverseStmt(S->getSyntacticForm()));
2042  for (PseudoObjectExpr::semantics_iterator
2043         i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i) {
2044    Expr *sub = *i;
2045    if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
2046      sub = OVE->getSourceExpr();
2047    TRY_TO(TraverseStmt(sub));
2048  }
2049  return true;
2050}
2051
2052DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
2053    // This is called for code like 'return T()' where T is a built-in
2054    // (i.e. non-class) type.
2055    TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2056  })
2057
2058DEF_TRAVERSE_STMT(CXXNewExpr, {
2059  // The child-iterator will pick up the other arguments.
2060  TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
2061  })
2062
2063DEF_TRAVERSE_STMT(OffsetOfExpr, {
2064    // The child-iterator will pick up the expression representing
2065    // the field.
2066    // FIMXE: for code like offsetof(Foo, a.b.c), should we get
2067    // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
2068    TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2069  })
2070
2071DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
2072    // The child-iterator will pick up the arg if it's an expression,
2073    // but not if it's a type.
2074    if (S->isArgumentType())
2075      TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
2076  })
2077
2078DEF_TRAVERSE_STMT(CXXTypeidExpr, {
2079    // The child-iterator will pick up the arg if it's an expression,
2080    // but not if it's a type.
2081    if (S->isTypeOperand())
2082      TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2083  })
2084
2085DEF_TRAVERSE_STMT(MSPropertyRefExpr, {
2086  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2087})
2088
2089DEF_TRAVERSE_STMT(CXXUuidofExpr, {
2090    // The child-iterator will pick up the arg if it's an expression,
2091    // but not if it's a type.
2092    if (S->isTypeOperand())
2093      TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
2094  })
2095
2096DEF_TRAVERSE_STMT(UnaryTypeTraitExpr, {
2097    TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2098  })
2099
2100DEF_TRAVERSE_STMT(BinaryTypeTraitExpr, {
2101    TRY_TO(TraverseTypeLoc(S->getLhsTypeSourceInfo()->getTypeLoc()));
2102    TRY_TO(TraverseTypeLoc(S->getRhsTypeSourceInfo()->getTypeLoc()));
2103  })
2104
2105DEF_TRAVERSE_STMT(TypeTraitExpr, {
2106  for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
2107    TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
2108})
2109
2110DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2111    TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2112  })
2113
2114DEF_TRAVERSE_STMT(ExpressionTraitExpr, {
2115    TRY_TO(TraverseStmt(S->getQueriedExpression()));
2116  })
2117
2118DEF_TRAVERSE_STMT(VAArgExpr, {
2119    // The child-iterator will pick up the expression argument.
2120    TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2121  })
2122
2123DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2124    // This is called for code like 'return T()' where T is a class type.
2125    TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2126  })
2127
2128// Walk only the visible parts of lambda expressions.
2129template<typename Derived>
2130bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2131  TRY_TO(WalkUpFromLambdaExpr(S));
2132
2133  for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2134                                 CEnd = S->explicit_capture_end();
2135       C != CEnd; ++C) {
2136    TRY_TO(TraverseLambdaCapture(S, C));
2137  }
2138
2139  if (S->hasExplicitParameters() || S->hasExplicitResultType()) {
2140    TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2141    if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2142      // Visit the whole type.
2143      TRY_TO(TraverseTypeLoc(TL));
2144    } else if (FunctionProtoTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) {
2145      if (S->hasExplicitParameters()) {
2146        // Visit parameters.
2147        for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) {
2148          TRY_TO(TraverseDecl(Proto.getArg(I)));
2149        }
2150      } else {
2151        TRY_TO(TraverseTypeLoc(Proto.getResultLoc()));
2152      }
2153    }
2154  }
2155
2156  TRY_TO(TraverseStmt(S->getBody()));
2157  return true;
2158}
2159
2160DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2161    // This is called for code like 'T()', where T is a template argument.
2162    TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2163  })
2164
2165// These expressions all might take explicit template arguments.
2166// We traverse those if so.  FIXME: implement these.
2167DEF_TRAVERSE_STMT(CXXConstructExpr, { })
2168DEF_TRAVERSE_STMT(CallExpr, { })
2169DEF_TRAVERSE_STMT(CXXMemberCallExpr, { })
2170
2171// These exprs (most of them), do not need any action except iterating
2172// over the children.
2173DEF_TRAVERSE_STMT(AddrLabelExpr, { })
2174DEF_TRAVERSE_STMT(ArraySubscriptExpr, { })
2175DEF_TRAVERSE_STMT(BlockExpr, {
2176  TRY_TO(TraverseDecl(S->getBlockDecl()));
2177  return true; // no child statements to loop through.
2178})
2179DEF_TRAVERSE_STMT(ChooseExpr, { })
2180DEF_TRAVERSE_STMT(CompoundLiteralExpr, {
2181  TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2182})
2183DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, { })
2184DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, { })
2185DEF_TRAVERSE_STMT(CXXDefaultArgExpr, { })
2186DEF_TRAVERSE_STMT(CXXDefaultInitExpr, { })
2187DEF_TRAVERSE_STMT(CXXDeleteExpr, { })
2188DEF_TRAVERSE_STMT(ExprWithCleanups, { })
2189DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, { })
2190DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, { })
2191DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2192  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2193  if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2194    TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2195  if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2196    TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2197})
2198DEF_TRAVERSE_STMT(CXXThisExpr, { })
2199DEF_TRAVERSE_STMT(CXXThrowExpr, { })
2200DEF_TRAVERSE_STMT(UserDefinedLiteral, { })
2201DEF_TRAVERSE_STMT(DesignatedInitExpr, { })
2202DEF_TRAVERSE_STMT(ExtVectorElementExpr, { })
2203DEF_TRAVERSE_STMT(GNUNullExpr, { })
2204DEF_TRAVERSE_STMT(ImplicitValueInitExpr, { })
2205DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, { })
2206DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2207  if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2208    TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2209})
2210DEF_TRAVERSE_STMT(ObjCIsaExpr, { })
2211DEF_TRAVERSE_STMT(ObjCIvarRefExpr, { })
2212DEF_TRAVERSE_STMT(ObjCMessageExpr, {
2213  if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo())
2214    TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2215})
2216DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, { })
2217DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, { })
2218DEF_TRAVERSE_STMT(ObjCProtocolExpr, { })
2219DEF_TRAVERSE_STMT(ObjCSelectorExpr, { })
2220DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, { })
2221DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2222  TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2223})
2224DEF_TRAVERSE_STMT(ParenExpr, { })
2225DEF_TRAVERSE_STMT(ParenListExpr, { })
2226DEF_TRAVERSE_STMT(PredefinedExpr, { })
2227DEF_TRAVERSE_STMT(ShuffleVectorExpr, { })
2228DEF_TRAVERSE_STMT(StmtExpr, { })
2229DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2230  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2231  if (S->hasExplicitTemplateArgs()) {
2232    TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2233                                              S->getNumTemplateArgs()));
2234  }
2235})
2236
2237DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2238  TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2239  if (S->hasExplicitTemplateArgs()) {
2240    TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2241                                              S->getNumTemplateArgs()));
2242  }
2243})
2244
2245DEF_TRAVERSE_STMT(SEHTryStmt, {})
2246DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2247DEF_TRAVERSE_STMT(SEHFinallyStmt,{})
2248DEF_TRAVERSE_STMT(CapturedStmt, {
2249  TRY_TO(TraverseDecl(S->getCapturedDecl()));
2250})
2251
2252DEF_TRAVERSE_STMT(CXXOperatorCallExpr, { })
2253DEF_TRAVERSE_STMT(OpaqueValueExpr, { })
2254DEF_TRAVERSE_STMT(CUDAKernelCallExpr, { })
2255
2256// These operators (all of them) do not need any action except
2257// iterating over the children.
2258DEF_TRAVERSE_STMT(BinaryConditionalOperator, { })
2259DEF_TRAVERSE_STMT(ConditionalOperator, { })
2260DEF_TRAVERSE_STMT(UnaryOperator, { })
2261DEF_TRAVERSE_STMT(BinaryOperator, { })
2262DEF_TRAVERSE_STMT(CompoundAssignOperator, { })
2263DEF_TRAVERSE_STMT(CXXNoexceptExpr, { })
2264DEF_TRAVERSE_STMT(PackExpansionExpr, { })
2265DEF_TRAVERSE_STMT(SizeOfPackExpr, { })
2266DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, { })
2267DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, { })
2268DEF_TRAVERSE_STMT(FunctionParmPackExpr, { })
2269DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, { })
2270DEF_TRAVERSE_STMT(AtomicExpr, { })
2271
2272// These literals (all of them) do not need any action.
2273DEF_TRAVERSE_STMT(IntegerLiteral, { })
2274DEF_TRAVERSE_STMT(CharacterLiteral, { })
2275DEF_TRAVERSE_STMT(FloatingLiteral, { })
2276DEF_TRAVERSE_STMT(ImaginaryLiteral, { })
2277DEF_TRAVERSE_STMT(StringLiteral, { })
2278DEF_TRAVERSE_STMT(ObjCStringLiteral, { })
2279DEF_TRAVERSE_STMT(ObjCBoxedExpr, { })
2280DEF_TRAVERSE_STMT(ObjCArrayLiteral, { })
2281DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, { })
2282
2283// Traverse OpenCL: AsType, Convert.
2284DEF_TRAVERSE_STMT(AsTypeExpr, { })
2285
2286// FIXME: look at the following tricky-seeming exprs to see if we
2287// need to recurse on anything.  These are ones that have methods
2288// returning decls or qualtypes or nestednamespecifier -- though I'm
2289// not sure if they own them -- or just seemed very complicated, or
2290// had lots of sub-types to explore.
2291//
2292// VisitOverloadExpr and its children: recurse on template args? etc?
2293
2294// FIXME: go through all the stmts and exprs again, and see which of them
2295// create new types, and recurse on the types (TypeLocs?) of those.
2296// Candidates:
2297//
2298//    http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2299//    http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2300//    http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2301//    Every class that has getQualifier.
2302
2303#undef DEF_TRAVERSE_STMT
2304
2305#undef TRY_TO
2306
2307} // end namespace clang
2308
2309#endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
2310