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