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