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