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