DeclBase.h revision 202879
1//===-- DeclBase.h - Base Classes for representing declarations -*- 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 Decl and DeclContext interfaces. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_DECLBASE_H 15#define LLVM_CLANG_AST_DECLBASE_H 16 17#include "clang/AST/Attr.h" 18#include "clang/AST/Type.h" 19#include "clang/Basic/Specifiers.h" 20#include "llvm/Support/PrettyStackTrace.h" 21#include "llvm/ADT/PointerUnion.h" 22 23namespace clang { 24class DeclContext; 25class TranslationUnitDecl; 26class NamespaceDecl; 27class UsingDirectiveDecl; 28class NamedDecl; 29class FunctionDecl; 30class CXXRecordDecl; 31class EnumDecl; 32class ObjCMethodDecl; 33class ObjCContainerDecl; 34class ObjCInterfaceDecl; 35class ObjCCategoryDecl; 36class ObjCProtocolDecl; 37class ObjCImplementationDecl; 38class ObjCCategoryImplDecl; 39class ObjCImplDecl; 40class LinkageSpecDecl; 41class BlockDecl; 42class DeclarationName; 43class CompoundStmt; 44} 45 46namespace llvm { 47// DeclContext* is only 4-byte aligned on 32-bit systems. 48template<> 49 class PointerLikeTypeTraits<clang::DeclContext*> { 50 typedef clang::DeclContext* PT; 51public: 52 static inline void *getAsVoidPointer(PT P) { return P; } 53 static inline PT getFromVoidPointer(void *P) { 54 return static_cast<PT>(P); 55 } 56 enum { NumLowBitsAvailable = 2 }; 57}; 58} 59 60namespace clang { 61 62/// Decl - This represents one declaration (or definition), e.g. a variable, 63/// typedef, function, struct, etc. 64/// 65class Decl { 66public: 67 /// \brief Lists the kind of concrete classes of Decl. 68 enum Kind { 69#define DECL(Derived, Base) Derived, 70#define DECL_RANGE(CommonBase, Start, End) \ 71 CommonBase##First = Start, CommonBase##Last = End, 72#define LAST_DECL_RANGE(CommonBase, Start, End) \ 73 CommonBase##First = Start, CommonBase##Last = End 74#include "clang/AST/DeclNodes.def" 75 }; 76 77 /// IdentifierNamespace - According to C99 6.2.3, there are four 78 /// namespaces, labels, tags, members and ordinary 79 /// identifiers. These are meant as bitmasks, so that searches in 80 /// C++ can look into the "tag" namespace during ordinary lookup. We 81 /// use additional namespaces for Objective-C entities. We also put 82 /// C++ friend declarations (of previously-undeclared entities) in 83 /// shadow namespaces, and 'using' declarations (as opposed to their 84 /// implicit shadow declarations) can be found in their own 85 /// namespace. 86 enum IdentifierNamespace { 87 IDNS_Label = 0x1, 88 IDNS_Tag = 0x2, 89 IDNS_Member = 0x4, 90 IDNS_Ordinary = 0x8, 91 IDNS_ObjCProtocol = 0x10, 92 IDNS_ObjCImplementation = 0x20, 93 IDNS_ObjCCategoryName = 0x40, 94 IDNS_OrdinaryFriend = 0x80, 95 IDNS_TagFriend = 0x100, 96 IDNS_Using = 0x200 97 }; 98 99 /// ObjCDeclQualifier - Qualifier used on types in method declarations 100 /// for remote messaging. They are meant for the arguments though and 101 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl). 102 enum ObjCDeclQualifier { 103 OBJC_TQ_None = 0x0, 104 OBJC_TQ_In = 0x1, 105 OBJC_TQ_Inout = 0x2, 106 OBJC_TQ_Out = 0x4, 107 OBJC_TQ_Bycopy = 0x8, 108 OBJC_TQ_Byref = 0x10, 109 OBJC_TQ_Oneway = 0x20 110 }; 111 112private: 113 /// NextDeclInContext - The next declaration within the same lexical 114 /// DeclContext. These pointers form the linked list that is 115 /// traversed via DeclContext's decls_begin()/decls_end(). 116 Decl *NextDeclInContext; 117 118 friend class DeclContext; 119 120 struct MultipleDC { 121 DeclContext *SemanticDC; 122 DeclContext *LexicalDC; 123 }; 124 125 126 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*. 127 /// For declarations that don't contain C++ scope specifiers, it contains 128 /// the DeclContext where the Decl was declared. 129 /// For declarations with C++ scope specifiers, it contains a MultipleDC* 130 /// with the context where it semantically belongs (SemanticDC) and the 131 /// context where it was lexically declared (LexicalDC). 132 /// e.g.: 133 /// 134 /// namespace A { 135 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 136 /// } 137 /// void A::f(); // SemanticDC == namespace 'A' 138 /// // LexicalDC == global namespace 139 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx; 140 141 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); } 142 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); } 143 inline MultipleDC *getMultipleDC() const { 144 return DeclCtx.get<MultipleDC*>(); 145 } 146 inline DeclContext *getSemanticDC() const { 147 return DeclCtx.get<DeclContext*>(); 148 } 149 150 /// Loc - The location that this decl. 151 SourceLocation Loc; 152 153 /// DeclKind - This indicates which class this is. 154 Kind DeclKind : 8; 155 156 /// InvalidDecl - This indicates a semantic error occurred. 157 unsigned int InvalidDecl : 1; 158 159 /// HasAttrs - This indicates whether the decl has attributes or not. 160 unsigned int HasAttrs : 1; 161 162 /// Implicit - Whether this declaration was implicitly generated by 163 /// the implementation rather than explicitly written by the user. 164 bool Implicit : 1; 165 166 /// \brief Whether this declaration was "used", meaning that a definition is 167 /// required. 168 bool Used : 1; 169 170protected: 171 /// Access - Used by C++ decls for the access specifier. 172 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum 173 unsigned Access : 2; 174 friend class CXXClassMemberWrapper; 175 176 // PCHLevel - the "level" of precompiled header/AST file from which this 177 // declaration was built. 178 unsigned PCHLevel : 2; 179 180 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in. 181 unsigned IdentifierNamespace : 16; 182 183private: 184#ifndef NDEBUG 185 void CheckAccessDeclContext() const; 186#else 187 void CheckAccessDeclContext() const { } 188#endif 189 190protected: 191 192 Decl(Kind DK, DeclContext *DC, SourceLocation L) 193 : NextDeclInContext(0), DeclCtx(DC), 194 Loc(L), DeclKind(DK), InvalidDecl(0), 195 HasAttrs(false), Implicit(false), Used(false), 196 Access(AS_none), PCHLevel(0), 197 IdentifierNamespace(getIdentifierNamespaceForKind(DK)) { 198 if (Decl::CollectingStats()) addDeclKind(DK); 199 } 200 201 virtual ~Decl(); 202 203public: 204 205 /// \brief Source range that this declaration covers. 206 virtual SourceRange getSourceRange() const { 207 return SourceRange(getLocation(), getLocation()); 208 } 209 SourceLocation getLocStart() const { return getSourceRange().getBegin(); } 210 SourceLocation getLocEnd() const { return getSourceRange().getEnd(); } 211 212 SourceLocation getLocation() const { return Loc; } 213 void setLocation(SourceLocation L) { Loc = L; } 214 215 Kind getKind() const { return DeclKind; } 216 const char *getDeclKindName() const; 217 218 Decl *getNextDeclInContext() { return NextDeclInContext; } 219 const Decl *getNextDeclInContext() const { return NextDeclInContext; } 220 221 DeclContext *getDeclContext() { 222 if (isInSemaDC()) 223 return getSemanticDC(); 224 return getMultipleDC()->SemanticDC; 225 } 226 const DeclContext *getDeclContext() const { 227 return const_cast<Decl*>(this)->getDeclContext(); 228 } 229 230 TranslationUnitDecl *getTranslationUnitDecl(); 231 const TranslationUnitDecl *getTranslationUnitDecl() const { 232 return const_cast<Decl*>(this)->getTranslationUnitDecl(); 233 } 234 235 bool isInAnonymousNamespace() const; 236 237 ASTContext &getASTContext() const; 238 239 void setAccess(AccessSpecifier AS) { 240 Access = AS; 241 CheckAccessDeclContext(); 242 } 243 244 AccessSpecifier getAccess() const { 245 CheckAccessDeclContext(); 246 return AccessSpecifier(Access); 247 } 248 249 bool hasAttrs() const { return HasAttrs; } 250 void addAttr(Attr *attr); 251 const Attr *getAttrs() const { 252 if (!HasAttrs) return 0; // common case, no attributes. 253 return getAttrsImpl(); // Uncommon case, out of line hash lookup. 254 } 255 void swapAttrs(Decl *D); 256 void invalidateAttrs(); 257 258 template<typename T> const T *getAttr() const { 259 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext()) 260 if (const T *V = dyn_cast<T>(attr)) 261 return V; 262 return 0; 263 } 264 265 template<typename T> bool hasAttr() const { 266 return getAttr<T>() != 0; 267 } 268 269 /// setInvalidDecl - Indicates the Decl had a semantic error. This 270 /// allows for graceful error recovery. 271 void setInvalidDecl(bool Invalid = true) { InvalidDecl = Invalid; } 272 bool isInvalidDecl() const { return (bool) InvalidDecl; } 273 274 /// isImplicit - Indicates whether the declaration was implicitly 275 /// generated by the implementation. If false, this declaration 276 /// was written explicitly in the source code. 277 bool isImplicit() const { return Implicit; } 278 void setImplicit(bool I = true) { Implicit = I; } 279 280 /// \brief Whether this declaration was used, meaning that a definition 281 /// is required. 282 bool isUsed() const { return Used; } 283 void setUsed(bool U = true) { Used = U; } 284 285 /// \brief Retrieve the level of precompiled header from which this 286 /// declaration was generated. 287 /// 288 /// The PCH level of a declaration describes where the declaration originated 289 /// from. A PCH level of 0 indicates that the declaration was not from a 290 /// precompiled header. A PCH level of 1 indicates that the declaration was 291 /// from a top-level precompiled header; 2 indicates that the declaration 292 /// comes from a precompiled header on which the top-level precompiled header 293 /// depends, and so on. 294 unsigned getPCHLevel() const { return PCHLevel; } 295 296 /// \brief The maximum PCH level that any declaration may have. 297 static const unsigned MaxPCHLevel = 3; 298 299 /// \brief Set the PCH level of this declaration. 300 void setPCHLevel(unsigned Level) { 301 assert(Level < MaxPCHLevel && "PCH level exceeds the maximum"); 302 PCHLevel = Level; 303 } 304 305 unsigned getIdentifierNamespace() const { 306 return IdentifierNamespace; 307 } 308 bool isInIdentifierNamespace(unsigned NS) const { 309 return getIdentifierNamespace() & NS; 310 } 311 static unsigned getIdentifierNamespaceForKind(Kind DK); 312 313 314 /// getLexicalDeclContext - The declaration context where this Decl was 315 /// lexically declared (LexicalDC). May be different from 316 /// getDeclContext() (SemanticDC). 317 /// e.g.: 318 /// 319 /// namespace A { 320 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 321 /// } 322 /// void A::f(); // SemanticDC == namespace 'A' 323 /// // LexicalDC == global namespace 324 DeclContext *getLexicalDeclContext() { 325 if (isInSemaDC()) 326 return getSemanticDC(); 327 return getMultipleDC()->LexicalDC; 328 } 329 const DeclContext *getLexicalDeclContext() const { 330 return const_cast<Decl*>(this)->getLexicalDeclContext(); 331 } 332 333 bool isOutOfLine() const { 334 return getLexicalDeclContext() != getDeclContext(); 335 } 336 337 /// setDeclContext - Set both the semantic and lexical DeclContext 338 /// to DC. 339 void setDeclContext(DeclContext *DC); 340 341 void setLexicalDeclContext(DeclContext *DC); 342 343 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this 344 // scoped decl is defined outside the current function or method. This is 345 // roughly global variables and functions, but also handles enums (which could 346 // be defined inside or outside a function etc). 347 bool isDefinedOutsideFunctionOrMethod() const; 348 349 /// \brief Retrieves the "canonical" declaration of the given declaration. 350 virtual Decl *getCanonicalDecl() { return this; } 351 const Decl *getCanonicalDecl() const { 352 return const_cast<Decl*>(this)->getCanonicalDecl(); 353 } 354 355 /// \brief Whether this particular Decl is a canonical one. 356 bool isCanonicalDecl() const { return getCanonicalDecl() == this; } 357 358protected: 359 /// \brief Returns the next redeclaration or itself if this is the only decl. 360 /// 361 /// Decl subclasses that can be redeclared should override this method so that 362 /// Decl::redecl_iterator can iterate over them. 363 virtual Decl *getNextRedeclaration() { return this; } 364 365public: 366 /// \brief Iterates through all the redeclarations of the same decl. 367 class redecl_iterator { 368 /// Current - The current declaration. 369 Decl *Current; 370 Decl *Starter; 371 372 public: 373 typedef Decl* value_type; 374 typedef Decl* reference; 375 typedef Decl* pointer; 376 typedef std::forward_iterator_tag iterator_category; 377 typedef std::ptrdiff_t difference_type; 378 379 redecl_iterator() : Current(0) { } 380 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { } 381 382 reference operator*() const { return Current; } 383 pointer operator->() const { return Current; } 384 385 redecl_iterator& operator++() { 386 assert(Current && "Advancing while iterator has reached end"); 387 // Get either previous decl or latest decl. 388 Decl *Next = Current->getNextRedeclaration(); 389 assert(Next && "Should return next redeclaration or itself, never null!"); 390 Current = (Next != Starter ? Next : 0); 391 return *this; 392 } 393 394 redecl_iterator operator++(int) { 395 redecl_iterator tmp(*this); 396 ++(*this); 397 return tmp; 398 } 399 400 friend bool operator==(redecl_iterator x, redecl_iterator y) { 401 return x.Current == y.Current; 402 } 403 friend bool operator!=(redecl_iterator x, redecl_iterator y) { 404 return x.Current != y.Current; 405 } 406 }; 407 408 /// \brief Returns iterator for all the redeclarations of the same decl. 409 /// It will iterate at least once (when this decl is the only one). 410 redecl_iterator redecls_begin() const { 411 return redecl_iterator(const_cast<Decl*>(this)); 412 } 413 redecl_iterator redecls_end() const { return redecl_iterator(); } 414 415 /// getBody - If this Decl represents a declaration for a body of code, 416 /// such as a function or method definition, this method returns the 417 /// top-level Stmt* of that body. Otherwise this method returns null. 418 virtual Stmt* getBody() const { return 0; } 419 420 /// getCompoundBody - Returns getBody(), dyn_casted to a CompoundStmt. 421 CompoundStmt* getCompoundBody() const; 422 423 /// getBodyRBrace - Gets the right brace of the body, if a body exists. 424 /// This works whether the body is a CompoundStmt or a CXXTryStmt. 425 SourceLocation getBodyRBrace() const; 426 427 // global temp stats (until we have a per-module visitor) 428 static void addDeclKind(Kind k); 429 static bool CollectingStats(bool Enable = false); 430 static void PrintStats(); 431 432 /// isTemplateParameter - Determines whether this declaration is a 433 /// template parameter. 434 bool isTemplateParameter() const; 435 436 /// isTemplateParameter - Determines whether this declaration is a 437 /// template parameter pack. 438 bool isTemplateParameterPack() const; 439 440 /// \brief Whether this declaration is a function or function template. 441 bool isFunctionOrFunctionTemplate() const; 442 443 /// \brief Changes the namespace of this declaration to reflect that it's 444 /// the object of a friend declaration. 445 /// 446 /// These declarations appear in the lexical context of the friending 447 /// class, but in the semantic context of the actual entity. This property 448 /// applies only to a specific decl object; other redeclarations of the 449 /// same entity may not (and probably don't) share this property. 450 void setObjectOfFriendDecl(bool PreviouslyDeclared) { 451 unsigned OldNS = IdentifierNamespace; 452 assert((OldNS == IDNS_Tag || OldNS == IDNS_Ordinary || 453 OldNS == (IDNS_Tag | IDNS_Ordinary)) 454 && "unsupported namespace for undeclared friend"); 455 if (!PreviouslyDeclared) IdentifierNamespace = 0; 456 457 if (OldNS == IDNS_Tag) 458 IdentifierNamespace |= IDNS_TagFriend; 459 else 460 IdentifierNamespace |= IDNS_OrdinaryFriend; 461 } 462 463 enum FriendObjectKind { 464 FOK_None, // not a friend object 465 FOK_Declared, // a friend of a previously-declared entity 466 FOK_Undeclared // a friend of a previously-undeclared entity 467 }; 468 469 /// \brief Determines whether this declaration is the object of a 470 /// friend declaration and, if so, what kind. 471 /// 472 /// There is currently no direct way to find the associated FriendDecl. 473 FriendObjectKind getFriendObjectKind() const { 474 unsigned mask 475 = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend)); 476 if (!mask) return FOK_None; 477 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? 478 FOK_Declared : FOK_Undeclared); 479 } 480 481 // Implement isa/cast/dyncast/etc. 482 static bool classof(const Decl *) { return true; } 483 static DeclContext *castToDeclContext(const Decl *); 484 static Decl *castFromDeclContext(const DeclContext *); 485 486 /// Destroy - Call destructors and release memory. 487 virtual void Destroy(ASTContext& C); 488 489 void print(llvm::raw_ostream &Out, unsigned Indentation = 0) const; 490 void print(llvm::raw_ostream &Out, const PrintingPolicy &Policy, 491 unsigned Indentation = 0) const; 492 static void printGroup(Decl** Begin, unsigned NumDecls, 493 llvm::raw_ostream &Out, const PrintingPolicy &Policy, 494 unsigned Indentation = 0); 495 void dump() const; 496 497private: 498 const Attr *getAttrsImpl() const; 499 500}; 501 502/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when 503/// doing something to a specific decl. 504class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { 505 const Decl *TheDecl; 506 SourceLocation Loc; 507 SourceManager &SM; 508 const char *Message; 509public: 510 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L, 511 SourceManager &sm, const char *Msg) 512 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} 513 514 virtual void print(llvm::raw_ostream &OS) const; 515}; 516 517 518/// DeclContext - This is used only as base class of specific decl types that 519/// can act as declaration contexts. These decls are (only the top classes 520/// that directly derive from DeclContext are mentioned, not their subclasses): 521/// 522/// TranslationUnitDecl 523/// NamespaceDecl 524/// FunctionDecl 525/// TagDecl 526/// ObjCMethodDecl 527/// ObjCContainerDecl 528/// LinkageSpecDecl 529/// BlockDecl 530/// 531class DeclContext { 532 /// DeclKind - This indicates which class this is. 533 Decl::Kind DeclKind : 8; 534 535 /// \brief Whether this declaration context also has some external 536 /// storage that contains additional declarations that are lexically 537 /// part of this context. 538 mutable bool ExternalLexicalStorage : 1; 539 540 /// \brief Whether this declaration context also has some external 541 /// storage that contains additional declarations that are visible 542 /// in this context. 543 mutable bool ExternalVisibleStorage : 1; 544 545 /// \brief Pointer to the data structure used to lookup declarations 546 /// within this context, which is a DenseMap<DeclarationName, 547 /// StoredDeclsList>. 548 mutable void* LookupPtr; 549 550 /// FirstDecl - The first declaration stored within this declaration 551 /// context. 552 mutable Decl *FirstDecl; 553 554 /// LastDecl - The last declaration stored within this declaration 555 /// context. FIXME: We could probably cache this value somewhere 556 /// outside of the DeclContext, to reduce the size of DeclContext by 557 /// another pointer. 558 mutable Decl *LastDecl; 559 560protected: 561 DeclContext(Decl::Kind K) 562 : DeclKind(K), ExternalLexicalStorage(false), 563 ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0), 564 LastDecl(0) { } 565 566 void DestroyDecls(ASTContext &C); 567 568public: 569 ~DeclContext(); 570 571 Decl::Kind getDeclKind() const { 572 return DeclKind; 573 } 574 const char *getDeclKindName() const; 575 576 /// getParent - Returns the containing DeclContext. 577 DeclContext *getParent() { 578 return cast<Decl>(this)->getDeclContext(); 579 } 580 const DeclContext *getParent() const { 581 return const_cast<DeclContext*>(this)->getParent(); 582 } 583 584 /// getLexicalParent - Returns the containing lexical DeclContext. May be 585 /// different from getParent, e.g.: 586 /// 587 /// namespace A { 588 /// struct S; 589 /// } 590 /// struct A::S {}; // getParent() == namespace 'A' 591 /// // getLexicalParent() == translation unit 592 /// 593 DeclContext *getLexicalParent() { 594 return cast<Decl>(this)->getLexicalDeclContext(); 595 } 596 const DeclContext *getLexicalParent() const { 597 return const_cast<DeclContext*>(this)->getLexicalParent(); 598 } 599 600 DeclContext *getLookupParent(); 601 602 const DeclContext *getLookupParent() const { 603 return const_cast<DeclContext*>(this)->getLookupParent(); 604 } 605 606 ASTContext &getParentASTContext() const { 607 return cast<Decl>(this)->getASTContext(); 608 } 609 610 bool isFunctionOrMethod() const { 611 switch (DeclKind) { 612 case Decl::Block: 613 case Decl::ObjCMethod: 614 return true; 615 default: 616 return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast; 617 } 618 } 619 620 bool isFileContext() const { 621 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; 622 } 623 624 bool isTranslationUnit() const { 625 return DeclKind == Decl::TranslationUnit; 626 } 627 628 bool isRecord() const { 629 return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast; 630 } 631 632 bool isNamespace() const { 633 return DeclKind == Decl::Namespace; 634 } 635 636 /// \brief Determines whether this context is dependent on a 637 /// template parameter. 638 bool isDependentContext() const; 639 640 /// isTransparentContext - Determines whether this context is a 641 /// "transparent" context, meaning that the members declared in this 642 /// context are semantically declared in the nearest enclosing 643 /// non-transparent (opaque) context but are lexically declared in 644 /// this context. For example, consider the enumerators of an 645 /// enumeration type: 646 /// @code 647 /// enum E { 648 /// Val1 649 /// }; 650 /// @endcode 651 /// Here, E is a transparent context, so its enumerator (Val1) will 652 /// appear (semantically) that it is in the same context of E. 653 /// Examples of transparent contexts include: enumerations (except for 654 /// C++0x scoped enums), C++ linkage specifications, and C++0x 655 /// inline namespaces. 656 bool isTransparentContext() const; 657 658 /// \brief Determine whether this declaration context is equivalent 659 /// to the declaration context DC. 660 bool Equals(DeclContext *DC) { 661 return this->getPrimaryContext() == DC->getPrimaryContext(); 662 } 663 664 /// \brief Determine whether this declaration context encloses the 665 /// declaration context DC. 666 bool Encloses(DeclContext *DC); 667 668 /// getPrimaryContext - There may be many different 669 /// declarations of the same entity (including forward declarations 670 /// of classes, multiple definitions of namespaces, etc.), each with 671 /// a different set of declarations. This routine returns the 672 /// "primary" DeclContext structure, which will contain the 673 /// information needed to perform name lookup into this context. 674 DeclContext *getPrimaryContext(); 675 676 /// getLookupContext - Retrieve the innermost non-transparent 677 /// context of this context, which corresponds to the innermost 678 /// location from which name lookup can find the entities in this 679 /// context. 680 DeclContext *getLookupContext(); 681 const DeclContext *getLookupContext() const { 682 return const_cast<DeclContext *>(this)->getLookupContext(); 683 } 684 685 /// \brief Retrieve the nearest enclosing namespace context. 686 DeclContext *getEnclosingNamespaceContext(); 687 const DeclContext *getEnclosingNamespaceContext() const { 688 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); 689 } 690 691 /// getNextContext - If this is a DeclContext that may have other 692 /// DeclContexts that are semantically connected but syntactically 693 /// different, such as C++ namespaces, this routine retrieves the 694 /// next DeclContext in the link. Iteration through the chain of 695 /// DeclContexts should begin at the primary DeclContext and 696 /// continue until this function returns NULL. For example, given: 697 /// @code 698 /// namespace N { 699 /// int x; 700 /// } 701 /// namespace N { 702 /// int y; 703 /// } 704 /// @endcode 705 /// The first occurrence of namespace N will be the primary 706 /// DeclContext. Its getNextContext will return the second 707 /// occurrence of namespace N. 708 DeclContext *getNextContext(); 709 710 /// decl_iterator - Iterates through the declarations stored 711 /// within this context. 712 class decl_iterator { 713 /// Current - The current declaration. 714 Decl *Current; 715 716 public: 717 typedef Decl* value_type; 718 typedef Decl* reference; 719 typedef Decl* pointer; 720 typedef std::forward_iterator_tag iterator_category; 721 typedef std::ptrdiff_t difference_type; 722 723 decl_iterator() : Current(0) { } 724 explicit decl_iterator(Decl *C) : Current(C) { } 725 726 reference operator*() const { return Current; } 727 pointer operator->() const { return Current; } 728 729 decl_iterator& operator++() { 730 Current = Current->getNextDeclInContext(); 731 return *this; 732 } 733 734 decl_iterator operator++(int) { 735 decl_iterator tmp(*this); 736 ++(*this); 737 return tmp; 738 } 739 740 friend bool operator==(decl_iterator x, decl_iterator y) { 741 return x.Current == y.Current; 742 } 743 friend bool operator!=(decl_iterator x, decl_iterator y) { 744 return x.Current != y.Current; 745 } 746 }; 747 748 /// decls_begin/decls_end - Iterate over the declarations stored in 749 /// this context. 750 decl_iterator decls_begin() const; 751 decl_iterator decls_end() const; 752 bool decls_empty() const; 753 754 /// specific_decl_iterator - Iterates over a subrange of 755 /// declarations stored in a DeclContext, providing only those that 756 /// are of type SpecificDecl (or a class derived from it). This 757 /// iterator is used, for example, to provide iteration over just 758 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). 759 template<typename SpecificDecl> 760 class specific_decl_iterator { 761 /// Current - The current, underlying declaration iterator, which 762 /// will either be NULL or will point to a declaration of 763 /// type SpecificDecl. 764 DeclContext::decl_iterator Current; 765 766 /// SkipToNextDecl - Advances the current position up to the next 767 /// declaration of type SpecificDecl that also meets the criteria 768 /// required by Acceptable. 769 void SkipToNextDecl() { 770 while (*Current && !isa<SpecificDecl>(*Current)) 771 ++Current; 772 } 773 774 public: 775 typedef SpecificDecl* value_type; 776 typedef SpecificDecl* reference; 777 typedef SpecificDecl* pointer; 778 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 779 difference_type; 780 typedef std::forward_iterator_tag iterator_category; 781 782 specific_decl_iterator() : Current() { } 783 784 /// specific_decl_iterator - Construct a new iterator over a 785 /// subset of the declarations the range [C, 786 /// end-of-declarations). If A is non-NULL, it is a pointer to a 787 /// member function of SpecificDecl that should return true for 788 /// all of the SpecificDecl instances that will be in the subset 789 /// of iterators. For example, if you want Objective-C instance 790 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 791 /// &ObjCMethodDecl::isInstanceMethod. 792 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 793 SkipToNextDecl(); 794 } 795 796 reference operator*() const { return cast<SpecificDecl>(*Current); } 797 pointer operator->() const { return cast<SpecificDecl>(*Current); } 798 799 specific_decl_iterator& operator++() { 800 ++Current; 801 SkipToNextDecl(); 802 return *this; 803 } 804 805 specific_decl_iterator operator++(int) { 806 specific_decl_iterator tmp(*this); 807 ++(*this); 808 return tmp; 809 } 810 811 friend bool 812 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) { 813 return x.Current == y.Current; 814 } 815 816 friend bool 817 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) { 818 return x.Current != y.Current; 819 } 820 }; 821 822 /// \brief Iterates over a filtered subrange of declarations stored 823 /// in a DeclContext. 824 /// 825 /// This iterator visits only those declarations that are of type 826 /// SpecificDecl (or a class derived from it) and that meet some 827 /// additional run-time criteria. This iterator is used, for 828 /// example, to provide access to the instance methods within an 829 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and 830 /// Acceptable = ObjCMethodDecl::isInstanceMethod). 831 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> 832 class filtered_decl_iterator { 833 /// Current - The current, underlying declaration iterator, which 834 /// will either be NULL or will point to a declaration of 835 /// type SpecificDecl. 836 DeclContext::decl_iterator Current; 837 838 /// SkipToNextDecl - Advances the current position up to the next 839 /// declaration of type SpecificDecl that also meets the criteria 840 /// required by Acceptable. 841 void SkipToNextDecl() { 842 while (*Current && 843 (!isa<SpecificDecl>(*Current) || 844 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) 845 ++Current; 846 } 847 848 public: 849 typedef SpecificDecl* value_type; 850 typedef SpecificDecl* reference; 851 typedef SpecificDecl* pointer; 852 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 853 difference_type; 854 typedef std::forward_iterator_tag iterator_category; 855 856 filtered_decl_iterator() : Current() { } 857 858 /// specific_decl_iterator - Construct a new iterator over a 859 /// subset of the declarations the range [C, 860 /// end-of-declarations). If A is non-NULL, it is a pointer to a 861 /// member function of SpecificDecl that should return true for 862 /// all of the SpecificDecl instances that will be in the subset 863 /// of iterators. For example, if you want Objective-C instance 864 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 865 /// &ObjCMethodDecl::isInstanceMethod. 866 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 867 SkipToNextDecl(); 868 } 869 870 reference operator*() const { return cast<SpecificDecl>(*Current); } 871 pointer operator->() const { return cast<SpecificDecl>(*Current); } 872 873 filtered_decl_iterator& operator++() { 874 ++Current; 875 SkipToNextDecl(); 876 return *this; 877 } 878 879 filtered_decl_iterator operator++(int) { 880 filtered_decl_iterator tmp(*this); 881 ++(*this); 882 return tmp; 883 } 884 885 friend bool 886 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 887 return x.Current == y.Current; 888 } 889 890 friend bool 891 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 892 return x.Current != y.Current; 893 } 894 }; 895 896 /// @brief Add the declaration D into this context. 897 /// 898 /// This routine should be invoked when the declaration D has first 899 /// been declared, to place D into the context where it was 900 /// (lexically) defined. Every declaration must be added to one 901 /// (and only one!) context, where it can be visited via 902 /// [decls_begin(), decls_end()). Once a declaration has been added 903 /// to its lexical context, the corresponding DeclContext owns the 904 /// declaration. 905 /// 906 /// If D is also a NamedDecl, it will be made visible within its 907 /// semantic context via makeDeclVisibleInContext. 908 void addDecl(Decl *D); 909 910 /// @brief Add the declaration D to this context without modifying 911 /// any lookup tables. 912 /// 913 /// This is useful for some operations in dependent contexts where 914 /// the semantic context might not be dependent; this basically 915 /// only happens with friends. 916 void addHiddenDecl(Decl *D); 917 918 /// @brief Removes a declaration from this context. 919 void removeDecl(Decl *D); 920 921 /// lookup_iterator - An iterator that provides access to the results 922 /// of looking up a name within this context. 923 typedef NamedDecl **lookup_iterator; 924 925 /// lookup_const_iterator - An iterator that provides non-mutable 926 /// access to the results of lookup up a name within this context. 927 typedef NamedDecl * const * lookup_const_iterator; 928 929 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result; 930 typedef std::pair<lookup_const_iterator, lookup_const_iterator> 931 lookup_const_result; 932 933 /// lookup - Find the declarations (if any) with the given Name in 934 /// this context. Returns a range of iterators that contains all of 935 /// the declarations with this name, with object, function, member, 936 /// and enumerator names preceding any tag name. Note that this 937 /// routine will not look into parent contexts. 938 lookup_result lookup(DeclarationName Name); 939 lookup_const_result lookup(DeclarationName Name) const; 940 941 /// @brief Makes a declaration visible within this context. 942 /// 943 /// This routine makes the declaration D visible to name lookup 944 /// within this context and, if this is a transparent context, 945 /// within its parent contexts up to the first enclosing 946 /// non-transparent context. Making a declaration visible within a 947 /// context does not transfer ownership of a declaration, and a 948 /// declaration can be visible in many contexts that aren't its 949 /// lexical context. 950 /// 951 /// If D is a redeclaration of an existing declaration that is 952 /// visible from this context, as determined by 953 /// NamedDecl::declarationReplaces, the previous declaration will be 954 /// replaced with D. 955 /// 956 /// @param Recoverable true if it's okay to not add this decl to 957 /// the lookup tables because it can be easily recovered by walking 958 /// the declaration chains. 959 void makeDeclVisibleInContext(NamedDecl *D, bool Recoverable = true); 960 961 /// udir_iterator - Iterates through the using-directives stored 962 /// within this context. 963 typedef UsingDirectiveDecl * const * udir_iterator; 964 965 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range; 966 967 udir_iterator_range getUsingDirectives() const; 968 969 udir_iterator using_directives_begin() const { 970 return getUsingDirectives().first; 971 } 972 973 udir_iterator using_directives_end() const { 974 return getUsingDirectives().second; 975 } 976 977 // Low-level accessors 978 979 /// \brief Retrieve the internal representation of the lookup structure. 980 void* getLookupPtr() const { return LookupPtr; } 981 982 /// \brief Whether this DeclContext has external storage containing 983 /// additional declarations that are lexically in this context. 984 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; } 985 986 /// \brief State whether this DeclContext has external storage for 987 /// declarations lexically in this context. 988 void setHasExternalLexicalStorage(bool ES = true) { 989 ExternalLexicalStorage = ES; 990 } 991 992 /// \brief Whether this DeclContext has external storage containing 993 /// additional declarations that are visible in this context. 994 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; } 995 996 /// \brief State whether this DeclContext has external storage for 997 /// declarations visible in this context. 998 void setHasExternalVisibleStorage(bool ES = true) { 999 ExternalVisibleStorage = ES; 1000 } 1001 1002 static bool classof(const Decl *D); 1003 static bool classof(const DeclContext *D) { return true; } 1004#define DECL_CONTEXT(Name) \ 1005 static bool classof(const Name##Decl *D) { return true; } 1006#include "clang/AST/DeclNodes.def" 1007 1008 void dumpDeclContext() const; 1009 1010private: 1011 void LoadLexicalDeclsFromExternalStorage() const; 1012 void LoadVisibleDeclsFromExternalStorage() const; 1013 1014 void buildLookup(DeclContext *DCtx); 1015 void makeDeclVisibleInContextImpl(NamedDecl *D); 1016}; 1017 1018inline bool Decl::isTemplateParameter() const { 1019 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm || 1020 getKind() == TemplateTemplateParm; 1021} 1022 1023} // end clang. 1024 1025namespace llvm { 1026 1027/// Implement a isa_impl_wrap specialization to check whether a DeclContext is 1028/// a specific Decl. 1029template<class ToTy> 1030struct isa_impl_wrap<ToTy, 1031 const ::clang::DeclContext,const ::clang::DeclContext> { 1032 static bool doit(const ::clang::DeclContext &Val) { 1033 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val)); 1034 } 1035}; 1036template<class ToTy> 1037struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> 1038 : public isa_impl_wrap<ToTy, 1039 const ::clang::DeclContext,const ::clang::DeclContext> {}; 1040 1041/// Implement cast_convert_val for Decl -> DeclContext conversions. 1042template<class FromTy> 1043struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { 1044 static ::clang::DeclContext &doit(const FromTy &Val) { 1045 return *FromTy::castToDeclContext(&Val); 1046 } 1047}; 1048 1049template<class FromTy> 1050struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { 1051 static ::clang::DeclContext *doit(const FromTy *Val) { 1052 return FromTy::castToDeclContext(Val); 1053 } 1054}; 1055 1056template<class FromTy> 1057struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { 1058 static const ::clang::DeclContext &doit(const FromTy &Val) { 1059 return *FromTy::castToDeclContext(&Val); 1060 } 1061}; 1062 1063template<class FromTy> 1064struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { 1065 static const ::clang::DeclContext *doit(const FromTy *Val) { 1066 return FromTy::castToDeclContext(Val); 1067 } 1068}; 1069 1070/// Implement cast_convert_val for DeclContext -> Decl conversions. 1071template<class ToTy> 1072struct cast_convert_val<ToTy, 1073 const ::clang::DeclContext,const ::clang::DeclContext> { 1074 static ToTy &doit(const ::clang::DeclContext &Val) { 1075 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val)); 1076 } 1077}; 1078template<class ToTy> 1079struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> 1080 : public cast_convert_val<ToTy, 1081 const ::clang::DeclContext,const ::clang::DeclContext> {}; 1082 1083template<class ToTy> 1084struct cast_convert_val<ToTy, 1085 const ::clang::DeclContext*, const ::clang::DeclContext*> { 1086 static ToTy *doit(const ::clang::DeclContext *Val) { 1087 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val)); 1088 } 1089}; 1090template<class ToTy> 1091struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> 1092 : public cast_convert_val<ToTy, 1093 const ::clang::DeclContext*,const ::clang::DeclContext*> {}; 1094 1095} // end namespace llvm 1096 1097#endif 1098