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