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