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