DeclBase.h revision 204793
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); 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; 283 284 void setUsed(bool U = true) { Used = U; } 285 286 /// \brief Retrieve the level of precompiled header from which this 287 /// declaration was generated. 288 /// 289 /// The PCH level of a declaration describes where the declaration originated 290 /// from. A PCH level of 0 indicates that the declaration was not from a 291 /// precompiled header. A PCH level of 1 indicates that the declaration was 292 /// from a top-level precompiled header; 2 indicates that the declaration 293 /// comes from a precompiled header on which the top-level precompiled header 294 /// depends, and so on. 295 unsigned getPCHLevel() const { return PCHLevel; } 296 297 /// \brief The maximum PCH level that any declaration may have. 298 static const unsigned MaxPCHLevel = 3; 299 300 /// \brief Set the PCH level of this declaration. 301 void setPCHLevel(unsigned Level) { 302 assert(Level < MaxPCHLevel && "PCH level exceeds the maximum"); 303 PCHLevel = Level; 304 } 305 306 unsigned getIdentifierNamespace() const { 307 return IdentifierNamespace; 308 } 309 bool isInIdentifierNamespace(unsigned NS) const { 310 return getIdentifierNamespace() & NS; 311 } 312 static unsigned getIdentifierNamespaceForKind(Kind DK); 313 314 315 /// getLexicalDeclContext - The declaration context where this Decl was 316 /// lexically declared (LexicalDC). May be different from 317 /// getDeclContext() (SemanticDC). 318 /// e.g.: 319 /// 320 /// namespace A { 321 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 322 /// } 323 /// void A::f(); // SemanticDC == namespace 'A' 324 /// // LexicalDC == global namespace 325 DeclContext *getLexicalDeclContext() { 326 if (isInSemaDC()) 327 return getSemanticDC(); 328 return getMultipleDC()->LexicalDC; 329 } 330 const DeclContext *getLexicalDeclContext() const { 331 return const_cast<Decl*>(this)->getLexicalDeclContext(); 332 } 333 334 virtual bool isOutOfLine() const { 335 return getLexicalDeclContext() != getDeclContext(); 336 } 337 338 /// setDeclContext - Set both the semantic and lexical DeclContext 339 /// to DC. 340 void setDeclContext(DeclContext *DC); 341 342 void setLexicalDeclContext(DeclContext *DC); 343 344 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this 345 // scoped decl is defined outside the current function or method. This is 346 // roughly global variables and functions, but also handles enums (which could 347 // be defined inside or outside a function etc). 348 bool isDefinedOutsideFunctionOrMethod() const; 349 350 /// \brief Retrieves the "canonical" declaration of the given declaration. 351 virtual Decl *getCanonicalDecl() { return this; } 352 const Decl *getCanonicalDecl() const { 353 return const_cast<Decl*>(this)->getCanonicalDecl(); 354 } 355 356 /// \brief Whether this particular Decl is a canonical one. 357 bool isCanonicalDecl() const { return getCanonicalDecl() == this; } 358 359protected: 360 /// \brief Returns the next redeclaration or itself if this is the only decl. 361 /// 362 /// Decl subclasses that can be redeclared should override this method so that 363 /// Decl::redecl_iterator can iterate over them. 364 virtual Decl *getNextRedeclaration() { return this; } 365 366public: 367 /// \brief Iterates through all the redeclarations of the same decl. 368 class redecl_iterator { 369 /// Current - The current declaration. 370 Decl *Current; 371 Decl *Starter; 372 373 public: 374 typedef Decl* value_type; 375 typedef Decl* reference; 376 typedef Decl* pointer; 377 typedef std::forward_iterator_tag iterator_category; 378 typedef std::ptrdiff_t difference_type; 379 380 redecl_iterator() : Current(0) { } 381 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { } 382 383 reference operator*() const { return Current; } 384 pointer operator->() const { return Current; } 385 386 redecl_iterator& operator++() { 387 assert(Current && "Advancing while iterator has reached end"); 388 // Get either previous decl or latest decl. 389 Decl *Next = Current->getNextRedeclaration(); 390 assert(Next && "Should return next redeclaration or itself, never null!"); 391 Current = (Next != Starter ? Next : 0); 392 return *this; 393 } 394 395 redecl_iterator operator++(int) { 396 redecl_iterator tmp(*this); 397 ++(*this); 398 return tmp; 399 } 400 401 friend bool operator==(redecl_iterator x, redecl_iterator y) { 402 return x.Current == y.Current; 403 } 404 friend bool operator!=(redecl_iterator x, redecl_iterator y) { 405 return x.Current != y.Current; 406 } 407 }; 408 409 /// \brief Returns iterator for all the redeclarations of the same decl. 410 /// It will iterate at least once (when this decl is the only one). 411 redecl_iterator redecls_begin() const { 412 return redecl_iterator(const_cast<Decl*>(this)); 413 } 414 redecl_iterator redecls_end() const { return redecl_iterator(); } 415 416 /// getBody - If this Decl represents a declaration for a body of code, 417 /// such as a function or method definition, this method returns the 418 /// top-level Stmt* of that body. Otherwise this method returns null. 419 virtual Stmt* getBody() const { return 0; } 420 421 /// getCompoundBody - Returns getBody(), dyn_casted to a CompoundStmt. 422 CompoundStmt* getCompoundBody() const; 423 424 /// getBodyRBrace - Gets the right brace of the body, if a body exists. 425 /// This works whether the body is a CompoundStmt or a CXXTryStmt. 426 SourceLocation getBodyRBrace() const; 427 428 // global temp stats (until we have a per-module visitor) 429 static void addDeclKind(Kind k); 430 static bool CollectingStats(bool Enable = false); 431 static void PrintStats(); 432 433 /// isTemplateParameter - Determines whether this declaration is a 434 /// template parameter. 435 bool isTemplateParameter() const; 436 437 /// isTemplateParameter - Determines whether this declaration is a 438 /// template parameter pack. 439 bool isTemplateParameterPack() const; 440 441 /// \brief Whether this declaration is a function or function template. 442 bool isFunctionOrFunctionTemplate() const; 443 444 /// \brief Changes the namespace of this declaration to reflect that it's 445 /// the object of a friend declaration. 446 /// 447 /// These declarations appear in the lexical context of the friending 448 /// class, but in the semantic context of the actual entity. This property 449 /// applies only to a specific decl object; other redeclarations of the 450 /// same entity may not (and probably don't) share this property. 451 void setObjectOfFriendDecl(bool PreviouslyDeclared) { 452 unsigned OldNS = IdentifierNamespace; 453 assert((OldNS == IDNS_Tag || OldNS == IDNS_Ordinary || 454 OldNS == (IDNS_Tag | IDNS_Ordinary)) 455 && "unsupported namespace for undeclared friend"); 456 if (!PreviouslyDeclared) IdentifierNamespace = 0; 457 458 if (OldNS == IDNS_Tag) 459 IdentifierNamespace |= IDNS_TagFriend; 460 else 461 IdentifierNamespace |= IDNS_OrdinaryFriend; 462 } 463 464 enum FriendObjectKind { 465 FOK_None, // not a friend object 466 FOK_Declared, // a friend of a previously-declared entity 467 FOK_Undeclared // a friend of a previously-undeclared entity 468 }; 469 470 /// \brief Determines whether this declaration is the object of a 471 /// friend declaration and, if so, what kind. 472 /// 473 /// There is currently no direct way to find the associated FriendDecl. 474 FriendObjectKind getFriendObjectKind() const { 475 unsigned mask 476 = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend)); 477 if (!mask) return FOK_None; 478 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? 479 FOK_Declared : FOK_Undeclared); 480 } 481 482 // Implement isa/cast/dyncast/etc. 483 static bool classof(const Decl *) { return true; } 484 static bool classofKind(Kind K) { return true; } 485 static DeclContext *castToDeclContext(const Decl *); 486 static Decl *castFromDeclContext(const DeclContext *); 487 488 /// Destroy - Call destructors and release memory. 489 virtual void Destroy(ASTContext& C); 490 491 void print(llvm::raw_ostream &Out, unsigned Indentation = 0) const; 492 void print(llvm::raw_ostream &Out, const PrintingPolicy &Policy, 493 unsigned Indentation = 0) const; 494 static void printGroup(Decl** Begin, unsigned NumDecls, 495 llvm::raw_ostream &Out, const PrintingPolicy &Policy, 496 unsigned Indentation = 0); 497 void dump() const; 498 499private: 500 const Attr *getAttrsImpl() const; 501 502}; 503 504/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when 505/// doing something to a specific decl. 506class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { 507 const Decl *TheDecl; 508 SourceLocation Loc; 509 SourceManager &SM; 510 const char *Message; 511public: 512 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L, 513 SourceManager &sm, const char *Msg) 514 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} 515 516 virtual void print(llvm::raw_ostream &OS) const; 517}; 518 519 520/// DeclContext - This is used only as base class of specific decl types that 521/// can act as declaration contexts. These decls are (only the top classes 522/// that directly derive from DeclContext are mentioned, not their subclasses): 523/// 524/// TranslationUnitDecl 525/// NamespaceDecl 526/// FunctionDecl 527/// TagDecl 528/// ObjCMethodDecl 529/// ObjCContainerDecl 530/// LinkageSpecDecl 531/// BlockDecl 532/// 533class DeclContext { 534 /// DeclKind - This indicates which class this is. 535 Decl::Kind DeclKind : 8; 536 537 /// \brief Whether this declaration context also has some external 538 /// storage that contains additional declarations that are lexically 539 /// part of this context. 540 mutable bool ExternalLexicalStorage : 1; 541 542 /// \brief Whether this declaration context also has some external 543 /// storage that contains additional declarations that are visible 544 /// in this context. 545 mutable bool ExternalVisibleStorage : 1; 546 547 /// \brief Pointer to the data structure used to lookup declarations 548 /// within this context, which is a DenseMap<DeclarationName, 549 /// StoredDeclsList>. 550 mutable void* LookupPtr; 551 552 /// FirstDecl - The first declaration stored within this declaration 553 /// context. 554 mutable Decl *FirstDecl; 555 556 /// LastDecl - The last declaration stored within this declaration 557 /// context. FIXME: We could probably cache this value somewhere 558 /// outside of the DeclContext, to reduce the size of DeclContext by 559 /// another pointer. 560 mutable Decl *LastDecl; 561 562protected: 563 DeclContext(Decl::Kind K) 564 : DeclKind(K), ExternalLexicalStorage(false), 565 ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0), 566 LastDecl(0) { } 567 568 void DestroyDecls(ASTContext &C); 569 570public: 571 ~DeclContext(); 572 573 Decl::Kind getDeclKind() const { 574 return DeclKind; 575 } 576 const char *getDeclKindName() const; 577 578 /// getParent - Returns the containing DeclContext. 579 DeclContext *getParent() { 580 return cast<Decl>(this)->getDeclContext(); 581 } 582 const DeclContext *getParent() const { 583 return const_cast<DeclContext*>(this)->getParent(); 584 } 585 586 /// getLexicalParent - Returns the containing lexical DeclContext. May be 587 /// different from getParent, e.g.: 588 /// 589 /// namespace A { 590 /// struct S; 591 /// } 592 /// struct A::S {}; // getParent() == namespace 'A' 593 /// // getLexicalParent() == translation unit 594 /// 595 DeclContext *getLexicalParent() { 596 return cast<Decl>(this)->getLexicalDeclContext(); 597 } 598 const DeclContext *getLexicalParent() const { 599 return const_cast<DeclContext*>(this)->getLexicalParent(); 600 } 601 602 DeclContext *getLookupParent(); 603 604 const DeclContext *getLookupParent() const { 605 return const_cast<DeclContext*>(this)->getLookupParent(); 606 } 607 608 ASTContext &getParentASTContext() const { 609 return cast<Decl>(this)->getASTContext(); 610 } 611 612 bool isFunctionOrMethod() const { 613 switch (DeclKind) { 614 case Decl::Block: 615 case Decl::ObjCMethod: 616 return true; 617 default: 618 return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast; 619 } 620 } 621 622 bool isFileContext() const { 623 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; 624 } 625 626 bool isTranslationUnit() const { 627 return DeclKind == Decl::TranslationUnit; 628 } 629 630 bool isRecord() const { 631 return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast; 632 } 633 634 bool isNamespace() const { 635 return DeclKind == Decl::Namespace; 636 } 637 638 /// \brief Determines whether this context is dependent on a 639 /// template parameter. 640 bool isDependentContext() const; 641 642 /// isTransparentContext - Determines whether this context is a 643 /// "transparent" context, meaning that the members declared in this 644 /// context are semantically declared in the nearest enclosing 645 /// non-transparent (opaque) context but are lexically declared in 646 /// this context. For example, consider the enumerators of an 647 /// enumeration type: 648 /// @code 649 /// enum E { 650 /// Val1 651 /// }; 652 /// @endcode 653 /// Here, E is a transparent context, so its enumerator (Val1) will 654 /// appear (semantically) that it is in the same context of E. 655 /// Examples of transparent contexts include: enumerations (except for 656 /// C++0x scoped enums), C++ linkage specifications, and C++0x 657 /// inline namespaces. 658 bool isTransparentContext() const; 659 660 /// \brief Determine whether this declaration context is equivalent 661 /// to the declaration context DC. 662 bool Equals(DeclContext *DC) { 663 return this->getPrimaryContext() == DC->getPrimaryContext(); 664 } 665 666 /// \brief Determine whether this declaration context encloses the 667 /// declaration context DC. 668 bool Encloses(DeclContext *DC); 669 670 /// getPrimaryContext - There may be many different 671 /// declarations of the same entity (including forward declarations 672 /// of classes, multiple definitions of namespaces, etc.), each with 673 /// a different set of declarations. This routine returns the 674 /// "primary" DeclContext structure, which will contain the 675 /// information needed to perform name lookup into this context. 676 DeclContext *getPrimaryContext(); 677 678 /// getLookupContext - Retrieve the innermost non-transparent 679 /// context of this context, which corresponds to the innermost 680 /// location from which name lookup can find the entities in this 681 /// context. 682 DeclContext *getLookupContext(); 683 const DeclContext *getLookupContext() const { 684 return const_cast<DeclContext *>(this)->getLookupContext(); 685 } 686 687 /// \brief Retrieve the nearest enclosing namespace context. 688 DeclContext *getEnclosingNamespaceContext(); 689 const DeclContext *getEnclosingNamespaceContext() const { 690 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); 691 } 692 693 /// getNextContext - If this is a DeclContext that may have other 694 /// DeclContexts that are semantically connected but syntactically 695 /// different, such as C++ namespaces, this routine retrieves the 696 /// next DeclContext in the link. Iteration through the chain of 697 /// DeclContexts should begin at the primary DeclContext and 698 /// continue until this function returns NULL. For example, given: 699 /// @code 700 /// namespace N { 701 /// int x; 702 /// } 703 /// namespace N { 704 /// int y; 705 /// } 706 /// @endcode 707 /// The first occurrence of namespace N will be the primary 708 /// DeclContext. Its getNextContext will return the second 709 /// occurrence of namespace N. 710 DeclContext *getNextContext(); 711 712 /// decl_iterator - Iterates through the declarations stored 713 /// within this context. 714 class decl_iterator { 715 /// Current - The current declaration. 716 Decl *Current; 717 718 public: 719 typedef Decl* value_type; 720 typedef Decl* reference; 721 typedef Decl* pointer; 722 typedef std::forward_iterator_tag iterator_category; 723 typedef std::ptrdiff_t difference_type; 724 725 decl_iterator() : Current(0) { } 726 explicit decl_iterator(Decl *C) : Current(C) { } 727 728 reference operator*() const { return Current; } 729 pointer operator->() const { return Current; } 730 731 decl_iterator& operator++() { 732 Current = Current->getNextDeclInContext(); 733 return *this; 734 } 735 736 decl_iterator operator++(int) { 737 decl_iterator tmp(*this); 738 ++(*this); 739 return tmp; 740 } 741 742 friend bool operator==(decl_iterator x, decl_iterator y) { 743 return x.Current == y.Current; 744 } 745 friend bool operator!=(decl_iterator x, decl_iterator y) { 746 return x.Current != y.Current; 747 } 748 }; 749 750 /// decls_begin/decls_end - Iterate over the declarations stored in 751 /// this context. 752 decl_iterator decls_begin() const; 753 decl_iterator decls_end() const; 754 bool decls_empty() const; 755 756 /// specific_decl_iterator - Iterates over a subrange of 757 /// declarations stored in a DeclContext, providing only those that 758 /// are of type SpecificDecl (or a class derived from it). This 759 /// iterator is used, for example, to provide iteration over just 760 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). 761 template<typename SpecificDecl> 762 class specific_decl_iterator { 763 /// Current - The current, underlying declaration iterator, which 764 /// will either be NULL or will point to a declaration of 765 /// type SpecificDecl. 766 DeclContext::decl_iterator Current; 767 768 /// SkipToNextDecl - Advances the current position up to the next 769 /// declaration of type SpecificDecl that also meets the criteria 770 /// required by Acceptable. 771 void SkipToNextDecl() { 772 while (*Current && !isa<SpecificDecl>(*Current)) 773 ++Current; 774 } 775 776 public: 777 typedef SpecificDecl* value_type; 778 typedef SpecificDecl* reference; 779 typedef SpecificDecl* pointer; 780 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 781 difference_type; 782 typedef std::forward_iterator_tag iterator_category; 783 784 specific_decl_iterator() : Current() { } 785 786 /// specific_decl_iterator - Construct a new iterator over a 787 /// subset of the declarations the range [C, 788 /// end-of-declarations). If A is non-NULL, it is a pointer to a 789 /// member function of SpecificDecl that should return true for 790 /// all of the SpecificDecl instances that will be in the subset 791 /// of iterators. For example, if you want Objective-C instance 792 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 793 /// &ObjCMethodDecl::isInstanceMethod. 794 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 795 SkipToNextDecl(); 796 } 797 798 reference operator*() const { return cast<SpecificDecl>(*Current); } 799 pointer operator->() const { return cast<SpecificDecl>(*Current); } 800 801 specific_decl_iterator& operator++() { 802 ++Current; 803 SkipToNextDecl(); 804 return *this; 805 } 806 807 specific_decl_iterator operator++(int) { 808 specific_decl_iterator tmp(*this); 809 ++(*this); 810 return tmp; 811 } 812 813 friend bool 814 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) { 815 return x.Current == y.Current; 816 } 817 818 friend bool 819 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) { 820 return x.Current != y.Current; 821 } 822 }; 823 824 /// \brief Iterates over a filtered subrange of declarations stored 825 /// in a DeclContext. 826 /// 827 /// This iterator visits only those declarations that are of type 828 /// SpecificDecl (or a class derived from it) and that meet some 829 /// additional run-time criteria. This iterator is used, for 830 /// example, to provide access to the instance methods within an 831 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and 832 /// Acceptable = ObjCMethodDecl::isInstanceMethod). 833 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> 834 class filtered_decl_iterator { 835 /// Current - The current, underlying declaration iterator, which 836 /// will either be NULL or will point to a declaration of 837 /// type SpecificDecl. 838 DeclContext::decl_iterator Current; 839 840 /// SkipToNextDecl - Advances the current position up to the next 841 /// declaration of type SpecificDecl that also meets the criteria 842 /// required by Acceptable. 843 void SkipToNextDecl() { 844 while (*Current && 845 (!isa<SpecificDecl>(*Current) || 846 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) 847 ++Current; 848 } 849 850 public: 851 typedef SpecificDecl* value_type; 852 typedef SpecificDecl* reference; 853 typedef SpecificDecl* pointer; 854 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 855 difference_type; 856 typedef std::forward_iterator_tag iterator_category; 857 858 filtered_decl_iterator() : Current() { } 859 860 /// specific_decl_iterator - Construct a new iterator over a 861 /// subset of the declarations the range [C, 862 /// end-of-declarations). If A is non-NULL, it is a pointer to a 863 /// member function of SpecificDecl that should return true for 864 /// all of the SpecificDecl instances that will be in the subset 865 /// of iterators. For example, if you want Objective-C instance 866 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 867 /// &ObjCMethodDecl::isInstanceMethod. 868 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 869 SkipToNextDecl(); 870 } 871 872 reference operator*() const { return cast<SpecificDecl>(*Current); } 873 pointer operator->() const { return cast<SpecificDecl>(*Current); } 874 875 filtered_decl_iterator& operator++() { 876 ++Current; 877 SkipToNextDecl(); 878 return *this; 879 } 880 881 filtered_decl_iterator operator++(int) { 882 filtered_decl_iterator tmp(*this); 883 ++(*this); 884 return tmp; 885 } 886 887 friend bool 888 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 889 return x.Current == y.Current; 890 } 891 892 friend bool 893 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 894 return x.Current != y.Current; 895 } 896 }; 897 898 /// @brief Add the declaration D into this context. 899 /// 900 /// This routine should be invoked when the declaration D has first 901 /// been declared, to place D into the context where it was 902 /// (lexically) defined. Every declaration must be added to one 903 /// (and only one!) context, where it can be visited via 904 /// [decls_begin(), decls_end()). Once a declaration has been added 905 /// to its lexical context, the corresponding DeclContext owns the 906 /// declaration. 907 /// 908 /// If D is also a NamedDecl, it will be made visible within its 909 /// semantic context via makeDeclVisibleInContext. 910 void addDecl(Decl *D); 911 912 /// @brief Add the declaration D to this context without modifying 913 /// any lookup tables. 914 /// 915 /// This is useful for some operations in dependent contexts where 916 /// the semantic context might not be dependent; this basically 917 /// only happens with friends. 918 void addHiddenDecl(Decl *D); 919 920 /// @brief Removes a declaration from this context. 921 void removeDecl(Decl *D); 922 923 /// lookup_iterator - An iterator that provides access to the results 924 /// of looking up a name within this context. 925 typedef NamedDecl **lookup_iterator; 926 927 /// lookup_const_iterator - An iterator that provides non-mutable 928 /// access to the results of lookup up a name within this context. 929 typedef NamedDecl * const * lookup_const_iterator; 930 931 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result; 932 typedef std::pair<lookup_const_iterator, lookup_const_iterator> 933 lookup_const_result; 934 935 /// lookup - Find the declarations (if any) with the given Name in 936 /// this context. Returns a range of iterators that contains all of 937 /// the declarations with this name, with object, function, member, 938 /// and enumerator names preceding any tag name. Note that this 939 /// routine will not look into parent contexts. 940 lookup_result lookup(DeclarationName Name); 941 lookup_const_result lookup(DeclarationName Name) const; 942 943 /// @brief Makes a declaration visible within this context. 944 /// 945 /// This routine makes the declaration D visible to name lookup 946 /// within this context and, if this is a transparent context, 947 /// within its parent contexts up to the first enclosing 948 /// non-transparent context. Making a declaration visible within a 949 /// context does not transfer ownership of a declaration, and a 950 /// declaration can be visible in many contexts that aren't its 951 /// lexical context. 952 /// 953 /// If D is a redeclaration of an existing declaration that is 954 /// visible from this context, as determined by 955 /// NamedDecl::declarationReplaces, the previous declaration will be 956 /// replaced with D. 957 /// 958 /// @param Recoverable true if it's okay to not add this decl to 959 /// the lookup tables because it can be easily recovered by walking 960 /// the declaration chains. 961 void makeDeclVisibleInContext(NamedDecl *D, bool Recoverable = true); 962 963 /// udir_iterator - Iterates through the using-directives stored 964 /// within this context. 965 typedef UsingDirectiveDecl * const * udir_iterator; 966 967 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range; 968 969 udir_iterator_range getUsingDirectives() const; 970 971 udir_iterator using_directives_begin() const { 972 return getUsingDirectives().first; 973 } 974 975 udir_iterator using_directives_end() const { 976 return getUsingDirectives().second; 977 } 978 979 // Low-level accessors 980 981 /// \brief Retrieve the internal representation of the lookup structure. 982 void* getLookupPtr() const { return LookupPtr; } 983 984 /// \brief Whether this DeclContext has external storage containing 985 /// additional declarations that are lexically in this context. 986 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; } 987 988 /// \brief State whether this DeclContext has external storage for 989 /// declarations lexically in this context. 990 void setHasExternalLexicalStorage(bool ES = true) { 991 ExternalLexicalStorage = ES; 992 } 993 994 /// \brief Whether this DeclContext has external storage containing 995 /// additional declarations that are visible in this context. 996 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; } 997 998 /// \brief State whether this DeclContext has external storage for 999 /// declarations visible in this context. 1000 void setHasExternalVisibleStorage(bool ES = true) { 1001 ExternalVisibleStorage = ES; 1002 } 1003 1004 static bool classof(const Decl *D); 1005 static bool classof(const DeclContext *D) { return true; } 1006#define DECL_CONTEXT(Name) \ 1007 static bool classof(const Name##Decl *D) { return true; } 1008#include "clang/AST/DeclNodes.def" 1009 1010 void dumpDeclContext() const; 1011 1012private: 1013 void LoadLexicalDeclsFromExternalStorage() const; 1014 void LoadVisibleDeclsFromExternalStorage() const; 1015 1016 void buildLookup(DeclContext *DCtx); 1017 void makeDeclVisibleInContextImpl(NamedDecl *D); 1018}; 1019 1020inline bool Decl::isTemplateParameter() const { 1021 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm || 1022 getKind() == TemplateTemplateParm; 1023} 1024 1025 1026// Specialization selected when ToTy is not a known subclass of DeclContext. 1027template <class ToTy, 1028 bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value> 1029struct cast_convert_decl_context { 1030 static const ToTy *doit(const DeclContext *Val) { 1031 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val)); 1032 } 1033 1034 static ToTy *doit(DeclContext *Val) { 1035 return static_cast<ToTy*>(Decl::castFromDeclContext(Val)); 1036 } 1037}; 1038 1039// Specialization selected when ToTy is a known subclass of DeclContext. 1040template <class ToTy> 1041struct cast_convert_decl_context<ToTy, true> { 1042 static const ToTy *doit(const DeclContext *Val) { 1043 return static_cast<const ToTy*>(Val); 1044 } 1045 1046 static ToTy *doit(DeclContext *Val) { 1047 return static_cast<ToTy*>(Val); 1048 } 1049}; 1050 1051 1052} // end clang. 1053 1054namespace llvm { 1055 1056/// isa<T>(DeclContext*) 1057template<class ToTy> 1058struct isa_impl_wrap<ToTy, 1059 const ::clang::DeclContext,const ::clang::DeclContext> { 1060 static bool doit(const ::clang::DeclContext &Val) { 1061 return ToTy::classofKind(Val.getDeclKind()); 1062 } 1063}; 1064template<class ToTy> 1065struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> 1066 : public isa_impl_wrap<ToTy, 1067 const ::clang::DeclContext,const ::clang::DeclContext> {}; 1068 1069/// cast<T>(DeclContext*) 1070template<class ToTy> 1071struct cast_convert_val<ToTy, 1072 const ::clang::DeclContext,const ::clang::DeclContext> { 1073 static const ToTy &doit(const ::clang::DeclContext &Val) { 1074 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); 1075 } 1076}; 1077template<class ToTy> 1078struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> { 1079 static ToTy &doit(::clang::DeclContext &Val) { 1080 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); 1081 } 1082}; 1083template<class ToTy> 1084struct cast_convert_val<ToTy, 1085 const ::clang::DeclContext*, const ::clang::DeclContext*> { 1086 static const ToTy *doit(const ::clang::DeclContext *Val) { 1087 return ::clang::cast_convert_decl_context<ToTy>::doit(Val); 1088 } 1089}; 1090template<class ToTy> 1091struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> { 1092 static ToTy *doit(::clang::DeclContext *Val) { 1093 return ::clang::cast_convert_decl_context<ToTy>::doit(Val); 1094 } 1095}; 1096 1097/// Implement cast_convert_val for Decl -> DeclContext conversions. 1098template<class FromTy> 1099struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { 1100 static ::clang::DeclContext &doit(const FromTy &Val) { 1101 return *FromTy::castToDeclContext(&Val); 1102 } 1103}; 1104 1105template<class FromTy> 1106struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { 1107 static ::clang::DeclContext *doit(const FromTy *Val) { 1108 return FromTy::castToDeclContext(Val); 1109 } 1110}; 1111 1112template<class FromTy> 1113struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { 1114 static const ::clang::DeclContext &doit(const FromTy &Val) { 1115 return *FromTy::castToDeclContext(&Val); 1116 } 1117}; 1118 1119template<class FromTy> 1120struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { 1121 static const ::clang::DeclContext *doit(const FromTy *Val) { 1122 return FromTy::castToDeclContext(Val); 1123 } 1124}; 1125 1126} // end namespace llvm 1127 1128#endif 1129