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