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