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