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