DeclBase.h revision d04efc98d08731e31bd73f430803acc5ffa9040a
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; 46} 47 48namespace llvm { 49// DeclContext* is only 4-byte aligned on 32-bit systems. 50template<> 51 class PointerLikeTypeTraits<clang::DeclContext*> { 52 typedef clang::DeclContext* PT; 53public: 54 static inline void *getAsVoidPointer(PT P) { return P; } 55 static inline PT getFromVoidPointer(void *P) { 56 return static_cast<PT>(P); 57 } 58 enum { NumLowBitsAvailable = 2 }; 59}; 60} 61 62namespace clang { 63 64/// Decl - This represents one declaration (or definition), e.g. a variable, 65/// typedef, function, struct, etc. 66/// 67class Decl { 68public: 69 /// \brief Lists the kind of concrete classes of Decl. 70 enum Kind { 71#define DECL(Derived, Base) Derived, 72#define DECL_RANGE(CommonBase, Start, End) \ 73 CommonBase##First = Start, CommonBase##Last = End, 74#define LAST_DECL_RANGE(CommonBase, Start, End) \ 75 CommonBase##First = Start, CommonBase##Last = End 76#include "clang/AST/DeclNodes.def" 77 }; 78 79 /// IdentifierNamespace - The different namespaces in which 80 /// declarations may appear. According to C99 6.2.3, there are 81 /// four namespaces, labels, tags, members and ordinary 82 /// identifiers. C++ describes lookup completely differently: 83 /// certain lookups merely "ignore" certain kinds of declarations, 84 /// usually based on whether the declaration is of a type, etc. 85 /// 86 /// These are meant as bitmasks, so that searches in 87 /// C++ can look into the "tag" namespace during ordinary lookup. 88 /// 89 /// Decl currently provides 16 bits of IDNS bits. 90 enum IdentifierNamespace { 91 /// Labels, declared with 'x:' and referenced with 'goto x'. 92 IDNS_Label = 0x0001, 93 94 /// Tags, declared with 'struct foo;' and referenced with 95 /// 'struct foo'. All tags are also types. This is what 96 /// elaborated-type-specifiers look for in C. 97 IDNS_Tag = 0x0002, 98 99 /// Types, declared with 'struct foo', typedefs, etc. 100 /// This is what elaborated-type-specifiers look for in C++, 101 /// but note that it's ill-formed to find a non-tag. 102 IDNS_Type = 0x0004, 103 104 /// Members, declared with object declarations within tag 105 /// definitions. In C, these can only be found by "qualified" 106 /// lookup in member expressions. In C++, they're found by 107 /// normal lookup. 108 IDNS_Member = 0x0008, 109 110 /// Namespaces, declared with 'namespace foo {}'. 111 /// Lookup for nested-name-specifiers find these. 112 IDNS_Namespace = 0x0010, 113 114 /// Ordinary names. In C, everything that's not a label, tag, 115 /// or member ends up here. 116 IDNS_Ordinary = 0x0020, 117 118 /// Objective C @protocol. 119 IDNS_ObjCProtocol = 0x0040, 120 121 /// This declaration is a friend function. A friend function 122 /// declaration is always in this namespace but may also be in 123 /// IDNS_Ordinary if it was previously declared. 124 IDNS_OrdinaryFriend = 0x0080, 125 126 /// This declaration is a friend class. A friend class 127 /// declaration is always in this namespace but may also be in 128 /// IDNS_Tag|IDNS_Type if it was previously declared. 129 IDNS_TagFriend = 0x0100, 130 131 /// This declaration is a using declaration. A using declaration 132 /// *introduces* a number of other declarations into the current 133 /// scope, and those declarations use the IDNS of their targets, 134 /// but the actual using declarations go in this namespace. 135 IDNS_Using = 0x0200 136 }; 137 138 /// ObjCDeclQualifier - Qualifier used on types in method declarations 139 /// for remote messaging. They are meant for the arguments though and 140 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl). 141 enum ObjCDeclQualifier { 142 OBJC_TQ_None = 0x0, 143 OBJC_TQ_In = 0x1, 144 OBJC_TQ_Inout = 0x2, 145 OBJC_TQ_Out = 0x4, 146 OBJC_TQ_Bycopy = 0x8, 147 OBJC_TQ_Byref = 0x10, 148 OBJC_TQ_Oneway = 0x20 149 }; 150 151private: 152 /// NextDeclInContext - The next declaration within the same lexical 153 /// DeclContext. These pointers form the linked list that is 154 /// traversed via DeclContext's decls_begin()/decls_end(). 155 Decl *NextDeclInContext; 156 157 friend class DeclContext; 158 159 struct MultipleDC { 160 DeclContext *SemanticDC; 161 DeclContext *LexicalDC; 162 }; 163 164 165 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*. 166 /// For declarations that don't contain C++ scope specifiers, it contains 167 /// the DeclContext where the Decl was declared. 168 /// For declarations with C++ scope specifiers, it contains a MultipleDC* 169 /// with the context where it semantically belongs (SemanticDC) and the 170 /// context where it was lexically declared (LexicalDC). 171 /// e.g.: 172 /// 173 /// namespace A { 174 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 175 /// } 176 /// void A::f(); // SemanticDC == namespace 'A' 177 /// // LexicalDC == global namespace 178 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx; 179 180 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); } 181 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); } 182 inline MultipleDC *getMultipleDC() const { 183 return DeclCtx.get<MultipleDC*>(); 184 } 185 inline DeclContext *getSemanticDC() const { 186 return DeclCtx.get<DeclContext*>(); 187 } 188 189 /// Loc - The location that this decl. 190 SourceLocation Loc; 191 192 /// DeclKind - This indicates which class this is. 193 Kind DeclKind : 8; 194 195 /// InvalidDecl - This indicates a semantic error occurred. 196 unsigned int InvalidDecl : 1; 197 198 /// HasAttrs - This indicates whether the decl has attributes or not. 199 unsigned int HasAttrs : 1; 200 201 /// Implicit - Whether this declaration was implicitly generated by 202 /// the implementation rather than explicitly written by the user. 203 bool Implicit : 1; 204 205 /// \brief Whether this declaration was "used", meaning that a definition is 206 /// required. 207 bool Used : 1; 208 209protected: 210 /// Access - Used by C++ decls for the access specifier. 211 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum 212 unsigned Access : 2; 213 friend class CXXClassMemberWrapper; 214 215 // PCHLevel - the "level" of precompiled header/AST file from which this 216 // declaration was built. 217 unsigned PCHLevel : 2; 218 219 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in. 220 unsigned IdentifierNamespace : 16; 221 222private: 223#ifndef NDEBUG 224 void CheckAccessDeclContext() const; 225#else 226 void CheckAccessDeclContext() const { } 227#endif 228 229protected: 230 231 Decl(Kind DK, DeclContext *DC, SourceLocation L) 232 : NextDeclInContext(0), DeclCtx(DC), 233 Loc(L), DeclKind(DK), InvalidDecl(0), 234 HasAttrs(false), Implicit(false), Used(false), 235 Access(AS_none), PCHLevel(0), 236 IdentifierNamespace(getIdentifierNamespaceForKind(DK)) { 237 if (Decl::CollectingStats()) addDeclKind(DK); 238 } 239 240 virtual ~Decl(); 241 242public: 243 244 /// \brief Source range that this declaration covers. 245 virtual SourceRange getSourceRange() const { 246 return SourceRange(getLocation(), getLocation()); 247 } 248 SourceLocation getLocStart() const { return getSourceRange().getBegin(); } 249 SourceLocation getLocEnd() const { return getSourceRange().getEnd(); } 250 251 SourceLocation getLocation() const { return Loc; } 252 void setLocation(SourceLocation L) { Loc = L; } 253 254 Kind getKind() const { return DeclKind; } 255 const char *getDeclKindName() const; 256 257 Decl *getNextDeclInContext() { return NextDeclInContext; } 258 const Decl *getNextDeclInContext() const { return NextDeclInContext; } 259 260 DeclContext *getDeclContext() { 261 if (isInSemaDC()) 262 return getSemanticDC(); 263 return getMultipleDC()->SemanticDC; 264 } 265 const DeclContext *getDeclContext() const { 266 return const_cast<Decl*>(this)->getDeclContext(); 267 } 268 269 TranslationUnitDecl *getTranslationUnitDecl(); 270 const TranslationUnitDecl *getTranslationUnitDecl() const { 271 return const_cast<Decl*>(this)->getTranslationUnitDecl(); 272 } 273 274 bool isInAnonymousNamespace() const; 275 276 ASTContext &getASTContext() const; 277 278 void setAccess(AccessSpecifier AS) { 279 Access = AS; 280 CheckAccessDeclContext(); 281 } 282 283 AccessSpecifier getAccess() const { 284 CheckAccessDeclContext(); 285 return AccessSpecifier(Access); 286 } 287 288 bool hasAttrs() const { return HasAttrs; } 289 void addAttr(Attr *attr); 290 const Attr *getAttrs() const { 291 if (!HasAttrs) return 0; // common case, no attributes. 292 return getAttrsImpl(); // Uncommon case, out of line hash lookup. 293 } 294 void swapAttrs(Decl *D); 295 void invalidateAttrs(); 296 297 template<typename T> const T *getAttr() const { 298 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext()) 299 if (const T *V = dyn_cast<T>(attr)) 300 return V; 301 return 0; 302 } 303 304 template<typename T> bool hasAttr() const { 305 return getAttr<T>() != 0; 306 } 307 308 /// setInvalidDecl - Indicates the Decl had a semantic error. This 309 /// allows for graceful error recovery. 310 void setInvalidDecl(bool Invalid = true); 311 bool isInvalidDecl() const { return (bool) InvalidDecl; } 312 313 /// isImplicit - Indicates whether the declaration was implicitly 314 /// generated by the implementation. If false, this declaration 315 /// was written explicitly in the source code. 316 bool isImplicit() const { return Implicit; } 317 void setImplicit(bool I = true) { Implicit = I; } 318 319 /// \brief Whether this declaration was used, meaning that a definition 320 /// is required. 321 bool isUsed() const; 322 323 void setUsed(bool U = true) { Used = U; } 324 325 /// \brief Retrieve the level of precompiled header from which this 326 /// declaration was generated. 327 /// 328 /// The PCH level of a declaration describes where the declaration originated 329 /// from. A PCH level of 0 indicates that the declaration was not from a 330 /// precompiled header. A PCH level of 1 indicates that the declaration was 331 /// from a top-level precompiled header; 2 indicates that the declaration 332 /// comes from a precompiled header on which the top-level precompiled header 333 /// depends, and so on. 334 unsigned getPCHLevel() const { return PCHLevel; } 335 336 /// \brief The maximum PCH level that any declaration may have. 337 static const unsigned MaxPCHLevel = 3; 338 339 /// \brief Set the PCH level of this declaration. 340 void setPCHLevel(unsigned Level) { 341 assert(Level < MaxPCHLevel && "PCH level exceeds the maximum"); 342 PCHLevel = Level; 343 } 344 345 unsigned getIdentifierNamespace() const { 346 return IdentifierNamespace; 347 } 348 bool isInIdentifierNamespace(unsigned NS) const { 349 return getIdentifierNamespace() & NS; 350 } 351 static unsigned getIdentifierNamespaceForKind(Kind DK); 352 353 354 /// getLexicalDeclContext - The declaration context where this Decl was 355 /// lexically declared (LexicalDC). May be different from 356 /// getDeclContext() (SemanticDC). 357 /// e.g.: 358 /// 359 /// namespace A { 360 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 361 /// } 362 /// void A::f(); // SemanticDC == namespace 'A' 363 /// // LexicalDC == global namespace 364 DeclContext *getLexicalDeclContext() { 365 if (isInSemaDC()) 366 return getSemanticDC(); 367 return getMultipleDC()->LexicalDC; 368 } 369 const DeclContext *getLexicalDeclContext() const { 370 return const_cast<Decl*>(this)->getLexicalDeclContext(); 371 } 372 373 virtual bool isOutOfLine() const { 374 return getLexicalDeclContext() != getDeclContext(); 375 } 376 377 /// setDeclContext - Set both the semantic and lexical DeclContext 378 /// to DC. 379 void setDeclContext(DeclContext *DC); 380 381 void setLexicalDeclContext(DeclContext *DC); 382 383 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this 384 // scoped decl is defined outside the current function or method. This is 385 // roughly global variables and functions, but also handles enums (which could 386 // be defined inside or outside a function etc). 387 bool isDefinedOutsideFunctionOrMethod() const; 388 389 /// \brief Retrieves the "canonical" declaration of the given declaration. 390 virtual Decl *getCanonicalDecl() { return this; } 391 const Decl *getCanonicalDecl() const { 392 return const_cast<Decl*>(this)->getCanonicalDecl(); 393 } 394 395 /// \brief Whether this particular Decl is a canonical one. 396 bool isCanonicalDecl() const { return getCanonicalDecl() == this; } 397 398protected: 399 /// \brief Returns the next redeclaration or itself if this is the only decl. 400 /// 401 /// Decl subclasses that can be redeclared should override this method so that 402 /// Decl::redecl_iterator can iterate over them. 403 virtual Decl *getNextRedeclaration() { return this; } 404 405public: 406 /// \brief Iterates through all the redeclarations of the same decl. 407 class redecl_iterator { 408 /// Current - The current declaration. 409 Decl *Current; 410 Decl *Starter; 411 412 public: 413 typedef Decl* value_type; 414 typedef Decl* reference; 415 typedef Decl* pointer; 416 typedef std::forward_iterator_tag iterator_category; 417 typedef std::ptrdiff_t difference_type; 418 419 redecl_iterator() : Current(0) { } 420 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { } 421 422 reference operator*() const { return Current; } 423 pointer operator->() const { return Current; } 424 425 redecl_iterator& operator++() { 426 assert(Current && "Advancing while iterator has reached end"); 427 // Get either previous decl or latest decl. 428 Decl *Next = Current->getNextRedeclaration(); 429 assert(Next && "Should return next redeclaration or itself, never null!"); 430 Current = (Next != Starter ? Next : 0); 431 return *this; 432 } 433 434 redecl_iterator operator++(int) { 435 redecl_iterator tmp(*this); 436 ++(*this); 437 return tmp; 438 } 439 440 friend bool operator==(redecl_iterator x, redecl_iterator y) { 441 return x.Current == y.Current; 442 } 443 friend bool operator!=(redecl_iterator x, redecl_iterator y) { 444 return x.Current != y.Current; 445 } 446 }; 447 448 /// \brief Returns iterator for all the redeclarations of the same decl. 449 /// It will iterate at least once (when this decl is the only one). 450 redecl_iterator redecls_begin() const { 451 return redecl_iterator(const_cast<Decl*>(this)); 452 } 453 redecl_iterator redecls_end() const { return redecl_iterator(); } 454 455 /// getBody - If this Decl represents a declaration for a body of code, 456 /// such as a function or method definition, this method returns the 457 /// top-level Stmt* of that body. Otherwise this method returns null. 458 virtual Stmt* getBody() const { return 0; } 459 460 /// getCompoundBody - Returns getBody(), dyn_casted to a CompoundStmt. 461 CompoundStmt* getCompoundBody() const; 462 463 /// getBodyRBrace - Gets the right brace of the body, if a body exists. 464 /// This works whether the body is a CompoundStmt or a CXXTryStmt. 465 SourceLocation getBodyRBrace() const; 466 467 // global temp stats (until we have a per-module visitor) 468 static void addDeclKind(Kind k); 469 static bool CollectingStats(bool Enable = false); 470 static void PrintStats(); 471 472 /// isTemplateParameter - Determines whether this declaration is a 473 /// template parameter. 474 bool isTemplateParameter() const; 475 476 /// isTemplateParameter - Determines whether this declaration is a 477 /// template parameter pack. 478 bool isTemplateParameterPack() const; 479 480 /// \brief Whether this declaration is a function or function template. 481 bool isFunctionOrFunctionTemplate() const; 482 483 /// \brief Changes the namespace of this declaration to reflect that it's 484 /// the object of a friend declaration. 485 /// 486 /// These declarations appear in the lexical context of the friending 487 /// class, but in the semantic context of the actual entity. This property 488 /// applies only to a specific decl object; other redeclarations of the 489 /// same entity may not (and probably don't) share this property. 490 void setObjectOfFriendDecl(bool PreviouslyDeclared) { 491 unsigned OldNS = IdentifierNamespace; 492 assert((OldNS & (IDNS_Tag | IDNS_Ordinary | 493 IDNS_TagFriend | IDNS_OrdinaryFriend)) && 494 "namespace includes neither ordinary nor tag"); 495 assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | 496 IDNS_TagFriend | IDNS_OrdinaryFriend)) && 497 "namespace includes other than ordinary or tag"); 498 499 IdentifierNamespace = 0; 500 if (OldNS & (IDNS_Tag | IDNS_TagFriend)) { 501 IdentifierNamespace |= IDNS_TagFriend; 502 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Tag | IDNS_Type; 503 } 504 505 if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend)) { 506 IdentifierNamespace |= IDNS_OrdinaryFriend; 507 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Ordinary; 508 } 509 } 510 511 enum FriendObjectKind { 512 FOK_None, // not a friend object 513 FOK_Declared, // a friend of a previously-declared entity 514 FOK_Undeclared // a friend of a previously-undeclared entity 515 }; 516 517 /// \brief Determines whether this declaration is the object of a 518 /// friend declaration and, if so, what kind. 519 /// 520 /// There is currently no direct way to find the associated FriendDecl. 521 FriendObjectKind getFriendObjectKind() const { 522 unsigned mask 523 = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend)); 524 if (!mask) return FOK_None; 525 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? 526 FOK_Declared : FOK_Undeclared); 527 } 528 529 // Implement isa/cast/dyncast/etc. 530 static bool classof(const Decl *) { return true; } 531 static bool classofKind(Kind K) { return true; } 532 static DeclContext *castToDeclContext(const Decl *); 533 static Decl *castFromDeclContext(const DeclContext *); 534 535 /// Destroy - Call destructors and release memory. 536 virtual void Destroy(ASTContext& C); 537 538 void print(llvm::raw_ostream &Out, unsigned Indentation = 0) const; 539 void print(llvm::raw_ostream &Out, const PrintingPolicy &Policy, 540 unsigned Indentation = 0) const; 541 static void printGroup(Decl** Begin, unsigned NumDecls, 542 llvm::raw_ostream &Out, const PrintingPolicy &Policy, 543 unsigned Indentation = 0); 544 void dump() const; 545 546private: 547 const Attr *getAttrsImpl() const; 548 549}; 550 551/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when 552/// doing something to a specific decl. 553class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { 554 const Decl *TheDecl; 555 SourceLocation Loc; 556 SourceManager &SM; 557 const char *Message; 558public: 559 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L, 560 SourceManager &sm, const char *Msg) 561 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} 562 563 virtual void print(llvm::raw_ostream &OS) const; 564}; 565 566 567/// DeclContext - This is used only as base class of specific decl types that 568/// can act as declaration contexts. These decls are (only the top classes 569/// that directly derive from DeclContext are mentioned, not their subclasses): 570/// 571/// TranslationUnitDecl 572/// NamespaceDecl 573/// FunctionDecl 574/// TagDecl 575/// ObjCMethodDecl 576/// ObjCContainerDecl 577/// LinkageSpecDecl 578/// BlockDecl 579/// 580class DeclContext { 581 /// DeclKind - This indicates which class this is. 582 Decl::Kind DeclKind : 8; 583 584 /// \brief Whether this declaration context also has some external 585 /// storage that contains additional declarations that are lexically 586 /// part of this context. 587 mutable bool ExternalLexicalStorage : 1; 588 589 /// \brief Whether this declaration context also has some external 590 /// storage that contains additional declarations that are visible 591 /// in this context. 592 mutable bool ExternalVisibleStorage : 1; 593 594 /// \brief Pointer to the data structure used to lookup declarations 595 /// within this context (or a DependentStoredDeclsMap if this is a 596 /// dependent context). 597 mutable StoredDeclsMap *LookupPtr; 598 599 /// FirstDecl - The first declaration stored within this declaration 600 /// context. 601 mutable Decl *FirstDecl; 602 603 /// LastDecl - The last declaration stored within this declaration 604 /// context. FIXME: We could probably cache this value somewhere 605 /// outside of the DeclContext, to reduce the size of DeclContext by 606 /// another pointer. 607 mutable Decl *LastDecl; 608 609protected: 610 DeclContext(Decl::Kind K) 611 : DeclKind(K), ExternalLexicalStorage(false), 612 ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0), 613 LastDecl(0) { } 614 615 void DestroyDecls(ASTContext &C); 616 617public: 618 ~DeclContext(); 619 620 Decl::Kind getDeclKind() const { 621 return DeclKind; 622 } 623 const char *getDeclKindName() const; 624 625 /// getParent - Returns the containing DeclContext. 626 DeclContext *getParent() { 627 return cast<Decl>(this)->getDeclContext(); 628 } 629 const DeclContext *getParent() const { 630 return const_cast<DeclContext*>(this)->getParent(); 631 } 632 633 /// getLexicalParent - Returns the containing lexical DeclContext. May be 634 /// different from getParent, e.g.: 635 /// 636 /// namespace A { 637 /// struct S; 638 /// } 639 /// struct A::S {}; // getParent() == namespace 'A' 640 /// // getLexicalParent() == translation unit 641 /// 642 DeclContext *getLexicalParent() { 643 return cast<Decl>(this)->getLexicalDeclContext(); 644 } 645 const DeclContext *getLexicalParent() const { 646 return const_cast<DeclContext*>(this)->getLexicalParent(); 647 } 648 649 DeclContext *getLookupParent(); 650 651 const DeclContext *getLookupParent() const { 652 return const_cast<DeclContext*>(this)->getLookupParent(); 653 } 654 655 ASTContext &getParentASTContext() const { 656 return cast<Decl>(this)->getASTContext(); 657 } 658 659 bool isFunctionOrMethod() const { 660 switch (DeclKind) { 661 case Decl::Block: 662 case Decl::ObjCMethod: 663 return true; 664 default: 665 return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast; 666 } 667 } 668 669 bool isFileContext() const { 670 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; 671 } 672 673 bool isTranslationUnit() const { 674 return DeclKind == Decl::TranslationUnit; 675 } 676 677 bool isRecord() const { 678 return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast; 679 } 680 681 bool isNamespace() const { 682 return DeclKind == Decl::Namespace; 683 } 684 685 /// \brief Determines whether this context is dependent on a 686 /// template parameter. 687 bool isDependentContext() const; 688 689 /// isTransparentContext - Determines whether this context is a 690 /// "transparent" context, meaning that the members declared in this 691 /// context are semantically declared in the nearest enclosing 692 /// non-transparent (opaque) context but are lexically declared in 693 /// this context. For example, consider the enumerators of an 694 /// enumeration type: 695 /// @code 696 /// enum E { 697 /// Val1 698 /// }; 699 /// @endcode 700 /// Here, E is a transparent context, so its enumerator (Val1) will 701 /// appear (semantically) that it is in the same context of E. 702 /// Examples of transparent contexts include: enumerations (except for 703 /// C++0x scoped enums), C++ linkage specifications, and C++0x 704 /// inline namespaces. 705 bool isTransparentContext() const; 706 707 /// \brief Determine whether this declaration context is equivalent 708 /// to the declaration context DC. 709 bool Equals(DeclContext *DC) { 710 return DC && this->getPrimaryContext() == DC->getPrimaryContext(); 711 } 712 713 /// \brief Determine whether this declaration context encloses the 714 /// declaration context DC. 715 bool Encloses(DeclContext *DC); 716 717 /// getPrimaryContext - There may be many different 718 /// declarations of the same entity (including forward declarations 719 /// of classes, multiple definitions of namespaces, etc.), each with 720 /// a different set of declarations. This routine returns the 721 /// "primary" DeclContext structure, which will contain the 722 /// information needed to perform name lookup into this context. 723 DeclContext *getPrimaryContext(); 724 const DeclContext *getPrimaryContext() const { 725 return const_cast<DeclContext*>(this)->getPrimaryContext(); 726 } 727 728 /// getLookupContext - Retrieve the innermost non-transparent 729 /// context of this context, which corresponds to the innermost 730 /// location from which name lookup can find the entities in this 731 /// context. 732 DeclContext *getLookupContext(); 733 const DeclContext *getLookupContext() const { 734 return const_cast<DeclContext *>(this)->getLookupContext(); 735 } 736 737 /// \brief Retrieve the nearest enclosing namespace context. 738 DeclContext *getEnclosingNamespaceContext(); 739 const DeclContext *getEnclosingNamespaceContext() const { 740 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); 741 } 742 743 /// getNextContext - If this is a DeclContext that may have other 744 /// DeclContexts that are semantically connected but syntactically 745 /// different, such as C++ namespaces, this routine retrieves the 746 /// next DeclContext in the link. Iteration through the chain of 747 /// DeclContexts should begin at the primary DeclContext and 748 /// continue until this function returns NULL. For example, given: 749 /// @code 750 /// namespace N { 751 /// int x; 752 /// } 753 /// namespace N { 754 /// int y; 755 /// } 756 /// @endcode 757 /// The first occurrence of namespace N will be the primary 758 /// DeclContext. Its getNextContext will return the second 759 /// occurrence of namespace N. 760 DeclContext *getNextContext(); 761 762 /// decl_iterator - Iterates through the declarations stored 763 /// within this context. 764 class decl_iterator { 765 /// Current - The current declaration. 766 Decl *Current; 767 768 public: 769 typedef Decl* value_type; 770 typedef Decl* reference; 771 typedef Decl* pointer; 772 typedef std::forward_iterator_tag iterator_category; 773 typedef std::ptrdiff_t difference_type; 774 775 decl_iterator() : Current(0) { } 776 explicit decl_iterator(Decl *C) : Current(C) { } 777 778 reference operator*() const { return Current; } 779 pointer operator->() const { return Current; } 780 781 decl_iterator& operator++() { 782 Current = Current->getNextDeclInContext(); 783 return *this; 784 } 785 786 decl_iterator operator++(int) { 787 decl_iterator tmp(*this); 788 ++(*this); 789 return tmp; 790 } 791 792 friend bool operator==(decl_iterator x, decl_iterator y) { 793 return x.Current == y.Current; 794 } 795 friend bool operator!=(decl_iterator x, decl_iterator y) { 796 return x.Current != y.Current; 797 } 798 }; 799 800 /// decls_begin/decls_end - Iterate over the declarations stored in 801 /// this context. 802 decl_iterator decls_begin() const; 803 decl_iterator decls_end() const; 804 bool decls_empty() const; 805 806 /// specific_decl_iterator - Iterates over a subrange of 807 /// declarations stored in a DeclContext, providing only those that 808 /// are of type SpecificDecl (or a class derived from it). This 809 /// iterator is used, for example, to provide iteration over just 810 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). 811 template<typename SpecificDecl> 812 class specific_decl_iterator { 813 /// Current - The current, underlying declaration iterator, which 814 /// will either be NULL or will point to a declaration of 815 /// type SpecificDecl. 816 DeclContext::decl_iterator Current; 817 818 /// SkipToNextDecl - Advances the current position up to the next 819 /// declaration of type SpecificDecl that also meets the criteria 820 /// required by Acceptable. 821 void SkipToNextDecl() { 822 while (*Current && !isa<SpecificDecl>(*Current)) 823 ++Current; 824 } 825 826 public: 827 typedef SpecificDecl* value_type; 828 typedef SpecificDecl* reference; 829 typedef SpecificDecl* pointer; 830 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 831 difference_type; 832 typedef std::forward_iterator_tag iterator_category; 833 834 specific_decl_iterator() : Current() { } 835 836 /// specific_decl_iterator - Construct a new iterator over a 837 /// subset of the declarations the range [C, 838 /// end-of-declarations). If A is non-NULL, it is a pointer to a 839 /// member function of SpecificDecl that should return true for 840 /// all of the SpecificDecl instances that will be in the subset 841 /// of iterators. For example, if you want Objective-C instance 842 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 843 /// &ObjCMethodDecl::isInstanceMethod. 844 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 845 SkipToNextDecl(); 846 } 847 848 reference operator*() const { return cast<SpecificDecl>(*Current); } 849 pointer operator->() const { return cast<SpecificDecl>(*Current); } 850 851 specific_decl_iterator& operator++() { 852 ++Current; 853 SkipToNextDecl(); 854 return *this; 855 } 856 857 specific_decl_iterator operator++(int) { 858 specific_decl_iterator tmp(*this); 859 ++(*this); 860 return tmp; 861 } 862 863 friend bool 864 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) { 865 return x.Current == y.Current; 866 } 867 868 friend bool 869 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) { 870 return x.Current != y.Current; 871 } 872 }; 873 874 /// \brief Iterates over a filtered subrange of declarations stored 875 /// in a DeclContext. 876 /// 877 /// This iterator visits only those declarations that are of type 878 /// SpecificDecl (or a class derived from it) and that meet some 879 /// additional run-time criteria. This iterator is used, for 880 /// example, to provide access to the instance methods within an 881 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and 882 /// Acceptable = ObjCMethodDecl::isInstanceMethod). 883 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> 884 class filtered_decl_iterator { 885 /// Current - The current, underlying declaration iterator, which 886 /// will either be NULL or will point to a declaration of 887 /// type SpecificDecl. 888 DeclContext::decl_iterator Current; 889 890 /// SkipToNextDecl - Advances the current position up to the next 891 /// declaration of type SpecificDecl that also meets the criteria 892 /// required by Acceptable. 893 void SkipToNextDecl() { 894 while (*Current && 895 (!isa<SpecificDecl>(*Current) || 896 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) 897 ++Current; 898 } 899 900 public: 901 typedef SpecificDecl* value_type; 902 typedef SpecificDecl* reference; 903 typedef SpecificDecl* pointer; 904 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 905 difference_type; 906 typedef std::forward_iterator_tag iterator_category; 907 908 filtered_decl_iterator() : Current() { } 909 910 /// specific_decl_iterator - Construct a new iterator over a 911 /// subset of the declarations the range [C, 912 /// end-of-declarations). If A is non-NULL, it is a pointer to a 913 /// member function of SpecificDecl that should return true for 914 /// all of the SpecificDecl instances that will be in the subset 915 /// of iterators. For example, if you want Objective-C instance 916 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 917 /// &ObjCMethodDecl::isInstanceMethod. 918 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 919 SkipToNextDecl(); 920 } 921 922 reference operator*() const { return cast<SpecificDecl>(*Current); } 923 pointer operator->() const { return cast<SpecificDecl>(*Current); } 924 925 filtered_decl_iterator& operator++() { 926 ++Current; 927 SkipToNextDecl(); 928 return *this; 929 } 930 931 filtered_decl_iterator operator++(int) { 932 filtered_decl_iterator tmp(*this); 933 ++(*this); 934 return tmp; 935 } 936 937 friend bool 938 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 939 return x.Current == y.Current; 940 } 941 942 friend bool 943 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 944 return x.Current != y.Current; 945 } 946 }; 947 948 /// @brief Add the declaration D into this context. 949 /// 950 /// This routine should be invoked when the declaration D has first 951 /// been declared, to place D into the context where it was 952 /// (lexically) defined. Every declaration must be added to one 953 /// (and only one!) context, where it can be visited via 954 /// [decls_begin(), decls_end()). Once a declaration has been added 955 /// to its lexical context, the corresponding DeclContext owns the 956 /// declaration. 957 /// 958 /// If D is also a NamedDecl, it will be made visible within its 959 /// semantic context via makeDeclVisibleInContext. 960 void addDecl(Decl *D); 961 962 /// @brief Add the declaration D to this context without modifying 963 /// any lookup tables. 964 /// 965 /// This is useful for some operations in dependent contexts where 966 /// the semantic context might not be dependent; this basically 967 /// only happens with friends. 968 void addHiddenDecl(Decl *D); 969 970 /// @brief Removes a declaration from this context. 971 void removeDecl(Decl *D); 972 973 /// lookup_iterator - An iterator that provides access to the results 974 /// of looking up a name within this context. 975 typedef NamedDecl **lookup_iterator; 976 977 /// lookup_const_iterator - An iterator that provides non-mutable 978 /// access to the results of lookup up a name within this context. 979 typedef NamedDecl * const * lookup_const_iterator; 980 981 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result; 982 typedef std::pair<lookup_const_iterator, lookup_const_iterator> 983 lookup_const_result; 984 985 /// lookup - Find the declarations (if any) with the given Name in 986 /// this context. Returns a range of iterators that contains all of 987 /// the declarations with this name, with object, function, member, 988 /// and enumerator names preceding any tag name. Note that this 989 /// routine will not look into parent contexts. 990 lookup_result lookup(DeclarationName Name); 991 lookup_const_result lookup(DeclarationName Name) const; 992 993 /// @brief Makes a declaration visible within this context. 994 /// 995 /// This routine makes the declaration D visible to name lookup 996 /// within this context and, if this is a transparent context, 997 /// within its parent contexts up to the first enclosing 998 /// non-transparent context. Making a declaration visible within a 999 /// context does not transfer ownership of a declaration, and a 1000 /// declaration can be visible in many contexts that aren't its 1001 /// lexical context. 1002 /// 1003 /// If D is a redeclaration of an existing declaration that is 1004 /// visible from this context, as determined by 1005 /// NamedDecl::declarationReplaces, the previous declaration will be 1006 /// replaced with D. 1007 /// 1008 /// @param Recoverable true if it's okay to not add this decl to 1009 /// the lookup tables because it can be easily recovered by walking 1010 /// the declaration chains. 1011 void makeDeclVisibleInContext(NamedDecl *D, bool Recoverable = true); 1012 1013 /// udir_iterator - Iterates through the using-directives stored 1014 /// within this context. 1015 typedef UsingDirectiveDecl * const * udir_iterator; 1016 1017 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range; 1018 1019 udir_iterator_range getUsingDirectives() const; 1020 1021 udir_iterator using_directives_begin() const { 1022 return getUsingDirectives().first; 1023 } 1024 1025 udir_iterator using_directives_end() const { 1026 return getUsingDirectives().second; 1027 } 1028 1029 // These are all defined in DependentDiagnostic.h. 1030 class ddiag_iterator; 1031 inline ddiag_iterator ddiag_begin() const; 1032 inline ddiag_iterator ddiag_end() const; 1033 1034 // Low-level accessors 1035 1036 /// \brief Retrieve the internal representation of the lookup structure. 1037 StoredDeclsMap* getLookupPtr() const { return LookupPtr; } 1038 1039 /// \brief Whether this DeclContext has external storage containing 1040 /// additional declarations that are lexically in this context. 1041 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; } 1042 1043 /// \brief State whether this DeclContext has external storage for 1044 /// declarations lexically in this context. 1045 void setHasExternalLexicalStorage(bool ES = true) { 1046 ExternalLexicalStorage = ES; 1047 } 1048 1049 /// \brief Whether this DeclContext has external storage containing 1050 /// additional declarations that are visible in this context. 1051 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; } 1052 1053 /// \brief State whether this DeclContext has external storage for 1054 /// declarations visible in this context. 1055 void setHasExternalVisibleStorage(bool ES = true) { 1056 ExternalVisibleStorage = ES; 1057 } 1058 1059 static bool classof(const Decl *D); 1060 static bool classof(const DeclContext *D) { return true; } 1061#define DECL_CONTEXT(Name) \ 1062 static bool classof(const Name##Decl *D) { return true; } 1063#include "clang/AST/DeclNodes.def" 1064 1065 void dumpDeclContext() const; 1066 1067private: 1068 void LoadLexicalDeclsFromExternalStorage() const; 1069 void LoadVisibleDeclsFromExternalStorage() const; 1070 1071 friend class DependentDiagnostic; 1072 StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const; 1073 1074 void buildLookup(DeclContext *DCtx); 1075 void makeDeclVisibleInContextImpl(NamedDecl *D); 1076}; 1077 1078inline bool Decl::isTemplateParameter() const { 1079 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm || 1080 getKind() == TemplateTemplateParm; 1081} 1082 1083 1084// Specialization selected when ToTy is not a known subclass of DeclContext. 1085template <class ToTy, 1086 bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value> 1087struct cast_convert_decl_context { 1088 static const ToTy *doit(const DeclContext *Val) { 1089 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val)); 1090 } 1091 1092 static ToTy *doit(DeclContext *Val) { 1093 return static_cast<ToTy*>(Decl::castFromDeclContext(Val)); 1094 } 1095}; 1096 1097// Specialization selected when ToTy is a known subclass of DeclContext. 1098template <class ToTy> 1099struct cast_convert_decl_context<ToTy, true> { 1100 static const ToTy *doit(const DeclContext *Val) { 1101 return static_cast<const ToTy*>(Val); 1102 } 1103 1104 static ToTy *doit(DeclContext *Val) { 1105 return static_cast<ToTy*>(Val); 1106 } 1107}; 1108 1109 1110} // end clang. 1111 1112namespace llvm { 1113 1114/// isa<T>(DeclContext*) 1115template<class ToTy> 1116struct isa_impl_wrap<ToTy, 1117 const ::clang::DeclContext,const ::clang::DeclContext> { 1118 static bool doit(const ::clang::DeclContext &Val) { 1119 return ToTy::classofKind(Val.getDeclKind()); 1120 } 1121}; 1122template<class ToTy> 1123struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> 1124 : public isa_impl_wrap<ToTy, 1125 const ::clang::DeclContext,const ::clang::DeclContext> {}; 1126 1127/// cast<T>(DeclContext*) 1128template<class ToTy> 1129struct cast_convert_val<ToTy, 1130 const ::clang::DeclContext,const ::clang::DeclContext> { 1131 static const ToTy &doit(const ::clang::DeclContext &Val) { 1132 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); 1133 } 1134}; 1135template<class ToTy> 1136struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> { 1137 static ToTy &doit(::clang::DeclContext &Val) { 1138 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); 1139 } 1140}; 1141template<class ToTy> 1142struct cast_convert_val<ToTy, 1143 const ::clang::DeclContext*, const ::clang::DeclContext*> { 1144 static const ToTy *doit(const ::clang::DeclContext *Val) { 1145 return ::clang::cast_convert_decl_context<ToTy>::doit(Val); 1146 } 1147}; 1148template<class ToTy> 1149struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> { 1150 static ToTy *doit(::clang::DeclContext *Val) { 1151 return ::clang::cast_convert_decl_context<ToTy>::doit(Val); 1152 } 1153}; 1154 1155/// Implement cast_convert_val for Decl -> DeclContext conversions. 1156template<class FromTy> 1157struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { 1158 static ::clang::DeclContext &doit(const FromTy &Val) { 1159 return *FromTy::castToDeclContext(&Val); 1160 } 1161}; 1162 1163template<class FromTy> 1164struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { 1165 static ::clang::DeclContext *doit(const FromTy *Val) { 1166 return FromTy::castToDeclContext(Val); 1167 } 1168}; 1169 1170template<class FromTy> 1171struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { 1172 static const ::clang::DeclContext &doit(const FromTy &Val) { 1173 return *FromTy::castToDeclContext(&Val); 1174 } 1175}; 1176 1177template<class FromTy> 1178struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { 1179 static const ::clang::DeclContext *doit(const FromTy *Val) { 1180 return FromTy::castToDeclContext(Val); 1181 } 1182}; 1183 1184} // end namespace llvm 1185 1186#endif 1187