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