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