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