1 //===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the RecursiveASTVisitor interface, which recursively
11 // traverses the entire AST.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
15 #define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H
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"
35 #define DISPATCH(NAME, CLASS, Var) \
36 return getDerived().Visit ## NAME(static_cast<CLASS*>(Var))
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).
47 // Do not use this class directly - use RecursiveASTVisitor instead.
48 template<typename Derived>
49 class RecursiveASTVisitorImpl {
51 /// \brief Return a reference to the derived class.
52 Derived &getDerived() { return *static_cast<Derived*>(this); }
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.
58 /// \returns true if the visitation was terminated early, false
59 /// otherwise (including when the argument is NULL).
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.
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);
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.
74 /// \returns true if the visitation was terminated early, false
75 /// otherwise (including when the argument is NULL).
78 /// \brief Recursively visit a C++ nested-name-specifier.
80 /// \returns true if the visitation was terminated early, false otherwise.
81 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
83 /// \brief Recursively visit a template name.
85 /// \returns true if the visitation was terminated early, false otherwise.
86 bool VisitTemplateName(TemplateName Template);
88 /// \brief Recursively visit a template argument.
90 /// \returns true if the visitation was terminated early, false otherwise.
91 bool VisitTemplateArgument(const TemplateArgument &Arg);
93 /// \brief Recursively visit a set of template arguments.
95 /// \returns true if the visitation was terminated early, false otherwise.
96 bool VisitTemplateArguments(const TemplateArgument *Args, unsigned NumArgs);
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"
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); \
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)
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)
120 BINOP_FALLBACK(Assign)
121 BINOP_FALLBACK(Comma)
122 #undef BINOP_FALLBACK
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); \
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)
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); \
142 UNARYOP_FALLBACK(PostInc) UNARYOP_FALLBACK(PostDec)
143 UNARYOP_FALLBACK(PreInc) UNARYOP_FALLBACK(PreDec)
144 UNARYOP_FALLBACK(AddrOf) UNARYOP_FALLBACK(Deref)
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
152 /// \brief Basis for statement and expression visitation, which
153 /// visits all of the substatements and subexpressions.
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);
162 /// \brief Basis for type visitation, which by default does nothing.
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);
169 #define TYPE(Class, Base) \
170 bool Visit##Class##Type(Class##Type *T);
171 #include "clang/AST/TypeNodes.def"
173 /// \brief Basis for declaration and definition visitation, which
174 /// visits all of the subnodes.
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);
181 #define DECL(Class, Base) \
182 bool Visit##Class##Decl(Class##Decl *D) { \
183 return getDerived().Visit##Base(D); \
185 #define ABSTRACT_DECL(Class, Base) DECL(Class, Base)
186 #include "clang/AST/DeclNodes.def"
189 template<typename Derived>
190 bool RecursiveASTVisitorImpl<Derived>::Visit(Stmt *S) {
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
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);
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);
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);
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);
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"
275 template<typename Derived>
276 bool RecursiveASTVisitorImpl<Derived>::Visit(QualType T) {
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"
290 template<typename Derived>
291 bool RecursiveASTVisitorImpl<Derived>::Visit(Decl *D) {
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"
305 template<typename Derived>
306 bool RecursiveASTVisitorImpl<Derived>::VisitNestedNameSpecifier(
307 NestedNameSpecifier *NNS) {
308 if (NNS->getPrefix() &&
309 getDerived().VisitNestedNameSpecifier(NNS->getPrefix()))
312 switch (NNS->getKind()) {
313 case NestedNameSpecifier::Identifier:
314 case NestedNameSpecifier::Namespace:
315 case NestedNameSpecifier::Global:
318 case NestedNameSpecifier::TypeSpec:
319 case NestedNameSpecifier::TypeSpecWithTemplate:
320 return Visit(QualType(NNS->getAsType(), 0));
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());
332 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
333 return getDerived().VisitNestedNameSpecifier(QTN->getQualifier());
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:
347 case TemplateArgument::Type:
348 return Visit(Arg.getAsType());
350 case TemplateArgument::Template:
351 return getDerived().VisitTemplateName(Arg.getAsTemplate());
353 case TemplateArgument::Expression:
354 return getDerived().Visit(Arg.getAsExpr());
356 case TemplateArgument::Pack:
357 return getDerived().VisitTemplateArguments(Arg.pack_begin(),
364 template<typename Derived>
365 bool RecursiveASTVisitorImpl<Derived>::VisitTemplateArguments(
366 const TemplateArgument *Args,
368 for (unsigned I = 0; I != NumArgs; ++I)
369 if (getDerived().VisitTemplateArgument(Args[I]))
375 template<typename Derived>
376 bool RecursiveASTVisitorImpl<Derived>::VisitStmt(Stmt *Node) {
377 for (Stmt::child_iterator C = Node->child_begin(), CEnd = Node->child_end();
386 template<typename Derived>
387 bool RecursiveASTVisitorImpl<Derived>::VisitType(Type *T) {
391 template<typename Derived>
392 bool RecursiveASTVisitorImpl<Derived>::VisitBuiltinType(BuiltinType *T) {
393 return getDerived().VisitType(T);
396 template<typename Derived>
397 bool RecursiveASTVisitorImpl<Derived>::VisitComplexType(ComplexType *T) {
398 if (Visit(T->getElementType()))
401 return getDerived().VisitType(T);
404 template<typename Derived>
405 bool RecursiveASTVisitorImpl<Derived>::VisitPointerType(PointerType *T) {
406 if (Visit(T->getPointeeType()))
409 return getDerived().VisitType(T);
412 template<typename Derived>
413 bool RecursiveASTVisitorImpl<Derived>::VisitBlockPointerType(
414 BlockPointerType *T) {
415 if (Visit(T->getPointeeType()))
418 return getDerived().VisitType(T);
421 template<typename Derived>
422 bool RecursiveASTVisitorImpl<Derived>::VisitReferenceType(ReferenceType *T) {
423 if (Visit(T->getPointeeType()))
426 return getDerived().VisitType(T);
429 template<typename Derived>
430 bool RecursiveASTVisitorImpl<Derived>::VisitLValueReferenceType(
431 LValueReferenceType *T) {
432 return getDerived().VisitReferenceType(T);
435 template<typename Derived>
436 bool RecursiveASTVisitorImpl<Derived>::VisitRValueReferenceType(
437 RValueReferenceType *T) {
438 return getDerived().VisitReferenceType(T);
441 template<typename Derived>
442 bool RecursiveASTVisitorImpl<Derived>::VisitMemberPointerType(
443 MemberPointerType *T) {
444 if (Visit(QualType(T->getClass(), 0)) || Visit(T->getPointeeType()))
447 return getDerived().VisitType(T);
450 template<typename Derived>
451 bool RecursiveASTVisitorImpl<Derived>::VisitArrayType(ArrayType *T) {
452 if (Visit(T->getElementType()))
455 return getDerived().VisitType(T);
458 template<typename Derived>
459 bool RecursiveASTVisitorImpl<Derived>::VisitConstantArrayType(
460 ConstantArrayType *T) {
461 return getDerived().VisitArrayType(T);
464 template<typename Derived>
465 bool RecursiveASTVisitorImpl<Derived>::VisitIncompleteArrayType(
466 IncompleteArrayType *T) {
467 return getDerived().VisitArrayType(T);
470 template<typename Derived>
471 bool RecursiveASTVisitorImpl<Derived>::VisitVariableArrayType(
472 VariableArrayType *T) {
473 if (Visit(T->getSizeExpr()))
476 return getDerived().VisitArrayType(T);
479 template<typename Derived>
480 bool RecursiveASTVisitorImpl<Derived>::VisitDependentSizedArrayType(
481 DependentSizedArrayType *T) {
482 if (T->getSizeExpr() && Visit(T->getSizeExpr()))
485 return getDerived().VisitArrayType(T);
488 template<typename Derived>
489 bool RecursiveASTVisitorImpl<Derived>::VisitDependentSizedExtVectorType(
490 DependentSizedExtVectorType *T) {
491 if ((T->getSizeExpr() && Visit(T->getSizeExpr())) ||
492 Visit(T->getElementType()))
495 return getDerived().VisitType(T);
498 template<typename Derived>
499 bool RecursiveASTVisitorImpl<Derived>::VisitVectorType(VectorType *T) {
500 if (Visit(T->getElementType()))
503 return getDerived().VisitType(T);
506 template<typename Derived>
507 bool RecursiveASTVisitorImpl<Derived>::VisitExtVectorType(ExtVectorType *T) {
508 return getDerived().VisitVectorType(T);
511 template<typename Derived>
512 bool RecursiveASTVisitorImpl<Derived>::VisitFunctionType(FunctionType *T) {
513 if (Visit(T->getResultType()))
516 return getDerived().VisitType(T);
519 template<typename Derived>
520 bool RecursiveASTVisitorImpl<Derived>::VisitFunctionNoProtoType(
521 FunctionNoProtoType *T) {
522 return getDerived().VisitFunctionType(T);
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();
535 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
536 EEnd = T->exception_end();
542 return getDerived().VisitFunctionType(T);
545 template<typename Derived>
546 bool RecursiveASTVisitorImpl<Derived>::VisitUnresolvedUsingType(
547 UnresolvedUsingType *T) {
548 return getDerived().VisitType(T);
551 template<typename Derived>
552 bool RecursiveASTVisitorImpl<Derived>::VisitTypedefType(TypedefType *T) {
553 return getDerived().VisitType(T);
556 template<typename Derived>
557 bool RecursiveASTVisitorImpl<Derived>::VisitTypeOfExprType(TypeOfExprType *T) {
558 if (Visit(T->getUnderlyingExpr()))
561 return getDerived().VisitType(T);
564 template<typename Derived>
565 bool RecursiveASTVisitorImpl<Derived>::VisitTypeOfType(TypeOfType *T) {
566 if (Visit(T->getUnderlyingType()))
569 return getDerived().VisitType(T);
572 template<typename Derived>
573 bool RecursiveASTVisitorImpl<Derived>::VisitDecltypeType(DecltypeType *T) {
574 if (Visit(T->getUnderlyingExpr()))
577 return getDerived().VisitType(T);
580 template<typename Derived>
581 bool RecursiveASTVisitorImpl<Derived>::VisitTagType(TagType *T) {
582 return getDerived().VisitType(T);
585 template<typename Derived>
586 bool RecursiveASTVisitorImpl<Derived>::VisitRecordType(RecordType *T) {
587 return getDerived().VisitTagType(T);
590 template<typename Derived>
591 bool RecursiveASTVisitorImpl<Derived>::VisitEnumType(EnumType *T) {
592 return getDerived().VisitType(T);
595 template<typename Derived>
596 bool RecursiveASTVisitorImpl<Derived>::VisitTemplateTypeParmType(
597 TemplateTypeParmType *T) {
598 return getDerived().VisitType(T);
601 template<typename Derived>
602 bool RecursiveASTVisitorImpl<Derived>::VisitSubstTemplateTypeParmType(
603 SubstTemplateTypeParmType *T) {
604 return getDerived().VisitType(T);
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()))
614 return getDerived().VisitType(T);
617 template<typename Derived>
618 bool RecursiveASTVisitorImpl<Derived>::VisitInjectedClassNameType(
619 InjectedClassNameType *T) {
620 return getDerived().VisitType(T);
623 template<typename Derived>
624 bool RecursiveASTVisitorImpl<Derived>::VisitElaboratedType(ElaboratedType *T) {
625 if (T->getQualifier() &&
626 getDerived().VisitNestedNameSpecifier(T->getQualifier()))
628 if (Visit(T->getNamedType()))
631 return getDerived().VisitType(T);
634 template<typename Derived>
635 bool RecursiveASTVisitorImpl<Derived>::VisitDependentNameType(
636 DependentNameType *T) {
637 if (T->getQualifier() &&
638 getDerived().VisitNestedNameSpecifier(T->getQualifier()))
641 if (T->getTemplateId() &&
642 getDerived().VisitTemplateSpecializationType(
643 const_cast<TemplateSpecializationType *>(T->getTemplateId())))
646 return getDerived().VisitType(T);
649 template<typename Derived>
650 bool RecursiveASTVisitorImpl<Derived>::VisitObjCInterfaceType(
651 ObjCInterfaceType *T) {
652 return getDerived().VisitObjCObjectType(T);
655 template<typename Derived>
656 bool RecursiveASTVisitorImpl<Derived>::VisitObjCObjectType(ObjCObjectType *T) {
657 // We have to watch out here because an ObjCInterfaceType's base
659 if (T->getBaseType().getTypePtr() != T)
660 if (Visit(T->getBaseType()))
663 return getDerived().VisitType(T);
666 template<typename Derived>
667 bool RecursiveASTVisitorImpl<Derived>::VisitObjCObjectPointerType(
668 ObjCObjectPointerType *T) {
669 if (Visit(T->getPointeeType()))
672 return getDerived().VisitType(T);
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)
690 /// \brief A visitor that recursively walks the entire Clang AST.
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;
705 typedef RecursiveASTVisitor<Derived> Base;
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);
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; \
724 DEFINE_VISIT(DeclaratorDecl, D, {
725 if (TypeSourceInfo *TInfo = D->getTypeSourceInfo())
726 return this->Visit(TInfo->getType());
729 DEFINE_VISIT(FunctionDecl, D, {
730 if (D->isThisDeclarationADefinition())
731 return this->Visit(D->getBody());
734 DEFINE_VISIT(VarDecl, D, return this->Visit(D->getInit()))
736 DEFINE_VISIT(BlockDecl, D, return this->Visit(D->getBody()))
738 DEFINE_VISIT(DeclStmt, S, {
739 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
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()))
751 DEFINE_VISIT(FunctionProtoType, F, {
752 for (unsigned i = 0; i != F->getNumArgs(); ++i) {
753 if (this->Visit(F->getArgType(i)))
756 for (unsigned i = 0; i != F->getNumExceptions(); ++i) {
757 if (this->Visit(F->getExceptionType(i)))
766 } // end namespace clang
768 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H