blob: 9788c956da029df1e96d72c54d68eb01ac605173 [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: \
David Blaikie39e6ab42013-02-18 22:06:02 +0000538 return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>());
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +0000539#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:
Eli Friedmand7a6b162012-09-26 02:36:12 +0000660 case TemplateArgument::NullPtr:
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +0000661 return true;
662
663 case TemplateArgument::Type:
664 return getDerived().TraverseType(Arg.getAsType());
665
666 case TemplateArgument::Template:
667 case TemplateArgument::TemplateExpansion:
668 return getDerived().TraverseTemplateName(
669 Arg.getAsTemplateOrTemplatePattern());
670
671 case TemplateArgument::Expression:
672 return getDerived().TraverseStmt(Arg.getAsExpr());
673
674 case TemplateArgument::Pack:
675 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
676 Arg.pack_size());
677 }
678
679 return true;
680}
681
682// FIXME: no template name location?
683// FIXME: no source locations for a template argument pack?
684template<typename Derived>
685bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc(
686 const TemplateArgumentLoc &ArgLoc) {
687 const TemplateArgument &Arg = ArgLoc.getArgument();
688
689 switch (Arg.getKind()) {
690 case TemplateArgument::Null:
691 case TemplateArgument::Declaration:
692 case TemplateArgument::Integral:
Eli Friedmand7a6b162012-09-26 02:36:12 +0000693 case TemplateArgument::NullPtr:
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +0000694 return true;
695
696 case TemplateArgument::Type: {
697 // FIXME: how can TSI ever be NULL?
698 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo())
699 return getDerived().TraverseTypeLoc(TSI->getTypeLoc());
700 else
701 return getDerived().TraverseType(Arg.getAsType());
702 }
703
704 case TemplateArgument::Template:
705 case TemplateArgument::TemplateExpansion:
706 if (ArgLoc.getTemplateQualifierLoc())
707 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc(
708 ArgLoc.getTemplateQualifierLoc()));
709 return getDerived().TraverseTemplateName(
710 Arg.getAsTemplateOrTemplatePattern());
711
712 case TemplateArgument::Expression:
713 return getDerived().TraverseStmt(ArgLoc.getSourceExpression());
714
715 case TemplateArgument::Pack:
716 return getDerived().TraverseTemplateArguments(Arg.pack_begin(),
717 Arg.pack_size());
718 }
719
720 return true;
721}
722
723template<typename Derived>
724bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments(
725 const TemplateArgument *Args,
726 unsigned NumArgs) {
727 for (unsigned I = 0; I != NumArgs; ++I) {
728 TRY_TO(TraverseTemplateArgument(Args[I]));
729 }
730
731 return true;
732}
733
734template<typename Derived>
735bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
736 CXXCtorInitializer *Init) {
737 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo())
738 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
739
740 if (Init->isWritten())
741 TRY_TO(TraverseStmt(Init->getInit()));
742 return true;
743}
744
745template<typename Derived>
746bool RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr::Capture C){
747 return true;
748}
749
750// ----------------- Type traversal -----------------
751
752// This macro makes available a variable T, the passed-in type.
753#define DEF_TRAVERSE_TYPE(TYPE, CODE) \
754 template<typename Derived> \
755 bool RecursiveASTVisitor<Derived>::Traverse##TYPE (TYPE *T) { \
756 TRY_TO(WalkUpFrom##TYPE (T)); \
757 { CODE; } \
758 return true; \
759 }
760
761DEF_TRAVERSE_TYPE(BuiltinType, { })
762
763DEF_TRAVERSE_TYPE(ComplexType, {
764 TRY_TO(TraverseType(T->getElementType()));
765 })
766
767DEF_TRAVERSE_TYPE(PointerType, {
768 TRY_TO(TraverseType(T->getPointeeType()));
769 })
770
771DEF_TRAVERSE_TYPE(BlockPointerType, {
772 TRY_TO(TraverseType(T->getPointeeType()));
773 })
774
775DEF_TRAVERSE_TYPE(LValueReferenceType, {
776 TRY_TO(TraverseType(T->getPointeeType()));
777 })
778
779DEF_TRAVERSE_TYPE(RValueReferenceType, {
780 TRY_TO(TraverseType(T->getPointeeType()));
781 })
782
783DEF_TRAVERSE_TYPE(MemberPointerType, {
784 TRY_TO(TraverseType(QualType(T->getClass(), 0)));
785 TRY_TO(TraverseType(T->getPointeeType()));
786 })
787
788DEF_TRAVERSE_TYPE(ConstantArrayType, {
789 TRY_TO(TraverseType(T->getElementType()));
790 })
791
792DEF_TRAVERSE_TYPE(IncompleteArrayType, {
793 TRY_TO(TraverseType(T->getElementType()));
794 })
795
796DEF_TRAVERSE_TYPE(VariableArrayType, {
797 TRY_TO(TraverseType(T->getElementType()));
798 TRY_TO(TraverseStmt(T->getSizeExpr()));
799 })
800
801DEF_TRAVERSE_TYPE(DependentSizedArrayType, {
802 TRY_TO(TraverseType(T->getElementType()));
803 if (T->getSizeExpr())
804 TRY_TO(TraverseStmt(T->getSizeExpr()));
805 })
806
807DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, {
808 if (T->getSizeExpr())
809 TRY_TO(TraverseStmt(T->getSizeExpr()));
810 TRY_TO(TraverseType(T->getElementType()));
811 })
812
813DEF_TRAVERSE_TYPE(VectorType, {
814 TRY_TO(TraverseType(T->getElementType()));
815 })
816
817DEF_TRAVERSE_TYPE(ExtVectorType, {
818 TRY_TO(TraverseType(T->getElementType()));
819 })
820
821DEF_TRAVERSE_TYPE(FunctionNoProtoType, {
822 TRY_TO(TraverseType(T->getResultType()));
823 })
824
825DEF_TRAVERSE_TYPE(FunctionProtoType, {
826 TRY_TO(TraverseType(T->getResultType()));
827
828 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
829 AEnd = T->arg_type_end();
830 A != AEnd; ++A) {
831 TRY_TO(TraverseType(*A));
832 }
833
834 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
835 EEnd = T->exception_end();
836 E != EEnd; ++E) {
837 TRY_TO(TraverseType(*E));
838 }
839 })
840
841DEF_TRAVERSE_TYPE(UnresolvedUsingType, { })
842DEF_TRAVERSE_TYPE(TypedefType, { })
843
844DEF_TRAVERSE_TYPE(TypeOfExprType, {
845 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
846 })
847
848DEF_TRAVERSE_TYPE(TypeOfType, {
849 TRY_TO(TraverseType(T->getUnderlyingType()));
850 })
851
852DEF_TRAVERSE_TYPE(DecltypeType, {
853 TRY_TO(TraverseStmt(T->getUnderlyingExpr()));
854 })
855
856DEF_TRAVERSE_TYPE(UnaryTransformType, {
857 TRY_TO(TraverseType(T->getBaseType()));
858 TRY_TO(TraverseType(T->getUnderlyingType()));
859 })
860
861DEF_TRAVERSE_TYPE(AutoType, {
862 TRY_TO(TraverseType(T->getDeducedType()));
863 })
864
865DEF_TRAVERSE_TYPE(RecordType, { })
866DEF_TRAVERSE_TYPE(EnumType, { })
867DEF_TRAVERSE_TYPE(TemplateTypeParmType, { })
868DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, { })
869DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, { })
870
871DEF_TRAVERSE_TYPE(TemplateSpecializationType, {
872 TRY_TO(TraverseTemplateName(T->getTemplateName()));
873 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
874 })
875
876DEF_TRAVERSE_TYPE(InjectedClassNameType, { })
877
878DEF_TRAVERSE_TYPE(AttributedType, {
879 TRY_TO(TraverseType(T->getModifiedType()));
880 })
881
882DEF_TRAVERSE_TYPE(ParenType, {
883 TRY_TO(TraverseType(T->getInnerType()));
884 })
885
886DEF_TRAVERSE_TYPE(ElaboratedType, {
887 if (T->getQualifier()) {
888 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
889 }
890 TRY_TO(TraverseType(T->getNamedType()));
891 })
892
893DEF_TRAVERSE_TYPE(DependentNameType, {
894 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
895 })
896
897DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, {
898 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier()));
899 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs()));
900 })
901
902DEF_TRAVERSE_TYPE(PackExpansionType, {
903 TRY_TO(TraverseType(T->getPattern()));
904 })
905
906DEF_TRAVERSE_TYPE(ObjCInterfaceType, { })
907
908DEF_TRAVERSE_TYPE(ObjCObjectType, {
909 // We have to watch out here because an ObjCInterfaceType's base
910 // type is itself.
911 if (T->getBaseType().getTypePtr() != T)
912 TRY_TO(TraverseType(T->getBaseType()));
913 })
914
915DEF_TRAVERSE_TYPE(ObjCObjectPointerType, {
916 TRY_TO(TraverseType(T->getPointeeType()));
917 })
918
919DEF_TRAVERSE_TYPE(AtomicType, {
920 TRY_TO(TraverseType(T->getValueType()));
921 })
922
923#undef DEF_TRAVERSE_TYPE
924
925// ----------------- TypeLoc traversal -----------------
926
927// This macro makes available a variable TL, the passed-in TypeLoc.
928// If requested, it calls WalkUpFrom* for the Type in the given TypeLoc,
929// in addition to WalkUpFrom* for the TypeLoc itself, such that existing
930// clients that override the WalkUpFrom*Type() and/or Visit*Type() methods
931// continue to work.
932#define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \
933 template<typename Derived> \
934 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \
935 if (getDerived().shouldWalkTypesOfTypeLocs()) \
936 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE*>(TL.getTypePtr()))); \
937 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \
938 { CODE; } \
939 return true; \
940 }
941
942template<typename Derived>
943bool RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(
944 QualifiedTypeLoc TL) {
945 // Move this over to the 'main' typeloc tree. Note that this is a
946 // move -- we pretend that we were really looking at the unqualified
947 // typeloc all along -- rather than a recursion, so we don't follow
948 // the normal CRTP plan of going through
949 // getDerived().TraverseTypeLoc. If we did, we'd be traversing
950 // twice for the same type (once as a QualifiedTypeLoc version of
951 // the type, once as an UnqualifiedTypeLoc version of the type),
952 // which in effect means we'd call VisitTypeLoc twice with the
953 // 'same' type. This solves that problem, at the cost of never
954 // seeing the qualified version of the type (unless the client
955 // subclasses TraverseQualifiedTypeLoc themselves). It's not a
956 // perfect solution. A perfect solution probably requires making
957 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a
958 // wrapper around Type* -- rather than being its own class in the
959 // type hierarchy.
960 return TraverseTypeLoc(TL.getUnqualifiedLoc());
961}
962
963DEF_TRAVERSE_TYPELOC(BuiltinType, { })
964
965// FIXME: ComplexTypeLoc is unfinished
966DEF_TRAVERSE_TYPELOC(ComplexType, {
967 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
968 })
969
970DEF_TRAVERSE_TYPELOC(PointerType, {
971 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
972 })
973
974DEF_TRAVERSE_TYPELOC(BlockPointerType, {
975 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
976 })
977
978DEF_TRAVERSE_TYPELOC(LValueReferenceType, {
979 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
980 })
981
982DEF_TRAVERSE_TYPELOC(RValueReferenceType, {
983 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
984 })
985
986// FIXME: location of base class?
987// We traverse this in the type case as well, but how is it not reached through
988// the pointee type?
989DEF_TRAVERSE_TYPELOC(MemberPointerType, {
990 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0)));
991 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
992 })
993
994template<typename Derived>
995bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) {
996 // This isn't available for ArrayType, but is for the ArrayTypeLoc.
997 TRY_TO(TraverseStmt(TL.getSizeExpr()));
998 return true;
999}
1000
1001DEF_TRAVERSE_TYPELOC(ConstantArrayType, {
1002 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1003 return TraverseArrayTypeLocHelper(TL);
1004 })
1005
1006DEF_TRAVERSE_TYPELOC(IncompleteArrayType, {
1007 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1008 return TraverseArrayTypeLocHelper(TL);
1009 })
1010
1011DEF_TRAVERSE_TYPELOC(VariableArrayType, {
1012 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1013 return TraverseArrayTypeLocHelper(TL);
1014 })
1015
1016DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, {
1017 TRY_TO(TraverseTypeLoc(TL.getElementLoc()));
1018 return TraverseArrayTypeLocHelper(TL);
1019 })
1020
1021// FIXME: order? why not size expr first?
1022// FIXME: base VectorTypeLoc is unfinished
1023DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, {
1024 if (TL.getTypePtr()->getSizeExpr())
1025 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr()));
1026 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1027 })
1028
1029// FIXME: VectorTypeLoc is unfinished
1030DEF_TRAVERSE_TYPELOC(VectorType, {
1031 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1032 })
1033
1034// FIXME: size and attributes
1035// FIXME: base VectorTypeLoc is unfinished
1036DEF_TRAVERSE_TYPELOC(ExtVectorType, {
1037 TRY_TO(TraverseType(TL.getTypePtr()->getElementType()));
1038 })
1039
1040DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, {
1041 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1042 })
1043
1044// FIXME: location of exception specifications (attributes?)
1045DEF_TRAVERSE_TYPELOC(FunctionProtoType, {
1046 TRY_TO(TraverseTypeLoc(TL.getResultLoc()));
1047
1048 const FunctionProtoType *T = TL.getTypePtr();
1049
1050 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1051 if (TL.getArg(I)) {
1052 TRY_TO(TraverseDecl(TL.getArg(I)));
1053 } else if (I < T->getNumArgs()) {
1054 TRY_TO(TraverseType(T->getArgType(I)));
1055 }
1056 }
1057
1058 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1059 EEnd = T->exception_end();
1060 E != EEnd; ++E) {
1061 TRY_TO(TraverseType(*E));
1062 }
1063 })
1064
1065DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, { })
1066DEF_TRAVERSE_TYPELOC(TypedefType, { })
1067
1068DEF_TRAVERSE_TYPELOC(TypeOfExprType, {
1069 TRY_TO(TraverseStmt(TL.getUnderlyingExpr()));
1070 })
1071
1072DEF_TRAVERSE_TYPELOC(TypeOfType, {
1073 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1074 })
1075
1076// FIXME: location of underlying expr
1077DEF_TRAVERSE_TYPELOC(DecltypeType, {
1078 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr()));
1079 })
1080
1081DEF_TRAVERSE_TYPELOC(UnaryTransformType, {
1082 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc()));
1083 })
1084
1085DEF_TRAVERSE_TYPELOC(AutoType, {
1086 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType()));
1087 })
1088
1089DEF_TRAVERSE_TYPELOC(RecordType, { })
1090DEF_TRAVERSE_TYPELOC(EnumType, { })
1091DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, { })
1092DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, { })
1093DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, { })
1094
1095// FIXME: use the loc for the template name?
1096DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, {
1097 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName()));
1098 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1099 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1100 }
1101 })
1102
1103DEF_TRAVERSE_TYPELOC(InjectedClassNameType, { })
1104
1105DEF_TRAVERSE_TYPELOC(ParenType, {
1106 TRY_TO(TraverseTypeLoc(TL.getInnerLoc()));
1107 })
1108
1109DEF_TRAVERSE_TYPELOC(AttributedType, {
1110 TRY_TO(TraverseTypeLoc(TL.getModifiedLoc()));
1111 })
1112
1113DEF_TRAVERSE_TYPELOC(ElaboratedType, {
1114 if (TL.getQualifierLoc()) {
1115 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1116 }
1117 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc()));
1118 })
1119
1120DEF_TRAVERSE_TYPELOC(DependentNameType, {
1121 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1122 })
1123
1124DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, {
1125 if (TL.getQualifierLoc()) {
1126 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()));
1127 }
1128
1129 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) {
1130 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I)));
1131 }
1132 })
1133
1134DEF_TRAVERSE_TYPELOC(PackExpansionType, {
1135 TRY_TO(TraverseTypeLoc(TL.getPatternLoc()));
1136 })
1137
1138DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, { })
1139
1140DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
1141 // We have to watch out here because an ObjCInterfaceType's base
1142 // type is itself.
1143 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
1144 TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
1145 })
1146
1147DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, {
1148 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc()));
1149 })
1150
1151DEF_TRAVERSE_TYPELOC(AtomicType, {
1152 TRY_TO(TraverseTypeLoc(TL.getValueLoc()));
1153 })
1154
1155#undef DEF_TRAVERSE_TYPELOC
1156
1157// ----------------- Decl traversal -----------------
1158//
1159// For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing
1160// the children that come from the DeclContext associated with it.
1161// Therefore each Traverse* only needs to worry about children other
1162// than those.
1163
1164template<typename Derived>
1165bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) {
1166 if (!DC)
1167 return true;
1168
1169 for (DeclContext::decl_iterator Child = DC->decls_begin(),
1170 ChildEnd = DC->decls_end();
1171 Child != ChildEnd; ++Child) {
1172 // BlockDecls are traversed through BlockExprs.
1173 if (!isa<BlockDecl>(*Child))
1174 TRY_TO(TraverseDecl(*Child));
1175 }
1176
1177 return true;
1178}
1179
1180// This macro makes available a variable D, the passed-in decl.
1181#define DEF_TRAVERSE_DECL(DECL, CODE) \
1182template<typename Derived> \
1183bool RecursiveASTVisitor<Derived>::Traverse##DECL (DECL *D) { \
1184 TRY_TO(WalkUpFrom##DECL (D)); \
1185 { CODE; } \
1186 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \
1187 return true; \
1188}
1189
1190DEF_TRAVERSE_DECL(AccessSpecDecl, { })
1191
1192DEF_TRAVERSE_DECL(BlockDecl, {
Argyrios Kyrtzidis87014f32012-05-16 19:22:47 +00001193 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten())
1194 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001195 TRY_TO(TraverseStmt(D->getBody()));
1196 // This return statement makes sure the traversal of nodes in
1197 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1198 // is skipped - don't remove it.
1199 return true;
1200 })
1201
Michael Han684aa732013-02-22 17:15:32 +00001202DEF_TRAVERSE_DECL(EmptyDecl, { })
1203
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001204DEF_TRAVERSE_DECL(FileScopeAsmDecl, {
1205 TRY_TO(TraverseStmt(D->getAsmString()));
1206 })
1207
1208DEF_TRAVERSE_DECL(ImportDecl, { })
1209
1210DEF_TRAVERSE_DECL(FriendDecl, {
1211 // Friend is either decl or a type.
1212 if (D->getFriendType())
1213 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1214 else
1215 TRY_TO(TraverseDecl(D->getFriendDecl()));
1216 })
1217
1218DEF_TRAVERSE_DECL(FriendTemplateDecl, {
1219 if (D->getFriendType())
1220 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc()));
1221 else
1222 TRY_TO(TraverseDecl(D->getFriendDecl()));
1223 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) {
1224 TemplateParameterList *TPL = D->getTemplateParameterList(I);
1225 for (TemplateParameterList::iterator ITPL = TPL->begin(),
1226 ETPL = TPL->end();
1227 ITPL != ETPL; ++ITPL) {
1228 TRY_TO(TraverseDecl(*ITPL));
1229 }
1230 }
1231 })
1232
1233DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, {
1234 TRY_TO(TraverseDecl(D->getSpecialization()));
1235 })
1236
1237DEF_TRAVERSE_DECL(LinkageSpecDecl, { })
1238
1239DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {
1240 // FIXME: implement this
1241 })
1242
1243DEF_TRAVERSE_DECL(StaticAssertDecl, {
1244 TRY_TO(TraverseStmt(D->getAssertExpr()));
1245 TRY_TO(TraverseStmt(D->getMessage()));
1246 })
1247
1248DEF_TRAVERSE_DECL(TranslationUnitDecl, {
1249 // Code in an unnamed namespace shows up automatically in
1250 // decls_begin()/decls_end(). Thus we don't need to recurse on
1251 // D->getAnonymousNamespace().
1252 })
1253
1254DEF_TRAVERSE_DECL(NamespaceAliasDecl, {
1255 // We shouldn't traverse an aliased namespace, since it will be
1256 // defined (and, therefore, traversed) somewhere else.
1257 //
1258 // This return statement makes sure the traversal of nodes in
1259 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro)
1260 // is skipped - don't remove it.
1261 return true;
1262 })
1263
1264DEF_TRAVERSE_DECL(LabelDecl, {
1265 // There is no code in a LabelDecl.
1266})
1267
1268
1269DEF_TRAVERSE_DECL(NamespaceDecl, {
1270 // Code in an unnamed namespace shows up automatically in
1271 // decls_begin()/decls_end(). Thus we don't need to recurse on
1272 // D->getAnonymousNamespace().
1273 })
1274
1275DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {
1276 // FIXME: implement
1277 })
1278
1279DEF_TRAVERSE_DECL(ObjCCategoryDecl, {
1280 // FIXME: implement
1281 })
1282
1283DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {
1284 // FIXME: implement
1285 })
1286
1287DEF_TRAVERSE_DECL(ObjCImplementationDecl, {
1288 // FIXME: implement
1289 })
1290
1291DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {
1292 // FIXME: implement
1293 })
1294
1295DEF_TRAVERSE_DECL(ObjCProtocolDecl, {
1296 // FIXME: implement
1297 })
1298
1299DEF_TRAVERSE_DECL(ObjCMethodDecl, {
1300 if (D->getResultTypeSourceInfo()) {
1301 TRY_TO(TraverseTypeLoc(D->getResultTypeSourceInfo()->getTypeLoc()));
1302 }
1303 for (ObjCMethodDecl::param_iterator
1304 I = D->param_begin(), E = D->param_end(); I != E; ++I) {
1305 TRY_TO(TraverseDecl(*I));
1306 }
1307 if (D->isThisDeclarationADefinition()) {
1308 TRY_TO(TraverseStmt(D->getBody()));
1309 }
1310 return true;
1311 })
1312
1313DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
1314 // FIXME: implement
1315 })
1316
1317DEF_TRAVERSE_DECL(UsingDecl, {
1318 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1319 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1320 })
1321
1322DEF_TRAVERSE_DECL(UsingDirectiveDecl, {
1323 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1324 })
1325
1326DEF_TRAVERSE_DECL(UsingShadowDecl, { })
1327
1328// A helper method for TemplateDecl's children.
1329template<typename Derived>
1330bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper(
1331 TemplateParameterList *TPL) {
1332 if (TPL) {
1333 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1334 I != E; ++I) {
1335 TRY_TO(TraverseDecl(*I));
1336 }
1337 }
1338 return true;
1339}
1340
1341// A helper method for traversing the implicit instantiations of a
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001342// class template.
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001343template<typename Derived>
1344bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations(
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001345 ClassTemplateDecl *D) {
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001346 ClassTemplateDecl::spec_iterator end = D->spec_end();
1347 for (ClassTemplateDecl::spec_iterator it = D->spec_begin(); it != end; ++it) {
1348 ClassTemplateSpecializationDecl* SD = *it;
1349
1350 switch (SD->getSpecializationKind()) {
1351 // Visit the implicit instantiations with the requested pattern.
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001352 case TSK_Undeclared:
1353 case TSK_ImplicitInstantiation:
1354 TRY_TO(TraverseDecl(SD));
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001355 break;
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001356
1357 // We don't need to do anything on an explicit instantiation
1358 // or explicit specialization because there will be an explicit
1359 // node for it elsewhere.
1360 case TSK_ExplicitInstantiationDeclaration:
1361 case TSK_ExplicitInstantiationDefinition:
1362 case TSK_ExplicitSpecialization:
1363 break;
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001364 }
1365 }
1366
1367 return true;
1368}
1369
1370DEF_TRAVERSE_DECL(ClassTemplateDecl, {
1371 CXXRecordDecl* TempDecl = D->getTemplatedDecl();
1372 TRY_TO(TraverseDecl(TempDecl));
1373 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1374
1375 // By default, we do not traverse the instantiations of
1376 // class templates since they do not appear in the user code. The
1377 // following code optionally traverses them.
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001378 //
1379 // We only traverse the class instantiations when we see the canonical
1380 // declaration of the template, to ensure we only visit them once.
1381 if (getDerived().shouldVisitTemplateInstantiations() &&
1382 D == D->getCanonicalDecl())
1383 TRY_TO(TraverseClassInstantiations(D));
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001384
1385 // Note that getInstantiatedFromMemberTemplate() is just a link
1386 // from a template instantiation back to the template from which
1387 // it was instantiated, and thus should not be traversed.
1388 })
1389
1390// A helper method for traversing the instantiations of a
1391// function while skipping its specializations.
1392template<typename Derived>
1393bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations(
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001394 FunctionTemplateDecl *D) {
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001395 FunctionTemplateDecl::spec_iterator end = D->spec_end();
1396 for (FunctionTemplateDecl::spec_iterator it = D->spec_begin(); it != end;
1397 ++it) {
1398 FunctionDecl* FD = *it;
1399 switch (FD->getTemplateSpecializationKind()) {
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001400 case TSK_Undeclared:
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001401 case TSK_ImplicitInstantiation:
1402 // We don't know what kind of FunctionDecl this is.
1403 TRY_TO(TraverseDecl(FD));
1404 break;
1405
1406 // No need to visit explicit instantiations, we'll find the node
1407 // eventually.
1408 case TSK_ExplicitInstantiationDeclaration:
1409 case TSK_ExplicitInstantiationDefinition:
1410 break;
1411
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001412 case TSK_ExplicitSpecialization:
1413 break;
1414 }
1415 }
1416
1417 return true;
1418}
1419
1420DEF_TRAVERSE_DECL(FunctionTemplateDecl, {
1421 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1422 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1423
1424 // By default, we do not traverse the instantiations of
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001425 // function templates since they do not appear in the user code. The
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001426 // following code optionally traverses them.
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001427 //
1428 // We only traverse the function instantiations when we see the canonical
1429 // declaration of the template, to ensure we only visit them once.
1430 if (getDerived().shouldVisitTemplateInstantiations() &&
1431 D == D->getCanonicalDecl())
1432 TRY_TO(TraverseFunctionInstantiations(D));
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001433 })
1434
1435DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, {
1436 // D is the "T" in something like
1437 // template <template <typename> class T> class container { };
1438 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1439 if (D->hasDefaultArgument()) {
1440 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument()));
1441 }
1442 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1443 })
1444
1445DEF_TRAVERSE_DECL(TemplateTypeParmDecl, {
1446 // D is the "T" in something like "template<typename T> class vector;"
1447 if (D->getTypeForDecl())
1448 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1449 if (D->hasDefaultArgument())
1450 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc()));
1451 })
1452
1453DEF_TRAVERSE_DECL(TypedefDecl, {
1454 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1455 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1456 // declaring the typedef, not something that was written in the
1457 // source.
1458 })
1459
1460DEF_TRAVERSE_DECL(TypeAliasDecl, {
1461 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1462 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1463 // declaring the type alias, not something that was written in the
1464 // source.
1465 })
1466
1467DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, {
1468 TRY_TO(TraverseDecl(D->getTemplatedDecl()));
1469 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters()));
1470 })
1471
1472DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, {
1473 // A dependent using declaration which was marked with 'typename'.
1474 // template<class T> class A : public B<T> { using typename B<T>::foo; };
1475 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1476 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1477 // declaring the type, not something that was written in the
1478 // source.
1479 })
1480
1481DEF_TRAVERSE_DECL(EnumDecl, {
1482 if (D->getTypeForDecl())
1483 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0)));
1484
1485 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1486 // The enumerators are already traversed by
1487 // decls_begin()/decls_end().
1488 })
1489
1490
1491// Helper methods for RecordDecl and its children.
1492template<typename Derived>
1493bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(
1494 RecordDecl *D) {
1495 // We shouldn't traverse D->getTypeForDecl(); it's a result of
1496 // declaring the type, not something that was written in the source.
1497
1498 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1499 return true;
1500}
1501
1502template<typename Derived>
1503bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(
1504 CXXRecordDecl *D) {
1505 if (!TraverseRecordHelper(D))
1506 return false;
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001507 if (D->isCompleteDefinition()) {
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001508 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1509 E = D->bases_end();
1510 I != E; ++I) {
1511 TRY_TO(TraverseTypeLoc(I->getTypeSourceInfo()->getTypeLoc()));
1512 }
1513 // We don't traverse the friends or the conversions, as they are
1514 // already in decls_begin()/decls_end().
1515 }
1516 return true;
1517}
1518
1519DEF_TRAVERSE_DECL(RecordDecl, {
1520 TRY_TO(TraverseRecordHelper(D));
1521 })
1522
1523DEF_TRAVERSE_DECL(CXXRecordDecl, {
1524 TRY_TO(TraverseCXXRecordHelper(D));
1525 })
1526
1527DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, {
1528 // For implicit instantiations ("set<int> x;"), we don't want to
1529 // recurse at all, since the instatiated class isn't written in
1530 // the source code anywhere. (Note the instatiated *type* --
1531 // set<int> -- is written, and will still get a callback of
1532 // TemplateSpecializationType). For explicit instantiations
1533 // ("template set<int>;"), we do need a callback, since this
1534 // is the only callback that's made for this instantiation.
1535 // We use getTypeAsWritten() to distinguish.
1536 if (TypeSourceInfo *TSI = D->getTypeAsWritten())
1537 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc()));
1538
1539 if (!getDerived().shouldVisitTemplateInstantiations() &&
1540 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1541 // Returning from here skips traversing the
1542 // declaration context of the ClassTemplateSpecializationDecl
1543 // (embedded in the DEF_TRAVERSE_DECL() macro)
1544 // which contains the instantiated members of the class.
1545 return true;
1546 })
1547
1548template <typename Derived>
1549bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
1550 const TemplateArgumentLoc *TAL, unsigned Count) {
1551 for (unsigned I = 0; I < Count; ++I) {
1552 TRY_TO(TraverseTemplateArgumentLoc(TAL[I]));
1553 }
1554 return true;
1555}
1556
1557DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, {
1558 // The partial specialization.
1559 if (TemplateParameterList *TPL = D->getTemplateParameters()) {
1560 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end();
1561 I != E; ++I) {
1562 TRY_TO(TraverseDecl(*I));
1563 }
1564 }
1565 // The args that remains unspecialized.
1566 TRY_TO(TraverseTemplateArgumentLocsHelper(
1567 D->getTemplateArgsAsWritten(), D->getNumTemplateArgsAsWritten()));
1568
1569 // Don't need the ClassTemplatePartialSpecializationHelper, even
1570 // though that's our parent class -- we already visit all the
1571 // template args here.
1572 TRY_TO(TraverseCXXRecordHelper(D));
1573
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001574 // Instantiations will have been visited with the primary template.
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001575 })
1576
1577DEF_TRAVERSE_DECL(EnumConstantDecl, {
1578 TRY_TO(TraverseStmt(D->getInitExpr()));
1579 })
1580
1581DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, {
1582 // Like UnresolvedUsingTypenameDecl, but without the 'typename':
1583 // template <class T> Class A : public Base<T> { using Base<T>::foo; };
1584 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1585 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1586 })
1587
1588DEF_TRAVERSE_DECL(IndirectFieldDecl, {})
1589
1590template<typename Derived>
1591bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) {
1592 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1593 if (D->getTypeSourceInfo())
1594 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1595 else
1596 TRY_TO(TraverseType(D->getType()));
1597 return true;
1598}
1599
1600DEF_TRAVERSE_DECL(FieldDecl, {
1601 TRY_TO(TraverseDeclaratorHelper(D));
1602 if (D->isBitField())
1603 TRY_TO(TraverseStmt(D->getBitWidth()));
1604 else if (D->hasInClassInitializer())
1605 TRY_TO(TraverseStmt(D->getInClassInitializer()));
1606 })
1607
1608DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, {
1609 TRY_TO(TraverseDeclaratorHelper(D));
1610 if (D->isBitField())
1611 TRY_TO(TraverseStmt(D->getBitWidth()));
1612 // FIXME: implement the rest.
1613 })
1614
1615DEF_TRAVERSE_DECL(ObjCIvarDecl, {
1616 TRY_TO(TraverseDeclaratorHelper(D));
1617 if (D->isBitField())
1618 TRY_TO(TraverseStmt(D->getBitWidth()));
1619 // FIXME: implement the rest.
1620 })
1621
1622template<typename Derived>
1623bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) {
1624 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
1625 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo()));
1626
1627 // If we're an explicit template specialization, iterate over the
1628 // template args that were explicitly specified. If we were doing
1629 // this in typing order, we'd do it between the return type and
1630 // the function args, but both are handled by the FunctionTypeLoc
1631 // above, so we have to choose one side. I've decided to do before.
1632 if (const FunctionTemplateSpecializationInfo *FTSI =
1633 D->getTemplateSpecializationInfo()) {
1634 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared &&
1635 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) {
1636 // A specialization might not have explicit template arguments if it has
1637 // a templated return type and concrete arguments.
1638 if (const ASTTemplateArgumentListInfo *TALI =
1639 FTSI->TemplateArgumentsAsWritten) {
1640 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(),
1641 TALI->NumTemplateArgs));
1642 }
1643 }
1644 }
1645
1646 // Visit the function type itself, which can be either
1647 // FunctionNoProtoType or FunctionProtoType, or a typedef. This
1648 // also covers the return type and the function parameters,
1649 // including exception specifications.
1650 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
1651
1652 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1653 // Constructor initializers.
1654 for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
1655 E = Ctor->init_end();
1656 I != E; ++I) {
1657 TRY_TO(TraverseConstructorInitializer(*I));
1658 }
1659 }
1660
1661 if (D->isThisDeclarationADefinition()) {
1662 TRY_TO(TraverseStmt(D->getBody())); // Function body.
1663 }
1664 return true;
1665}
1666
1667DEF_TRAVERSE_DECL(FunctionDecl, {
1668 // We skip decls_begin/decls_end, which are already covered by
1669 // TraverseFunctionHelper().
1670 return TraverseFunctionHelper(D);
1671 })
1672
1673DEF_TRAVERSE_DECL(CXXMethodDecl, {
1674 // We skip decls_begin/decls_end, which are already covered by
1675 // TraverseFunctionHelper().
1676 return TraverseFunctionHelper(D);
1677 })
1678
1679DEF_TRAVERSE_DECL(CXXConstructorDecl, {
1680 // We skip decls_begin/decls_end, which are already covered by
1681 // TraverseFunctionHelper().
1682 return TraverseFunctionHelper(D);
1683 })
1684
1685// CXXConversionDecl is the declaration of a type conversion operator.
1686// It's not a cast expression.
1687DEF_TRAVERSE_DECL(CXXConversionDecl, {
1688 // We skip decls_begin/decls_end, which are already covered by
1689 // TraverseFunctionHelper().
1690 return TraverseFunctionHelper(D);
1691 })
1692
1693DEF_TRAVERSE_DECL(CXXDestructorDecl, {
1694 // We skip decls_begin/decls_end, which are already covered by
1695 // TraverseFunctionHelper().
1696 return TraverseFunctionHelper(D);
1697 })
1698
1699template<typename Derived>
1700bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) {
1701 TRY_TO(TraverseDeclaratorHelper(D));
1702 // Default params are taken care of when we traverse the ParmVarDecl.
1703 if (!isa<ParmVarDecl>(D))
1704 TRY_TO(TraverseStmt(D->getInit()));
1705 return true;
1706}
1707
1708DEF_TRAVERSE_DECL(VarDecl, {
1709 TRY_TO(TraverseVarHelper(D));
1710 })
1711
1712DEF_TRAVERSE_DECL(ImplicitParamDecl, {
1713 TRY_TO(TraverseVarHelper(D));
1714 })
1715
1716DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, {
1717 // A non-type template parameter, e.g. "S" in template<int S> class Foo ...
1718 TRY_TO(TraverseDeclaratorHelper(D));
1719 TRY_TO(TraverseStmt(D->getDefaultArgument()));
1720 })
1721
1722DEF_TRAVERSE_DECL(ParmVarDecl, {
1723 TRY_TO(TraverseVarHelper(D));
1724
1725 if (D->hasDefaultArg() &&
1726 D->hasUninstantiatedDefaultArg() &&
1727 !D->hasUnparsedDefaultArg())
1728 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg()));
1729
1730 if (D->hasDefaultArg() &&
1731 !D->hasUninstantiatedDefaultArg() &&
1732 !D->hasUnparsedDefaultArg())
1733 TRY_TO(TraverseStmt(D->getDefaultArg()));
1734 })
1735
1736#undef DEF_TRAVERSE_DECL
1737
1738// ----------------- Stmt traversal -----------------
1739//
1740// For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating
1741// over the children defined in children() (every stmt defines these,
1742// though sometimes the range is empty). Each individual Traverse*
1743// method only needs to worry about children other than those. To see
1744// what children() does for a given class, see, e.g.,
1745// http://clang.llvm.org/doxygen/Stmt_8cpp_source.html
1746
1747// This macro makes available a variable S, the passed-in stmt.
1748#define DEF_TRAVERSE_STMT(STMT, CODE) \
1749template<typename Derived> \
1750bool RecursiveASTVisitor<Derived>::Traverse##STMT (STMT *S) { \
1751 TRY_TO(WalkUpFrom##STMT(S)); \
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001752 StmtQueueAction StmtQueue(*this); \
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001753 { CODE; } \
1754 for (Stmt::child_range range = S->children(); range; ++range) { \
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001755 StmtQueue.queue(*range); \
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001756 } \
1757 return true; \
1758}
1759
Chad Rosierdf5faf52012-08-25 00:11:56 +00001760DEF_TRAVERSE_STMT(GCCAsmStmt, {
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001761 StmtQueue.queue(S->getAsmString());
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001762 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) {
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001763 StmtQueue.queue(S->getInputConstraintLiteral(I));
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001764 }
1765 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) {
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001766 StmtQueue.queue(S->getOutputConstraintLiteral(I));
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001767 }
1768 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) {
Chad Rosier5c7f5942012-08-27 23:28:41 +00001769 StmtQueue.queue(S->getClobberStringLiteral(I));
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001770 }
1771 // children() iterates over inputExpr and outputExpr.
1772 })
1773
Chad Rosier8cd64b42012-06-11 20:47:18 +00001774DEF_TRAVERSE_STMT(MSAsmStmt, {
1775 // FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once
1776 // added this needs to be implemented.
1777 })
1778
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001779DEF_TRAVERSE_STMT(CXXCatchStmt, {
1780 TRY_TO(TraverseDecl(S->getExceptionDecl()));
1781 // children() iterates over the handler block.
1782 })
1783
1784DEF_TRAVERSE_STMT(DeclStmt, {
1785 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
1786 I != E; ++I) {
1787 TRY_TO(TraverseDecl(*I));
1788 }
1789 // Suppress the default iteration over children() by
1790 // returning. Here's why: A DeclStmt looks like 'type var [=
1791 // initializer]'. The decls above already traverse over the
1792 // initializers, so we don't have to do it again (which
1793 // children() would do).
1794 return true;
1795 })
1796
1797
1798// These non-expr stmts (most of them), do not need any action except
1799// iterating over the children.
1800DEF_TRAVERSE_STMT(BreakStmt, { })
1801DEF_TRAVERSE_STMT(CXXTryStmt, { })
1802DEF_TRAVERSE_STMT(CaseStmt, { })
1803DEF_TRAVERSE_STMT(CompoundStmt, { })
1804DEF_TRAVERSE_STMT(ContinueStmt, { })
1805DEF_TRAVERSE_STMT(DefaultStmt, { })
1806DEF_TRAVERSE_STMT(DoStmt, { })
1807DEF_TRAVERSE_STMT(ForStmt, { })
1808DEF_TRAVERSE_STMT(GotoStmt, { })
1809DEF_TRAVERSE_STMT(IfStmt, { })
1810DEF_TRAVERSE_STMT(IndirectGotoStmt, { })
1811DEF_TRAVERSE_STMT(LabelStmt, { })
1812DEF_TRAVERSE_STMT(AttributedStmt, { })
1813DEF_TRAVERSE_STMT(NullStmt, { })
1814DEF_TRAVERSE_STMT(ObjCAtCatchStmt, { })
1815DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, { })
1816DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, { })
1817DEF_TRAVERSE_STMT(ObjCAtThrowStmt, { })
1818DEF_TRAVERSE_STMT(ObjCAtTryStmt, { })
1819DEF_TRAVERSE_STMT(ObjCForCollectionStmt, { })
1820DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, { })
1821DEF_TRAVERSE_STMT(CXXForRangeStmt, { })
1822DEF_TRAVERSE_STMT(MSDependentExistsStmt, {
1823 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1824 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1825})
1826DEF_TRAVERSE_STMT(ReturnStmt, { })
1827DEF_TRAVERSE_STMT(SwitchStmt, { })
1828DEF_TRAVERSE_STMT(WhileStmt, { })
1829
1830
1831DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
1832 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1833 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1834 if (S->hasExplicitTemplateArgs()) {
1835 TRY_TO(TraverseTemplateArgumentLocsHelper(
1836 S->getTemplateArgs(), S->getNumTemplateArgs()));
1837 }
1838 })
1839
1840DEF_TRAVERSE_STMT(DeclRefExpr, {
1841 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1842 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1843 TRY_TO(TraverseTemplateArgumentLocsHelper(
1844 S->getTemplateArgs(), S->getNumTemplateArgs()));
1845 })
1846
1847DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, {
1848 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1849 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo()));
1850 if (S->hasExplicitTemplateArgs()) {
1851 TRY_TO(TraverseTemplateArgumentLocsHelper(
1852 S->getExplicitTemplateArgs().getTemplateArgs(),
1853 S->getNumTemplateArgs()));
1854 }
1855 })
1856
1857DEF_TRAVERSE_STMT(MemberExpr, {
1858 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
1859 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo()));
1860 TRY_TO(TraverseTemplateArgumentLocsHelper(
1861 S->getTemplateArgs(), S->getNumTemplateArgs()));
1862 })
1863
1864DEF_TRAVERSE_STMT(ImplicitCastExpr, {
1865 // We don't traverse the cast type, as it's not written in the
1866 // source code.
1867 })
1868
1869DEF_TRAVERSE_STMT(CStyleCastExpr, {
1870 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1871 })
1872
1873DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, {
1874 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1875 })
1876
1877DEF_TRAVERSE_STMT(CXXConstCastExpr, {
1878 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1879 })
1880
1881DEF_TRAVERSE_STMT(CXXDynamicCastExpr, {
1882 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1883 })
1884
1885DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, {
1886 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1887 })
1888
1889DEF_TRAVERSE_STMT(CXXStaticCastExpr, {
1890 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
1891 })
1892
1893// InitListExpr is a tricky one, because we want to do all our work on
1894// the syntactic form of the listexpr, but this method takes the
1895// semantic form by default. We can't use the macro helper because it
1896// calls WalkUp*() on the semantic form, before our code can convert
1897// to the syntactic form.
1898template<typename Derived>
1899bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) {
1900 if (InitListExpr *Syn = S->getSyntacticForm())
1901 S = Syn;
1902 TRY_TO(WalkUpFromInitListExpr(S));
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001903 StmtQueueAction StmtQueue(*this);
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001904 // All we need are the default actions. FIXME: use a helper function.
1905 for (Stmt::child_range range = S->children(); range; ++range) {
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001906 StmtQueue.queue(*range);
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001907 }
1908 return true;
1909}
1910
1911// GenericSelectionExpr is a special case because the types and expressions
1912// are interleaved. We also need to watch out for null types (default
1913// generic associations).
1914template<typename Derived>
1915bool RecursiveASTVisitor<Derived>::
1916TraverseGenericSelectionExpr(GenericSelectionExpr *S) {
1917 TRY_TO(WalkUpFromGenericSelectionExpr(S));
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001918 StmtQueueAction StmtQueue(*this);
1919 StmtQueue.queue(S->getControllingExpr());
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001920 for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
1921 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i))
1922 TRY_TO(TraverseTypeLoc(TS->getTypeLoc()));
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001923 StmtQueue.queue(S->getAssocExpr(i));
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001924 }
1925 return true;
1926}
1927
1928// PseudoObjectExpr is a special case because of the wierdness with
1929// syntactic expressions and opaque values.
1930template<typename Derived>
1931bool RecursiveASTVisitor<Derived>::
1932TraversePseudoObjectExpr(PseudoObjectExpr *S) {
1933 TRY_TO(WalkUpFromPseudoObjectExpr(S));
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001934 StmtQueueAction StmtQueue(*this);
1935 StmtQueue.queue(S->getSyntacticForm());
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001936 for (PseudoObjectExpr::semantics_iterator
1937 i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i) {
1938 Expr *sub = *i;
1939 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub))
1940 sub = OVE->getSourceExpr();
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00001941 StmtQueue.queue(sub);
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00001942 }
1943 return true;
1944}
1945
1946DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, {
1947 // This is called for code like 'return T()' where T is a built-in
1948 // (i.e. non-class) type.
1949 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1950 })
1951
1952DEF_TRAVERSE_STMT(CXXNewExpr, {
1953 // The child-iterator will pick up the other arguments.
1954 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc()));
1955 })
1956
1957DEF_TRAVERSE_STMT(OffsetOfExpr, {
1958 // The child-iterator will pick up the expression representing
1959 // the field.
1960 // FIMXE: for code like offsetof(Foo, a.b.c), should we get
1961 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c?
1962 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
1963 })
1964
1965DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, {
1966 // The child-iterator will pick up the arg if it's an expression,
1967 // but not if it's a type.
1968 if (S->isArgumentType())
1969 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc()));
1970 })
1971
1972DEF_TRAVERSE_STMT(CXXTypeidExpr, {
1973 // The child-iterator will pick up the arg if it's an expression,
1974 // but not if it's a type.
1975 if (S->isTypeOperand())
1976 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
1977 })
1978
1979DEF_TRAVERSE_STMT(CXXUuidofExpr, {
1980 // The child-iterator will pick up the arg if it's an expression,
1981 // but not if it's a type.
1982 if (S->isTypeOperand())
1983 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc()));
1984 })
1985
1986DEF_TRAVERSE_STMT(UnaryTypeTraitExpr, {
1987 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
1988 })
1989
1990DEF_TRAVERSE_STMT(BinaryTypeTraitExpr, {
1991 TRY_TO(TraverseTypeLoc(S->getLhsTypeSourceInfo()->getTypeLoc()));
1992 TRY_TO(TraverseTypeLoc(S->getRhsTypeSourceInfo()->getTypeLoc()));
1993 })
1994
1995DEF_TRAVERSE_STMT(TypeTraitExpr, {
1996 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
1997 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc()));
1998})
1999
2000DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, {
2001 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc()));
2002 })
2003
2004DEF_TRAVERSE_STMT(ExpressionTraitExpr, {
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00002005 StmtQueue.queue(S->getQueriedExpression());
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00002006 })
2007
2008DEF_TRAVERSE_STMT(VAArgExpr, {
2009 // The child-iterator will pick up the expression argument.
2010 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc()));
2011 })
2012
2013DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, {
2014 // This is called for code like 'return T()' where T is a class type.
2015 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2016 })
2017
2018// Walk only the visible parts of lambda expressions.
2019template<typename Derived>
2020bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) {
2021 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
2022 CEnd = S->explicit_capture_end();
2023 C != CEnd; ++C) {
2024 TRY_TO(TraverseLambdaCapture(*C));
2025 }
2026
2027 if (S->hasExplicitParameters() || S->hasExplicitResultType()) {
2028 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2029 if (S->hasExplicitParameters() && S->hasExplicitResultType()) {
2030 // Visit the whole type.
2031 TRY_TO(TraverseTypeLoc(TL));
David Blaikie39e6ab42013-02-18 22:06:02 +00002032 } else if (FunctionProtoTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) {
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00002033 if (S->hasExplicitParameters()) {
2034 // Visit parameters.
2035 for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) {
2036 TRY_TO(TraverseDecl(Proto.getArg(I)));
2037 }
2038 } else {
2039 TRY_TO(TraverseTypeLoc(Proto.getResultLoc()));
2040 }
2041 }
2042 }
2043
Argyrios Kyrtzidis428499e2012-05-07 23:23:03 +00002044 StmtQueueAction StmtQueue(*this);
2045 StmtQueue.queue(S->getBody());
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00002046 return true;
2047}
2048
2049DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, {
2050 // This is called for code like 'T()', where T is a template argument.
2051 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc()));
2052 })
2053
2054// These expressions all might take explicit template arguments.
2055// We traverse those if so. FIXME: implement these.
2056DEF_TRAVERSE_STMT(CXXConstructExpr, { })
2057DEF_TRAVERSE_STMT(CallExpr, { })
2058DEF_TRAVERSE_STMT(CXXMemberCallExpr, { })
2059
2060// These exprs (most of them), do not need any action except iterating
2061// over the children.
2062DEF_TRAVERSE_STMT(AddrLabelExpr, { })
2063DEF_TRAVERSE_STMT(ArraySubscriptExpr, { })
2064DEF_TRAVERSE_STMT(BlockExpr, {
2065 TRY_TO(TraverseDecl(S->getBlockDecl()));
2066 return true; // no child statements to loop through.
2067})
2068DEF_TRAVERSE_STMT(ChooseExpr, { })
2069DEF_TRAVERSE_STMT(CompoundLiteralExpr, { })
2070DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, { })
2071DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, { })
2072DEF_TRAVERSE_STMT(CXXDefaultArgExpr, { })
2073DEF_TRAVERSE_STMT(CXXDeleteExpr, { })
2074DEF_TRAVERSE_STMT(ExprWithCleanups, { })
2075DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, { })
2076DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, {
2077 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2078 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo())
2079 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc()));
2080 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo())
2081 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc()));
2082})
2083DEF_TRAVERSE_STMT(CXXThisExpr, { })
2084DEF_TRAVERSE_STMT(CXXThrowExpr, { })
2085DEF_TRAVERSE_STMT(UserDefinedLiteral, { })
2086DEF_TRAVERSE_STMT(DesignatedInitExpr, { })
2087DEF_TRAVERSE_STMT(ExtVectorElementExpr, { })
2088DEF_TRAVERSE_STMT(GNUNullExpr, { })
2089DEF_TRAVERSE_STMT(ImplicitValueInitExpr, { })
2090DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, { })
Argyrios Kyrtzidis87014f32012-05-16 19:22:47 +00002091DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
2092 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
2093 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc()));
2094})
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00002095DEF_TRAVERSE_STMT(ObjCIsaExpr, { })
2096DEF_TRAVERSE_STMT(ObjCIvarRefExpr, { })
2097DEF_TRAVERSE_STMT(ObjCMessageExpr, { })
2098DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, { })
2099DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, { })
2100DEF_TRAVERSE_STMT(ObjCProtocolExpr, { })
2101DEF_TRAVERSE_STMT(ObjCSelectorExpr, { })
2102DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, { })
2103DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, {
2104 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc()));
2105})
2106DEF_TRAVERSE_STMT(ParenExpr, { })
2107DEF_TRAVERSE_STMT(ParenListExpr, { })
2108DEF_TRAVERSE_STMT(PredefinedExpr, { })
2109DEF_TRAVERSE_STMT(ShuffleVectorExpr, { })
2110DEF_TRAVERSE_STMT(StmtExpr, { })
2111DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
2112 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2113 if (S->hasExplicitTemplateArgs()) {
2114 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2115 S->getNumTemplateArgs()));
2116 }
2117})
2118
2119DEF_TRAVERSE_STMT(UnresolvedMemberExpr, {
2120 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
2121 if (S->hasExplicitTemplateArgs()) {
2122 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(),
2123 S->getNumTemplateArgs()));
2124 }
2125})
2126
2127DEF_TRAVERSE_STMT(SEHTryStmt, {})
2128DEF_TRAVERSE_STMT(SEHExceptStmt, {})
2129DEF_TRAVERSE_STMT(SEHFinallyStmt,{})
2130
2131DEF_TRAVERSE_STMT(CXXOperatorCallExpr, { })
2132DEF_TRAVERSE_STMT(OpaqueValueExpr, { })
2133DEF_TRAVERSE_STMT(CUDAKernelCallExpr, { })
2134
2135// These operators (all of them) do not need any action except
2136// iterating over the children.
2137DEF_TRAVERSE_STMT(BinaryConditionalOperator, { })
2138DEF_TRAVERSE_STMT(ConditionalOperator, { })
2139DEF_TRAVERSE_STMT(UnaryOperator, { })
2140DEF_TRAVERSE_STMT(BinaryOperator, { })
2141DEF_TRAVERSE_STMT(CompoundAssignOperator, { })
2142DEF_TRAVERSE_STMT(CXXNoexceptExpr, { })
2143DEF_TRAVERSE_STMT(PackExpansionExpr, { })
2144DEF_TRAVERSE_STMT(SizeOfPackExpr, { })
2145DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, { })
2146DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, { })
Richard Smith9a4db032012-09-12 00:56:43 +00002147DEF_TRAVERSE_STMT(FunctionParmPackExpr, { })
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00002148DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, { })
2149DEF_TRAVERSE_STMT(AtomicExpr, { })
2150
2151// These literals (all of them) do not need any action.
2152DEF_TRAVERSE_STMT(IntegerLiteral, { })
2153DEF_TRAVERSE_STMT(CharacterLiteral, { })
2154DEF_TRAVERSE_STMT(FloatingLiteral, { })
2155DEF_TRAVERSE_STMT(ImaginaryLiteral, { })
2156DEF_TRAVERSE_STMT(StringLiteral, { })
2157DEF_TRAVERSE_STMT(ObjCStringLiteral, { })
2158DEF_TRAVERSE_STMT(ObjCBoxedExpr, { })
2159DEF_TRAVERSE_STMT(ObjCArrayLiteral, { })
2160DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, { })
2161
2162// Traverse OpenCL: AsType, Convert.
2163DEF_TRAVERSE_STMT(AsTypeExpr, { })
2164
2165// FIXME: look at the following tricky-seeming exprs to see if we
2166// need to recurse on anything. These are ones that have methods
2167// returning decls or qualtypes or nestednamespecifier -- though I'm
2168// not sure if they own them -- or just seemed very complicated, or
2169// had lots of sub-types to explore.
2170//
2171// VisitOverloadExpr and its children: recurse on template args? etc?
2172
2173// FIXME: go through all the stmts and exprs again, and see which of them
2174// create new types, and recurse on the types (TypeLocs?) of those.
2175// Candidates:
2176//
2177// http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html
2178// http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html
2179// http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html
2180// Every class that has getQualifier.
2181
2182#undef DEF_TRAVERSE_STMT
2183
2184#undef TRY_TO
2185
Argyrios Kyrtzidis98180d42012-05-07 22:22:58 +00002186} // end namespace cxindex
Argyrios Kyrtzidisdec35a92012-05-07 22:16:46 +00002187} // end namespace clang
2188
2189#endif // LLVM_CLANG_LIBCLANG_RECURSIVEASTVISITOR_H