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