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