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/AttrIterator.h" 18#include "clang/AST/DeclarationName.h" 19#include "clang/Basic/Specifiers.h" 20#include "clang/Basic/VersionTuple.h" 21#include "llvm/ADT/PointerUnion.h" 22#include "llvm/ADT/iterator.h" 23#include "llvm/ADT/iterator_range.h" 24#include "llvm/Support/Compiler.h" 25#include "llvm/Support/PrettyStackTrace.h" 26 27namespace clang { 28class ASTMutationListener; 29class BlockDecl; 30class CXXRecordDecl; 31class CompoundStmt; 32class DeclContext; 33class DeclarationName; 34class DependentDiagnostic; 35class EnumDecl; 36class ExportDecl; 37class ExternalSourceSymbolAttr; 38class FunctionDecl; 39class FunctionType; 40enum Linkage : unsigned char; 41class LinkageComputer; 42class LinkageSpecDecl; 43class Module; 44class NamedDecl; 45class NamespaceDecl; 46class ObjCCategoryDecl; 47class ObjCCategoryImplDecl; 48class ObjCContainerDecl; 49class ObjCImplDecl; 50class ObjCImplementationDecl; 51class ObjCInterfaceDecl; 52class ObjCMethodDecl; 53class ObjCProtocolDecl; 54struct PrintingPolicy; 55class RecordDecl; 56class Stmt; 57class StoredDeclsMap; 58class TemplateDecl; 59class TranslationUnitDecl; 60class UsingDirectiveDecl; 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/// 77/// Note: There are objects tacked on before the *beginning* of Decl 78/// (and its subclasses) in its Decl::operator new(). Proper alignment 79/// of all subclasses (not requiring more than the alignment of Decl) is 80/// asserted in DeclBase.cpp. 81class LLVM_ALIGNAS(/*alignof(uint64_t)*/ 8) Decl { 82public: 83 /// \brief Lists the kind of concrete classes of Decl. 84 enum Kind { 85#define DECL(DERIVED, BASE) DERIVED, 86#define ABSTRACT_DECL(DECL) 87#define DECL_RANGE(BASE, START, END) \ 88 first##BASE = START, last##BASE = END, 89#define LAST_DECL_RANGE(BASE, START, END) \ 90 first##BASE = START, last##BASE = END 91#include "clang/AST/DeclNodes.inc" 92 }; 93 94 /// \brief A placeholder type used to construct an empty shell of a 95 /// decl-derived type that will be filled in later (e.g., by some 96 /// deserialization method). 97 struct EmptyShell { }; 98 99 /// IdentifierNamespace - The different namespaces in which 100 /// declarations may appear. According to C99 6.2.3, there are 101 /// four namespaces, labels, tags, members and ordinary 102 /// identifiers. C++ describes lookup completely differently: 103 /// certain lookups merely "ignore" certain kinds of declarations, 104 /// usually based on whether the declaration is of a type, etc. 105 /// 106 /// These are meant as bitmasks, so that searches in 107 /// C++ can look into the "tag" namespace during ordinary lookup. 108 /// 109 /// Decl currently provides 15 bits of IDNS bits. 110 enum IdentifierNamespace { 111 /// Labels, declared with 'x:' and referenced with 'goto x'. 112 IDNS_Label = 0x0001, 113 114 /// Tags, declared with 'struct foo;' and referenced with 115 /// 'struct foo'. All tags are also types. This is what 116 /// elaborated-type-specifiers look for in C. 117 /// This also contains names that conflict with tags in the 118 /// same scope but that are otherwise ordinary names (non-type 119 /// template parameters and indirect field declarations). 120 IDNS_Tag = 0x0002, 121 122 /// Types, declared with 'struct foo', typedefs, etc. 123 /// This is what elaborated-type-specifiers look for in C++, 124 /// but note that it's ill-formed to find a non-tag. 125 IDNS_Type = 0x0004, 126 127 /// Members, declared with object declarations within tag 128 /// definitions. In C, these can only be found by "qualified" 129 /// lookup in member expressions. In C++, they're found by 130 /// normal lookup. 131 IDNS_Member = 0x0008, 132 133 /// Namespaces, declared with 'namespace foo {}'. 134 /// Lookup for nested-name-specifiers find these. 135 IDNS_Namespace = 0x0010, 136 137 /// Ordinary names. In C, everything that's not a label, tag, 138 /// member, or function-local extern ends up here. 139 IDNS_Ordinary = 0x0020, 140 141 /// Objective C \@protocol. 142 IDNS_ObjCProtocol = 0x0040, 143 144 /// This declaration is a friend function. A friend function 145 /// declaration is always in this namespace but may also be in 146 /// IDNS_Ordinary if it was previously declared. 147 IDNS_OrdinaryFriend = 0x0080, 148 149 /// This declaration is a friend class. A friend class 150 /// declaration is always in this namespace but may also be in 151 /// IDNS_Tag|IDNS_Type if it was previously declared. 152 IDNS_TagFriend = 0x0100, 153 154 /// This declaration is a using declaration. A using declaration 155 /// *introduces* a number of other declarations into the current 156 /// scope, and those declarations use the IDNS of their targets, 157 /// but the actual using declarations go in this namespace. 158 IDNS_Using = 0x0200, 159 160 /// This declaration is a C++ operator declared in a non-class 161 /// context. All such operators are also in IDNS_Ordinary. 162 /// C++ lexical operator lookup looks for these. 163 IDNS_NonMemberOperator = 0x0400, 164 165 /// This declaration is a function-local extern declaration of a 166 /// variable or function. This may also be IDNS_Ordinary if it 167 /// has been declared outside any function. These act mostly like 168 /// invisible friend declarations, but are also visible to unqualified 169 /// lookup within the scope of the declaring function. 170 IDNS_LocalExtern = 0x0800, 171 172 /// This declaration is an OpenMP user defined reduction construction. 173 IDNS_OMPReduction = 0x1000 174 }; 175 176 /// ObjCDeclQualifier - 'Qualifiers' written next to the return and 177 /// parameter types in method declarations. Other than remembering 178 /// them and mangling them into the method's signature string, these 179 /// are ignored by the compiler; they are consumed by certain 180 /// remote-messaging frameworks. 181 /// 182 /// in, inout, and out are mutually exclusive and apply only to 183 /// method parameters. bycopy and byref are mutually exclusive and 184 /// apply only to method parameters (?). oneway applies only to 185 /// results. All of these expect their corresponding parameter to 186 /// have a particular type. None of this is currently enforced by 187 /// clang. 188 /// 189 /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier. 190 enum ObjCDeclQualifier { 191 OBJC_TQ_None = 0x0, 192 OBJC_TQ_In = 0x1, 193 OBJC_TQ_Inout = 0x2, 194 OBJC_TQ_Out = 0x4, 195 OBJC_TQ_Bycopy = 0x8, 196 OBJC_TQ_Byref = 0x10, 197 OBJC_TQ_Oneway = 0x20, 198 199 /// The nullability qualifier is set when the nullability of the 200 /// result or parameter was expressed via a context-sensitive 201 /// keyword. 202 OBJC_TQ_CSNullability = 0x40 203 }; 204 205 /// The kind of ownership a declaration has, for visibility purposes. 206 /// This enumeration is designed such that higher values represent higher 207 /// levels of name hiding. 208 enum class ModuleOwnershipKind : unsigned { 209 /// This declaration is not owned by a module. 210 Unowned, 211 /// This declaration has an owning module, but is globally visible 212 /// (typically because its owning module is visible and we know that 213 /// modules cannot later become hidden in this compilation). 214 /// After serialization and deserialization, this will be converted 215 /// to VisibleWhenImported. 216 Visible, 217 /// This declaration has an owning module, and is visible when that 218 /// module is imported. 219 VisibleWhenImported, 220 /// This declaration has an owning module, but is only visible to 221 /// lookups that occur within that module. 222 ModulePrivate 223 }; 224 225protected: 226 /// \brief The next declaration within the same lexical 227 /// DeclContext. These pointers form the linked list that is 228 /// traversed via DeclContext's decls_begin()/decls_end(). 229 /// 230 /// The extra two bits are used for the ModuleOwnershipKind. 231 llvm::PointerIntPair<Decl *, 2, ModuleOwnershipKind> NextInContextAndBits; 232 233private: 234 friend class DeclContext; 235 236 struct MultipleDC { 237 DeclContext *SemanticDC; 238 DeclContext *LexicalDC; 239 }; 240 241 242 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*. 243 /// For declarations that don't contain C++ scope specifiers, it contains 244 /// the DeclContext where the Decl was declared. 245 /// For declarations with C++ scope specifiers, it contains a MultipleDC* 246 /// with the context where it semantically belongs (SemanticDC) and the 247 /// context where it was lexically declared (LexicalDC). 248 /// e.g.: 249 /// 250 /// namespace A { 251 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 252 /// } 253 /// void A::f(); // SemanticDC == namespace 'A' 254 /// // LexicalDC == global namespace 255 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx; 256 257 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); } 258 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); } 259 inline MultipleDC *getMultipleDC() const { 260 return DeclCtx.get<MultipleDC*>(); 261 } 262 inline DeclContext *getSemanticDC() const { 263 return DeclCtx.get<DeclContext*>(); 264 } 265 266 /// Loc - The location of this decl. 267 SourceLocation Loc; 268 269 /// DeclKind - This indicates which class this is. 270 unsigned DeclKind : 7; 271 272 /// InvalidDecl - This indicates a semantic error occurred. 273 unsigned InvalidDecl : 1; 274 275 /// HasAttrs - This indicates whether the decl has attributes or not. 276 unsigned HasAttrs : 1; 277 278 /// Implicit - Whether this declaration was implicitly generated by 279 /// the implementation rather than explicitly written by the user. 280 unsigned Implicit : 1; 281 282 /// \brief Whether this declaration was "used", meaning that a definition is 283 /// required. 284 unsigned Used : 1; 285 286 /// \brief Whether this declaration was "referenced". 287 /// The difference with 'Used' is whether the reference appears in a 288 /// evaluated context or not, e.g. functions used in uninstantiated templates 289 /// are regarded as "referenced" but not "used". 290 unsigned Referenced : 1; 291 292 /// \brief Whether this declaration is a top-level declaration (function, 293 /// global variable, etc.) that is lexically inside an objc container 294 /// definition. 295 unsigned TopLevelDeclInObjCContainer : 1; 296 297 /// \brief Whether statistic collection is enabled. 298 static bool StatisticsEnabled; 299 300protected: 301 /// Access - Used by C++ decls for the access specifier. 302 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum 303 unsigned Access : 2; 304 friend class CXXClassMemberWrapper; 305 306 /// \brief Whether this declaration was loaded from an AST file. 307 unsigned FromASTFile : 1; 308 309 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in. 310 unsigned IdentifierNamespace : 13; 311 312 /// \brief If 0, we have not computed the linkage of this declaration. 313 /// Otherwise, it is the linkage + 1. 314 mutable unsigned CacheValidAndLinkage : 3; 315 316 friend class ASTDeclWriter; 317 friend class ASTDeclReader; 318 friend class ASTReader; 319 friend class LinkageComputer; 320 321 template<typename decl_type> friend class Redeclarable; 322 323 /// \brief Allocate memory for a deserialized declaration. 324 /// 325 /// This routine must be used to allocate memory for any declaration that is 326 /// deserialized from a module file. 327 /// 328 /// \param Size The size of the allocated object. 329 /// \param Ctx The context in which we will allocate memory. 330 /// \param ID The global ID of the deserialized declaration. 331 /// \param Extra The amount of extra space to allocate after the object. 332 void *operator new(std::size_t Size, const ASTContext &Ctx, unsigned ID, 333 std::size_t Extra = 0); 334 335 /// \brief Allocate memory for a non-deserialized declaration. 336 void *operator new(std::size_t Size, const ASTContext &Ctx, 337 DeclContext *Parent, std::size_t Extra = 0); 338 339private: 340 bool AccessDeclContextSanity() const; 341 342 /// Get the module ownership kind to use for a local lexical child of \p DC, 343 /// which may be either a local or (rarely) an imported declaration. 344 static ModuleOwnershipKind getModuleOwnershipKindForChildOf(DeclContext *DC) { 345 if (DC) { 346 auto *D = cast<Decl>(DC); 347 auto MOK = D->getModuleOwnershipKind(); 348 if (MOK != ModuleOwnershipKind::Unowned && 349 (!D->isFromASTFile() || D->hasLocalOwningModuleStorage())) 350 return MOK; 351 // If D is not local and we have no local module storage, then we don't 352 // need to track module ownership at all. 353 } 354 return ModuleOwnershipKind::Unowned; 355 } 356 357protected: 358 Decl(Kind DK, DeclContext *DC, SourceLocation L) 359 : NextInContextAndBits(nullptr, getModuleOwnershipKindForChildOf(DC)), 360 DeclCtx(DC), Loc(L), DeclKind(DK), InvalidDecl(0), HasAttrs(false), 361 Implicit(false), Used(false), Referenced(false), 362 TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0), 363 IdentifierNamespace(getIdentifierNamespaceForKind(DK)), 364 CacheValidAndLinkage(0) { 365 if (StatisticsEnabled) add(DK); 366 } 367 368 Decl(Kind DK, EmptyShell Empty) 369 : NextInContextAndBits(), DeclKind(DK), InvalidDecl(0), HasAttrs(false), 370 Implicit(false), Used(false), Referenced(false), 371 TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0), 372 IdentifierNamespace(getIdentifierNamespaceForKind(DK)), 373 CacheValidAndLinkage(0) { 374 if (StatisticsEnabled) add(DK); 375 } 376 377 virtual ~Decl(); 378 379 /// \brief Update a potentially out-of-date declaration. 380 void updateOutOfDate(IdentifierInfo &II) const; 381 382 Linkage getCachedLinkage() const { 383 return Linkage(CacheValidAndLinkage - 1); 384 } 385 386 void setCachedLinkage(Linkage L) const { 387 CacheValidAndLinkage = L + 1; 388 } 389 390 bool hasCachedLinkage() const { 391 return CacheValidAndLinkage; 392 } 393 394public: 395 396 /// \brief Source range that this declaration covers. 397 virtual SourceRange getSourceRange() const LLVM_READONLY { 398 return SourceRange(getLocation(), getLocation()); 399 } 400 SourceLocation getLocStart() const LLVM_READONLY { 401 return getSourceRange().getBegin(); 402 } 403 SourceLocation getLocEnd() const LLVM_READONLY { 404 return getSourceRange().getEnd(); 405 } 406 407 SourceLocation getLocation() const { return Loc; } 408 void setLocation(SourceLocation L) { Loc = L; } 409 410 Kind getKind() const { return static_cast<Kind>(DeclKind); } 411 const char *getDeclKindName() const; 412 413 Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); } 414 const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();} 415 416 DeclContext *getDeclContext() { 417 if (isInSemaDC()) 418 return getSemanticDC(); 419 return getMultipleDC()->SemanticDC; 420 } 421 const DeclContext *getDeclContext() const { 422 return const_cast<Decl*>(this)->getDeclContext(); 423 } 424 425 /// Find the innermost non-closure ancestor of this declaration, 426 /// walking up through blocks, lambdas, etc. If that ancestor is 427 /// not a code context (!isFunctionOrMethod()), returns null. 428 /// 429 /// A declaration may be its own non-closure context. 430 Decl *getNonClosureContext(); 431 const Decl *getNonClosureContext() const { 432 return const_cast<Decl*>(this)->getNonClosureContext(); 433 } 434 435 TranslationUnitDecl *getTranslationUnitDecl(); 436 const TranslationUnitDecl *getTranslationUnitDecl() const { 437 return const_cast<Decl*>(this)->getTranslationUnitDecl(); 438 } 439 440 bool isInAnonymousNamespace() const; 441 442 bool isInStdNamespace() const; 443 444 ASTContext &getASTContext() const LLVM_READONLY; 445 446 void setAccess(AccessSpecifier AS) { 447 Access = AS; 448 assert(AccessDeclContextSanity()); 449 } 450 451 AccessSpecifier getAccess() const { 452 assert(AccessDeclContextSanity()); 453 return AccessSpecifier(Access); 454 } 455 456 /// \brief Retrieve the access specifier for this declaration, even though 457 /// it may not yet have been properly set. 458 AccessSpecifier getAccessUnsafe() const { 459 return AccessSpecifier(Access); 460 } 461 462 bool hasAttrs() const { return HasAttrs; } 463 void setAttrs(const AttrVec& Attrs) { 464 return setAttrsImpl(Attrs, getASTContext()); 465 } 466 AttrVec &getAttrs() { 467 return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs()); 468 } 469 const AttrVec &getAttrs() const; 470 void dropAttrs(); 471 472 void addAttr(Attr *A) { 473 if (hasAttrs()) 474 getAttrs().push_back(A); 475 else 476 setAttrs(AttrVec(1, A)); 477 } 478 479 typedef AttrVec::const_iterator attr_iterator; 480 typedef llvm::iterator_range<attr_iterator> attr_range; 481 482 attr_range attrs() const { 483 return attr_range(attr_begin(), attr_end()); 484 } 485 486 attr_iterator attr_begin() const { 487 return hasAttrs() ? getAttrs().begin() : nullptr; 488 } 489 attr_iterator attr_end() const { 490 return hasAttrs() ? getAttrs().end() : nullptr; 491 } 492 493 template <typename T> 494 void dropAttr() { 495 if (!HasAttrs) return; 496 497 AttrVec &Vec = getAttrs(); 498 Vec.erase(std::remove_if(Vec.begin(), Vec.end(), isa<T, Attr*>), Vec.end()); 499 500 if (Vec.empty()) 501 HasAttrs = false; 502 } 503 504 template <typename T> 505 llvm::iterator_range<specific_attr_iterator<T>> specific_attrs() const { 506 return llvm::make_range(specific_attr_begin<T>(), specific_attr_end<T>()); 507 } 508 509 template <typename T> 510 specific_attr_iterator<T> specific_attr_begin() const { 511 return specific_attr_iterator<T>(attr_begin()); 512 } 513 template <typename T> 514 specific_attr_iterator<T> specific_attr_end() const { 515 return specific_attr_iterator<T>(attr_end()); 516 } 517 518 template<typename T> T *getAttr() const { 519 return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : nullptr; 520 } 521 template<typename T> bool hasAttr() const { 522 return hasAttrs() && hasSpecificAttr<T>(getAttrs()); 523 } 524 525 /// getMaxAlignment - return the maximum alignment specified by attributes 526 /// on this decl, 0 if there are none. 527 unsigned getMaxAlignment() const; 528 529 /// setInvalidDecl - Indicates the Decl had a semantic error. This 530 /// allows for graceful error recovery. 531 void setInvalidDecl(bool Invalid = true); 532 bool isInvalidDecl() const { return (bool) InvalidDecl; } 533 534 /// isImplicit - Indicates whether the declaration was implicitly 535 /// generated by the implementation. If false, this declaration 536 /// was written explicitly in the source code. 537 bool isImplicit() const { return Implicit; } 538 void setImplicit(bool I = true) { Implicit = I; } 539 540 /// \brief Whether *any* (re-)declaration of the entity was used, meaning that 541 /// a definition is required. 542 /// 543 /// \param CheckUsedAttr When true, also consider the "used" attribute 544 /// (in addition to the "used" bit set by \c setUsed()) when determining 545 /// whether the function is used. 546 bool isUsed(bool CheckUsedAttr = true) const; 547 548 /// \brief Set whether the declaration is used, in the sense of odr-use. 549 /// 550 /// This should only be used immediately after creating a declaration. 551 /// It intentionally doesn't notify any listeners. 552 void setIsUsed() { getCanonicalDecl()->Used = true; } 553 554 /// \brief Mark the declaration used, in the sense of odr-use. 555 /// 556 /// This notifies any mutation listeners in addition to setting a bit 557 /// indicating the declaration is used. 558 void markUsed(ASTContext &C); 559 560 /// \brief Whether any declaration of this entity was referenced. 561 bool isReferenced() const; 562 563 /// \brief Whether this declaration was referenced. This should not be relied 564 /// upon for anything other than debugging. 565 bool isThisDeclarationReferenced() const { return Referenced; } 566 567 void setReferenced(bool R = true) { Referenced = R; } 568 569 /// \brief Whether this declaration is a top-level declaration (function, 570 /// global variable, etc.) that is lexically inside an objc container 571 /// definition. 572 bool isTopLevelDeclInObjCContainer() const { 573 return TopLevelDeclInObjCContainer; 574 } 575 576 void setTopLevelDeclInObjCContainer(bool V = true) { 577 TopLevelDeclInObjCContainer = V; 578 } 579 580 /// \brief Looks on this and related declarations for an applicable 581 /// external source symbol attribute. 582 ExternalSourceSymbolAttr *getExternalSourceSymbolAttr() const; 583 584 /// \brief Whether this declaration was marked as being private to the 585 /// module in which it was defined. 586 bool isModulePrivate() const { 587 return getModuleOwnershipKind() == ModuleOwnershipKind::ModulePrivate; 588 } 589 590 /// \brief Whether this declaration is exported (by virtue of being lexically 591 /// within an ExportDecl or by being a NamespaceDecl). 592 bool isExported() const; 593 594 /// Return true if this declaration has an attribute which acts as 595 /// definition of the entity, such as 'alias' or 'ifunc'. 596 bool hasDefiningAttr() const; 597 598 /// Return this declaration's defining attribute if it has one. 599 const Attr *getDefiningAttr() const; 600 601protected: 602 /// \brief Specify that this declaration was marked as being private 603 /// to the module in which it was defined. 604 void setModulePrivate() { 605 // The module-private specifier has no effect on unowned declarations. 606 // FIXME: We should track this in some way for source fidelity. 607 if (getModuleOwnershipKind() == ModuleOwnershipKind::Unowned) 608 return; 609 setModuleOwnershipKind(ModuleOwnershipKind::ModulePrivate); 610 } 611 612 /// \brief Set the owning module ID. 613 void setOwningModuleID(unsigned ID) { 614 assert(isFromASTFile() && "Only works on a deserialized declaration"); 615 *((unsigned*)this - 2) = ID; 616 } 617 618public: 619 620 /// \brief Determine the availability of the given declaration. 621 /// 622 /// This routine will determine the most restrictive availability of 623 /// the given declaration (e.g., preferring 'unavailable' to 624 /// 'deprecated'). 625 /// 626 /// \param Message If non-NULL and the result is not \c 627 /// AR_Available, will be set to a (possibly empty) message 628 /// describing why the declaration has not been introduced, is 629 /// deprecated, or is unavailable. 630 /// 631 /// \param EnclosingVersion The version to compare with. If empty, assume the 632 /// deployment target version. 633 AvailabilityResult 634 getAvailability(std::string *Message = nullptr, 635 VersionTuple EnclosingVersion = VersionTuple()) const; 636 637 /// \brief Retrieve the version of the target platform in which this 638 /// declaration was introduced. 639 /// 640 /// \returns An empty version tuple if this declaration has no 'introduced' 641 /// availability attributes, or the version tuple that's specified in the 642 /// attribute otherwise. 643 VersionTuple getVersionIntroduced() const; 644 645 /// \brief Determine whether this declaration is marked 'deprecated'. 646 /// 647 /// \param Message If non-NULL and the declaration is deprecated, 648 /// this will be set to the message describing why the declaration 649 /// was deprecated (which may be empty). 650 bool isDeprecated(std::string *Message = nullptr) const { 651 return getAvailability(Message) == AR_Deprecated; 652 } 653 654 /// \brief Determine whether this declaration is marked 'unavailable'. 655 /// 656 /// \param Message If non-NULL and the declaration is unavailable, 657 /// this will be set to the message describing why the declaration 658 /// was made unavailable (which may be empty). 659 bool isUnavailable(std::string *Message = nullptr) const { 660 return getAvailability(Message) == AR_Unavailable; 661 } 662 663 /// \brief Determine whether this is a weak-imported symbol. 664 /// 665 /// Weak-imported symbols are typically marked with the 666 /// 'weak_import' attribute, but may also be marked with an 667 /// 'availability' attribute where we're targing a platform prior to 668 /// the introduction of this feature. 669 bool isWeakImported() const; 670 671 /// \brief Determines whether this symbol can be weak-imported, 672 /// e.g., whether it would be well-formed to add the weak_import 673 /// attribute. 674 /// 675 /// \param IsDefinition Set to \c true to indicate that this 676 /// declaration cannot be weak-imported because it has a definition. 677 bool canBeWeakImported(bool &IsDefinition) const; 678 679 /// \brief Determine whether this declaration came from an AST file (such as 680 /// a precompiled header or module) rather than having been parsed. 681 bool isFromASTFile() const { return FromASTFile; } 682 683 /// \brief Retrieve the global declaration ID associated with this 684 /// declaration, which specifies where this Decl was loaded from. 685 unsigned getGlobalID() const { 686 if (isFromASTFile()) 687 return *((const unsigned*)this - 1); 688 return 0; 689 } 690 691 /// \brief Retrieve the global ID of the module that owns this particular 692 /// declaration. 693 unsigned getOwningModuleID() const { 694 if (isFromASTFile()) 695 return *((const unsigned*)this - 2); 696 return 0; 697 } 698 699private: 700 Module *getOwningModuleSlow() const; 701protected: 702 bool hasLocalOwningModuleStorage() const; 703 704public: 705 /// \brief Get the imported owning module, if this decl is from an imported 706 /// (non-local) module. 707 Module *getImportedOwningModule() const { 708 if (!isFromASTFile() || !hasOwningModule()) 709 return nullptr; 710 711 return getOwningModuleSlow(); 712 } 713 714 /// \brief Get the local owning module, if known. Returns nullptr if owner is 715 /// not yet known or declaration is not from a module. 716 Module *getLocalOwningModule() const { 717 if (isFromASTFile() || !hasOwningModule()) 718 return nullptr; 719 720 assert(hasLocalOwningModuleStorage() && 721 "owned local decl but no local module storage"); 722 return reinterpret_cast<Module *const *>(this)[-1]; 723 } 724 void setLocalOwningModule(Module *M) { 725 assert(!isFromASTFile() && hasOwningModule() && 726 hasLocalOwningModuleStorage() && 727 "should not have a cached owning module"); 728 reinterpret_cast<Module **>(this)[-1] = M; 729 } 730 731 /// Is this declaration owned by some module? 732 bool hasOwningModule() const { 733 return getModuleOwnershipKind() != ModuleOwnershipKind::Unowned; 734 } 735 736 /// Get the module that owns this declaration (for visibility purposes). 737 Module *getOwningModule() const { 738 return isFromASTFile() ? getImportedOwningModule() : getLocalOwningModule(); 739 } 740 741 /// Get the module that owns this declaration for linkage purposes. 742 /// There only ever is such a module under the C++ Modules TS. 743 /// 744 /// \param IgnoreLinkage Ignore the linkage of the entity; assume that 745 /// all declarations in a global module fragment are unowned. 746 Module *getOwningModuleForLinkage(bool IgnoreLinkage = false) const; 747 748 /// \brief Determine whether this declaration might be hidden from name 749 /// lookup. Note that the declaration might be visible even if this returns 750 /// \c false, if the owning module is visible within the query context. 751 // FIXME: Rename this to make it clearer what it does. 752 bool isHidden() const { 753 return (int)getModuleOwnershipKind() > (int)ModuleOwnershipKind::Visible; 754 } 755 756 /// Set that this declaration is globally visible, even if it came from a 757 /// module that is not visible. 758 void setVisibleDespiteOwningModule() { 759 if (isHidden()) 760 setModuleOwnershipKind(ModuleOwnershipKind::Visible); 761 } 762 763 /// \brief Get the kind of module ownership for this declaration. 764 ModuleOwnershipKind getModuleOwnershipKind() const { 765 return NextInContextAndBits.getInt(); 766 } 767 768 /// \brief Set whether this declaration is hidden from name lookup. 769 void setModuleOwnershipKind(ModuleOwnershipKind MOK) { 770 assert(!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned && 771 MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && 772 !hasLocalOwningModuleStorage()) && 773 "no storage available for owning module for this declaration"); 774 NextInContextAndBits.setInt(MOK); 775 } 776 777 unsigned getIdentifierNamespace() const { 778 return IdentifierNamespace; 779 } 780 bool isInIdentifierNamespace(unsigned NS) const { 781 return getIdentifierNamespace() & NS; 782 } 783 static unsigned getIdentifierNamespaceForKind(Kind DK); 784 785 bool hasTagIdentifierNamespace() const { 786 return isTagIdentifierNamespace(getIdentifierNamespace()); 787 } 788 static bool isTagIdentifierNamespace(unsigned NS) { 789 // TagDecls have Tag and Type set and may also have TagFriend. 790 return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type); 791 } 792 793 /// getLexicalDeclContext - The declaration context where this Decl was 794 /// lexically declared (LexicalDC). May be different from 795 /// getDeclContext() (SemanticDC). 796 /// e.g.: 797 /// 798 /// namespace A { 799 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 800 /// } 801 /// void A::f(); // SemanticDC == namespace 'A' 802 /// // LexicalDC == global namespace 803 DeclContext *getLexicalDeclContext() { 804 if (isInSemaDC()) 805 return getSemanticDC(); 806 return getMultipleDC()->LexicalDC; 807 } 808 const DeclContext *getLexicalDeclContext() const { 809 return const_cast<Decl*>(this)->getLexicalDeclContext(); 810 } 811 812 /// Determine whether this declaration is declared out of line (outside its 813 /// semantic context). 814 virtual bool isOutOfLine() const; 815 816 /// setDeclContext - Set both the semantic and lexical DeclContext 817 /// to DC. 818 void setDeclContext(DeclContext *DC); 819 820 void setLexicalDeclContext(DeclContext *DC); 821 822 /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this 823 /// scoped decl is defined outside the current function or method. This is 824 /// roughly global variables and functions, but also handles enums (which 825 /// could be defined inside or outside a function etc). 826 bool isDefinedOutsideFunctionOrMethod() const { 827 return getParentFunctionOrMethod() == nullptr; 828 } 829 830 /// \brief Returns true if this declaration lexically is inside a function. 831 /// It recognizes non-defining declarations as well as members of local 832 /// classes: 833 /// \code 834 /// void foo() { void bar(); } 835 /// void foo2() { class ABC { void bar(); }; } 836 /// \endcode 837 bool isLexicallyWithinFunctionOrMethod() const; 838 839 /// \brief If this decl is defined inside a function/method/block it returns 840 /// the corresponding DeclContext, otherwise it returns null. 841 const DeclContext *getParentFunctionOrMethod() const; 842 DeclContext *getParentFunctionOrMethod() { 843 return const_cast<DeclContext*>( 844 const_cast<const Decl*>(this)->getParentFunctionOrMethod()); 845 } 846 847 /// \brief Retrieves the "canonical" declaration of the given declaration. 848 virtual Decl *getCanonicalDecl() { return this; } 849 const Decl *getCanonicalDecl() const { 850 return const_cast<Decl*>(this)->getCanonicalDecl(); 851 } 852 853 /// \brief Whether this particular Decl is a canonical one. 854 bool isCanonicalDecl() const { return getCanonicalDecl() == this; } 855 856protected: 857 /// \brief Returns the next redeclaration or itself if this is the only decl. 858 /// 859 /// Decl subclasses that can be redeclared should override this method so that 860 /// Decl::redecl_iterator can iterate over them. 861 virtual Decl *getNextRedeclarationImpl() { return this; } 862 863 /// \brief Implementation of getPreviousDecl(), to be overridden by any 864 /// subclass that has a redeclaration chain. 865 virtual Decl *getPreviousDeclImpl() { return nullptr; } 866 867 /// \brief Implementation of getMostRecentDecl(), to be overridden by any 868 /// subclass that has a redeclaration chain. 869 virtual Decl *getMostRecentDeclImpl() { return this; } 870 871public: 872 /// \brief Iterates through all the redeclarations of the same decl. 873 class redecl_iterator { 874 /// Current - The current declaration. 875 Decl *Current; 876 Decl *Starter; 877 878 public: 879 typedef Decl *value_type; 880 typedef const value_type &reference; 881 typedef const value_type *pointer; 882 typedef std::forward_iterator_tag iterator_category; 883 typedef std::ptrdiff_t difference_type; 884 885 redecl_iterator() : Current(nullptr) { } 886 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { } 887 888 reference operator*() const { return Current; } 889 value_type operator->() const { return Current; } 890 891 redecl_iterator& operator++() { 892 assert(Current && "Advancing while iterator has reached end"); 893 // Get either previous decl or latest decl. 894 Decl *Next = Current->getNextRedeclarationImpl(); 895 assert(Next && "Should return next redeclaration or itself, never null!"); 896 Current = (Next != Starter) ? Next : nullptr; 897 return *this; 898 } 899 900 redecl_iterator operator++(int) { 901 redecl_iterator tmp(*this); 902 ++(*this); 903 return tmp; 904 } 905 906 friend bool operator==(redecl_iterator x, redecl_iterator y) { 907 return x.Current == y.Current; 908 } 909 friend bool operator!=(redecl_iterator x, redecl_iterator y) { 910 return x.Current != y.Current; 911 } 912 }; 913 914 typedef llvm::iterator_range<redecl_iterator> redecl_range; 915 916 /// \brief Returns an iterator range for all the redeclarations of the same 917 /// decl. It will iterate at least once (when this decl is the only one). 918 redecl_range redecls() const { 919 return redecl_range(redecls_begin(), redecls_end()); 920 } 921 922 redecl_iterator redecls_begin() const { 923 return redecl_iterator(const_cast<Decl *>(this)); 924 } 925 redecl_iterator redecls_end() const { return redecl_iterator(); } 926 927 /// \brief Retrieve the previous declaration that declares the same entity 928 /// as this declaration, or NULL if there is no previous declaration. 929 Decl *getPreviousDecl() { return getPreviousDeclImpl(); } 930 931 /// \brief Retrieve the most recent declaration that declares the same entity 932 /// as this declaration, or NULL if there is no previous declaration. 933 const Decl *getPreviousDecl() const { 934 return const_cast<Decl *>(this)->getPreviousDeclImpl(); 935 } 936 937 /// \brief True if this is the first declaration in its redeclaration chain. 938 bool isFirstDecl() const { 939 return getPreviousDecl() == nullptr; 940 } 941 942 /// \brief Retrieve the most recent declaration that declares the same entity 943 /// as this declaration (which may be this declaration). 944 Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); } 945 946 /// \brief Retrieve the most recent declaration that declares the same entity 947 /// as this declaration (which may be this declaration). 948 const Decl *getMostRecentDecl() const { 949 return const_cast<Decl *>(this)->getMostRecentDeclImpl(); 950 } 951 952 /// getBody - If this Decl represents a declaration for a body of code, 953 /// such as a function or method definition, this method returns the 954 /// top-level Stmt* of that body. Otherwise this method returns null. 955 virtual Stmt* getBody() const { return nullptr; } 956 957 /// \brief Returns true if this \c Decl represents a declaration for a body of 958 /// code, such as a function or method definition. 959 /// Note that \c hasBody can also return true if any redeclaration of this 960 /// \c Decl represents a declaration for a body of code. 961 virtual bool hasBody() const { return getBody() != nullptr; } 962 963 /// getBodyRBrace - Gets the right brace of the body, if a body exists. 964 /// This works whether the body is a CompoundStmt or a CXXTryStmt. 965 SourceLocation getBodyRBrace() const; 966 967 // global temp stats (until we have a per-module visitor) 968 static void add(Kind k); 969 static void EnableStatistics(); 970 static void PrintStats(); 971 972 /// isTemplateParameter - Determines whether this declaration is a 973 /// template parameter. 974 bool isTemplateParameter() const; 975 976 /// isTemplateParameter - Determines whether this declaration is a 977 /// template parameter pack. 978 bool isTemplateParameterPack() const; 979 980 /// \brief Whether this declaration is a parameter pack. 981 bool isParameterPack() const; 982 983 /// \brief returns true if this declaration is a template 984 bool isTemplateDecl() const; 985 986 /// \brief Whether this declaration is a function or function template. 987 bool isFunctionOrFunctionTemplate() const { 988 return (DeclKind >= Decl::firstFunction && 989 DeclKind <= Decl::lastFunction) || 990 DeclKind == FunctionTemplate; 991 } 992 993 /// \brief If this is a declaration that describes some template, this 994 /// method returns that template declaration. 995 TemplateDecl *getDescribedTemplate() const; 996 997 /// \brief Returns the function itself, or the templated function if this is a 998 /// function template. 999 FunctionDecl *getAsFunction() LLVM_READONLY; 1000 1001 const FunctionDecl *getAsFunction() const { 1002 return const_cast<Decl *>(this)->getAsFunction(); 1003 } 1004 1005 /// \brief Changes the namespace of this declaration to reflect that it's 1006 /// a function-local extern declaration. 1007 /// 1008 /// These declarations appear in the lexical context of the extern 1009 /// declaration, but in the semantic context of the enclosing namespace 1010 /// scope. 1011 void setLocalExternDecl() { 1012 Decl *Prev = getPreviousDecl(); 1013 IdentifierNamespace &= ~IDNS_Ordinary; 1014 1015 // It's OK for the declaration to still have the "invisible friend" flag or 1016 // the "conflicts with tag declarations in this scope" flag for the outer 1017 // scope. 1018 assert((IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 && 1019 "namespace is not ordinary"); 1020 1021 IdentifierNamespace |= IDNS_LocalExtern; 1022 if (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary) 1023 IdentifierNamespace |= IDNS_Ordinary; 1024 } 1025 1026 /// \brief Determine whether this is a block-scope declaration with linkage. 1027 /// This will either be a local variable declaration declared 'extern', or a 1028 /// local function declaration. 1029 bool isLocalExternDecl() { 1030 return IdentifierNamespace & IDNS_LocalExtern; 1031 } 1032 1033 /// \brief Changes the namespace of this declaration to reflect that it's 1034 /// the object of a friend declaration. 1035 /// 1036 /// These declarations appear in the lexical context of the friending 1037 /// class, but in the semantic context of the actual entity. This property 1038 /// applies only to a specific decl object; other redeclarations of the 1039 /// same entity may not (and probably don't) share this property. 1040 void setObjectOfFriendDecl(bool PerformFriendInjection = false) { 1041 unsigned OldNS = IdentifierNamespace; 1042 assert((OldNS & (IDNS_Tag | IDNS_Ordinary | 1043 IDNS_TagFriend | IDNS_OrdinaryFriend | 1044 IDNS_LocalExtern)) && 1045 "namespace includes neither ordinary nor tag"); 1046 assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | 1047 IDNS_TagFriend | IDNS_OrdinaryFriend | 1048 IDNS_LocalExtern)) && 1049 "namespace includes other than ordinary or tag"); 1050 1051 Decl *Prev = getPreviousDecl(); 1052 IdentifierNamespace &= ~(IDNS_Ordinary | IDNS_Tag | IDNS_Type); 1053 1054 if (OldNS & (IDNS_Tag | IDNS_TagFriend)) { 1055 IdentifierNamespace |= IDNS_TagFriend; 1056 if (PerformFriendInjection || 1057 (Prev && Prev->getIdentifierNamespace() & IDNS_Tag)) 1058 IdentifierNamespace |= IDNS_Tag | IDNS_Type; 1059 } 1060 1061 if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend | IDNS_LocalExtern)) { 1062 IdentifierNamespace |= IDNS_OrdinaryFriend; 1063 if (PerformFriendInjection || 1064 (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary)) 1065 IdentifierNamespace |= IDNS_Ordinary; 1066 } 1067 } 1068 1069 enum FriendObjectKind { 1070 FOK_None, ///< Not a friend object. 1071 FOK_Declared, ///< A friend of a previously-declared entity. 1072 FOK_Undeclared ///< A friend of a previously-undeclared entity. 1073 }; 1074 1075 /// \brief Determines whether this declaration is the object of a 1076 /// friend declaration and, if so, what kind. 1077 /// 1078 /// There is currently no direct way to find the associated FriendDecl. 1079 FriendObjectKind getFriendObjectKind() const { 1080 unsigned mask = 1081 (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend)); 1082 if (!mask) return FOK_None; 1083 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? FOK_Declared 1084 : FOK_Undeclared); 1085 } 1086 1087 /// Specifies that this declaration is a C++ overloaded non-member. 1088 void setNonMemberOperator() { 1089 assert(getKind() == Function || getKind() == FunctionTemplate); 1090 assert((IdentifierNamespace & IDNS_Ordinary) && 1091 "visible non-member operators should be in ordinary namespace"); 1092 IdentifierNamespace |= IDNS_NonMemberOperator; 1093 } 1094 1095 static bool classofKind(Kind K) { return true; } 1096 static DeclContext *castToDeclContext(const Decl *); 1097 static Decl *castFromDeclContext(const DeclContext *); 1098 1099 void print(raw_ostream &Out, unsigned Indentation = 0, 1100 bool PrintInstantiation = false) const; 1101 void print(raw_ostream &Out, const PrintingPolicy &Policy, 1102 unsigned Indentation = 0, bool PrintInstantiation = false) const; 1103 static void printGroup(Decl** Begin, unsigned NumDecls, 1104 raw_ostream &Out, const PrintingPolicy &Policy, 1105 unsigned Indentation = 0); 1106 // Debuggers don't usually respect default arguments. 1107 void dump() const; 1108 // Same as dump(), but forces color printing. 1109 void dumpColor() const; 1110 void dump(raw_ostream &Out, bool Deserialize = false) const; 1111 1112 /// \brief Looks through the Decl's underlying type to extract a FunctionType 1113 /// when possible. Will return null if the type underlying the Decl does not 1114 /// have a FunctionType. 1115 const FunctionType *getFunctionType(bool BlocksToo = true) const; 1116 1117private: 1118 void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx); 1119 void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC, 1120 ASTContext &Ctx); 1121 1122protected: 1123 ASTMutationListener *getASTMutationListener() const; 1124}; 1125 1126/// \brief Determine whether two declarations declare the same entity. 1127inline bool declaresSameEntity(const Decl *D1, const Decl *D2) { 1128 if (!D1 || !D2) 1129 return false; 1130 1131 if (D1 == D2) 1132 return true; 1133 1134 return D1->getCanonicalDecl() == D2->getCanonicalDecl(); 1135} 1136 1137/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when 1138/// doing something to a specific decl. 1139class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { 1140 const Decl *TheDecl; 1141 SourceLocation Loc; 1142 SourceManager &SM; 1143 const char *Message; 1144public: 1145 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L, 1146 SourceManager &sm, const char *Msg) 1147 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} 1148 1149 void print(raw_ostream &OS) const override; 1150}; 1151 1152/// \brief The results of name lookup within a DeclContext. This is either a 1153/// single result (with no stable storage) or a collection of results (with 1154/// stable storage provided by the lookup table). 1155class DeclContextLookupResult { 1156 typedef ArrayRef<NamedDecl *> ResultTy; 1157 ResultTy Result; 1158 // If there is only one lookup result, it would be invalidated by 1159 // reallocations of the name table, so store it separately. 1160 NamedDecl *Single; 1161 1162 static NamedDecl *const SingleElementDummyList; 1163 1164public: 1165 DeclContextLookupResult() : Result(), Single() {} 1166 DeclContextLookupResult(ArrayRef<NamedDecl *> Result) 1167 : Result(Result), Single() {} 1168 DeclContextLookupResult(NamedDecl *Single) 1169 : Result(SingleElementDummyList), Single(Single) {} 1170 1171 class iterator; 1172 typedef llvm::iterator_adaptor_base<iterator, ResultTy::iterator, 1173 std::random_access_iterator_tag, 1174 NamedDecl *const> IteratorBase; 1175 class iterator : public IteratorBase { 1176 value_type SingleElement; 1177 1178 public: 1179 iterator() : IteratorBase(), SingleElement() {} 1180 explicit iterator(pointer Pos, value_type Single = nullptr) 1181 : IteratorBase(Pos), SingleElement(Single) {} 1182 1183 reference operator*() const { 1184 return SingleElement ? SingleElement : IteratorBase::operator*(); 1185 } 1186 }; 1187 typedef iterator const_iterator; 1188 typedef iterator::pointer pointer; 1189 typedef iterator::reference reference; 1190 1191 iterator begin() const { return iterator(Result.begin(), Single); } 1192 iterator end() const { return iterator(Result.end(), Single); } 1193 1194 bool empty() const { return Result.empty(); } 1195 pointer data() const { return Single ? &Single : Result.data(); } 1196 size_t size() const { return Single ? 1 : Result.size(); } 1197 reference front() const { return Single ? Single : Result.front(); } 1198 reference back() const { return Single ? Single : Result.back(); } 1199 reference operator[](size_t N) const { return Single ? Single : Result[N]; } 1200 1201 // FIXME: Remove this from the interface 1202 DeclContextLookupResult slice(size_t N) const { 1203 DeclContextLookupResult Sliced = Result.slice(N); 1204 Sliced.Single = Single; 1205 return Sliced; 1206 } 1207}; 1208 1209/// DeclContext - This is used only as base class of specific decl types that 1210/// can act as declaration contexts. These decls are (only the top classes 1211/// that directly derive from DeclContext are mentioned, not their subclasses): 1212/// 1213/// TranslationUnitDecl 1214/// NamespaceDecl 1215/// FunctionDecl 1216/// TagDecl 1217/// ObjCMethodDecl 1218/// ObjCContainerDecl 1219/// LinkageSpecDecl 1220/// ExportDecl 1221/// BlockDecl 1222/// OMPDeclareReductionDecl 1223/// 1224class DeclContext { 1225 /// DeclKind - This indicates which class this is. 1226 unsigned DeclKind : 8; 1227 1228 /// \brief Whether this declaration context also has some external 1229 /// storage that contains additional declarations that are lexically 1230 /// part of this context. 1231 mutable bool ExternalLexicalStorage : 1; 1232 1233 /// \brief Whether this declaration context also has some external 1234 /// storage that contains additional declarations that are visible 1235 /// in this context. 1236 mutable bool ExternalVisibleStorage : 1; 1237 1238 /// \brief Whether this declaration context has had external visible 1239 /// storage added since the last lookup. In this case, \c LookupPtr's 1240 /// invariant may not hold and needs to be fixed before we perform 1241 /// another lookup. 1242 mutable bool NeedToReconcileExternalVisibleStorage : 1; 1243 1244 /// \brief If \c true, this context may have local lexical declarations 1245 /// that are missing from the lookup table. 1246 mutable bool HasLazyLocalLexicalLookups : 1; 1247 1248 /// \brief If \c true, the external source may have lexical declarations 1249 /// that are missing from the lookup table. 1250 mutable bool HasLazyExternalLexicalLookups : 1; 1251 1252 /// \brief If \c true, lookups should only return identifier from 1253 /// DeclContext scope (for example TranslationUnit). Used in 1254 /// LookupQualifiedName() 1255 mutable bool UseQualifiedLookup : 1; 1256 1257 /// \brief Pointer to the data structure used to lookup declarations 1258 /// within this context (or a DependentStoredDeclsMap if this is a 1259 /// dependent context). We maintain the invariant that, if the map 1260 /// contains an entry for a DeclarationName (and we haven't lazily 1261 /// omitted anything), then it contains all relevant entries for that 1262 /// name (modulo the hasExternalDecls() flag). 1263 mutable StoredDeclsMap *LookupPtr; 1264 1265protected: 1266 /// FirstDecl - The first declaration stored within this declaration 1267 /// context. 1268 mutable Decl *FirstDecl; 1269 1270 /// LastDecl - The last declaration stored within this declaration 1271 /// context. FIXME: We could probably cache this value somewhere 1272 /// outside of the DeclContext, to reduce the size of DeclContext by 1273 /// another pointer. 1274 mutable Decl *LastDecl; 1275 1276 friend class ExternalASTSource; 1277 friend class ASTDeclReader; 1278 friend class ASTWriter; 1279 1280 /// \brief Build up a chain of declarations. 1281 /// 1282 /// \returns the first/last pair of declarations. 1283 static std::pair<Decl *, Decl *> 1284 BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded); 1285 1286 DeclContext(Decl::Kind K) 1287 : DeclKind(K), ExternalLexicalStorage(false), 1288 ExternalVisibleStorage(false), 1289 NeedToReconcileExternalVisibleStorage(false), 1290 HasLazyLocalLexicalLookups(false), HasLazyExternalLexicalLookups(false), 1291 UseQualifiedLookup(false), 1292 LookupPtr(nullptr), FirstDecl(nullptr), LastDecl(nullptr) {} 1293 1294public: 1295 ~DeclContext(); 1296 1297 Decl::Kind getDeclKind() const { 1298 return static_cast<Decl::Kind>(DeclKind); 1299 } 1300 const char *getDeclKindName() const; 1301 1302 /// getParent - Returns the containing DeclContext. 1303 DeclContext *getParent() { 1304 return cast<Decl>(this)->getDeclContext(); 1305 } 1306 const DeclContext *getParent() const { 1307 return const_cast<DeclContext*>(this)->getParent(); 1308 } 1309 1310 /// getLexicalParent - Returns the containing lexical DeclContext. May be 1311 /// different from getParent, e.g.: 1312 /// 1313 /// namespace A { 1314 /// struct S; 1315 /// } 1316 /// struct A::S {}; // getParent() == namespace 'A' 1317 /// // getLexicalParent() == translation unit 1318 /// 1319 DeclContext *getLexicalParent() { 1320 return cast<Decl>(this)->getLexicalDeclContext(); 1321 } 1322 const DeclContext *getLexicalParent() const { 1323 return const_cast<DeclContext*>(this)->getLexicalParent(); 1324 } 1325 1326 DeclContext *getLookupParent(); 1327 1328 const DeclContext *getLookupParent() const { 1329 return const_cast<DeclContext*>(this)->getLookupParent(); 1330 } 1331 1332 ASTContext &getParentASTContext() const { 1333 return cast<Decl>(this)->getASTContext(); 1334 } 1335 1336 bool isClosure() const { 1337 return DeclKind == Decl::Block; 1338 } 1339 1340 bool isObjCContainer() const { 1341 switch (DeclKind) { 1342 case Decl::ObjCCategory: 1343 case Decl::ObjCCategoryImpl: 1344 case Decl::ObjCImplementation: 1345 case Decl::ObjCInterface: 1346 case Decl::ObjCProtocol: 1347 return true; 1348 } 1349 return false; 1350 } 1351 1352 bool isFunctionOrMethod() const { 1353 switch (DeclKind) { 1354 case Decl::Block: 1355 case Decl::Captured: 1356 case Decl::ObjCMethod: 1357 return true; 1358 default: 1359 return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction; 1360 } 1361 } 1362 1363 /// \brief Test whether the context supports looking up names. 1364 bool isLookupContext() const { 1365 return !isFunctionOrMethod() && DeclKind != Decl::LinkageSpec && 1366 DeclKind != Decl::Export; 1367 } 1368 1369 bool isFileContext() const { 1370 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; 1371 } 1372 1373 bool isTranslationUnit() const { 1374 return DeclKind == Decl::TranslationUnit; 1375 } 1376 1377 bool isRecord() const { 1378 return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord; 1379 } 1380 1381 bool isNamespace() const { 1382 return DeclKind == Decl::Namespace; 1383 } 1384 1385 bool isStdNamespace() const; 1386 1387 bool isInlineNamespace() const; 1388 1389 /// \brief Determines whether this context is dependent on a 1390 /// template parameter. 1391 bool isDependentContext() const; 1392 1393 /// isTransparentContext - Determines whether this context is a 1394 /// "transparent" context, meaning that the members declared in this 1395 /// context are semantically declared in the nearest enclosing 1396 /// non-transparent (opaque) context but are lexically declared in 1397 /// this context. For example, consider the enumerators of an 1398 /// enumeration type: 1399 /// @code 1400 /// enum E { 1401 /// Val1 1402 /// }; 1403 /// @endcode 1404 /// Here, E is a transparent context, so its enumerator (Val1) will 1405 /// appear (semantically) that it is in the same context of E. 1406 /// Examples of transparent contexts include: enumerations (except for 1407 /// C++0x scoped enums), and C++ linkage specifications. 1408 bool isTransparentContext() const; 1409 1410 /// \brief Determines whether this context or some of its ancestors is a 1411 /// linkage specification context that specifies C linkage. 1412 bool isExternCContext() const; 1413 1414 /// \brief Retrieve the nearest enclosing C linkage specification context. 1415 const LinkageSpecDecl *getExternCContext() const; 1416 1417 /// \brief Determines whether this context or some of its ancestors is a 1418 /// linkage specification context that specifies C++ linkage. 1419 bool isExternCXXContext() const; 1420 1421 /// \brief Determine whether this declaration context is equivalent 1422 /// to the declaration context DC. 1423 bool Equals(const DeclContext *DC) const { 1424 return DC && this->getPrimaryContext() == DC->getPrimaryContext(); 1425 } 1426 1427 /// \brief Determine whether this declaration context encloses the 1428 /// declaration context DC. 1429 bool Encloses(const DeclContext *DC) const; 1430 1431 /// \brief Find the nearest non-closure ancestor of this context, 1432 /// i.e. the innermost semantic parent of this context which is not 1433 /// a closure. A context may be its own non-closure ancestor. 1434 Decl *getNonClosureAncestor(); 1435 const Decl *getNonClosureAncestor() const { 1436 return const_cast<DeclContext*>(this)->getNonClosureAncestor(); 1437 } 1438 1439 /// getPrimaryContext - There may be many different 1440 /// declarations of the same entity (including forward declarations 1441 /// of classes, multiple definitions of namespaces, etc.), each with 1442 /// a different set of declarations. This routine returns the 1443 /// "primary" DeclContext structure, which will contain the 1444 /// information needed to perform name lookup into this context. 1445 DeclContext *getPrimaryContext(); 1446 const DeclContext *getPrimaryContext() const { 1447 return const_cast<DeclContext*>(this)->getPrimaryContext(); 1448 } 1449 1450 /// getRedeclContext - Retrieve the context in which an entity conflicts with 1451 /// other entities of the same name, or where it is a redeclaration if the 1452 /// two entities are compatible. This skips through transparent contexts. 1453 DeclContext *getRedeclContext(); 1454 const DeclContext *getRedeclContext() const { 1455 return const_cast<DeclContext *>(this)->getRedeclContext(); 1456 } 1457 1458 /// \brief Retrieve the nearest enclosing namespace context. 1459 DeclContext *getEnclosingNamespaceContext(); 1460 const DeclContext *getEnclosingNamespaceContext() const { 1461 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); 1462 } 1463 1464 /// \brief Retrieve the outermost lexically enclosing record context. 1465 RecordDecl *getOuterLexicalRecordContext(); 1466 const RecordDecl *getOuterLexicalRecordContext() const { 1467 return const_cast<DeclContext *>(this)->getOuterLexicalRecordContext(); 1468 } 1469 1470 /// \brief Test if this context is part of the enclosing namespace set of 1471 /// the context NS, as defined in C++0x [namespace.def]p9. If either context 1472 /// isn't a namespace, this is equivalent to Equals(). 1473 /// 1474 /// The enclosing namespace set of a namespace is the namespace and, if it is 1475 /// inline, its enclosing namespace, recursively. 1476 bool InEnclosingNamespaceSetOf(const DeclContext *NS) const; 1477 1478 /// \brief Collects all of the declaration contexts that are semantically 1479 /// connected to this declaration context. 1480 /// 1481 /// For declaration contexts that have multiple semantically connected but 1482 /// syntactically distinct contexts, such as C++ namespaces, this routine 1483 /// retrieves the complete set of such declaration contexts in source order. 1484 /// For example, given: 1485 /// 1486 /// \code 1487 /// namespace N { 1488 /// int x; 1489 /// } 1490 /// namespace N { 1491 /// int y; 1492 /// } 1493 /// \endcode 1494 /// 1495 /// The \c Contexts parameter will contain both definitions of N. 1496 /// 1497 /// \param Contexts Will be cleared and set to the set of declaration 1498 /// contexts that are semanticaly connected to this declaration context, 1499 /// in source order, including this context (which may be the only result, 1500 /// for non-namespace contexts). 1501 void collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts); 1502 1503 /// decl_iterator - Iterates through the declarations stored 1504 /// within this context. 1505 class decl_iterator { 1506 /// Current - The current declaration. 1507 Decl *Current; 1508 1509 public: 1510 typedef Decl *value_type; 1511 typedef const value_type &reference; 1512 typedef const value_type *pointer; 1513 typedef std::forward_iterator_tag iterator_category; 1514 typedef std::ptrdiff_t difference_type; 1515 1516 decl_iterator() : Current(nullptr) { } 1517 explicit decl_iterator(Decl *C) : Current(C) { } 1518 1519 reference operator*() const { return Current; } 1520 // This doesn't meet the iterator requirements, but it's convenient 1521 value_type operator->() const { return Current; } 1522 1523 decl_iterator& operator++() { 1524 Current = Current->getNextDeclInContext(); 1525 return *this; 1526 } 1527 1528 decl_iterator operator++(int) { 1529 decl_iterator tmp(*this); 1530 ++(*this); 1531 return tmp; 1532 } 1533 1534 friend bool operator==(decl_iterator x, decl_iterator y) { 1535 return x.Current == y.Current; 1536 } 1537 friend bool operator!=(decl_iterator x, decl_iterator y) { 1538 return x.Current != y.Current; 1539 } 1540 }; 1541 1542 typedef llvm::iterator_range<decl_iterator> decl_range; 1543 1544 /// decls_begin/decls_end - Iterate over the declarations stored in 1545 /// this context. 1546 decl_range decls() const { return decl_range(decls_begin(), decls_end()); } 1547 decl_iterator decls_begin() const; 1548 decl_iterator decls_end() const { return decl_iterator(); } 1549 bool decls_empty() const; 1550 1551 /// noload_decls_begin/end - Iterate over the declarations stored in this 1552 /// context that are currently loaded; don't attempt to retrieve anything 1553 /// from an external source. 1554 decl_range noload_decls() const { 1555 return decl_range(noload_decls_begin(), noload_decls_end()); 1556 } 1557 decl_iterator noload_decls_begin() const { return decl_iterator(FirstDecl); } 1558 decl_iterator noload_decls_end() const { return decl_iterator(); } 1559 1560 /// specific_decl_iterator - Iterates over a subrange of 1561 /// declarations stored in a DeclContext, providing only those that 1562 /// are of type SpecificDecl (or a class derived from it). This 1563 /// iterator is used, for example, to provide iteration over just 1564 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). 1565 template<typename SpecificDecl> 1566 class specific_decl_iterator { 1567 /// Current - The current, underlying declaration iterator, which 1568 /// will either be NULL or will point to a declaration of 1569 /// type SpecificDecl. 1570 DeclContext::decl_iterator Current; 1571 1572 /// SkipToNextDecl - Advances the current position up to the next 1573 /// declaration of type SpecificDecl that also meets the criteria 1574 /// required by Acceptable. 1575 void SkipToNextDecl() { 1576 while (*Current && !isa<SpecificDecl>(*Current)) 1577 ++Current; 1578 } 1579 1580 public: 1581 typedef SpecificDecl *value_type; 1582 // TODO: Add reference and pointer typedefs (with some appropriate proxy 1583 // type) if we ever have a need for them. 1584 typedef void reference; 1585 typedef void pointer; 1586 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 1587 difference_type; 1588 typedef std::forward_iterator_tag iterator_category; 1589 1590 specific_decl_iterator() : Current() { } 1591 1592 /// specific_decl_iterator - Construct a new iterator over a 1593 /// subset of the declarations the range [C, 1594 /// end-of-declarations). If A is non-NULL, it is a pointer to a 1595 /// member function of SpecificDecl that should return true for 1596 /// all of the SpecificDecl instances that will be in the subset 1597 /// of iterators. For example, if you want Objective-C instance 1598 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 1599 /// &ObjCMethodDecl::isInstanceMethod. 1600 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 1601 SkipToNextDecl(); 1602 } 1603 1604 value_type operator*() const { return cast<SpecificDecl>(*Current); } 1605 // This doesn't meet the iterator requirements, but it's convenient 1606 value_type operator->() const { return **this; } 1607 1608 specific_decl_iterator& operator++() { 1609 ++Current; 1610 SkipToNextDecl(); 1611 return *this; 1612 } 1613 1614 specific_decl_iterator operator++(int) { 1615 specific_decl_iterator tmp(*this); 1616 ++(*this); 1617 return tmp; 1618 } 1619 1620 friend bool operator==(const specific_decl_iterator& x, 1621 const specific_decl_iterator& y) { 1622 return x.Current == y.Current; 1623 } 1624 1625 friend bool operator!=(const specific_decl_iterator& x, 1626 const specific_decl_iterator& y) { 1627 return x.Current != y.Current; 1628 } 1629 }; 1630 1631 /// \brief Iterates over a filtered subrange of declarations stored 1632 /// in a DeclContext. 1633 /// 1634 /// This iterator visits only those declarations that are of type 1635 /// SpecificDecl (or a class derived from it) and that meet some 1636 /// additional run-time criteria. This iterator is used, for 1637 /// example, to provide access to the instance methods within an 1638 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and 1639 /// Acceptable = ObjCMethodDecl::isInstanceMethod). 1640 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> 1641 class filtered_decl_iterator { 1642 /// Current - The current, underlying declaration iterator, which 1643 /// will either be NULL or will point to a declaration of 1644 /// type SpecificDecl. 1645 DeclContext::decl_iterator Current; 1646 1647 /// SkipToNextDecl - Advances the current position up to the next 1648 /// declaration of type SpecificDecl that also meets the criteria 1649 /// required by Acceptable. 1650 void SkipToNextDecl() { 1651 while (*Current && 1652 (!isa<SpecificDecl>(*Current) || 1653 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) 1654 ++Current; 1655 } 1656 1657 public: 1658 typedef SpecificDecl *value_type; 1659 // TODO: Add reference and pointer typedefs (with some appropriate proxy 1660 // type) if we ever have a need for them. 1661 typedef void reference; 1662 typedef void pointer; 1663 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 1664 difference_type; 1665 typedef std::forward_iterator_tag iterator_category; 1666 1667 filtered_decl_iterator() : Current() { } 1668 1669 /// filtered_decl_iterator - Construct a new iterator over a 1670 /// subset of the declarations the range [C, 1671 /// end-of-declarations). If A is non-NULL, it is a pointer to a 1672 /// member function of SpecificDecl that should return true for 1673 /// all of the SpecificDecl instances that will be in the subset 1674 /// of iterators. For example, if you want Objective-C instance 1675 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 1676 /// &ObjCMethodDecl::isInstanceMethod. 1677 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 1678 SkipToNextDecl(); 1679 } 1680 1681 value_type operator*() const { return cast<SpecificDecl>(*Current); } 1682 value_type operator->() const { return cast<SpecificDecl>(*Current); } 1683 1684 filtered_decl_iterator& operator++() { 1685 ++Current; 1686 SkipToNextDecl(); 1687 return *this; 1688 } 1689 1690 filtered_decl_iterator operator++(int) { 1691 filtered_decl_iterator tmp(*this); 1692 ++(*this); 1693 return tmp; 1694 } 1695 1696 friend bool operator==(const filtered_decl_iterator& x, 1697 const filtered_decl_iterator& y) { 1698 return x.Current == y.Current; 1699 } 1700 1701 friend bool operator!=(const filtered_decl_iterator& x, 1702 const filtered_decl_iterator& y) { 1703 return x.Current != y.Current; 1704 } 1705 }; 1706 1707 /// @brief Add the declaration D into this context. 1708 /// 1709 /// This routine should be invoked when the declaration D has first 1710 /// been declared, to place D into the context where it was 1711 /// (lexically) defined. Every declaration must be added to one 1712 /// (and only one!) context, where it can be visited via 1713 /// [decls_begin(), decls_end()). Once a declaration has been added 1714 /// to its lexical context, the corresponding DeclContext owns the 1715 /// declaration. 1716 /// 1717 /// If D is also a NamedDecl, it will be made visible within its 1718 /// semantic context via makeDeclVisibleInContext. 1719 void addDecl(Decl *D); 1720 1721 /// @brief Add the declaration D into this context, but suppress 1722 /// searches for external declarations with the same name. 1723 /// 1724 /// Although analogous in function to addDecl, this removes an 1725 /// important check. This is only useful if the Decl is being 1726 /// added in response to an external search; in all other cases, 1727 /// addDecl() is the right function to use. 1728 /// See the ASTImporter for use cases. 1729 void addDeclInternal(Decl *D); 1730 1731 /// @brief Add the declaration D to this context without modifying 1732 /// any lookup tables. 1733 /// 1734 /// This is useful for some operations in dependent contexts where 1735 /// the semantic context might not be dependent; this basically 1736 /// only happens with friends. 1737 void addHiddenDecl(Decl *D); 1738 1739 /// @brief Removes a declaration from this context. 1740 void removeDecl(Decl *D); 1741 1742 /// @brief Checks whether a declaration is in this context. 1743 bool containsDecl(Decl *D) const; 1744 1745 typedef DeclContextLookupResult lookup_result; 1746 typedef lookup_result::iterator lookup_iterator; 1747 1748 /// lookup - Find the declarations (if any) with the given Name in 1749 /// this context. Returns a range of iterators that contains all of 1750 /// the declarations with this name, with object, function, member, 1751 /// and enumerator names preceding any tag name. Note that this 1752 /// routine will not look into parent contexts. 1753 lookup_result lookup(DeclarationName Name) const; 1754 1755 /// \brief Find the declarations with the given name that are visible 1756 /// within this context; don't attempt to retrieve anything from an 1757 /// external source. 1758 lookup_result noload_lookup(DeclarationName Name); 1759 1760 /// \brief A simplistic name lookup mechanism that performs name lookup 1761 /// into this declaration context without consulting the external source. 1762 /// 1763 /// This function should almost never be used, because it subverts the 1764 /// usual relationship between a DeclContext and the external source. 1765 /// See the ASTImporter for the (few, but important) use cases. 1766 /// 1767 /// FIXME: This is very inefficient; replace uses of it with uses of 1768 /// noload_lookup. 1769 void localUncachedLookup(DeclarationName Name, 1770 SmallVectorImpl<NamedDecl *> &Results); 1771 1772 /// @brief Makes a declaration visible within this context. 1773 /// 1774 /// This routine makes the declaration D visible to name lookup 1775 /// within this context and, if this is a transparent context, 1776 /// within its parent contexts up to the first enclosing 1777 /// non-transparent context. Making a declaration visible within a 1778 /// context does not transfer ownership of a declaration, and a 1779 /// declaration can be visible in many contexts that aren't its 1780 /// lexical context. 1781 /// 1782 /// If D is a redeclaration of an existing declaration that is 1783 /// visible from this context, as determined by 1784 /// NamedDecl::declarationReplaces, the previous declaration will be 1785 /// replaced with D. 1786 void makeDeclVisibleInContext(NamedDecl *D); 1787 1788 /// all_lookups_iterator - An iterator that provides a view over the results 1789 /// of looking up every possible name. 1790 class all_lookups_iterator; 1791 1792 typedef llvm::iterator_range<all_lookups_iterator> lookups_range; 1793 1794 lookups_range lookups() const; 1795 lookups_range noload_lookups() const; 1796 1797 /// \brief Iterators over all possible lookups within this context. 1798 all_lookups_iterator lookups_begin() const; 1799 all_lookups_iterator lookups_end() const; 1800 1801 /// \brief Iterators over all possible lookups within this context that are 1802 /// currently loaded; don't attempt to retrieve anything from an external 1803 /// source. 1804 all_lookups_iterator noload_lookups_begin() const; 1805 all_lookups_iterator noload_lookups_end() const; 1806 1807 struct udir_iterator; 1808 typedef llvm::iterator_adaptor_base<udir_iterator, lookup_iterator, 1809 std::random_access_iterator_tag, 1810 UsingDirectiveDecl *> udir_iterator_base; 1811 struct udir_iterator : udir_iterator_base { 1812 udir_iterator(lookup_iterator I) : udir_iterator_base(I) {} 1813 UsingDirectiveDecl *operator*() const; 1814 }; 1815 1816 typedef llvm::iterator_range<udir_iterator> udir_range; 1817 1818 udir_range using_directives() const; 1819 1820 // These are all defined in DependentDiagnostic.h. 1821 class ddiag_iterator; 1822 typedef llvm::iterator_range<DeclContext::ddiag_iterator> ddiag_range; 1823 1824 inline ddiag_range ddiags() const; 1825 1826 // Low-level accessors 1827 1828 /// \brief Mark that there are external lexical declarations that we need 1829 /// to include in our lookup table (and that are not available as external 1830 /// visible lookups). These extra lookup results will be found by walking 1831 /// the lexical declarations of this context. This should be used only if 1832 /// setHasExternalLexicalStorage() has been called on any decl context for 1833 /// which this is the primary context. 1834 void setMustBuildLookupTable() { 1835 assert(this == getPrimaryContext() && 1836 "should only be called on primary context"); 1837 HasLazyExternalLexicalLookups = true; 1838 } 1839 1840 /// \brief Retrieve the internal representation of the lookup structure. 1841 /// This may omit some names if we are lazily building the structure. 1842 StoredDeclsMap *getLookupPtr() const { return LookupPtr; } 1843 1844 /// \brief Ensure the lookup structure is fully-built and return it. 1845 StoredDeclsMap *buildLookup(); 1846 1847 /// \brief Whether this DeclContext has external storage containing 1848 /// additional declarations that are lexically in this context. 1849 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; } 1850 1851 /// \brief State whether this DeclContext has external storage for 1852 /// declarations lexically in this context. 1853 void setHasExternalLexicalStorage(bool ES = true) { 1854 ExternalLexicalStorage = ES; 1855 } 1856 1857 /// \brief Whether this DeclContext has external storage containing 1858 /// additional declarations that are visible in this context. 1859 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; } 1860 1861 /// \brief State whether this DeclContext has external storage for 1862 /// declarations visible in this context. 1863 void setHasExternalVisibleStorage(bool ES = true) { 1864 ExternalVisibleStorage = ES; 1865 if (ES && LookupPtr) 1866 NeedToReconcileExternalVisibleStorage = true; 1867 } 1868 1869 /// \brief Determine whether the given declaration is stored in the list of 1870 /// declarations lexically within this context. 1871 bool isDeclInLexicalTraversal(const Decl *D) const { 1872 return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl || 1873 D == LastDecl); 1874 } 1875 1876 bool setUseQualifiedLookup(bool use = true) { 1877 bool old_value = UseQualifiedLookup; 1878 UseQualifiedLookup = use; 1879 return old_value; 1880 } 1881 1882 bool shouldUseQualifiedLookup() const { 1883 return UseQualifiedLookup; 1884 } 1885 1886 static bool classof(const Decl *D); 1887 static bool classof(const DeclContext *D) { return true; } 1888 1889 void dumpDeclContext() const; 1890 void dumpLookups() const; 1891 void dumpLookups(llvm::raw_ostream &OS, bool DumpDecls = false, 1892 bool Deserialize = false) const; 1893 1894private: 1895 void reconcileExternalVisibleStorage() const; 1896 bool LoadLexicalDeclsFromExternalStorage() const; 1897 1898 /// @brief Makes a declaration visible within this context, but 1899 /// suppresses searches for external declarations with the same 1900 /// name. 1901 /// 1902 /// Analogous to makeDeclVisibleInContext, but for the exclusive 1903 /// use of addDeclInternal(). 1904 void makeDeclVisibleInContextInternal(NamedDecl *D); 1905 1906 friend class DependentDiagnostic; 1907 StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const; 1908 1909 void buildLookupImpl(DeclContext *DCtx, bool Internal); 1910 void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, 1911 bool Rediscoverable); 1912 void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal); 1913}; 1914 1915inline bool Decl::isTemplateParameter() const { 1916 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm || 1917 getKind() == TemplateTemplateParm; 1918} 1919 1920// Specialization selected when ToTy is not a known subclass of DeclContext. 1921template <class ToTy, 1922 bool IsKnownSubtype = ::std::is_base_of<DeclContext, ToTy>::value> 1923struct cast_convert_decl_context { 1924 static const ToTy *doit(const DeclContext *Val) { 1925 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val)); 1926 } 1927 1928 static ToTy *doit(DeclContext *Val) { 1929 return static_cast<ToTy*>(Decl::castFromDeclContext(Val)); 1930 } 1931}; 1932 1933// Specialization selected when ToTy is a known subclass of DeclContext. 1934template <class ToTy> 1935struct cast_convert_decl_context<ToTy, true> { 1936 static const ToTy *doit(const DeclContext *Val) { 1937 return static_cast<const ToTy*>(Val); 1938 } 1939 1940 static ToTy *doit(DeclContext *Val) { 1941 return static_cast<ToTy*>(Val); 1942 } 1943}; 1944 1945 1946} // end clang. 1947 1948namespace llvm { 1949 1950/// isa<T>(DeclContext*) 1951template <typename To> 1952struct isa_impl<To, ::clang::DeclContext> { 1953 static bool doit(const ::clang::DeclContext &Val) { 1954 return To::classofKind(Val.getDeclKind()); 1955 } 1956}; 1957 1958/// cast<T>(DeclContext*) 1959template<class ToTy> 1960struct cast_convert_val<ToTy, 1961 const ::clang::DeclContext,const ::clang::DeclContext> { 1962 static const ToTy &doit(const ::clang::DeclContext &Val) { 1963 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); 1964 } 1965}; 1966template<class ToTy> 1967struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> { 1968 static ToTy &doit(::clang::DeclContext &Val) { 1969 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); 1970 } 1971}; 1972template<class ToTy> 1973struct cast_convert_val<ToTy, 1974 const ::clang::DeclContext*, const ::clang::DeclContext*> { 1975 static const ToTy *doit(const ::clang::DeclContext *Val) { 1976 return ::clang::cast_convert_decl_context<ToTy>::doit(Val); 1977 } 1978}; 1979template<class ToTy> 1980struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> { 1981 static ToTy *doit(::clang::DeclContext *Val) { 1982 return ::clang::cast_convert_decl_context<ToTy>::doit(Val); 1983 } 1984}; 1985 1986/// Implement cast_convert_val for Decl -> DeclContext conversions. 1987template<class FromTy> 1988struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { 1989 static ::clang::DeclContext &doit(const FromTy &Val) { 1990 return *FromTy::castToDeclContext(&Val); 1991 } 1992}; 1993 1994template<class FromTy> 1995struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { 1996 static ::clang::DeclContext *doit(const FromTy *Val) { 1997 return FromTy::castToDeclContext(Val); 1998 } 1999}; 2000 2001template<class FromTy> 2002struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { 2003 static const ::clang::DeclContext &doit(const FromTy &Val) { 2004 return *FromTy::castToDeclContext(&Val); 2005 } 2006}; 2007 2008template<class FromTy> 2009struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { 2010 static const ::clang::DeclContext *doit(const FromTy *Val) { 2011 return FromTy::castToDeclContext(Val); 2012 } 2013}; 2014 2015} // end namespace llvm 2016 2017#endif 2018