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