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