]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/RecursiveASTVisitor.h
Merge ACPICA 20100702.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / RecursiveASTVisitor.h
1 //===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the RecursiveASTVisitor interface, which recursively
11 //  traverses the entire AST.
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
15 #define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
16
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclFriend.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/ExprCXX.h"
24 #include "clang/AST/ExprObjC.h"
25 #include "clang/AST/NestedNameSpecifier.h"
26 #include "clang/AST/Stmt.h"
27 #include "clang/AST/StmtCXX.h"
28 #include "clang/AST/StmtObjC.h"
29 #include "clang/AST/TemplateBase.h"
30 #include "clang/AST/TemplateName.h"
31 #include "clang/AST/Type.h"
32
33 namespace clang {
34
35 #define DISPATCH(NAME, CLASS, Var) \
36 return getDerived().Visit ## NAME(static_cast<CLASS*>(Var))
37
38 // We use preprocessor meta-programming to generate the Visit*()
39 // methods for all subclasses of Stmt, Decl, and Type.  Some of the
40 // generated definitions, however, need to be customized.  The
41 // meta-programming technique we use doesn't let us select which
42 // methods to generate.  Therefore we have to generate ALL of them in
43 // a helper class RecursiveASTVisitorImpl, and override the ones we
44 // don't like in a child class RecursiveASTVisitor (C++ doesn't allow
45 // overriding a method in the same class).
46 //
47 // Do not use this class directly - use RecursiveASTVisitor instead.
48 template<typename Derived>
49 class RecursiveASTVisitorImpl {
50 public:
51   /// \brief Return a reference to the derived class.
52   Derived &getDerived() { return *static_cast<Derived*>(this); }
53
54   /// \brief Recursively visit a statement or expression, by
55   /// dispatching to Visit*() based on the argument's dynamic type.
56   /// This is NOT meant to be overridden by a subclass.
57   ///
58   /// \returns true if the visitation was terminated early, false
59   /// otherwise (including when the argument is NULL).
60   bool Visit(Stmt *S);
61
62   /// \brief Recursively visit a type, by dispatching to
63   /// Visit*Type() based on the argument's getTypeClass() property.
64   /// This is NOT meant to be overridden by a subclass.
65   ///
66   /// \returns true if the visitation was terminated early, false
67   /// otherwise (including when the argument is a Null type).
68   bool Visit(QualType T);
69
70   /// \brief Recursively visit a declaration, by dispatching to
71   /// Visit*Decl() based on the argument's dynamic type.  This is
72   /// NOT meant to be overridden by a subclass.
73   ///
74   /// \returns true if the visitation was terminated early, false
75   /// otherwise (including when the argument is NULL).
76   bool Visit(Decl *D);
77
78   /// \brief Recursively visit a C++ nested-name-specifier.
79   ///
80   /// \returns true if the visitation was terminated early, false otherwise.
81   bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
82
83   /// \brief Recursively visit a template name.
84   ///
85   /// \returns true if the visitation was terminated early, false otherwise.
86   bool VisitTemplateName(TemplateName Template);
87
88   /// \brief Recursively visit a template argument.
89   ///
90   /// \returns true if the visitation was terminated early, false otherwise.
91   bool VisitTemplateArgument(const TemplateArgument &Arg);
92
93   /// \brief Recursively visit a set of template arguments.
94   ///
95   /// \returns true if the visitation was terminated early, false otherwise.
96   bool VisitTemplateArguments(const TemplateArgument *Args, unsigned NumArgs);
97
98   // If the implementation chooses not to implement a certain visit method, fall
99   // back on VisitExpr or whatever else is the superclass.
100 #define STMT(CLASS, PARENT)                                   \
101 bool Visit ## CLASS(CLASS *S) { DISPATCH(PARENT, PARENT, S); }
102 #include "clang/AST/StmtNodes.inc"
103
104   // If the implementation doesn't implement binary operator methods, fall back
105   // on VisitBinaryOperator.
106 #define BINOP_FALLBACK(NAME) \
107 bool VisitBin ## NAME(BinaryOperator *S) {   \
108 DISPATCH(BinaryOperator, BinaryOperator, S); \
109 }
110   BINOP_FALLBACK(PtrMemD)                    BINOP_FALLBACK(PtrMemI)
111   BINOP_FALLBACK(Mul)   BINOP_FALLBACK(Div)  BINOP_FALLBACK(Rem)
112   BINOP_FALLBACK(Add)   BINOP_FALLBACK(Sub)  BINOP_FALLBACK(Shl)
113   BINOP_FALLBACK(Shr)
114
115   BINOP_FALLBACK(LT)    BINOP_FALLBACK(GT)   BINOP_FALLBACK(LE)
116   BINOP_FALLBACK(GE)    BINOP_FALLBACK(EQ)   BINOP_FALLBACK(NE)
117   BINOP_FALLBACK(And)   BINOP_FALLBACK(Xor)  BINOP_FALLBACK(Or)
118   BINOP_FALLBACK(LAnd)  BINOP_FALLBACK(LOr)
119
120   BINOP_FALLBACK(Assign)
121   BINOP_FALLBACK(Comma)
122 #undef BINOP_FALLBACK
123
124   // If the implementation doesn't implement compound assignment operator
125   // methods, fall back on VisitCompoundAssignOperator.
126 #define CAO_FALLBACK(NAME) \
127 bool VisitBin ## NAME(CompoundAssignOperator *S) { \
128 DISPATCH(CompoundAssignOperator, CompoundAssignOperator, S); \
129 }
130   CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign)
131   CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign)
132   CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign)
133   CAO_FALLBACK(XorAssign)
134 #undef CAO_FALLBACK
135
136   // If the implementation doesn't implement unary operator methods, fall back
137   // on VisitUnaryOperator.
138 #define UNARYOP_FALLBACK(NAME) \
139 bool VisitUnary ## NAME(UnaryOperator *S) { \
140 DISPATCH(UnaryOperator, UnaryOperator, S);    \
141 }
142   UNARYOP_FALLBACK(PostInc)   UNARYOP_FALLBACK(PostDec)
143   UNARYOP_FALLBACK(PreInc)    UNARYOP_FALLBACK(PreDec)
144   UNARYOP_FALLBACK(AddrOf)    UNARYOP_FALLBACK(Deref)
145
146   UNARYOP_FALLBACK(Plus)      UNARYOP_FALLBACK(Minus)
147   UNARYOP_FALLBACK(Not)       UNARYOP_FALLBACK(LNot)
148   UNARYOP_FALLBACK(Real)      UNARYOP_FALLBACK(Imag)
149   UNARYOP_FALLBACK(Extension) UNARYOP_FALLBACK(OffsetOf)
150 #undef UNARYOP_FALLBACK
151
152   /// \brief Basis for statement and expression visitation, which
153   /// visits all of the substatements and subexpressions.
154   ///
155   /// The relation between Visit(Stmt *S) and this method is that
156   /// the former dispatches to Visit*() based on S's dynamic type,
157   /// which forwards the call up the inheritance chain until
158   /// reaching VisitStmt(), which then calls Visit() on each
159   /// substatement/subexpression.
160   bool VisitStmt(Stmt *S);
161
162   /// \brief Basis for type visitation, which by default does nothing.
163   ///
164   /// The relation between Visit(QualType T) and this method is
165   /// that the former dispatches to Visit*Type(), which forwards the
166   /// call up the inheritance chain until reaching VisitType().
167   bool VisitType(Type *T);
168
169 #define TYPE(Class, Base) \
170   bool Visit##Class##Type(Class##Type *T);
171 #include "clang/AST/TypeNodes.def"
172
173   /// \brief Basis for declaration and definition visitation, which
174   /// visits all of the subnodes.
175   ///
176   /// The relation between Visit(Decl *) and this method is that the
177   /// former dispatches to Visit*Decl(), which forwards the call up
178   /// the inheritance chain until reaching VisitDecl().
179   bool VisitDecl(Decl *D);
180
181 #define DECL(Class, Base)                        \
182   bool Visit##Class##Decl(Class##Decl *D) {      \
183     return getDerived().Visit##Base(D);          \
184   }
185 #define ABSTRACT_DECL(Class, Base) DECL(Class, Base)
186 #include "clang/AST/DeclNodes.def"
187 };
188
189 template<typename Derived>
190 bool RecursiveASTVisitorImpl<Derived>::Visit(Stmt *S) {
191   if (!S)
192     return false;
193
194   // If we have a binary expr, dispatch to the subcode of the binop.  A smart
195   // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
196   // below.
197   if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
198     switch (BinOp->getOpcode()) {
199     case BinaryOperator::PtrMemD:   DISPATCH(BinPtrMemD, BinaryOperator, S);
200     case BinaryOperator::PtrMemI:   DISPATCH(BinPtrMemI, BinaryOperator, S);
201     case BinaryOperator::Mul:       DISPATCH(BinMul,     BinaryOperator, S);
202     case BinaryOperator::Div:       DISPATCH(BinDiv,     BinaryOperator, S);
203     case BinaryOperator::Rem:       DISPATCH(BinRem,     BinaryOperator, S);
204     case BinaryOperator::Add:       DISPATCH(BinAdd,     BinaryOperator, S);
205     case BinaryOperator::Sub:       DISPATCH(BinSub,     BinaryOperator, S);
206     case BinaryOperator::Shl:       DISPATCH(BinShl,     BinaryOperator, S);
207     case BinaryOperator::Shr:       DISPATCH(BinShr,     BinaryOperator, S);
208
209     case BinaryOperator::LT:        DISPATCH(BinLT,      BinaryOperator, S);
210     case BinaryOperator::GT:        DISPATCH(BinGT,      BinaryOperator, S);
211     case BinaryOperator::LE:        DISPATCH(BinLE,      BinaryOperator, S);
212     case BinaryOperator::GE:        DISPATCH(BinGE,      BinaryOperator, S);
213     case BinaryOperator::EQ:        DISPATCH(BinEQ,      BinaryOperator, S);
214     case BinaryOperator::NE:        DISPATCH(BinNE,      BinaryOperator, S);
215
216     case BinaryOperator::And:       DISPATCH(BinAnd,     BinaryOperator, S);
217     case BinaryOperator::Xor:       DISPATCH(BinXor,     BinaryOperator, S);
218     case BinaryOperator::Or :       DISPATCH(BinOr,      BinaryOperator, S);
219     case BinaryOperator::LAnd:      DISPATCH(BinLAnd,    BinaryOperator, S);
220     case BinaryOperator::LOr :      DISPATCH(BinLOr,     BinaryOperator, S);
221     case BinaryOperator::Assign:    DISPATCH(BinAssign,  BinaryOperator, S);
222     case BinaryOperator::MulAssign:
223       DISPATCH(BinMulAssign, CompoundAssignOperator, S);
224     case BinaryOperator::DivAssign:
225       DISPATCH(BinDivAssign, CompoundAssignOperator, S);
226     case BinaryOperator::RemAssign:
227       DISPATCH(BinRemAssign, CompoundAssignOperator, S);
228     case BinaryOperator::AddAssign:
229       DISPATCH(BinAddAssign, CompoundAssignOperator, S);
230     case BinaryOperator::SubAssign:
231       DISPATCH(BinSubAssign, CompoundAssignOperator, S);
232     case BinaryOperator::ShlAssign:
233       DISPATCH(BinShlAssign, CompoundAssignOperator, S);
234     case BinaryOperator::ShrAssign:
235       DISPATCH(BinShrAssign, CompoundAssignOperator, S);
236     case BinaryOperator::AndAssign:
237       DISPATCH(BinAndAssign, CompoundAssignOperator, S);
238     case BinaryOperator::OrAssign:
239       DISPATCH(BinOrAssign,  CompoundAssignOperator, S);
240     case BinaryOperator::XorAssign:
241       DISPATCH(BinXorAssign, CompoundAssignOperator, S);
242     case BinaryOperator::Comma:     DISPATCH(BinComma,     BinaryOperator, S);
243     }
244   } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
245     switch (UnOp->getOpcode()) {
246     case UnaryOperator::PostInc:   DISPATCH(UnaryPostInc,   UnaryOperator, S);
247     case UnaryOperator::PostDec:   DISPATCH(UnaryPostDec,   UnaryOperator, S);
248     case UnaryOperator::PreInc:    DISPATCH(UnaryPreInc,    UnaryOperator, S);
249     case UnaryOperator::PreDec:    DISPATCH(UnaryPreDec,    UnaryOperator, S);
250     case UnaryOperator::AddrOf:    DISPATCH(UnaryAddrOf,    UnaryOperator, S);
251     case UnaryOperator::Deref:     DISPATCH(UnaryDeref,     UnaryOperator, S);
252     case UnaryOperator::Plus:      DISPATCH(UnaryPlus,      UnaryOperator, S);
253     case UnaryOperator::Minus:     DISPATCH(UnaryMinus,     UnaryOperator, S);
254     case UnaryOperator::Not:       DISPATCH(UnaryNot,       UnaryOperator, S);
255     case UnaryOperator::LNot:      DISPATCH(UnaryLNot,      UnaryOperator, S);
256     case UnaryOperator::Real:      DISPATCH(UnaryReal,      UnaryOperator, S);
257     case UnaryOperator::Imag:      DISPATCH(UnaryImag,      UnaryOperator, S);
258     case UnaryOperator::Extension: DISPATCH(UnaryExtension, UnaryOperator, S);
259     case UnaryOperator::OffsetOf:  DISPATCH(UnaryOffsetOf,  UnaryOperator, S);
260     }
261   }
262
263   // Top switch stmt: dispatch to VisitFooStmt for each FooStmt.
264   switch (S->getStmtClass()) {
265   case Stmt::NoStmtClass: break;
266 #define ABSTRACT_STMT(STMT)
267 #define STMT(CLASS, PARENT)                              \
268 case Stmt::CLASS ## Class: DISPATCH(CLASS, CLASS, S);
269 #include "clang/AST/StmtNodes.inc"
270   }
271
272   return false;
273 }
274
275 template<typename Derived>
276 bool RecursiveASTVisitorImpl<Derived>::Visit(QualType T) {
277   if (T.isNull())
278     return false;
279
280   switch (T->getTypeClass()) {
281 #define ABSTRACT_TYPE(Class, Base)
282 #define TYPE(Class, Base) \
283   case Type::Class: DISPATCH(Class##Type, Class##Type, T.getTypePtr());
284 #include "clang/AST/TypeNodes.def"
285   }
286
287   return false;
288 }
289
290 template<typename Derived>
291 bool RecursiveASTVisitorImpl<Derived>::Visit(Decl *D) {
292   if (!D)
293     return false;
294
295   switch (D->getKind()) {
296 #define ABSTRACT_DECL(Class, Base)
297 #define DECL(Class, Base) \
298   case Decl::Class: DISPATCH(Class##Decl, Class##Decl, D);
299 #include "clang/AST/DeclNodes.def"
300   }
301
302   return false;
303 }
304
305 template<typename Derived>
306 bool RecursiveASTVisitorImpl<Derived>::VisitNestedNameSpecifier(
307                                                     NestedNameSpecifier *NNS) {
308   if (NNS->getPrefix() &&
309       getDerived().VisitNestedNameSpecifier(NNS->getPrefix()))
310     return true;
311
312   switch (NNS->getKind()) {
313   case NestedNameSpecifier::Identifier:
314   case NestedNameSpecifier::Namespace:
315   case NestedNameSpecifier::Global:
316     return false;
317
318   case NestedNameSpecifier::TypeSpec:
319   case NestedNameSpecifier::TypeSpecWithTemplate:
320     return Visit(QualType(NNS->getAsType(), 0));
321   }
322
323   return false;
324 }
325
326 template<typename Derived>
327 bool RecursiveASTVisitorImpl<Derived>::VisitTemplateName(TemplateName Template) {
328   if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
329     return DTN->getQualifier() &&
330            getDerived().VisitNestedNameSpecifier(DTN->getQualifier());
331
332   if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
333     return getDerived().VisitNestedNameSpecifier(QTN->getQualifier());
334
335   return false;
336 }
337
338 template<typename Derived>
339 bool RecursiveASTVisitorImpl<Derived>::VisitTemplateArgument(
340                                                 const TemplateArgument &Arg) {
341   switch (Arg.getKind()) {
342   case TemplateArgument::Null:
343   case TemplateArgument::Declaration:
344   case TemplateArgument::Integral:
345     return false;
346
347   case TemplateArgument::Type:
348     return Visit(Arg.getAsType());
349
350   case TemplateArgument::Template:
351     return getDerived().VisitTemplateName(Arg.getAsTemplate());
352
353   case TemplateArgument::Expression:
354     return getDerived().Visit(Arg.getAsExpr());
355
356   case TemplateArgument::Pack:
357     return getDerived().VisitTemplateArguments(Arg.pack_begin(),
358                                                Arg.pack_size());
359   }
360
361   return false;
362 }
363
364 template<typename Derived>
365 bool RecursiveASTVisitorImpl<Derived>::VisitTemplateArguments(
366                                                   const TemplateArgument *Args,
367                                                             unsigned NumArgs) {
368   for (unsigned I = 0; I != NumArgs; ++I)
369     if (getDerived().VisitTemplateArgument(Args[I]))
370       return true;
371
372   return false;
373 }
374
375 template<typename Derived>
376 bool RecursiveASTVisitorImpl<Derived>::VisitStmt(Stmt *Node) {
377   for (Stmt::child_iterator C = Node->child_begin(), CEnd = Node->child_end();
378        C != CEnd; ++C) {
379     if (Visit(*C))
380       return true;
381   }
382
383   return false;
384 }
385
386 template<typename Derived>
387 bool RecursiveASTVisitorImpl<Derived>::VisitType(Type *T) {
388   return false;
389 }
390
391 template<typename Derived>
392 bool RecursiveASTVisitorImpl<Derived>::VisitBuiltinType(BuiltinType *T) {
393   return getDerived().VisitType(T);
394 }
395
396 template<typename Derived>
397 bool RecursiveASTVisitorImpl<Derived>::VisitComplexType(ComplexType *T) {
398   if (Visit(T->getElementType()))
399     return true;
400
401   return getDerived().VisitType(T);
402 }
403
404 template<typename Derived>
405 bool RecursiveASTVisitorImpl<Derived>::VisitPointerType(PointerType *T) {
406   if (Visit(T->getPointeeType()))
407     return true;
408
409   return getDerived().VisitType(T);
410 }
411
412 template<typename Derived>
413 bool RecursiveASTVisitorImpl<Derived>::VisitBlockPointerType(
414                                                          BlockPointerType *T) {
415   if (Visit(T->getPointeeType()))
416     return true;
417
418   return getDerived().VisitType(T);
419 }
420
421 template<typename Derived>
422 bool RecursiveASTVisitorImpl<Derived>::VisitReferenceType(ReferenceType *T) {
423   if (Visit(T->getPointeeType()))
424     return true;
425
426   return getDerived().VisitType(T);
427 }
428
429 template<typename Derived>
430 bool RecursiveASTVisitorImpl<Derived>::VisitLValueReferenceType(
431                                                       LValueReferenceType *T) {
432   return getDerived().VisitReferenceType(T);
433 }
434
435 template<typename Derived>
436 bool RecursiveASTVisitorImpl<Derived>::VisitRValueReferenceType(
437                                                       RValueReferenceType *T) {
438   return getDerived().VisitReferenceType(T);
439 }
440
441 template<typename Derived>
442 bool RecursiveASTVisitorImpl<Derived>::VisitMemberPointerType(
443                                                         MemberPointerType *T) {
444   if (Visit(QualType(T->getClass(), 0)) || Visit(T->getPointeeType()))
445     return true;
446
447   return getDerived().VisitType(T);
448 }
449
450 template<typename Derived>
451 bool RecursiveASTVisitorImpl<Derived>::VisitArrayType(ArrayType *T) {
452   if (Visit(T->getElementType()))
453     return true;
454
455   return getDerived().VisitType(T);
456 }
457
458 template<typename Derived>
459 bool RecursiveASTVisitorImpl<Derived>::VisitConstantArrayType(
460                                                         ConstantArrayType *T) {
461   return getDerived().VisitArrayType(T);
462 }
463
464 template<typename Derived>
465 bool RecursiveASTVisitorImpl<Derived>::VisitIncompleteArrayType(
466                                                       IncompleteArrayType *T) {
467   return getDerived().VisitArrayType(T);
468 }
469
470 template<typename Derived>
471 bool RecursiveASTVisitorImpl<Derived>::VisitVariableArrayType(
472                                                         VariableArrayType *T) {
473   if (Visit(T->getSizeExpr()))
474     return true;
475
476   return getDerived().VisitArrayType(T);
477 }
478
479 template<typename Derived>
480 bool RecursiveASTVisitorImpl<Derived>::VisitDependentSizedArrayType(
481                                                   DependentSizedArrayType *T) {
482   if (T->getSizeExpr() && Visit(T->getSizeExpr()))
483     return true;
484
485   return getDerived().VisitArrayType(T);
486 }
487
488 template<typename Derived>
489 bool RecursiveASTVisitorImpl<Derived>::VisitDependentSizedExtVectorType(
490                                               DependentSizedExtVectorType *T) {
491   if ((T->getSizeExpr() && Visit(T->getSizeExpr())) ||
492       Visit(T->getElementType()))
493     return true;
494
495   return getDerived().VisitType(T);
496 }
497
498 template<typename Derived>
499 bool RecursiveASTVisitorImpl<Derived>::VisitVectorType(VectorType *T) {
500   if (Visit(T->getElementType()))
501     return true;
502
503   return getDerived().VisitType(T);
504 }
505
506 template<typename Derived>
507 bool RecursiveASTVisitorImpl<Derived>::VisitExtVectorType(ExtVectorType *T) {
508   return getDerived().VisitVectorType(T);
509 }
510
511 template<typename Derived>
512 bool RecursiveASTVisitorImpl<Derived>::VisitFunctionType(FunctionType *T) {
513   if (Visit(T->getResultType()))
514     return true;
515
516   return getDerived().VisitType(T);
517 }
518
519 template<typename Derived>
520 bool RecursiveASTVisitorImpl<Derived>::VisitFunctionNoProtoType(
521                                                       FunctionNoProtoType *T) {
522   return getDerived().VisitFunctionType(T);
523 }
524
525 template<typename Derived>
526 bool RecursiveASTVisitorImpl<Derived>::VisitFunctionProtoType(
527                                                         FunctionProtoType *T) {
528   for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
529                                          AEnd = T->arg_type_end();
530        A != AEnd; ++A) {
531     if (Visit(*A))
532       return true;
533   }
534
535   for (FunctionProtoType::exception_iterator E = T->exception_begin(),
536                                           EEnd = T->exception_end();
537        E != EEnd; ++E) {
538     if (Visit(*E))
539       return true;
540   }
541
542   return getDerived().VisitFunctionType(T);
543 }
544
545 template<typename Derived>
546 bool RecursiveASTVisitorImpl<Derived>::VisitUnresolvedUsingType(
547                                                       UnresolvedUsingType *T) {
548   return getDerived().VisitType(T);
549 }
550
551 template<typename Derived>
552 bool RecursiveASTVisitorImpl<Derived>::VisitTypedefType(TypedefType *T) {
553   return getDerived().VisitType(T);
554 }
555
556 template<typename Derived>
557 bool RecursiveASTVisitorImpl<Derived>::VisitTypeOfExprType(TypeOfExprType *T) {
558   if (Visit(T->getUnderlyingExpr()))
559     return true;
560
561   return getDerived().VisitType(T);
562 }
563
564 template<typename Derived>
565 bool RecursiveASTVisitorImpl<Derived>::VisitTypeOfType(TypeOfType *T) {
566   if (Visit(T->getUnderlyingType()))
567     return true;
568
569   return getDerived().VisitType(T);
570 }
571
572 template<typename Derived>
573 bool RecursiveASTVisitorImpl<Derived>::VisitDecltypeType(DecltypeType *T) {
574   if (Visit(T->getUnderlyingExpr()))
575     return true;
576
577   return getDerived().VisitType(T);
578 }
579
580 template<typename Derived>
581 bool RecursiveASTVisitorImpl<Derived>::VisitTagType(TagType *T) {
582   return getDerived().VisitType(T);
583 }
584
585 template<typename Derived>
586 bool RecursiveASTVisitorImpl<Derived>::VisitRecordType(RecordType *T) {
587   return getDerived().VisitTagType(T);
588 }
589
590 template<typename Derived>
591 bool RecursiveASTVisitorImpl<Derived>::VisitEnumType(EnumType *T) {
592   return getDerived().VisitType(T);
593 }
594
595 template<typename Derived>
596 bool RecursiveASTVisitorImpl<Derived>::VisitTemplateTypeParmType(
597                                                       TemplateTypeParmType *T) {
598   return getDerived().VisitType(T);
599 }
600
601 template<typename Derived>
602 bool RecursiveASTVisitorImpl<Derived>::VisitSubstTemplateTypeParmType(
603                                                 SubstTemplateTypeParmType *T) {
604   return getDerived().VisitType(T);
605 }
606
607 template<typename Derived>
608 bool RecursiveASTVisitorImpl<Derived>::VisitTemplateSpecializationType(
609                                                TemplateSpecializationType *T) {
610   if (getDerived().VisitTemplateName(T->getTemplateName()) ||
611       getDerived().VisitTemplateArguments(T->getArgs(), T->getNumArgs()))
612     return true;
613
614   return getDerived().VisitType(T);
615 }
616
617 template<typename Derived>
618 bool RecursiveASTVisitorImpl<Derived>::VisitInjectedClassNameType(
619                                                     InjectedClassNameType *T) {
620   return getDerived().VisitType(T);
621 }
622
623 template<typename Derived>
624 bool RecursiveASTVisitorImpl<Derived>::VisitElaboratedType(ElaboratedType *T) {
625   if (T->getQualifier() &&
626       getDerived().VisitNestedNameSpecifier(T->getQualifier()))
627     return true;
628   if (Visit(T->getNamedType()))
629     return true;
630
631   return getDerived().VisitType(T);
632 }
633
634 template<typename Derived>
635 bool RecursiveASTVisitorImpl<Derived>::VisitDependentNameType(
636                                                         DependentNameType *T) {
637   if (T->getQualifier() &&
638       getDerived().VisitNestedNameSpecifier(T->getQualifier()))
639     return true;
640
641   if (T->getTemplateId() &&
642       getDerived().VisitTemplateSpecializationType(
643                 const_cast<TemplateSpecializationType *>(T->getTemplateId())))
644     return true;
645
646   return getDerived().VisitType(T);
647 }
648
649 template<typename Derived>
650 bool RecursiveASTVisitorImpl<Derived>::VisitObjCInterfaceType(
651                                                         ObjCInterfaceType *T) {
652   return getDerived().VisitObjCObjectType(T);
653 }
654
655 template<typename Derived>
656 bool RecursiveASTVisitorImpl<Derived>::VisitObjCObjectType(ObjCObjectType *T) {
657   // We have to watch out here because an ObjCInterfaceType's base
658   // type is itself.
659   if (T->getBaseType().getTypePtr() != T)
660     if (Visit(T->getBaseType()))
661       return true;
662
663   return getDerived().VisitType(T);
664 }
665
666 template<typename Derived>
667 bool RecursiveASTVisitorImpl<Derived>::VisitObjCObjectPointerType(
668                                                     ObjCObjectPointerType *T) {
669   if (Visit(T->getPointeeType()))
670     return true;
671
672   return getDerived().VisitType(T);
673 }
674
675 template<typename Derived>
676 bool RecursiveASTVisitorImpl<Derived>::VisitDecl(Decl *D) {
677   if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
678     for (DeclContext::decl_iterator Child = DC->decls_begin(),
679                                  ChildEnd = DC->decls_end();
680          Child != ChildEnd; ++Child)
681       if (Visit(*Child))
682         return true;
683
684     return false;
685   }
686
687   return false;
688 }
689
690 /// \brief A visitor that recursively walks the entire Clang AST.
691 ///
692 /// Clients of this visitor should subclass the visitor (providing
693 /// themselves as the template argument, using the curiously
694 /// recurring template pattern) and override any of the Visit*
695 /// methods (except Visit()) for declaration, type, statement,
696 /// expression, or other AST nodes where the visitor should customize
697 /// behavior. Returning "true" from one of these overridden functions
698 /// will abort the entire traversal.  An overridden Visit* method
699 /// will not descend further into the AST for that node unless
700 /// Base::Visit* is called.
701 template<typename Derived>
702 class RecursiveASTVisitor : public RecursiveASTVisitorImpl<Derived> {
703   typedef RecursiveASTVisitorImpl<Derived> Impl;
704 public:
705   typedef RecursiveASTVisitor<Derived> Base;
706
707   bool VisitDeclaratorDecl(DeclaratorDecl *D);
708   bool VisitFunctionDecl(FunctionDecl *D);
709   bool VisitVarDecl(VarDecl *D);
710   bool VisitBlockDecl(BlockDecl *D);
711   bool VisitDeclStmt(DeclStmt *S);
712   bool VisitFunctionType(FunctionType *F);
713   bool VisitFunctionProtoType(FunctionProtoType *F);
714 };
715
716 #define DEFINE_VISIT(Type, Name, Statement)                       \
717   template<typename Derived>                                      \
718   bool RecursiveASTVisitor<Derived>::Visit ## Type (Type *Name) { \
719     if (Impl::Visit ## Type (Name)) return true;                  \
720     { Statement; }                                                \
721     return false;                                                 \
722   }
723
724 DEFINE_VISIT(DeclaratorDecl, D, {
725     if (TypeSourceInfo *TInfo = D->getTypeSourceInfo())
726       return this->Visit(TInfo->getType());
727   })
728
729 DEFINE_VISIT(FunctionDecl, D, {
730     if (D->isThisDeclarationADefinition())
731       return this->Visit(D->getBody());
732   })
733
734 DEFINE_VISIT(VarDecl, D, return this->Visit(D->getInit()))
735
736 DEFINE_VISIT(BlockDecl, D, return this->Visit(D->getBody()))
737
738 DEFINE_VISIT(DeclStmt, S, {
739     for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
740          I != E; ++I) {
741       if (this->Visit(*I))
742         return true;
743     }
744   })
745
746 // FunctionType is the common base class of FunctionNoProtoType (a
747 // K&R-style function declaration that has no information about
748 // its arguments) and FunctionProtoType.
749 DEFINE_VISIT(FunctionType, F, return this->Visit(F->getResultType()))
750
751 DEFINE_VISIT(FunctionProtoType, F, {
752     for (unsigned i = 0; i != F->getNumArgs(); ++i) {
753       if (this->Visit(F->getArgType(i)))
754         return true;
755     }
756     for (unsigned i = 0; i != F->getNumExceptions(); ++i) {
757       if (this->Visit(F->getExceptionType(i)))
758         return true;
759     }
760   })
761
762 #undef DEFINE_VISIT
763
764 #undef DISPATCH
765
766 } // end namespace clang
767
768 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H