DeclBase.h revision 223017
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; 44class StoredDeclsMap; 45class DependentDiagnostic; 46class ASTMutationListener; 47} 48 49namespace llvm { 50// DeclContext* is only 4-byte aligned on 32-bit systems. 51template<> 52 class PointerLikeTypeTraits<clang::DeclContext*> { 53 typedef clang::DeclContext* PT; 54public: 55 static inline void *getAsVoidPointer(PT P) { return P; } 56 static inline PT getFromVoidPointer(void *P) { 57 return static_cast<PT>(P); 58 } 59 enum { NumLowBitsAvailable = 2 }; 60}; 61} 62 63namespace clang { 64 65 /// \brief Captures the result of checking the availability of a 66 /// declaration. 67 enum AvailabilityResult { 68 AR_Available = 0, 69 AR_NotYetIntroduced, 70 AR_Deprecated, 71 AR_Unavailable 72 }; 73 74/// Decl - This represents one declaration (or definition), e.g. a variable, 75/// typedef, function, struct, etc. 76/// 77class Decl { 78public: 79 /// \brief Lists the kind of concrete classes of Decl. 80 enum Kind { 81#define DECL(DERIVED, BASE) DERIVED, 82#define ABSTRACT_DECL(DECL) 83#define DECL_RANGE(BASE, START, END) \ 84 first##BASE = START, last##BASE = END, 85#define LAST_DECL_RANGE(BASE, START, END) \ 86 first##BASE = START, last##BASE = END 87#include "clang/AST/DeclNodes.inc" 88 }; 89 90 /// \brief A placeholder type used to construct an empty shell of a 91 /// decl-derived type that will be filled in later (e.g., by some 92 /// deserialization method). 93 struct EmptyShell { }; 94 95 /// IdentifierNamespace - The different namespaces in which 96 /// declarations may appear. According to C99 6.2.3, there are 97 /// four namespaces, labels, tags, members and ordinary 98 /// identifiers. C++ describes lookup completely differently: 99 /// certain lookups merely "ignore" certain kinds of declarations, 100 /// usually based on whether the declaration is of a type, etc. 101 /// 102 /// These are meant as bitmasks, so that searches in 103 /// C++ can look into the "tag" namespace during ordinary lookup. 104 /// 105 /// Decl currently provides 15 bits of IDNS bits. 106 enum IdentifierNamespace { 107 /// Labels, declared with 'x:' and referenced with 'goto x'. 108 IDNS_Label = 0x0001, 109 110 /// Tags, declared with 'struct foo;' and referenced with 111 /// 'struct foo'. All tags are also types. This is what 112 /// elaborated-type-specifiers look for in C. 113 IDNS_Tag = 0x0002, 114 115 /// Types, declared with 'struct foo', typedefs, etc. 116 /// This is what elaborated-type-specifiers look for in C++, 117 /// but note that it's ill-formed to find a non-tag. 118 IDNS_Type = 0x0004, 119 120 /// Members, declared with object declarations within tag 121 /// definitions. In C, these can only be found by "qualified" 122 /// lookup in member expressions. In C++, they're found by 123 /// normal lookup. 124 IDNS_Member = 0x0008, 125 126 /// Namespaces, declared with 'namespace foo {}'. 127 /// Lookup for nested-name-specifiers find these. 128 IDNS_Namespace = 0x0010, 129 130 /// Ordinary names. In C, everything that's not a label, tag, 131 /// or member ends up here. 132 IDNS_Ordinary = 0x0020, 133 134 /// Objective C @protocol. 135 IDNS_ObjCProtocol = 0x0040, 136 137 /// This declaration is a friend function. A friend function 138 /// declaration is always in this namespace but may also be in 139 /// IDNS_Ordinary if it was previously declared. 140 IDNS_OrdinaryFriend = 0x0080, 141 142 /// This declaration is a friend class. A friend class 143 /// declaration is always in this namespace but may also be in 144 /// IDNS_Tag|IDNS_Type if it was previously declared. 145 IDNS_TagFriend = 0x0100, 146 147 /// This declaration is a using declaration. A using declaration 148 /// *introduces* a number of other declarations into the current 149 /// scope, and those declarations use the IDNS of their targets, 150 /// but the actual using declarations go in this namespace. 151 IDNS_Using = 0x0200, 152 153 /// This declaration is a C++ operator declared in a non-class 154 /// context. All such operators are also in IDNS_Ordinary. 155 /// C++ lexical operator lookup looks for these. 156 IDNS_NonMemberOperator = 0x0400 157 }; 158 159 /// ObjCDeclQualifier - 'Qualifiers' written next to the return and 160 /// parameter types in method declarations. Other than remembering 161 /// them and mangling them into the method's signature string, these 162 /// are ignored by the compiler; they are consumed by certain 163 /// remote-messaging frameworks. 164 /// 165 /// in, inout, and out are mutually exclusive and apply only to 166 /// method parameters. bycopy and byref are mutually exclusive and 167 /// apply only to method parameters (?). oneway applies only to 168 /// results. All of these expect their corresponding parameter to 169 /// have a particular type. None of this is currently enforced by 170 /// clang. 171 /// 172 /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier. 173 enum ObjCDeclQualifier { 174 OBJC_TQ_None = 0x0, 175 OBJC_TQ_In = 0x1, 176 OBJC_TQ_Inout = 0x2, 177 OBJC_TQ_Out = 0x4, 178 OBJC_TQ_Bycopy = 0x8, 179 OBJC_TQ_Byref = 0x10, 180 OBJC_TQ_Oneway = 0x20 181 }; 182 183private: 184 /// NextDeclInContext - The next declaration within the same lexical 185 /// DeclContext. These pointers form the linked list that is 186 /// traversed via DeclContext's decls_begin()/decls_end(). 187 Decl *NextDeclInContext; 188 189 friend class DeclContext; 190 191 struct MultipleDC { 192 DeclContext *SemanticDC; 193 DeclContext *LexicalDC; 194 }; 195 196 197 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*. 198 /// For declarations that don't contain C++ scope specifiers, it contains 199 /// the DeclContext where the Decl was declared. 200 /// For declarations with C++ scope specifiers, it contains a MultipleDC* 201 /// with the context where it semantically belongs (SemanticDC) and the 202 /// context where it was lexically declared (LexicalDC). 203 /// e.g.: 204 /// 205 /// namespace A { 206 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 207 /// } 208 /// void A::f(); // SemanticDC == namespace 'A' 209 /// // LexicalDC == global namespace 210 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx; 211 212 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); } 213 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); } 214 inline MultipleDC *getMultipleDC() const { 215 return DeclCtx.get<MultipleDC*>(); 216 } 217 inline DeclContext *getSemanticDC() const { 218 return DeclCtx.get<DeclContext*>(); 219 } 220 221 /// Loc - The location of this decl. 222 SourceLocation Loc; 223 224 /// DeclKind - This indicates which class this is. 225 unsigned DeclKind : 8; 226 227 /// InvalidDecl - This indicates a semantic error occurred. 228 unsigned InvalidDecl : 1; 229 230 /// HasAttrs - This indicates whether the decl has attributes or not. 231 unsigned HasAttrs : 1; 232 233 /// Implicit - Whether this declaration was implicitly generated by 234 /// the implementation rather than explicitly written by the user. 235 unsigned Implicit : 1; 236 237 /// \brief Whether this declaration was "used", meaning that a definition is 238 /// required. 239 unsigned Used : 1; 240 241 /// \brief Whether this declaration was "referenced". 242 /// The difference with 'Used' is whether the reference appears in a 243 /// evaluated context or not, e.g. functions used in uninstantiated templates 244 /// are regarded as "referenced" but not "used". 245 unsigned Referenced : 1; 246 247protected: 248 /// Access - Used by C++ decls for the access specifier. 249 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum 250 unsigned Access : 2; 251 friend class CXXClassMemberWrapper; 252 253 /// PCHLevel - the "level" of AST file from which this declaration was built. 254 unsigned PCHLevel : 2; 255 256 /// ChangedAfterLoad - if this declaration has changed since being loaded 257 unsigned ChangedAfterLoad : 1; 258 259 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in. 260 unsigned IdentifierNamespace : 12; 261 262 /// \brief Whether the \c CachedLinkage field is active. 263 /// 264 /// This field is only valid for NamedDecls subclasses. 265 mutable unsigned HasCachedLinkage : 1; 266 267 /// \brief If \c HasCachedLinkage, the linkage of this declaration. 268 /// 269 /// This field is only valid for NamedDecls subclasses. 270 mutable unsigned CachedLinkage : 2; 271 272 273private: 274 void CheckAccessDeclContext() const; 275 276protected: 277 278 Decl(Kind DK, DeclContext *DC, SourceLocation L) 279 : NextDeclInContext(0), DeclCtx(DC), 280 Loc(L), DeclKind(DK), InvalidDecl(0), 281 HasAttrs(false), Implicit(false), Used(false), Referenced(false), 282 Access(AS_none), PCHLevel(0), ChangedAfterLoad(false), 283 IdentifierNamespace(getIdentifierNamespaceForKind(DK)), 284 HasCachedLinkage(0) 285 { 286 if (Decl::CollectingStats()) add(DK); 287 } 288 289 Decl(Kind DK, EmptyShell Empty) 290 : NextDeclInContext(0), DeclKind(DK), InvalidDecl(0), 291 HasAttrs(false), Implicit(false), Used(false), Referenced(false), 292 Access(AS_none), PCHLevel(0), ChangedAfterLoad(false), 293 IdentifierNamespace(getIdentifierNamespaceForKind(DK)), 294 HasCachedLinkage(0) 295 { 296 if (Decl::CollectingStats()) add(DK); 297 } 298 299 virtual ~Decl(); 300 301public: 302 303 /// \brief Source range that this declaration covers. 304 virtual SourceRange getSourceRange() const { 305 return SourceRange(getLocation(), getLocation()); 306 } 307 SourceLocation getLocStart() const { return getSourceRange().getBegin(); } 308 SourceLocation getLocEnd() const { return getSourceRange().getEnd(); } 309 310 SourceLocation getLocation() const { return Loc; } 311 void setLocation(SourceLocation L) { Loc = L; } 312 313 Kind getKind() const { return static_cast<Kind>(DeclKind); } 314 const char *getDeclKindName() const; 315 316 Decl *getNextDeclInContext() { return NextDeclInContext; } 317 const Decl *getNextDeclInContext() const { return NextDeclInContext; } 318 319 DeclContext *getDeclContext() { 320 if (isInSemaDC()) 321 return getSemanticDC(); 322 return getMultipleDC()->SemanticDC; 323 } 324 const DeclContext *getDeclContext() const { 325 return const_cast<Decl*>(this)->getDeclContext(); 326 } 327 328 /// Finds the innermost non-closure context of this declaration. 329 /// That is, walk out the DeclContext chain, skipping any blocks. 330 DeclContext *getNonClosureContext(); 331 const DeclContext *getNonClosureContext() const { 332 return const_cast<Decl*>(this)->getNonClosureContext(); 333 } 334 335 TranslationUnitDecl *getTranslationUnitDecl(); 336 const TranslationUnitDecl *getTranslationUnitDecl() const { 337 return const_cast<Decl*>(this)->getTranslationUnitDecl(); 338 } 339 340 bool isInAnonymousNamespace() const; 341 342 ASTContext &getASTContext() const; 343 344 void setAccess(AccessSpecifier AS) { 345 Access = AS; 346#ifndef NDEBUG 347 CheckAccessDeclContext(); 348#endif 349 } 350 351 AccessSpecifier getAccess() const { 352#ifndef NDEBUG 353 CheckAccessDeclContext(); 354#endif 355 return AccessSpecifier(Access); 356 } 357 358 bool hasAttrs() const { return HasAttrs; } 359 void setAttrs(const AttrVec& Attrs); 360 AttrVec &getAttrs() { 361 return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs()); 362 } 363 const AttrVec &getAttrs() const; 364 void swapAttrs(Decl *D); 365 void dropAttrs(); 366 367 void addAttr(Attr *A) { 368 if (hasAttrs()) 369 getAttrs().push_back(A); 370 else 371 setAttrs(AttrVec(1, A)); 372 } 373 374 typedef AttrVec::const_iterator attr_iterator; 375 376 // FIXME: Do not rely on iterators having comparable singular values. 377 // Note that this should error out if they do not. 378 attr_iterator attr_begin() const { 379 return hasAttrs() ? getAttrs().begin() : 0; 380 } 381 attr_iterator attr_end() const { 382 return hasAttrs() ? getAttrs().end() : 0; 383 } 384 385 template <typename T> 386 specific_attr_iterator<T> specific_attr_begin() const { 387 return specific_attr_iterator<T>(attr_begin()); 388 } 389 template <typename T> 390 specific_attr_iterator<T> specific_attr_end() const { 391 return specific_attr_iterator<T>(attr_end()); 392 } 393 394 template<typename T> T *getAttr() const { 395 return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : 0; 396 } 397 template<typename T> bool hasAttr() const { 398 return hasAttrs() && hasSpecificAttr<T>(getAttrs()); 399 } 400 401 /// getMaxAlignment - return the maximum alignment specified by attributes 402 /// on this decl, 0 if there are none. 403 unsigned getMaxAlignment() const { 404 return hasAttrs() ? getMaxAttrAlignment(getAttrs(), getASTContext()) : 0; 405 } 406 407 /// setInvalidDecl - Indicates the Decl had a semantic error. This 408 /// allows for graceful error recovery. 409 void setInvalidDecl(bool Invalid = true); 410 bool isInvalidDecl() const { return (bool) InvalidDecl; } 411 412 /// isImplicit - Indicates whether the declaration was implicitly 413 /// generated by the implementation. If false, this declaration 414 /// was written explicitly in the source code. 415 bool isImplicit() const { return Implicit; } 416 void setImplicit(bool I = true) { Implicit = I; } 417 418 /// \brief Whether this declaration was used, meaning that a definition 419 /// is required. 420 /// 421 /// \param CheckUsedAttr When true, also consider the "used" attribute 422 /// (in addition to the "used" bit set by \c setUsed()) when determining 423 /// whether the function is used. 424 bool isUsed(bool CheckUsedAttr = true) const; 425 426 void setUsed(bool U = true) { Used = U; } 427 428 /// \brief Whether this declaration was referenced. 429 bool isReferenced() const; 430 431 void setReferenced(bool R = true) { Referenced = R; } 432 433 /// \brief Determine the availability of the given declaration. 434 /// 435 /// This routine will determine the most restrictive availability of 436 /// the given declaration (e.g., preferring 'unavailable' to 437 /// 'deprecated'). 438 /// 439 /// \param Message If non-NULL and the result is not \c 440 /// AR_Available, will be set to a (possibly empty) message 441 /// describing why the declaration has not been introduced, is 442 /// deprecated, or is unavailable. 443 AvailabilityResult getAvailability(std::string *Message = 0) const; 444 445 /// \brief Determine whether this declaration is marked 'deprecated'. 446 /// 447 /// \param Message If non-NULL and the declaration is deprecated, 448 /// this will be set to the message describing why the declaration 449 /// was deprecated (which may be empty). 450 bool isDeprecated(std::string *Message = 0) const { 451 return getAvailability(Message) == AR_Deprecated; 452 } 453 454 /// \brief Determine whether this declaration is marked 'unavailable'. 455 /// 456 /// \param Message If non-NULL and the declaration is unavailable, 457 /// this will be set to the message describing why the declaration 458 /// was made unavailable (which may be empty). 459 bool isUnavailable(std::string *Message = 0) const { 460 return getAvailability(Message) == AR_Unavailable; 461 } 462 463 /// \brief Determine whether this is a weak-imported symbol. 464 /// 465 /// Weak-imported symbols are typically marked with the 466 /// 'weak_import' attribute, but may also be marked with an 467 /// 'availability' attribute where we're targing a platform prior to 468 /// the introduction of this feature. 469 bool isWeakImported() const; 470 471 /// \brief Determines whether this symbol can be weak-imported, 472 /// e.g., whether it would be well-formed to add the weak_import 473 /// attribute. 474 /// 475 /// \param IsDefinition Set to \c true to indicate that this 476 /// declaration cannot be weak-imported because it has a definition. 477 bool canBeWeakImported(bool &IsDefinition) const; 478 479 /// \brief Retrieve the level of precompiled header from which this 480 /// declaration was generated. 481 /// 482 /// The PCH level of a declaration describes where the declaration originated 483 /// from. A PCH level of 0 indicates that the declaration was parsed from 484 /// source. A PCH level of 1 indicates that the declaration was loaded from 485 /// a top-level AST file. A PCH level 2 indicates that the declaration was 486 /// loaded from a PCH file the AST file depends on, and so on. 487 unsigned getPCHLevel() const { return PCHLevel; } 488 489 /// \brief The maximum PCH level that any declaration may have. 490 static const unsigned MaxPCHLevel = 3; 491 492 /// \brief Set the PCH level of this declaration. 493 void setPCHLevel(unsigned Level) { 494 assert(Level <= MaxPCHLevel && "PCH level exceeds the maximum"); 495 PCHLevel = Level; 496 } 497 498 /// \brief Query whether this declaration was changed in a significant way 499 /// since being loaded from an AST file. 500 /// 501 /// In an epic violation of layering, what is "significant" is entirely 502 /// up to the serialization system, but implemented in AST and Sema. 503 bool isChangedSinceDeserialization() const { return ChangedAfterLoad; } 504 505 /// \brief Mark this declaration as having changed since deserialization, or 506 /// reset the flag. 507 void setChangedSinceDeserialization(bool Changed) { 508 ChangedAfterLoad = Changed; 509 } 510 511 unsigned getIdentifierNamespace() const { 512 return IdentifierNamespace; 513 } 514 bool isInIdentifierNamespace(unsigned NS) const { 515 return getIdentifierNamespace() & NS; 516 } 517 static unsigned getIdentifierNamespaceForKind(Kind DK); 518 519 bool hasTagIdentifierNamespace() const { 520 return isTagIdentifierNamespace(getIdentifierNamespace()); 521 } 522 static bool isTagIdentifierNamespace(unsigned NS) { 523 // TagDecls have Tag and Type set and may also have TagFriend. 524 return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type); 525 } 526 527 /// getLexicalDeclContext - The declaration context where this Decl was 528 /// lexically declared (LexicalDC). May be different from 529 /// getDeclContext() (SemanticDC). 530 /// e.g.: 531 /// 532 /// namespace A { 533 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 534 /// } 535 /// void A::f(); // SemanticDC == namespace 'A' 536 /// // LexicalDC == global namespace 537 DeclContext *getLexicalDeclContext() { 538 if (isInSemaDC()) 539 return getSemanticDC(); 540 return getMultipleDC()->LexicalDC; 541 } 542 const DeclContext *getLexicalDeclContext() const { 543 return const_cast<Decl*>(this)->getLexicalDeclContext(); 544 } 545 546 virtual bool isOutOfLine() const { 547 return getLexicalDeclContext() != getDeclContext(); 548 } 549 550 /// setDeclContext - Set both the semantic and lexical DeclContext 551 /// to DC. 552 void setDeclContext(DeclContext *DC); 553 554 void setLexicalDeclContext(DeclContext *DC); 555 556 /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this 557 /// scoped decl is defined outside the current function or method. This is 558 /// roughly global variables and functions, but also handles enums (which 559 /// could be defined inside or outside a function etc). 560 bool isDefinedOutsideFunctionOrMethod() const; 561 562 /// \brief Retrieves the "canonical" declaration of the given declaration. 563 virtual Decl *getCanonicalDecl() { return this; } 564 const Decl *getCanonicalDecl() const { 565 return const_cast<Decl*>(this)->getCanonicalDecl(); 566 } 567 568 /// \brief Whether this particular Decl is a canonical one. 569 bool isCanonicalDecl() const { return getCanonicalDecl() == this; } 570 571protected: 572 /// \brief Returns the next redeclaration or itself if this is the only decl. 573 /// 574 /// Decl subclasses that can be redeclared should override this method so that 575 /// Decl::redecl_iterator can iterate over them. 576 virtual Decl *getNextRedeclaration() { return this; } 577 578public: 579 /// \brief Iterates through all the redeclarations of the same decl. 580 class redecl_iterator { 581 /// Current - The current declaration. 582 Decl *Current; 583 Decl *Starter; 584 585 public: 586 typedef Decl* value_type; 587 typedef Decl* reference; 588 typedef Decl* pointer; 589 typedef std::forward_iterator_tag iterator_category; 590 typedef std::ptrdiff_t difference_type; 591 592 redecl_iterator() : Current(0) { } 593 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { } 594 595 reference operator*() const { return Current; } 596 pointer operator->() const { return Current; } 597 598 redecl_iterator& operator++() { 599 assert(Current && "Advancing while iterator has reached end"); 600 // Get either previous decl or latest decl. 601 Decl *Next = Current->getNextRedeclaration(); 602 assert(Next && "Should return next redeclaration or itself, never null!"); 603 Current = (Next != Starter ? Next : 0); 604 return *this; 605 } 606 607 redecl_iterator operator++(int) { 608 redecl_iterator tmp(*this); 609 ++(*this); 610 return tmp; 611 } 612 613 friend bool operator==(redecl_iterator x, redecl_iterator y) { 614 return x.Current == y.Current; 615 } 616 friend bool operator!=(redecl_iterator x, redecl_iterator y) { 617 return x.Current != y.Current; 618 } 619 }; 620 621 /// \brief Returns iterator for all the redeclarations of the same decl. 622 /// It will iterate at least once (when this decl is the only one). 623 redecl_iterator redecls_begin() const { 624 return redecl_iterator(const_cast<Decl*>(this)); 625 } 626 redecl_iterator redecls_end() const { return redecl_iterator(); } 627 628 /// getBody - If this Decl represents a declaration for a body of code, 629 /// such as a function or method definition, this method returns the 630 /// top-level Stmt* of that body. Otherwise this method returns null. 631 virtual Stmt* getBody() const { return 0; } 632 633 /// \brief Returns true if this Decl represents a declaration for a body of 634 /// code, such as a function or method definition. 635 virtual bool hasBody() const { return getBody() != 0; } 636 637 /// getBodyRBrace - Gets the right brace of the body, if a body exists. 638 /// This works whether the body is a CompoundStmt or a CXXTryStmt. 639 SourceLocation getBodyRBrace() const; 640 641 // global temp stats (until we have a per-module visitor) 642 static void add(Kind k); 643 static bool CollectingStats(bool Enable = false); 644 static void PrintStats(); 645 646 /// isTemplateParameter - Determines whether this declaration is a 647 /// template parameter. 648 bool isTemplateParameter() const; 649 650 /// isTemplateParameter - Determines whether this declaration is a 651 /// template parameter pack. 652 bool isTemplateParameterPack() const; 653 654 /// \brief Whether this declaration is a parameter pack. 655 bool isParameterPack() const; 656 657 /// \brief Whether this declaration is a function or function template. 658 bool isFunctionOrFunctionTemplate() const; 659 660 /// \brief Changes the namespace of this declaration to reflect that it's 661 /// the object of a friend declaration. 662 /// 663 /// These declarations appear in the lexical context of the friending 664 /// class, but in the semantic context of the actual entity. This property 665 /// applies only to a specific decl object; other redeclarations of the 666 /// same entity may not (and probably don't) share this property. 667 void setObjectOfFriendDecl(bool PreviouslyDeclared) { 668 unsigned OldNS = IdentifierNamespace; 669 assert((OldNS & (IDNS_Tag | IDNS_Ordinary | 670 IDNS_TagFriend | IDNS_OrdinaryFriend)) && 671 "namespace includes neither ordinary nor tag"); 672 assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | 673 IDNS_TagFriend | IDNS_OrdinaryFriend)) && 674 "namespace includes other than ordinary or tag"); 675 676 IdentifierNamespace = 0; 677 if (OldNS & (IDNS_Tag | IDNS_TagFriend)) { 678 IdentifierNamespace |= IDNS_TagFriend; 679 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Tag | IDNS_Type; 680 } 681 682 if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend)) { 683 IdentifierNamespace |= IDNS_OrdinaryFriend; 684 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Ordinary; 685 } 686 } 687 688 enum FriendObjectKind { 689 FOK_None, // not a friend object 690 FOK_Declared, // a friend of a previously-declared entity 691 FOK_Undeclared // a friend of a previously-undeclared entity 692 }; 693 694 /// \brief Determines whether this declaration is the object of a 695 /// friend declaration and, if so, what kind. 696 /// 697 /// There is currently no direct way to find the associated FriendDecl. 698 FriendObjectKind getFriendObjectKind() const { 699 unsigned mask 700 = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend)); 701 if (!mask) return FOK_None; 702 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? 703 FOK_Declared : FOK_Undeclared); 704 } 705 706 /// Specifies that this declaration is a C++ overloaded non-member. 707 void setNonMemberOperator() { 708 assert(getKind() == Function || getKind() == FunctionTemplate); 709 assert((IdentifierNamespace & IDNS_Ordinary) && 710 "visible non-member operators should be in ordinary namespace"); 711 IdentifierNamespace |= IDNS_NonMemberOperator; 712 } 713 714 // Implement isa/cast/dyncast/etc. 715 static bool classof(const Decl *) { return true; } 716 static bool classofKind(Kind K) { return true; } 717 static DeclContext *castToDeclContext(const Decl *); 718 static Decl *castFromDeclContext(const DeclContext *); 719 720 void print(llvm::raw_ostream &Out, unsigned Indentation = 0) const; 721 void print(llvm::raw_ostream &Out, const PrintingPolicy &Policy, 722 unsigned Indentation = 0) const; 723 static void printGroup(Decl** Begin, unsigned NumDecls, 724 llvm::raw_ostream &Out, const PrintingPolicy &Policy, 725 unsigned Indentation = 0); 726 void dump() const; 727 void dumpXML() const; 728 void dumpXML(llvm::raw_ostream &OS) const; 729 730private: 731 const Attr *getAttrsImpl() const; 732 733protected: 734 ASTMutationListener *getASTMutationListener() const; 735}; 736 737/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when 738/// doing something to a specific decl. 739class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { 740 const Decl *TheDecl; 741 SourceLocation Loc; 742 SourceManager &SM; 743 const char *Message; 744public: 745 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L, 746 SourceManager &sm, const char *Msg) 747 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} 748 749 virtual void print(llvm::raw_ostream &OS) const; 750}; 751 752class DeclContextLookupResult 753 : public std::pair<NamedDecl**,NamedDecl**> { 754public: 755 DeclContextLookupResult(NamedDecl **I, NamedDecl **E) 756 : std::pair<NamedDecl**,NamedDecl**>(I, E) {} 757 DeclContextLookupResult() 758 : std::pair<NamedDecl**,NamedDecl**>() {} 759 760 using std::pair<NamedDecl**,NamedDecl**>::operator=; 761}; 762 763class DeclContextLookupConstResult 764 : public std::pair<NamedDecl*const*, NamedDecl*const*> { 765public: 766 DeclContextLookupConstResult(std::pair<NamedDecl**,NamedDecl**> R) 767 : std::pair<NamedDecl*const*, NamedDecl*const*>(R) {} 768 DeclContextLookupConstResult(NamedDecl * const *I, NamedDecl * const *E) 769 : std::pair<NamedDecl*const*, NamedDecl*const*>(I, E) {} 770 DeclContextLookupConstResult() 771 : std::pair<NamedDecl*const*, NamedDecl*const*>() {} 772 773 using std::pair<NamedDecl*const*,NamedDecl*const*>::operator=; 774}; 775 776/// DeclContext - This is used only as base class of specific decl types that 777/// can act as declaration contexts. These decls are (only the top classes 778/// that directly derive from DeclContext are mentioned, not their subclasses): 779/// 780/// TranslationUnitDecl 781/// NamespaceDecl 782/// FunctionDecl 783/// TagDecl 784/// ObjCMethodDecl 785/// ObjCContainerDecl 786/// LinkageSpecDecl 787/// BlockDecl 788/// 789class DeclContext { 790 /// DeclKind - This indicates which class this is. 791 unsigned DeclKind : 8; 792 793 /// \brief Whether this declaration context also has some external 794 /// storage that contains additional declarations that are lexically 795 /// part of this context. 796 mutable unsigned ExternalLexicalStorage : 1; 797 798 /// \brief Whether this declaration context also has some external 799 /// storage that contains additional declarations that are visible 800 /// in this context. 801 mutable unsigned ExternalVisibleStorage : 1; 802 803 /// \brief Pointer to the data structure used to lookup declarations 804 /// within this context (or a DependentStoredDeclsMap if this is a 805 /// dependent context). 806 mutable StoredDeclsMap *LookupPtr; 807 808protected: 809 /// FirstDecl - The first declaration stored within this declaration 810 /// context. 811 mutable Decl *FirstDecl; 812 813 /// LastDecl - The last declaration stored within this declaration 814 /// context. FIXME: We could probably cache this value somewhere 815 /// outside of the DeclContext, to reduce the size of DeclContext by 816 /// another pointer. 817 mutable Decl *LastDecl; 818 819 friend class ExternalASTSource; 820 821 /// \brief Build up a chain of declarations. 822 /// 823 /// \returns the first/last pair of declarations. 824 static std::pair<Decl *, Decl *> 825 BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls); 826 827 DeclContext(Decl::Kind K) 828 : DeclKind(K), ExternalLexicalStorage(false), 829 ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0), 830 LastDecl(0) { } 831 832public: 833 ~DeclContext(); 834 835 Decl::Kind getDeclKind() const { 836 return static_cast<Decl::Kind>(DeclKind); 837 } 838 const char *getDeclKindName() const; 839 840 /// getParent - Returns the containing DeclContext. 841 DeclContext *getParent() { 842 return cast<Decl>(this)->getDeclContext(); 843 } 844 const DeclContext *getParent() const { 845 return const_cast<DeclContext*>(this)->getParent(); 846 } 847 848 /// getLexicalParent - Returns the containing lexical DeclContext. May be 849 /// different from getParent, e.g.: 850 /// 851 /// namespace A { 852 /// struct S; 853 /// } 854 /// struct A::S {}; // getParent() == namespace 'A' 855 /// // getLexicalParent() == translation unit 856 /// 857 DeclContext *getLexicalParent() { 858 return cast<Decl>(this)->getLexicalDeclContext(); 859 } 860 const DeclContext *getLexicalParent() const { 861 return const_cast<DeclContext*>(this)->getLexicalParent(); 862 } 863 864 DeclContext *getLookupParent(); 865 866 const DeclContext *getLookupParent() const { 867 return const_cast<DeclContext*>(this)->getLookupParent(); 868 } 869 870 ASTContext &getParentASTContext() const { 871 return cast<Decl>(this)->getASTContext(); 872 } 873 874 bool isClosure() const { 875 return DeclKind == Decl::Block; 876 } 877 878 bool isFunctionOrMethod() const { 879 switch (DeclKind) { 880 case Decl::Block: 881 case Decl::ObjCMethod: 882 return true; 883 default: 884 return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction; 885 } 886 } 887 888 bool isFileContext() const { 889 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; 890 } 891 892 bool isTranslationUnit() const { 893 return DeclKind == Decl::TranslationUnit; 894 } 895 896 bool isRecord() const { 897 return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord; 898 } 899 900 bool isNamespace() const { 901 return DeclKind == Decl::Namespace; 902 } 903 904 bool isInlineNamespace() const; 905 906 /// \brief Determines whether this context is dependent on a 907 /// template parameter. 908 bool isDependentContext() const; 909 910 /// isTransparentContext - Determines whether this context is a 911 /// "transparent" context, meaning that the members declared in this 912 /// context are semantically declared in the nearest enclosing 913 /// non-transparent (opaque) context but are lexically declared in 914 /// this context. For example, consider the enumerators of an 915 /// enumeration type: 916 /// @code 917 /// enum E { 918 /// Val1 919 /// }; 920 /// @endcode 921 /// Here, E is a transparent context, so its enumerator (Val1) will 922 /// appear (semantically) that it is in the same context of E. 923 /// Examples of transparent contexts include: enumerations (except for 924 /// C++0x scoped enums), and C++ linkage specifications. 925 bool isTransparentContext() const; 926 927 /// \brief Determines whether this context is, or is nested within, 928 /// a C++ extern "C" linkage spec. 929 bool isExternCContext() const; 930 931 /// \brief Determine whether this declaration context is equivalent 932 /// to the declaration context DC. 933 bool Equals(const DeclContext *DC) const { 934 return DC && this->getPrimaryContext() == DC->getPrimaryContext(); 935 } 936 937 /// \brief Determine whether this declaration context encloses the 938 /// declaration context DC. 939 bool Encloses(const DeclContext *DC) const; 940 941 /// getPrimaryContext - There may be many different 942 /// declarations of the same entity (including forward declarations 943 /// of classes, multiple definitions of namespaces, etc.), each with 944 /// a different set of declarations. This routine returns the 945 /// "primary" DeclContext structure, which will contain the 946 /// information needed to perform name lookup into this context. 947 DeclContext *getPrimaryContext(); 948 const DeclContext *getPrimaryContext() const { 949 return const_cast<DeclContext*>(this)->getPrimaryContext(); 950 } 951 952 /// getRedeclContext - Retrieve the context in which an entity conflicts with 953 /// other entities of the same name, or where it is a redeclaration if the 954 /// two entities are compatible. This skips through transparent contexts. 955 DeclContext *getRedeclContext(); 956 const DeclContext *getRedeclContext() const { 957 return const_cast<DeclContext *>(this)->getRedeclContext(); 958 } 959 960 /// \brief Retrieve the nearest enclosing namespace context. 961 DeclContext *getEnclosingNamespaceContext(); 962 const DeclContext *getEnclosingNamespaceContext() const { 963 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); 964 } 965 966 /// \brief Test if this context is part of the enclosing namespace set of 967 /// the context NS, as defined in C++0x [namespace.def]p9. If either context 968 /// isn't a namespace, this is equivalent to Equals(). 969 /// 970 /// The enclosing namespace set of a namespace is the namespace and, if it is 971 /// inline, its enclosing namespace, recursively. 972 bool InEnclosingNamespaceSetOf(const DeclContext *NS) const; 973 974 /// getNextContext - If this is a DeclContext that may have other 975 /// DeclContexts that are semantically connected but syntactically 976 /// different, such as C++ namespaces, this routine retrieves the 977 /// next DeclContext in the link. Iteration through the chain of 978 /// DeclContexts should begin at the primary DeclContext and 979 /// continue until this function returns NULL. For example, given: 980 /// @code 981 /// namespace N { 982 /// int x; 983 /// } 984 /// namespace N { 985 /// int y; 986 /// } 987 /// @endcode 988 /// The first occurrence of namespace N will be the primary 989 /// DeclContext. Its getNextContext will return the second 990 /// occurrence of namespace N. 991 DeclContext *getNextContext(); 992 993 /// decl_iterator - Iterates through the declarations stored 994 /// within this context. 995 class decl_iterator { 996 /// Current - The current declaration. 997 Decl *Current; 998 999 public: 1000 typedef Decl* value_type; 1001 typedef Decl* reference; 1002 typedef Decl* pointer; 1003 typedef std::forward_iterator_tag iterator_category; 1004 typedef std::ptrdiff_t difference_type; 1005 1006 decl_iterator() : Current(0) { } 1007 explicit decl_iterator(Decl *C) : Current(C) { } 1008 1009 reference operator*() const { return Current; } 1010 pointer operator->() const { return Current; } 1011 1012 decl_iterator& operator++() { 1013 Current = Current->getNextDeclInContext(); 1014 return *this; 1015 } 1016 1017 decl_iterator operator++(int) { 1018 decl_iterator tmp(*this); 1019 ++(*this); 1020 return tmp; 1021 } 1022 1023 friend bool operator==(decl_iterator x, decl_iterator y) { 1024 return x.Current == y.Current; 1025 } 1026 friend bool operator!=(decl_iterator x, decl_iterator y) { 1027 return x.Current != y.Current; 1028 } 1029 }; 1030 1031 /// decls_begin/decls_end - Iterate over the declarations stored in 1032 /// this context. 1033 decl_iterator decls_begin() const; 1034 decl_iterator decls_end() const; 1035 bool decls_empty() const; 1036 1037 /// noload_decls_begin/end - Iterate over the declarations stored in this 1038 /// context that are currently loaded; don't attempt to retrieve anything 1039 /// from an external source. 1040 decl_iterator noload_decls_begin() const; 1041 decl_iterator noload_decls_end() const; 1042 1043 /// specific_decl_iterator - Iterates over a subrange of 1044 /// declarations stored in a DeclContext, providing only those that 1045 /// are of type SpecificDecl (or a class derived from it). This 1046 /// iterator is used, for example, to provide iteration over just 1047 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). 1048 template<typename SpecificDecl> 1049 class specific_decl_iterator { 1050 /// Current - The current, underlying declaration iterator, which 1051 /// will either be NULL or will point to a declaration of 1052 /// type SpecificDecl. 1053 DeclContext::decl_iterator Current; 1054 1055 /// SkipToNextDecl - Advances the current position up to the next 1056 /// declaration of type SpecificDecl that also meets the criteria 1057 /// required by Acceptable. 1058 void SkipToNextDecl() { 1059 while (*Current && !isa<SpecificDecl>(*Current)) 1060 ++Current; 1061 } 1062 1063 public: 1064 typedef SpecificDecl* value_type; 1065 typedef SpecificDecl* reference; 1066 typedef SpecificDecl* pointer; 1067 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 1068 difference_type; 1069 typedef std::forward_iterator_tag iterator_category; 1070 1071 specific_decl_iterator() : Current() { } 1072 1073 /// specific_decl_iterator - Construct a new iterator over a 1074 /// subset of the declarations the range [C, 1075 /// end-of-declarations). If A is non-NULL, it is a pointer to a 1076 /// member function of SpecificDecl that should return true for 1077 /// all of the SpecificDecl instances that will be in the subset 1078 /// of iterators. For example, if you want Objective-C instance 1079 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 1080 /// &ObjCMethodDecl::isInstanceMethod. 1081 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 1082 SkipToNextDecl(); 1083 } 1084 1085 reference operator*() const { return cast<SpecificDecl>(*Current); } 1086 pointer operator->() const { return cast<SpecificDecl>(*Current); } 1087 1088 specific_decl_iterator& operator++() { 1089 ++Current; 1090 SkipToNextDecl(); 1091 return *this; 1092 } 1093 1094 specific_decl_iterator operator++(int) { 1095 specific_decl_iterator tmp(*this); 1096 ++(*this); 1097 return tmp; 1098 } 1099 1100 friend bool 1101 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) { 1102 return x.Current == y.Current; 1103 } 1104 1105 friend bool 1106 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) { 1107 return x.Current != y.Current; 1108 } 1109 }; 1110 1111 /// \brief Iterates over a filtered subrange of declarations stored 1112 /// in a DeclContext. 1113 /// 1114 /// This iterator visits only those declarations that are of type 1115 /// SpecificDecl (or a class derived from it) and that meet some 1116 /// additional run-time criteria. This iterator is used, for 1117 /// example, to provide access to the instance methods within an 1118 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and 1119 /// Acceptable = ObjCMethodDecl::isInstanceMethod). 1120 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> 1121 class filtered_decl_iterator { 1122 /// Current - The current, underlying declaration iterator, which 1123 /// will either be NULL or will point to a declaration of 1124 /// type SpecificDecl. 1125 DeclContext::decl_iterator Current; 1126 1127 /// SkipToNextDecl - Advances the current position up to the next 1128 /// declaration of type SpecificDecl that also meets the criteria 1129 /// required by Acceptable. 1130 void SkipToNextDecl() { 1131 while (*Current && 1132 (!isa<SpecificDecl>(*Current) || 1133 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) 1134 ++Current; 1135 } 1136 1137 public: 1138 typedef SpecificDecl* value_type; 1139 typedef SpecificDecl* reference; 1140 typedef SpecificDecl* pointer; 1141 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 1142 difference_type; 1143 typedef std::forward_iterator_tag iterator_category; 1144 1145 filtered_decl_iterator() : Current() { } 1146 1147 /// specific_decl_iterator - Construct a new iterator over a 1148 /// subset of the declarations the range [C, 1149 /// end-of-declarations). If A is non-NULL, it is a pointer to a 1150 /// member function of SpecificDecl that should return true for 1151 /// all of the SpecificDecl instances that will be in the subset 1152 /// of iterators. For example, if you want Objective-C instance 1153 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 1154 /// &ObjCMethodDecl::isInstanceMethod. 1155 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 1156 SkipToNextDecl(); 1157 } 1158 1159 reference operator*() const { return cast<SpecificDecl>(*Current); } 1160 pointer operator->() const { return cast<SpecificDecl>(*Current); } 1161 1162 filtered_decl_iterator& operator++() { 1163 ++Current; 1164 SkipToNextDecl(); 1165 return *this; 1166 } 1167 1168 filtered_decl_iterator operator++(int) { 1169 filtered_decl_iterator tmp(*this); 1170 ++(*this); 1171 return tmp; 1172 } 1173 1174 friend bool 1175 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 1176 return x.Current == y.Current; 1177 } 1178 1179 friend bool 1180 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 1181 return x.Current != y.Current; 1182 } 1183 }; 1184 1185 /// @brief Add the declaration D into this context. 1186 /// 1187 /// This routine should be invoked when the declaration D has first 1188 /// been declared, to place D into the context where it was 1189 /// (lexically) defined. Every declaration must be added to one 1190 /// (and only one!) context, where it can be visited via 1191 /// [decls_begin(), decls_end()). Once a declaration has been added 1192 /// to its lexical context, the corresponding DeclContext owns the 1193 /// declaration. 1194 /// 1195 /// If D is also a NamedDecl, it will be made visible within its 1196 /// semantic context via makeDeclVisibleInContext. 1197 void addDecl(Decl *D); 1198 1199 /// @brief Add the declaration D to this context without modifying 1200 /// any lookup tables. 1201 /// 1202 /// This is useful for some operations in dependent contexts where 1203 /// the semantic context might not be dependent; this basically 1204 /// only happens with friends. 1205 void addHiddenDecl(Decl *D); 1206 1207 /// @brief Removes a declaration from this context. 1208 void removeDecl(Decl *D); 1209 1210 /// lookup_iterator - An iterator that provides access to the results 1211 /// of looking up a name within this context. 1212 typedef NamedDecl **lookup_iterator; 1213 1214 /// lookup_const_iterator - An iterator that provides non-mutable 1215 /// access to the results of lookup up a name within this context. 1216 typedef NamedDecl * const * lookup_const_iterator; 1217 1218 typedef DeclContextLookupResult lookup_result; 1219 typedef DeclContextLookupConstResult lookup_const_result; 1220 1221 /// lookup - Find the declarations (if any) with the given Name in 1222 /// this context. Returns a range of iterators that contains all of 1223 /// the declarations with this name, with object, function, member, 1224 /// and enumerator names preceding any tag name. Note that this 1225 /// routine will not look into parent contexts. 1226 lookup_result lookup(DeclarationName Name); 1227 lookup_const_result lookup(DeclarationName Name) const; 1228 1229 /// @brief Makes a declaration visible within this context. 1230 /// 1231 /// This routine makes the declaration D visible to name lookup 1232 /// within this context and, if this is a transparent context, 1233 /// within its parent contexts up to the first enclosing 1234 /// non-transparent context. Making a declaration visible within a 1235 /// context does not transfer ownership of a declaration, and a 1236 /// declaration can be visible in many contexts that aren't its 1237 /// lexical context. 1238 /// 1239 /// If D is a redeclaration of an existing declaration that is 1240 /// visible from this context, as determined by 1241 /// NamedDecl::declarationReplaces, the previous declaration will be 1242 /// replaced with D. 1243 /// 1244 /// @param Recoverable true if it's okay to not add this decl to 1245 /// the lookup tables because it can be easily recovered by walking 1246 /// the declaration chains. 1247 void makeDeclVisibleInContext(NamedDecl *D, bool Recoverable = true); 1248 1249 /// \brief Deserialize all the visible declarations from external storage. 1250 /// 1251 /// Name lookup deserializes visible declarations lazily, thus a DeclContext 1252 /// may not have a complete name lookup table. This function deserializes 1253 /// the rest of visible declarations from the external storage and completes 1254 /// the name lookup table. 1255 void MaterializeVisibleDeclsFromExternalStorage(); 1256 1257 /// udir_iterator - Iterates through the using-directives stored 1258 /// within this context. 1259 typedef UsingDirectiveDecl * const * udir_iterator; 1260 1261 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range; 1262 1263 udir_iterator_range getUsingDirectives() const; 1264 1265 udir_iterator using_directives_begin() const { 1266 return getUsingDirectives().first; 1267 } 1268 1269 udir_iterator using_directives_end() const { 1270 return getUsingDirectives().second; 1271 } 1272 1273 // These are all defined in DependentDiagnostic.h. 1274 class ddiag_iterator; 1275 inline ddiag_iterator ddiag_begin() const; 1276 inline ddiag_iterator ddiag_end() const; 1277 1278 // Low-level accessors 1279 1280 /// \brief Retrieve the internal representation of the lookup structure. 1281 StoredDeclsMap* getLookupPtr() const { return LookupPtr; } 1282 1283 /// \brief Whether this DeclContext has external storage containing 1284 /// additional declarations that are lexically in this context. 1285 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; } 1286 1287 /// \brief State whether this DeclContext has external storage for 1288 /// declarations lexically in this context. 1289 void setHasExternalLexicalStorage(bool ES = true) { 1290 ExternalLexicalStorage = ES; 1291 } 1292 1293 /// \brief Whether this DeclContext has external storage containing 1294 /// additional declarations that are visible in this context. 1295 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; } 1296 1297 /// \brief State whether this DeclContext has external storage for 1298 /// declarations visible in this context. 1299 void setHasExternalVisibleStorage(bool ES = true) { 1300 ExternalVisibleStorage = ES; 1301 } 1302 1303 static bool classof(const Decl *D); 1304 static bool classof(const DeclContext *D) { return true; } 1305#define DECL(NAME, BASE) 1306#define DECL_CONTEXT(NAME) \ 1307 static bool classof(const NAME##Decl *D) { return true; } 1308#include "clang/AST/DeclNodes.inc" 1309 1310 void dumpDeclContext() const; 1311 1312private: 1313 void LoadLexicalDeclsFromExternalStorage() const; 1314 1315 friend class DependentDiagnostic; 1316 StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const; 1317 1318 void buildLookup(DeclContext *DCtx); 1319 void makeDeclVisibleInContextImpl(NamedDecl *D); 1320}; 1321 1322inline bool Decl::isTemplateParameter() const { 1323 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm || 1324 getKind() == TemplateTemplateParm; 1325} 1326 1327// Specialization selected when ToTy is not a known subclass of DeclContext. 1328template <class ToTy, 1329 bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value> 1330struct cast_convert_decl_context { 1331 static const ToTy *doit(const DeclContext *Val) { 1332 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val)); 1333 } 1334 1335 static ToTy *doit(DeclContext *Val) { 1336 return static_cast<ToTy*>(Decl::castFromDeclContext(Val)); 1337 } 1338}; 1339 1340// Specialization selected when ToTy is a known subclass of DeclContext. 1341template <class ToTy> 1342struct cast_convert_decl_context<ToTy, true> { 1343 static const ToTy *doit(const DeclContext *Val) { 1344 return static_cast<const ToTy*>(Val); 1345 } 1346 1347 static ToTy *doit(DeclContext *Val) { 1348 return static_cast<ToTy*>(Val); 1349 } 1350}; 1351 1352 1353} // end clang. 1354 1355namespace llvm { 1356 1357/// isa<T>(DeclContext*) 1358template <typename To> 1359struct isa_impl<To, ::clang::DeclContext> { 1360 static bool doit(const ::clang::DeclContext &Val) { 1361 return To::classofKind(Val.getDeclKind()); 1362 } 1363}; 1364 1365/// cast<T>(DeclContext*) 1366template<class ToTy> 1367struct cast_convert_val<ToTy, 1368 const ::clang::DeclContext,const ::clang::DeclContext> { 1369 static const ToTy &doit(const ::clang::DeclContext &Val) { 1370 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); 1371 } 1372}; 1373template<class ToTy> 1374struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> { 1375 static ToTy &doit(::clang::DeclContext &Val) { 1376 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); 1377 } 1378}; 1379template<class ToTy> 1380struct cast_convert_val<ToTy, 1381 const ::clang::DeclContext*, const ::clang::DeclContext*> { 1382 static const ToTy *doit(const ::clang::DeclContext *Val) { 1383 return ::clang::cast_convert_decl_context<ToTy>::doit(Val); 1384 } 1385}; 1386template<class ToTy> 1387struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> { 1388 static ToTy *doit(::clang::DeclContext *Val) { 1389 return ::clang::cast_convert_decl_context<ToTy>::doit(Val); 1390 } 1391}; 1392 1393/// Implement cast_convert_val for Decl -> DeclContext conversions. 1394template<class FromTy> 1395struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { 1396 static ::clang::DeclContext &doit(const FromTy &Val) { 1397 return *FromTy::castToDeclContext(&Val); 1398 } 1399}; 1400 1401template<class FromTy> 1402struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { 1403 static ::clang::DeclContext *doit(const FromTy *Val) { 1404 return FromTy::castToDeclContext(Val); 1405 } 1406}; 1407 1408template<class FromTy> 1409struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { 1410 static const ::clang::DeclContext &doit(const FromTy &Val) { 1411 return *FromTy::castToDeclContext(&Val); 1412 } 1413}; 1414 1415template<class FromTy> 1416struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { 1417 static const ::clang::DeclContext *doit(const FromTy *Val) { 1418 return FromTy::castToDeclContext(Val); 1419 } 1420}; 1421 1422} // end namespace llvm 1423 1424#endif 1425