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