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