DeclBase.h revision 42f42c0dd5cf71fbfc6fa282d03079a902f6e342
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 313 void print(llvm::raw_ostream &Out, ASTContext &Context, 314 unsigned Indentation = 0); 315 void print(llvm::raw_ostream &Out, ASTContext &Context, 316 const PrintingPolicy &Policy, unsigned Indentation = 0); 317 static void printGroup(Decl** Begin, unsigned NumDecls, 318 llvm::raw_ostream &Out, ASTContext &Context, 319 const PrintingPolicy &Policy, 320 unsigned Indentation = 0); 321 void dump(ASTContext &Context); 322 323private: 324 const Attr *getAttrsImpl() const; 325 326}; 327 328/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when 329/// doing something to a specific decl. 330class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { 331 Decl *TheDecl; 332 SourceLocation Loc; 333 SourceManager &SM; 334 const char *Message; 335public: 336 PrettyStackTraceDecl(Decl *theDecl, SourceLocation L, 337 SourceManager &sm, const char *Msg) 338 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} 339 340 virtual void print(llvm::raw_ostream &OS) const; 341}; 342 343 344/// DeclContext - This is used only as base class of specific decl types that 345/// can act as declaration contexts. These decls are (only the top classes 346/// that directly derive from DeclContext are mentioned, not their subclasses): 347/// 348/// TranslationUnitDecl 349/// NamespaceDecl 350/// FunctionDecl 351/// TagDecl 352/// ObjCMethodDecl 353/// ObjCContainerDecl 354/// ObjCCategoryImplDecl 355/// ObjCImplementationDecl 356/// LinkageSpecDecl 357/// BlockDecl 358/// 359class DeclContext { 360 /// DeclKind - This indicates which class this is. 361 Decl::Kind DeclKind : 8; 362 363 /// \brief Whether this declaration context also has some external 364 /// storage that contains additional declarations that are lexically 365 /// part of this context. 366 mutable bool ExternalLexicalStorage : 1; 367 368 /// \brief Whether this declaration context also has some external 369 /// storage that contains additional declarations that are visible 370 /// in this context. 371 mutable bool ExternalVisibleStorage : 1; 372 373 /// \brief Pointer to the data structure used to lookup declarations 374 /// within this context, which is a DenseMap<DeclarationName, 375 /// StoredDeclsList>. 376 mutable void* LookupPtr; 377 378 /// FirstDecl - The first declaration stored within this declaration 379 /// context. 380 mutable Decl *FirstDecl; 381 382 /// LastDecl - The last declaration stored within this declaration 383 /// context. FIXME: We could probably cache this value somewhere 384 /// outside of the DeclContext, to reduce the size of DeclContext by 385 /// another pointer. 386 mutable Decl *LastDecl; 387 388protected: 389 DeclContext(Decl::Kind K) 390 : DeclKind(K), ExternalLexicalStorage(false), 391 ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0), 392 LastDecl(0) { } 393 394 void DestroyDecls(ASTContext &C); 395 396public: 397 ~DeclContext(); 398 399 Decl::Kind getDeclKind() const { 400 return DeclKind; 401 } 402 const char *getDeclKindName() const; 403 404 /// getParent - Returns the containing DeclContext. 405 DeclContext *getParent() { 406 return cast<Decl>(this)->getDeclContext(); 407 } 408 const DeclContext *getParent() const { 409 return const_cast<DeclContext*>(this)->getParent(); 410 } 411 412 /// getLexicalParent - Returns the containing lexical DeclContext. May be 413 /// different from getParent, e.g.: 414 /// 415 /// namespace A { 416 /// struct S; 417 /// } 418 /// struct A::S {}; // getParent() == namespace 'A' 419 /// // getLexicalParent() == translation unit 420 /// 421 DeclContext *getLexicalParent() { 422 return cast<Decl>(this)->getLexicalDeclContext(); 423 } 424 const DeclContext *getLexicalParent() const { 425 return const_cast<DeclContext*>(this)->getLexicalParent(); 426 } 427 428 bool isFunctionOrMethod() const { 429 switch (DeclKind) { 430 case Decl::Block: 431 case Decl::ObjCMethod: 432 return true; 433 default: 434 return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast; 435 } 436 } 437 438 bool isFileContext() const { 439 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; 440 } 441 442 bool isTranslationUnit() const { 443 return DeclKind == Decl::TranslationUnit; 444 } 445 446 bool isRecord() const { 447 return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast; 448 } 449 450 bool isNamespace() const { 451 return DeclKind == Decl::Namespace; 452 } 453 454 /// \brief Determines whether this context is dependent on a 455 /// template parameter. 456 bool isDependentContext() const; 457 458 /// isTransparentContext - Determines whether this context is a 459 /// "transparent" context, meaning that the members declared in this 460 /// context are semantically declared in the nearest enclosing 461 /// non-transparent (opaque) context but are lexically declared in 462 /// this context. For example, consider the enumerators of an 463 /// enumeration type: 464 /// @code 465 /// enum E { 466 /// Val1 467 /// }; 468 /// @endcode 469 /// Here, E is a transparent context, so its enumerator (Val1) will 470 /// appear (semantically) that it is in the same context of E. 471 /// Examples of transparent contexts include: enumerations (except for 472 /// C++0x scoped enums), C++ linkage specifications, and C++0x 473 /// inline namespaces. 474 bool isTransparentContext() const; 475 476 bool Encloses(DeclContext *DC) const { 477 for (; DC; DC = DC->getParent()) 478 if (DC == this) 479 return true; 480 return false; 481 } 482 483 /// getPrimaryContext - There may be many different 484 /// declarations of the same entity (including forward declarations 485 /// of classes, multiple definitions of namespaces, etc.), each with 486 /// a different set of declarations. This routine returns the 487 /// "primary" DeclContext structure, which will contain the 488 /// information needed to perform name lookup into this context. 489 DeclContext *getPrimaryContext(); 490 491 /// getLookupContext - Retrieve the innermost non-transparent 492 /// context of this context, which corresponds to the innermost 493 /// location from which name lookup can find the entities in this 494 /// context. 495 DeclContext *getLookupContext(); 496 const DeclContext *getLookupContext() const { 497 return const_cast<DeclContext *>(this)->getLookupContext(); 498 } 499 500 /// \brief Retrieve the nearest enclosing namespace context. 501 DeclContext *getEnclosingNamespaceContext(); 502 const DeclContext *getEnclosingNamespaceContext() const { 503 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); 504 } 505 506 /// getNextContext - If this is a DeclContext that may have other 507 /// DeclContexts that are semantically connected but syntactically 508 /// different, such as C++ namespaces, this routine retrieves the 509 /// next DeclContext in the link. Iteration through the chain of 510 /// DeclContexts should begin at the primary DeclContext and 511 /// continue until this function returns NULL. For example, given: 512 /// @code 513 /// namespace N { 514 /// int x; 515 /// } 516 /// namespace N { 517 /// int y; 518 /// } 519 /// @endcode 520 /// The first occurrence of namespace N will be the primary 521 /// DeclContext. Its getNextContext will return the second 522 /// occurrence of namespace N. 523 DeclContext *getNextContext(); 524 525 /// decl_iterator - Iterates through the declarations stored 526 /// within this context. 527 class decl_iterator { 528 /// Current - The current declaration. 529 Decl *Current; 530 531 public: 532 typedef Decl* value_type; 533 typedef Decl* reference; 534 typedef Decl* pointer; 535 typedef std::forward_iterator_tag iterator_category; 536 typedef std::ptrdiff_t difference_type; 537 538 decl_iterator() : Current(0) { } 539 explicit decl_iterator(Decl *C) : Current(C) { } 540 541 reference operator*() const { return Current; } 542 pointer operator->() const { return Current; } 543 544 decl_iterator& operator++() { 545 Current = Current->getNextDeclInContext(); 546 return *this; 547 } 548 549 decl_iterator operator++(int) { 550 decl_iterator tmp(*this); 551 ++(*this); 552 return tmp; 553 } 554 555 friend bool operator==(decl_iterator x, decl_iterator y) { 556 return x.Current == y.Current; 557 } 558 friend bool operator!=(decl_iterator x, decl_iterator y) { 559 return x.Current != y.Current; 560 } 561 }; 562 563 /// decls_begin/decls_end - Iterate over the declarations stored in 564 /// this context. 565 decl_iterator decls_begin(ASTContext &Context) const; 566 decl_iterator decls_end(ASTContext &Context) const; 567 bool decls_empty(ASTContext &Context) const; 568 569 /// specific_decl_iterator - Iterates over a subrange of 570 /// declarations stored in a DeclContext, providing only those that 571 /// are of type SpecificDecl (or a class derived from it). This 572 /// iterator is used, for example, to provide iteration over just 573 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). 574 template<typename SpecificDecl> 575 class specific_decl_iterator { 576 /// Current - The current, underlying declaration iterator, which 577 /// will either be NULL or will point to a declaration of 578 /// type SpecificDecl. 579 DeclContext::decl_iterator Current; 580 581 /// SkipToNextDecl - Advances the current position up to the next 582 /// declaration of type SpecificDecl that also meets the criteria 583 /// required by Acceptable. 584 void SkipToNextDecl() { 585 while (*Current && !isa<SpecificDecl>(*Current)) 586 ++Current; 587 } 588 589 public: 590 typedef SpecificDecl* value_type; 591 typedef SpecificDecl* reference; 592 typedef SpecificDecl* pointer; 593 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 594 difference_type; 595 typedef std::forward_iterator_tag iterator_category; 596 597 specific_decl_iterator() : Current() { } 598 599 /// specific_decl_iterator - Construct a new iterator over a 600 /// subset of the declarations the range [C, 601 /// end-of-declarations). If A is non-NULL, it is a pointer to a 602 /// member function of SpecificDecl that should return true for 603 /// all of the SpecificDecl instances that will be in the subset 604 /// of iterators. For example, if you want Objective-C instance 605 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 606 /// &ObjCMethodDecl::isInstanceMethod. 607 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 608 SkipToNextDecl(); 609 } 610 611 reference operator*() const { return cast<SpecificDecl>(*Current); } 612 pointer operator->() const { return cast<SpecificDecl>(*Current); } 613 614 specific_decl_iterator& operator++() { 615 ++Current; 616 SkipToNextDecl(); 617 return *this; 618 } 619 620 specific_decl_iterator operator++(int) { 621 specific_decl_iterator tmp(*this); 622 ++(*this); 623 return tmp; 624 } 625 626 friend bool 627 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) { 628 return x.Current == y.Current; 629 } 630 631 friend bool 632 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) { 633 return x.Current != y.Current; 634 } 635 }; 636 637 /// \brief Iterates over a filtered subrange of declarations stored 638 /// in a DeclContext. 639 /// 640 /// This iterator visits only those declarations that are of type 641 /// SpecificDecl (or a class derived from it) and that meet some 642 /// additional run-time criteria. This iterator is used, for 643 /// example, to provide access to the instance methods within an 644 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and 645 /// Acceptable = ObjCMethodDecl::isInstanceMethod). 646 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> 647 class filtered_decl_iterator { 648 /// Current - The current, underlying declaration iterator, which 649 /// will either be NULL or will point to a declaration of 650 /// type SpecificDecl. 651 DeclContext::decl_iterator Current; 652 653 /// SkipToNextDecl - Advances the current position up to the next 654 /// declaration of type SpecificDecl that also meets the criteria 655 /// required by Acceptable. 656 void SkipToNextDecl() { 657 while (*Current && 658 (!isa<SpecificDecl>(*Current) || 659 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) 660 ++Current; 661 } 662 663 public: 664 typedef SpecificDecl* value_type; 665 typedef SpecificDecl* reference; 666 typedef SpecificDecl* pointer; 667 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type 668 difference_type; 669 typedef std::forward_iterator_tag iterator_category; 670 671 filtered_decl_iterator() : Current() { } 672 673 /// specific_decl_iterator - Construct a new iterator over a 674 /// subset of the declarations the range [C, 675 /// end-of-declarations). If A is non-NULL, it is a pointer to a 676 /// member function of SpecificDecl that should return true for 677 /// all of the SpecificDecl instances that will be in the subset 678 /// of iterators. For example, if you want Objective-C instance 679 /// methods, SpecificDecl will be ObjCMethodDecl and A will be 680 /// &ObjCMethodDecl::isInstanceMethod. 681 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { 682 SkipToNextDecl(); 683 } 684 685 reference operator*() const { return cast<SpecificDecl>(*Current); } 686 pointer operator->() const { return cast<SpecificDecl>(*Current); } 687 688 filtered_decl_iterator& operator++() { 689 ++Current; 690 SkipToNextDecl(); 691 return *this; 692 } 693 694 filtered_decl_iterator operator++(int) { 695 filtered_decl_iterator tmp(*this); 696 ++(*this); 697 return tmp; 698 } 699 700 friend bool 701 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 702 return x.Current == y.Current; 703 } 704 705 friend bool 706 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { 707 return x.Current != y.Current; 708 } 709 }; 710 711 /// @brief Add the declaration D into this context. 712 /// 713 /// This routine should be invoked when the declaration D has first 714 /// been declared, to place D into the context where it was 715 /// (lexically) defined. Every declaration must be added to one 716 /// (and only one!) context, where it can be visited via 717 /// [decls_begin(), decls_end()). Once a declaration has been added 718 /// to its lexical context, the corresponding DeclContext owns the 719 /// declaration. 720 /// 721 /// If D is also a NamedDecl, it will be made visible within its 722 /// semantic context via makeDeclVisibleInContext. 723 void addDecl(ASTContext &Context, Decl *D); 724 725 /// lookup_iterator - An iterator that provides access to the results 726 /// of looking up a name within this context. 727 typedef NamedDecl **lookup_iterator; 728 729 /// lookup_const_iterator - An iterator that provides non-mutable 730 /// access to the results of lookup up a name within this context. 731 typedef NamedDecl * const * lookup_const_iterator; 732 733 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result; 734 typedef std::pair<lookup_const_iterator, lookup_const_iterator> 735 lookup_const_result; 736 737 /// lookup - Find the declarations (if any) with the given Name in 738 /// this context. Returns a range of iterators that contains all of 739 /// the declarations with this name, with object, function, member, 740 /// and enumerator names preceding any tag name. Note that this 741 /// routine will not look into parent contexts. 742 lookup_result lookup(ASTContext &Context, DeclarationName Name); 743 lookup_const_result lookup(ASTContext &Context, DeclarationName Name) const; 744 745 /// @brief Makes a declaration visible within this context. 746 /// 747 /// This routine makes the declaration D visible to name lookup 748 /// within this context and, if this is a transparent context, 749 /// within its parent contexts up to the first enclosing 750 /// non-transparent context. Making a declaration visible within a 751 /// context does not transfer ownership of a declaration, and a 752 /// declaration can be visible in many contexts that aren't its 753 /// lexical context. 754 /// 755 /// If D is a redeclaration of an existing declaration that is 756 /// visible from this context, as determined by 757 /// NamedDecl::declarationReplaces, the previous declaration will be 758 /// replaced with D. 759 void makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D); 760 761 /// udir_iterator - Iterates through the using-directives stored 762 /// within this context. 763 typedef UsingDirectiveDecl * const * udir_iterator; 764 765 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range; 766 767 udir_iterator_range getUsingDirectives(ASTContext &Context) const; 768 769 udir_iterator using_directives_begin(ASTContext &Context) const { 770 return getUsingDirectives(Context).first; 771 } 772 773 udir_iterator using_directives_end(ASTContext &Context) const { 774 return getUsingDirectives(Context).second; 775 } 776 777 // Low-level accessors 778 779 /// \brief Retrieve the internal representation of the lookup structure. 780 void* getLookupPtr() const { return LookupPtr; } 781 782 /// \brief Whether this DeclContext has external storage containing 783 /// additional declarations that are lexically in this context. 784 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; } 785 786 /// \brief State whether this DeclContext has external storage for 787 /// declarations lexically in this context. 788 void setHasExternalLexicalStorage(bool ES = true) { 789 ExternalLexicalStorage = ES; 790 } 791 792 /// \brief Whether this DeclContext has external storage containing 793 /// additional declarations that are visible in this context. 794 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; } 795 796 /// \brief State whether this DeclContext has external storage for 797 /// declarations visible in this context. 798 void setHasExternalVisibleStorage(bool ES = true) { 799 ExternalVisibleStorage = ES; 800 } 801 802 static bool classof(const Decl *D); 803 static bool classof(const DeclContext *D) { return true; } 804#define DECL_CONTEXT(Name) \ 805 static bool classof(const Name##Decl *D) { return true; } 806#include "clang/AST/DeclNodes.def" 807 808private: 809 void LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const; 810 void LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const; 811 812 void buildLookup(ASTContext &Context, DeclContext *DCtx); 813 void makeDeclVisibleInContextImpl(ASTContext &Context, NamedDecl *D); 814}; 815 816inline bool Decl::isTemplateParameter() const { 817 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm; 818} 819 820inline bool Decl::isDefinedOutsideFunctionOrMethod() const { 821 if (getDeclContext()) 822 return !getDeclContext()->getLookupContext()->isFunctionOrMethod(); 823 return true; 824} 825 826} // end clang. 827 828namespace llvm { 829 830/// Implement a isa_impl_wrap specialization to check whether a DeclContext is 831/// a specific Decl. 832template<class ToTy> 833struct isa_impl_wrap<ToTy, 834 const ::clang::DeclContext,const ::clang::DeclContext> { 835 static bool doit(const ::clang::DeclContext &Val) { 836 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val)); 837 } 838}; 839template<class ToTy> 840struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> 841 : public isa_impl_wrap<ToTy, 842 const ::clang::DeclContext,const ::clang::DeclContext> {}; 843 844/// Implement cast_convert_val for Decl -> DeclContext conversions. 845template<class FromTy> 846struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { 847 static ::clang::DeclContext &doit(const FromTy &Val) { 848 return *FromTy::castToDeclContext(&Val); 849 } 850}; 851 852template<class FromTy> 853struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { 854 static ::clang::DeclContext *doit(const FromTy *Val) { 855 return FromTy::castToDeclContext(Val); 856 } 857}; 858 859template<class FromTy> 860struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { 861 static const ::clang::DeclContext &doit(const FromTy &Val) { 862 return *FromTy::castToDeclContext(&Val); 863 } 864}; 865 866template<class FromTy> 867struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { 868 static const ::clang::DeclContext *doit(const FromTy *Val) { 869 return FromTy::castToDeclContext(Val); 870 } 871}; 872 873/// Implement cast_convert_val for DeclContext -> Decl conversions. 874template<class ToTy> 875struct cast_convert_val<ToTy, 876 const ::clang::DeclContext,const ::clang::DeclContext> { 877 static ToTy &doit(const ::clang::DeclContext &Val) { 878 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val)); 879 } 880}; 881template<class ToTy> 882struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> 883 : public cast_convert_val<ToTy, 884 const ::clang::DeclContext,const ::clang::DeclContext> {}; 885 886template<class ToTy> 887struct cast_convert_val<ToTy, 888 const ::clang::DeclContext*, const ::clang::DeclContext*> { 889 static ToTy *doit(const ::clang::DeclContext *Val) { 890 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val)); 891 } 892}; 893template<class ToTy> 894struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> 895 : public cast_convert_val<ToTy, 896 const ::clang::DeclContext*,const ::clang::DeclContext*> {}; 897 898} // end namespace llvm 899 900#endif 901