1 //===-- DeclBase.h - Base Classes for representing declarations -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Decl and DeclContext interfaces.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECLBASE_H
15 #define LLVM_CLANG_AST_DECLBASE_H
17 #include "clang/AST/AttrIterator.h"
18 #include "clang/AST/DeclarationName.h"
19 #include "clang/Basic/Specifiers.h"
20 #include "llvm/ADT/PointerUnion.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/PrettyStackTrace.h"
25 class ASTMutationListener;
30 class DeclarationName;
31 class DependentDiagnostic;
34 class LinkageSpecDecl;
38 class ObjCCategoryDecl;
39 class ObjCCategoryImplDecl;
40 class ObjCContainerDecl;
42 class ObjCImplementationDecl;
43 class ObjCInterfaceDecl;
45 class ObjCProtocolDecl;
46 struct PrintingPolicy;
49 class TranslationUnitDecl;
50 class UsingDirectiveDecl;
54 // DeclContext* is only 4-byte aligned on 32-bit systems.
56 class PointerLikeTypeTraits<clang::DeclContext*> {
57 typedef clang::DeclContext* PT;
59 static inline void *getAsVoidPointer(PT P) { return P; }
60 static inline PT getFromVoidPointer(void *P) {
61 return static_cast<PT>(P);
63 enum { NumLowBitsAvailable = 2 };
69 /// \brief Captures the result of checking the availability of a
71 enum AvailabilityResult {
78 /// Decl - This represents one declaration (or definition), e.g. a variable,
79 /// typedef, function, struct, etc.
83 /// \brief Lists the kind of concrete classes of Decl.
85 #define DECL(DERIVED, BASE) DERIVED,
86 #define ABSTRACT_DECL(DECL)
87 #define DECL_RANGE(BASE, START, END) \
88 first##BASE = START, last##BASE = END,
89 #define LAST_DECL_RANGE(BASE, START, END) \
90 first##BASE = START, last##BASE = END
91 #include "clang/AST/DeclNodes.inc"
94 /// \brief A placeholder type used to construct an empty shell of a
95 /// decl-derived type that will be filled in later (e.g., by some
96 /// deserialization method).
97 struct EmptyShell { };
99 /// IdentifierNamespace - The different namespaces in which
100 /// declarations may appear. According to C99 6.2.3, there are
101 /// four namespaces, labels, tags, members and ordinary
102 /// identifiers. C++ describes lookup completely differently:
103 /// certain lookups merely "ignore" certain kinds of declarations,
104 /// usually based on whether the declaration is of a type, etc.
106 /// These are meant as bitmasks, so that searches in
107 /// C++ can look into the "tag" namespace during ordinary lookup.
109 /// Decl currently provides 15 bits of IDNS bits.
110 enum IdentifierNamespace {
111 /// Labels, declared with 'x:' and referenced with 'goto x'.
114 /// Tags, declared with 'struct foo;' and referenced with
115 /// 'struct foo'. All tags are also types. This is what
116 /// elaborated-type-specifiers look for in C.
119 /// Types, declared with 'struct foo', typedefs, etc.
120 /// This is what elaborated-type-specifiers look for in C++,
121 /// but note that it's ill-formed to find a non-tag.
124 /// Members, declared with object declarations within tag
125 /// definitions. In C, these can only be found by "qualified"
126 /// lookup in member expressions. In C++, they're found by
128 IDNS_Member = 0x0008,
130 /// Namespaces, declared with 'namespace foo {}'.
131 /// Lookup for nested-name-specifiers find these.
132 IDNS_Namespace = 0x0010,
134 /// Ordinary names. In C, everything that's not a label, tag,
135 /// or member ends up here.
136 IDNS_Ordinary = 0x0020,
138 /// Objective C \@protocol.
139 IDNS_ObjCProtocol = 0x0040,
141 /// This declaration is a friend function. A friend function
142 /// declaration is always in this namespace but may also be in
143 /// IDNS_Ordinary if it was previously declared.
144 IDNS_OrdinaryFriend = 0x0080,
146 /// This declaration is a friend class. A friend class
147 /// declaration is always in this namespace but may also be in
148 /// IDNS_Tag|IDNS_Type if it was previously declared.
149 IDNS_TagFriend = 0x0100,
151 /// This declaration is a using declaration. A using declaration
152 /// *introduces* a number of other declarations into the current
153 /// scope, and those declarations use the IDNS of their targets,
154 /// but the actual using declarations go in this namespace.
157 /// This declaration is a C++ operator declared in a non-class
158 /// context. All such operators are also in IDNS_Ordinary.
159 /// C++ lexical operator lookup looks for these.
160 IDNS_NonMemberOperator = 0x0400
163 /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
164 /// parameter types in method declarations. Other than remembering
165 /// them and mangling them into the method's signature string, these
166 /// are ignored by the compiler; they are consumed by certain
167 /// remote-messaging frameworks.
169 /// in, inout, and out are mutually exclusive and apply only to
170 /// method parameters. bycopy and byref are mutually exclusive and
171 /// apply only to method parameters (?). oneway applies only to
172 /// results. All of these expect their corresponding parameter to
173 /// have a particular type. None of this is currently enforced by
176 /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
177 enum ObjCDeclQualifier {
182 OBJC_TQ_Bycopy = 0x8,
183 OBJC_TQ_Byref = 0x10,
184 OBJC_TQ_Oneway = 0x20
188 // Enumeration values used in the bits stored in NextInContextAndBits.
190 /// \brief Whether this declaration is a top-level declaration (function,
191 /// global variable, etc.) that is lexically inside an objc container
193 TopLevelDeclInObjCContainerFlag = 0x01,
195 /// \brief Whether this declaration is private to the module in which it was
197 ModulePrivateFlag = 0x02
200 /// \brief The next declaration within the same lexical
201 /// DeclContext. These pointers form the linked list that is
202 /// traversed via DeclContext's decls_begin()/decls_end().
204 /// The extra two bits are used for the TopLevelDeclInObjCContainer and
205 /// ModulePrivate bits.
206 llvm::PointerIntPair<Decl *, 2, unsigned> NextInContextAndBits;
209 friend class DeclContext;
212 DeclContext *SemanticDC;
213 DeclContext *LexicalDC;
217 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
218 /// For declarations that don't contain C++ scope specifiers, it contains
219 /// the DeclContext where the Decl was declared.
220 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
221 /// with the context where it semantically belongs (SemanticDC) and the
222 /// context where it was lexically declared (LexicalDC).
226 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
228 /// void A::f(); // SemanticDC == namespace 'A'
229 /// // LexicalDC == global namespace
230 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
232 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
233 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
234 inline MultipleDC *getMultipleDC() const {
235 return DeclCtx.get<MultipleDC*>();
237 inline DeclContext *getSemanticDC() const {
238 return DeclCtx.get<DeclContext*>();
241 /// Loc - The location of this decl.
244 /// DeclKind - This indicates which class this is.
245 unsigned DeclKind : 8;
247 /// InvalidDecl - This indicates a semantic error occurred.
248 unsigned InvalidDecl : 1;
250 /// HasAttrs - This indicates whether the decl has attributes or not.
251 unsigned HasAttrs : 1;
253 /// Implicit - Whether this declaration was implicitly generated by
254 /// the implementation rather than explicitly written by the user.
255 unsigned Implicit : 1;
257 /// \brief Whether this declaration was "used", meaning that a definition is
261 /// \brief Whether this declaration was "referenced".
262 /// The difference with 'Used' is whether the reference appears in a
263 /// evaluated context or not, e.g. functions used in uninstantiated templates
264 /// are regarded as "referenced" but not "used".
265 unsigned Referenced : 1;
267 /// \brief Whether statistic collection is enabled.
268 static bool StatisticsEnabled;
271 /// Access - Used by C++ decls for the access specifier.
272 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
274 friend class CXXClassMemberWrapper;
276 /// \brief Whether this declaration was loaded from an AST file.
277 unsigned FromASTFile : 1;
279 /// \brief Whether this declaration is hidden from normal name lookup, e.g.,
280 /// because it is was loaded from an AST file is either module-private or
281 /// because its submodule has not been made visible.
284 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
285 unsigned IdentifierNamespace : 12;
287 /// \brief Whether the \c CachedLinkage field is active.
289 /// This field is only valid for NamedDecls subclasses.
290 mutable unsigned HasCachedLinkage : 1;
292 /// \brief If \c HasCachedLinkage, the linkage of this declaration.
294 /// This field is only valid for NamedDecls subclasses.
295 mutable unsigned CachedLinkage : 2;
297 friend class ASTDeclWriter;
298 friend class ASTDeclReader;
299 friend class ASTReader;
302 void CheckAccessDeclContext() const;
306 Decl(Kind DK, DeclContext *DC, SourceLocation L)
307 : NextInContextAndBits(), DeclCtx(DC),
308 Loc(L), DeclKind(DK), InvalidDecl(0),
309 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
310 Access(AS_none), FromASTFile(0), Hidden(0),
311 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
314 if (StatisticsEnabled) add(DK);
317 Decl(Kind DK, EmptyShell Empty)
318 : NextInContextAndBits(), DeclKind(DK), InvalidDecl(0),
319 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
320 Access(AS_none), FromASTFile(0), Hidden(0),
321 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
324 if (StatisticsEnabled) add(DK);
329 /// \brief Allocate memory for a deserialized declaration.
331 /// This routine must be used to allocate memory for any declaration that is
332 /// deserialized from a module file.
334 /// \param Context The context in which we will allocate memory.
335 /// \param ID The global ID of the deserialized declaration.
336 /// \param Size The size of the allocated object.
337 static void *AllocateDeserializedDecl(const ASTContext &Context,
341 /// \brief Update a potentially out-of-date declaration.
342 void updateOutOfDate(IdentifierInfo &II) const;
346 /// \brief Source range that this declaration covers.
347 virtual SourceRange getSourceRange() const LLVM_READONLY {
348 return SourceRange(getLocation(), getLocation());
350 SourceLocation getLocStart() const LLVM_READONLY {
351 return getSourceRange().getBegin();
353 SourceLocation getLocEnd() const LLVM_READONLY {
354 return getSourceRange().getEnd();
357 SourceLocation getLocation() const { return Loc; }
358 void setLocation(SourceLocation L) { Loc = L; }
360 Kind getKind() const { return static_cast<Kind>(DeclKind); }
361 const char *getDeclKindName() const;
363 Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); }
364 const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();}
366 DeclContext *getDeclContext() {
368 return getSemanticDC();
369 return getMultipleDC()->SemanticDC;
371 const DeclContext *getDeclContext() const {
372 return const_cast<Decl*>(this)->getDeclContext();
375 /// Finds the innermost non-closure context of this declaration.
376 /// That is, walk out the DeclContext chain, skipping any blocks.
377 DeclContext *getNonClosureContext();
378 const DeclContext *getNonClosureContext() const {
379 return const_cast<Decl*>(this)->getNonClosureContext();
382 TranslationUnitDecl *getTranslationUnitDecl();
383 const TranslationUnitDecl *getTranslationUnitDecl() const {
384 return const_cast<Decl*>(this)->getTranslationUnitDecl();
387 bool isInAnonymousNamespace() const;
389 ASTContext &getASTContext() const LLVM_READONLY;
391 void setAccess(AccessSpecifier AS) {
394 CheckAccessDeclContext();
398 AccessSpecifier getAccess() const {
400 CheckAccessDeclContext();
402 return AccessSpecifier(Access);
405 bool hasAttrs() const { return HasAttrs; }
406 void setAttrs(const AttrVec& Attrs) {
407 return setAttrsImpl(Attrs, getASTContext());
409 AttrVec &getAttrs() {
410 return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
412 const AttrVec &getAttrs() const;
413 void swapAttrs(Decl *D);
416 void addAttr(Attr *A) {
418 getAttrs().push_back(A);
420 setAttrs(AttrVec(1, A));
423 typedef AttrVec::const_iterator attr_iterator;
425 // FIXME: Do not rely on iterators having comparable singular values.
426 // Note that this should error out if they do not.
427 attr_iterator attr_begin() const {
428 return hasAttrs() ? getAttrs().begin() : 0;
430 attr_iterator attr_end() const {
431 return hasAttrs() ? getAttrs().end() : 0;
434 template <typename T>
436 if (!HasAttrs) return;
438 AttrVec &Vec = getAttrs();
439 Vec.erase(std::remove_if(Vec.begin(), Vec.end(), isa<T, Attr*>), Vec.end());
445 template <typename T>
446 specific_attr_iterator<T> specific_attr_begin() const {
447 return specific_attr_iterator<T>(attr_begin());
449 template <typename T>
450 specific_attr_iterator<T> specific_attr_end() const {
451 return specific_attr_iterator<T>(attr_end());
454 template<typename T> T *getAttr() const {
455 return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : 0;
457 template<typename T> bool hasAttr() const {
458 return hasAttrs() && hasSpecificAttr<T>(getAttrs());
461 /// getMaxAlignment - return the maximum alignment specified by attributes
462 /// on this decl, 0 if there are none.
463 unsigned getMaxAlignment() const;
465 /// setInvalidDecl - Indicates the Decl had a semantic error. This
466 /// allows for graceful error recovery.
467 void setInvalidDecl(bool Invalid = true);
468 bool isInvalidDecl() const { return (bool) InvalidDecl; }
470 /// isImplicit - Indicates whether the declaration was implicitly
471 /// generated by the implementation. If false, this declaration
472 /// was written explicitly in the source code.
473 bool isImplicit() const { return Implicit; }
474 void setImplicit(bool I = true) { Implicit = I; }
476 /// \brief Whether this declaration was used, meaning that a definition
479 /// \param CheckUsedAttr When true, also consider the "used" attribute
480 /// (in addition to the "used" bit set by \c setUsed()) when determining
481 /// whether the function is used.
482 bool isUsed(bool CheckUsedAttr = true) const;
484 void setUsed(bool U = true) { Used = U; }
486 /// \brief Whether this declaration was referenced.
487 bool isReferenced() const;
489 void setReferenced(bool R = true) { Referenced = R; }
491 /// \brief Whether this declaration is a top-level declaration (function,
492 /// global variable, etc.) that is lexically inside an objc container
494 bool isTopLevelDeclInObjCContainer() const {
495 return NextInContextAndBits.getInt() & TopLevelDeclInObjCContainerFlag;
498 void setTopLevelDeclInObjCContainer(bool V = true) {
499 unsigned Bits = NextInContextAndBits.getInt();
501 Bits |= TopLevelDeclInObjCContainerFlag;
503 Bits &= ~TopLevelDeclInObjCContainerFlag;
504 NextInContextAndBits.setInt(Bits);
508 /// \brief Whether this declaration was marked as being private to the
509 /// module in which it was defined.
510 bool isModulePrivate() const {
511 return NextInContextAndBits.getInt() & ModulePrivateFlag;
514 /// \brief Specify whether this declaration was marked as being private
515 /// to the module in which it was defined.
516 void setModulePrivate(bool MP = true) {
517 unsigned Bits = NextInContextAndBits.getInt();
519 Bits |= ModulePrivateFlag;
521 Bits &= ~ModulePrivateFlag;
522 NextInContextAndBits.setInt(Bits);
525 /// \brief Set the owning module ID.
526 void setOwningModuleID(unsigned ID) {
527 assert(isFromASTFile() && "Only works on a deserialized declaration");
528 *((unsigned*)this - 2) = ID;
533 /// \brief Determine the availability of the given declaration.
535 /// This routine will determine the most restrictive availability of
536 /// the given declaration (e.g., preferring 'unavailable' to
539 /// \param Message If non-NULL and the result is not \c
540 /// AR_Available, will be set to a (possibly empty) message
541 /// describing why the declaration has not been introduced, is
542 /// deprecated, or is unavailable.
543 AvailabilityResult getAvailability(std::string *Message = 0) const;
545 /// \brief Determine whether this declaration is marked 'deprecated'.
547 /// \param Message If non-NULL and the declaration is deprecated,
548 /// this will be set to the message describing why the declaration
549 /// was deprecated (which may be empty).
550 bool isDeprecated(std::string *Message = 0) const {
551 return getAvailability(Message) == AR_Deprecated;
554 /// \brief Determine whether this declaration is marked 'unavailable'.
556 /// \param Message If non-NULL and the declaration is unavailable,
557 /// this will be set to the message describing why the declaration
558 /// was made unavailable (which may be empty).
559 bool isUnavailable(std::string *Message = 0) const {
560 return getAvailability(Message) == AR_Unavailable;
563 /// \brief Determine whether this is a weak-imported symbol.
565 /// Weak-imported symbols are typically marked with the
566 /// 'weak_import' attribute, but may also be marked with an
567 /// 'availability' attribute where we're targing a platform prior to
568 /// the introduction of this feature.
569 bool isWeakImported() const;
571 /// \brief Determines whether this symbol can be weak-imported,
572 /// e.g., whether it would be well-formed to add the weak_import
575 /// \param IsDefinition Set to \c true to indicate that this
576 /// declaration cannot be weak-imported because it has a definition.
577 bool canBeWeakImported(bool &IsDefinition) const;
579 /// \brief Determine whether this declaration came from an AST file (such as
580 /// a precompiled header or module) rather than having been parsed.
581 bool isFromASTFile() const { return FromASTFile; }
583 /// \brief Retrieve the global declaration ID associated with this
584 /// declaration, which specifies where in the
585 unsigned getGlobalID() const {
587 return *((const unsigned*)this - 1);
591 /// \brief Retrieve the global ID of the module that owns this particular
593 unsigned getOwningModuleID() const {
595 return *((const unsigned*)this - 2);
601 Module *getOwningModuleSlow() const;
604 Module *getOwningModule() const {
605 if (!isFromASTFile())
608 return getOwningModuleSlow();
611 unsigned getIdentifierNamespace() const {
612 return IdentifierNamespace;
614 bool isInIdentifierNamespace(unsigned NS) const {
615 return getIdentifierNamespace() & NS;
617 static unsigned getIdentifierNamespaceForKind(Kind DK);
619 bool hasTagIdentifierNamespace() const {
620 return isTagIdentifierNamespace(getIdentifierNamespace());
622 static bool isTagIdentifierNamespace(unsigned NS) {
623 // TagDecls have Tag and Type set and may also have TagFriend.
624 return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
627 /// getLexicalDeclContext - The declaration context where this Decl was
628 /// lexically declared (LexicalDC). May be different from
629 /// getDeclContext() (SemanticDC).
633 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
635 /// void A::f(); // SemanticDC == namespace 'A'
636 /// // LexicalDC == global namespace
637 DeclContext *getLexicalDeclContext() {
639 return getSemanticDC();
640 return getMultipleDC()->LexicalDC;
642 const DeclContext *getLexicalDeclContext() const {
643 return const_cast<Decl*>(this)->getLexicalDeclContext();
646 virtual bool isOutOfLine() const {
647 return getLexicalDeclContext() != getDeclContext();
650 /// setDeclContext - Set both the semantic and lexical DeclContext
652 void setDeclContext(DeclContext *DC);
654 void setLexicalDeclContext(DeclContext *DC);
656 /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
657 /// scoped decl is defined outside the current function or method. This is
658 /// roughly global variables and functions, but also handles enums (which
659 /// could be defined inside or outside a function etc).
660 bool isDefinedOutsideFunctionOrMethod() const {
661 return getParentFunctionOrMethod() == 0;
664 /// \brief If this decl is defined inside a function/method/block it returns
665 /// the corresponding DeclContext, otherwise it returns null.
666 const DeclContext *getParentFunctionOrMethod() const;
667 DeclContext *getParentFunctionOrMethod() {
668 return const_cast<DeclContext*>(
669 const_cast<const Decl*>(this)->getParentFunctionOrMethod());
672 /// \brief Retrieves the "canonical" declaration of the given declaration.
673 virtual Decl *getCanonicalDecl() { return this; }
674 const Decl *getCanonicalDecl() const {
675 return const_cast<Decl*>(this)->getCanonicalDecl();
678 /// \brief Whether this particular Decl is a canonical one.
679 bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
682 /// \brief Returns the next redeclaration or itself if this is the only decl.
684 /// Decl subclasses that can be redeclared should override this method so that
685 /// Decl::redecl_iterator can iterate over them.
686 virtual Decl *getNextRedeclaration() { return this; }
688 /// \brief Implementation of getPreviousDecl(), to be overridden by any
689 /// subclass that has a redeclaration chain.
690 virtual Decl *getPreviousDeclImpl() { return 0; }
692 /// \brief Implementation of getMostRecentDecl(), to be overridden by any
693 /// subclass that has a redeclaration chain.
694 virtual Decl *getMostRecentDeclImpl() { return this; }
697 /// \brief Iterates through all the redeclarations of the same decl.
698 class redecl_iterator {
699 /// Current - The current declaration.
704 typedef Decl *value_type;
705 typedef const value_type &reference;
706 typedef const value_type *pointer;
707 typedef std::forward_iterator_tag iterator_category;
708 typedef std::ptrdiff_t difference_type;
710 redecl_iterator() : Current(0) { }
711 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
713 reference operator*() const { return Current; }
714 value_type operator->() const { return Current; }
716 redecl_iterator& operator++() {
717 assert(Current && "Advancing while iterator has reached end");
718 // Get either previous decl or latest decl.
719 Decl *Next = Current->getNextRedeclaration();
720 assert(Next && "Should return next redeclaration or itself, never null!");
721 Current = (Next != Starter ? Next : 0);
725 redecl_iterator operator++(int) {
726 redecl_iterator tmp(*this);
731 friend bool operator==(redecl_iterator x, redecl_iterator y) {
732 return x.Current == y.Current;
734 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
735 return x.Current != y.Current;
739 /// \brief Returns iterator for all the redeclarations of the same decl.
740 /// It will iterate at least once (when this decl is the only one).
741 redecl_iterator redecls_begin() const {
742 return redecl_iterator(const_cast<Decl*>(this));
744 redecl_iterator redecls_end() const { return redecl_iterator(); }
746 /// \brief Retrieve the previous declaration that declares the same entity
747 /// as this declaration, or NULL if there is no previous declaration.
748 Decl *getPreviousDecl() { return getPreviousDeclImpl(); }
750 /// \brief Retrieve the most recent declaration that declares the same entity
751 /// as this declaration, or NULL if there is no previous declaration.
752 const Decl *getPreviousDecl() const {
753 return const_cast<Decl *>(this)->getPreviousDeclImpl();
756 /// \brief Retrieve the most recent declaration that declares the same entity
757 /// as this declaration (which may be this declaration).
758 Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); }
760 /// \brief Retrieve the most recent declaration that declares the same entity
761 /// as this declaration (which may be this declaration).
762 const Decl *getMostRecentDecl() const {
763 return const_cast<Decl *>(this)->getMostRecentDeclImpl();
766 /// getBody - If this Decl represents a declaration for a body of code,
767 /// such as a function or method definition, this method returns the
768 /// top-level Stmt* of that body. Otherwise this method returns null.
769 virtual Stmt* getBody() const { return 0; }
771 /// \brief Returns true if this Decl represents a declaration for a body of
772 /// code, such as a function or method definition.
773 virtual bool hasBody() const { return getBody() != 0; }
775 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
776 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
777 SourceLocation getBodyRBrace() const;
779 // global temp stats (until we have a per-module visitor)
780 static void add(Kind k);
781 static void EnableStatistics();
782 static void PrintStats();
784 /// isTemplateParameter - Determines whether this declaration is a
785 /// template parameter.
786 bool isTemplateParameter() const;
788 /// isTemplateParameter - Determines whether this declaration is a
789 /// template parameter pack.
790 bool isTemplateParameterPack() const;
792 /// \brief Whether this declaration is a parameter pack.
793 bool isParameterPack() const;
795 /// \brief returns true if this declaration is a template
796 bool isTemplateDecl() const;
798 /// \brief Whether this declaration is a function or function template.
799 bool isFunctionOrFunctionTemplate() const;
801 /// \brief Changes the namespace of this declaration to reflect that it's
802 /// the object of a friend declaration.
804 /// These declarations appear in the lexical context of the friending
805 /// class, but in the semantic context of the actual entity. This property
806 /// applies only to a specific decl object; other redeclarations of the
807 /// same entity may not (and probably don't) share this property.
808 void setObjectOfFriendDecl(bool PreviouslyDeclared) {
809 unsigned OldNS = IdentifierNamespace;
810 assert((OldNS & (IDNS_Tag | IDNS_Ordinary |
811 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
812 "namespace includes neither ordinary nor tag");
813 assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |
814 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
815 "namespace includes other than ordinary or tag");
817 IdentifierNamespace = 0;
818 if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
819 IdentifierNamespace |= IDNS_TagFriend;
820 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Tag | IDNS_Type;
823 if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend)) {
824 IdentifierNamespace |= IDNS_OrdinaryFriend;
825 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Ordinary;
829 enum FriendObjectKind {
830 FOK_None, // not a friend object
831 FOK_Declared, // a friend of a previously-declared entity
832 FOK_Undeclared // a friend of a previously-undeclared entity
835 /// \brief Determines whether this declaration is the object of a
836 /// friend declaration and, if so, what kind.
838 /// There is currently no direct way to find the associated FriendDecl.
839 FriendObjectKind getFriendObjectKind() const {
841 = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
842 if (!mask) return FOK_None;
843 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ?
844 FOK_Declared : FOK_Undeclared);
847 /// Specifies that this declaration is a C++ overloaded non-member.
848 void setNonMemberOperator() {
849 assert(getKind() == Function || getKind() == FunctionTemplate);
850 assert((IdentifierNamespace & IDNS_Ordinary) &&
851 "visible non-member operators should be in ordinary namespace");
852 IdentifierNamespace |= IDNS_NonMemberOperator;
855 static bool classofKind(Kind K) { return true; }
856 static DeclContext *castToDeclContext(const Decl *);
857 static Decl *castFromDeclContext(const DeclContext *);
859 void print(raw_ostream &Out, unsigned Indentation = 0,
860 bool PrintInstantiation = false) const;
861 void print(raw_ostream &Out, const PrintingPolicy &Policy,
862 unsigned Indentation = 0, bool PrintInstantiation = false) const;
863 static void printGroup(Decl** Begin, unsigned NumDecls,
864 raw_ostream &Out, const PrintingPolicy &Policy,
865 unsigned Indentation = 0);
866 // Debuggers don't usually respect default arguments.
867 LLVM_ATTRIBUTE_USED void dump() const;
868 // Same as dump(), but forces color printing.
869 LLVM_ATTRIBUTE_USED void dumpColor() const;
870 void dump(raw_ostream &Out) const;
871 // Debuggers don't usually respect default arguments.
872 LLVM_ATTRIBUTE_USED void dumpXML() const;
873 void dumpXML(raw_ostream &OS) const;
876 void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
877 void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
881 ASTMutationListener *getASTMutationListener() const;
884 /// \brief Determine whether two declarations declare the same entity.
885 inline bool declaresSameEntity(const Decl *D1, const Decl *D2) {
892 return D1->getCanonicalDecl() == D2->getCanonicalDecl();
895 /// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
896 /// doing something to a specific decl.
897 class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
903 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
904 SourceManager &sm, const char *Msg)
905 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
907 virtual void print(raw_ostream &OS) const;
910 typedef llvm::MutableArrayRef<NamedDecl*> DeclContextLookupResult;
912 typedef ArrayRef<NamedDecl *> DeclContextLookupConstResult;
914 /// DeclContext - This is used only as base class of specific decl types that
915 /// can act as declaration contexts. These decls are (only the top classes
916 /// that directly derive from DeclContext are mentioned, not their subclasses):
918 /// TranslationUnitDecl
923 /// ObjCContainerDecl
928 /// DeclKind - This indicates which class this is.
929 unsigned DeclKind : 8;
931 /// \brief Whether this declaration context also has some external
932 /// storage that contains additional declarations that are lexically
933 /// part of this context.
934 mutable bool ExternalLexicalStorage : 1;
936 /// \brief Whether this declaration context also has some external
937 /// storage that contains additional declarations that are visible
939 mutable bool ExternalVisibleStorage : 1;
941 /// \brief Whether this declaration context has had external visible
942 /// storage added since the last lookup. In this case, \c LookupPtr's
943 /// invariant may not hold and needs to be fixed before we perform
945 mutable bool NeedToReconcileExternalVisibleStorage : 1;
947 /// \brief Pointer to the data structure used to lookup declarations
948 /// within this context (or a DependentStoredDeclsMap if this is a
949 /// dependent context), and a bool indicating whether we have lazily
950 /// omitted any declarations from the map. We maintain the invariant
951 /// that, if the map contains an entry for a DeclarationName (and we
952 /// haven't lazily omitted anything), then it contains all relevant
953 /// entries for that name.
954 mutable llvm::PointerIntPair<StoredDeclsMap*, 1, bool> LookupPtr;
957 /// FirstDecl - The first declaration stored within this declaration
959 mutable Decl *FirstDecl;
961 /// LastDecl - The last declaration stored within this declaration
962 /// context. FIXME: We could probably cache this value somewhere
963 /// outside of the DeclContext, to reduce the size of DeclContext by
965 mutable Decl *LastDecl;
967 friend class ExternalASTSource;
968 friend class ASTWriter;
970 /// \brief Build up a chain of declarations.
972 /// \returns the first/last pair of declarations.
973 static std::pair<Decl *, Decl *>
974 BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded);
976 DeclContext(Decl::Kind K)
977 : DeclKind(K), ExternalLexicalStorage(false),
978 ExternalVisibleStorage(false),
979 NeedToReconcileExternalVisibleStorage(false), LookupPtr(0, false),
980 FirstDecl(0), LastDecl(0) {}
985 Decl::Kind getDeclKind() const {
986 return static_cast<Decl::Kind>(DeclKind);
988 const char *getDeclKindName() const;
990 /// getParent - Returns the containing DeclContext.
991 DeclContext *getParent() {
992 return cast<Decl>(this)->getDeclContext();
994 const DeclContext *getParent() const {
995 return const_cast<DeclContext*>(this)->getParent();
998 /// getLexicalParent - Returns the containing lexical DeclContext. May be
999 /// different from getParent, e.g.:
1004 /// struct A::S {}; // getParent() == namespace 'A'
1005 /// // getLexicalParent() == translation unit
1007 DeclContext *getLexicalParent() {
1008 return cast<Decl>(this)->getLexicalDeclContext();
1010 const DeclContext *getLexicalParent() const {
1011 return const_cast<DeclContext*>(this)->getLexicalParent();
1014 DeclContext *getLookupParent();
1016 const DeclContext *getLookupParent() const {
1017 return const_cast<DeclContext*>(this)->getLookupParent();
1020 ASTContext &getParentASTContext() const {
1021 return cast<Decl>(this)->getASTContext();
1024 bool isClosure() const {
1025 return DeclKind == Decl::Block;
1028 bool isObjCContainer() const {
1030 case Decl::ObjCCategory:
1031 case Decl::ObjCCategoryImpl:
1032 case Decl::ObjCImplementation:
1033 case Decl::ObjCInterface:
1034 case Decl::ObjCProtocol:
1040 bool isFunctionOrMethod() const {
1043 case Decl::ObjCMethod:
1046 return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction;
1050 bool isFileContext() const {
1051 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
1054 bool isTranslationUnit() const {
1055 return DeclKind == Decl::TranslationUnit;
1058 bool isRecord() const {
1059 return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord;
1062 bool isNamespace() const {
1063 return DeclKind == Decl::Namespace;
1066 bool isInlineNamespace() const;
1068 /// \brief Determines whether this context is dependent on a
1069 /// template parameter.
1070 bool isDependentContext() const;
1072 /// isTransparentContext - Determines whether this context is a
1073 /// "transparent" context, meaning that the members declared in this
1074 /// context are semantically declared in the nearest enclosing
1075 /// non-transparent (opaque) context but are lexically declared in
1076 /// this context. For example, consider the enumerators of an
1077 /// enumeration type:
1083 /// Here, E is a transparent context, so its enumerator (Val1) will
1084 /// appear (semantically) that it is in the same context of E.
1085 /// Examples of transparent contexts include: enumerations (except for
1086 /// C++0x scoped enums), and C++ linkage specifications.
1087 bool isTransparentContext() const;
1089 /// \brief Determines whether this context is, or is nested within,
1090 /// a C++ extern "C" linkage spec.
1091 bool isExternCContext() const;
1093 /// \brief Determines whether this context is, or is nested within,
1094 /// a C++ extern "C++" linkage spec.
1095 bool isExternCXXContext() const;
1097 /// \brief Determine whether this declaration context is equivalent
1098 /// to the declaration context DC.
1099 bool Equals(const DeclContext *DC) const {
1100 return DC && this->getPrimaryContext() == DC->getPrimaryContext();
1103 /// \brief Determine whether this declaration context encloses the
1104 /// declaration context DC.
1105 bool Encloses(const DeclContext *DC) const;
1107 /// \brief Find the nearest non-closure ancestor of this context,
1108 /// i.e. the innermost semantic parent of this context which is not
1109 /// a closure. A context may be its own non-closure ancestor.
1110 DeclContext *getNonClosureAncestor();
1111 const DeclContext *getNonClosureAncestor() const {
1112 return const_cast<DeclContext*>(this)->getNonClosureAncestor();
1115 /// getPrimaryContext - There may be many different
1116 /// declarations of the same entity (including forward declarations
1117 /// of classes, multiple definitions of namespaces, etc.), each with
1118 /// a different set of declarations. This routine returns the
1119 /// "primary" DeclContext structure, which will contain the
1120 /// information needed to perform name lookup into this context.
1121 DeclContext *getPrimaryContext();
1122 const DeclContext *getPrimaryContext() const {
1123 return const_cast<DeclContext*>(this)->getPrimaryContext();
1126 /// getRedeclContext - Retrieve the context in which an entity conflicts with
1127 /// other entities of the same name, or where it is a redeclaration if the
1128 /// two entities are compatible. This skips through transparent contexts.
1129 DeclContext *getRedeclContext();
1130 const DeclContext *getRedeclContext() const {
1131 return const_cast<DeclContext *>(this)->getRedeclContext();
1134 /// \brief Retrieve the nearest enclosing namespace context.
1135 DeclContext *getEnclosingNamespaceContext();
1136 const DeclContext *getEnclosingNamespaceContext() const {
1137 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
1140 /// \brief Test if this context is part of the enclosing namespace set of
1141 /// the context NS, as defined in C++0x [namespace.def]p9. If either context
1142 /// isn't a namespace, this is equivalent to Equals().
1144 /// The enclosing namespace set of a namespace is the namespace and, if it is
1145 /// inline, its enclosing namespace, recursively.
1146 bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
1148 /// \brief Collects all of the declaration contexts that are semantically
1149 /// connected to this declaration context.
1151 /// For declaration contexts that have multiple semantically connected but
1152 /// syntactically distinct contexts, such as C++ namespaces, this routine
1153 /// retrieves the complete set of such declaration contexts in source order.
1154 /// For example, given:
1165 /// The \c Contexts parameter will contain both definitions of N.
1167 /// \param Contexts Will be cleared and set to the set of declaration
1168 /// contexts that are semanticaly connected to this declaration context,
1169 /// in source order, including this context (which may be the only result,
1170 /// for non-namespace contexts).
1171 void collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts);
1173 /// decl_iterator - Iterates through the declarations stored
1174 /// within this context.
1175 class decl_iterator {
1176 /// Current - The current declaration.
1180 typedef Decl *value_type;
1181 typedef const value_type &reference;
1182 typedef const value_type *pointer;
1183 typedef std::forward_iterator_tag iterator_category;
1184 typedef std::ptrdiff_t difference_type;
1186 decl_iterator() : Current(0) { }
1187 explicit decl_iterator(Decl *C) : Current(C) { }
1189 reference operator*() const { return Current; }
1190 // This doesn't meet the iterator requirements, but it's convenient
1191 value_type operator->() const { return Current; }
1193 decl_iterator& operator++() {
1194 Current = Current->getNextDeclInContext();
1198 decl_iterator operator++(int) {
1199 decl_iterator tmp(*this);
1204 friend bool operator==(decl_iterator x, decl_iterator y) {
1205 return x.Current == y.Current;
1207 friend bool operator!=(decl_iterator x, decl_iterator y) {
1208 return x.Current != y.Current;
1212 /// decls_begin/decls_end - Iterate over the declarations stored in
1214 decl_iterator decls_begin() const;
1215 decl_iterator decls_end() const { return decl_iterator(); }
1216 bool decls_empty() const;
1218 /// noload_decls_begin/end - Iterate over the declarations stored in this
1219 /// context that are currently loaded; don't attempt to retrieve anything
1220 /// from an external source.
1221 decl_iterator noload_decls_begin() const;
1222 decl_iterator noload_decls_end() const { return decl_iterator(); }
1224 /// specific_decl_iterator - Iterates over a subrange of
1225 /// declarations stored in a DeclContext, providing only those that
1226 /// are of type SpecificDecl (or a class derived from it). This
1227 /// iterator is used, for example, to provide iteration over just
1228 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
1229 template<typename SpecificDecl>
1230 class specific_decl_iterator {
1231 /// Current - The current, underlying declaration iterator, which
1232 /// will either be NULL or will point to a declaration of
1233 /// type SpecificDecl.
1234 DeclContext::decl_iterator Current;
1236 /// SkipToNextDecl - Advances the current position up to the next
1237 /// declaration of type SpecificDecl that also meets the criteria
1238 /// required by Acceptable.
1239 void SkipToNextDecl() {
1240 while (*Current && !isa<SpecificDecl>(*Current))
1245 typedef SpecificDecl *value_type;
1246 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1247 // type) if we ever have a need for them.
1248 typedef void reference;
1249 typedef void pointer;
1250 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1252 typedef std::forward_iterator_tag iterator_category;
1254 specific_decl_iterator() : Current() { }
1256 /// specific_decl_iterator - Construct a new iterator over a
1257 /// subset of the declarations the range [C,
1258 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1259 /// member function of SpecificDecl that should return true for
1260 /// all of the SpecificDecl instances that will be in the subset
1261 /// of iterators. For example, if you want Objective-C instance
1262 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1263 /// &ObjCMethodDecl::isInstanceMethod.
1264 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1268 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1269 // This doesn't meet the iterator requirements, but it's convenient
1270 value_type operator->() const { return **this; }
1272 specific_decl_iterator& operator++() {
1278 specific_decl_iterator operator++(int) {
1279 specific_decl_iterator tmp(*this);
1284 friend bool operator==(const specific_decl_iterator& x,
1285 const specific_decl_iterator& y) {
1286 return x.Current == y.Current;
1289 friend bool operator!=(const specific_decl_iterator& x,
1290 const specific_decl_iterator& y) {
1291 return x.Current != y.Current;
1295 /// \brief Iterates over a filtered subrange of declarations stored
1296 /// in a DeclContext.
1298 /// This iterator visits only those declarations that are of type
1299 /// SpecificDecl (or a class derived from it) and that meet some
1300 /// additional run-time criteria. This iterator is used, for
1301 /// example, to provide access to the instance methods within an
1302 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
1303 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
1304 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
1305 class filtered_decl_iterator {
1306 /// Current - The current, underlying declaration iterator, which
1307 /// will either be NULL or will point to a declaration of
1308 /// type SpecificDecl.
1309 DeclContext::decl_iterator Current;
1311 /// SkipToNextDecl - Advances the current position up to the next
1312 /// declaration of type SpecificDecl that also meets the criteria
1313 /// required by Acceptable.
1314 void SkipToNextDecl() {
1316 (!isa<SpecificDecl>(*Current) ||
1317 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
1322 typedef SpecificDecl *value_type;
1323 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1324 // type) if we ever have a need for them.
1325 typedef void reference;
1326 typedef void pointer;
1327 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1329 typedef std::forward_iterator_tag iterator_category;
1331 filtered_decl_iterator() : Current() { }
1333 /// filtered_decl_iterator - Construct a new iterator over a
1334 /// subset of the declarations the range [C,
1335 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1336 /// member function of SpecificDecl that should return true for
1337 /// all of the SpecificDecl instances that will be in the subset
1338 /// of iterators. For example, if you want Objective-C instance
1339 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1340 /// &ObjCMethodDecl::isInstanceMethod.
1341 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1345 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1346 value_type operator->() const { return cast<SpecificDecl>(*Current); }
1348 filtered_decl_iterator& operator++() {
1354 filtered_decl_iterator operator++(int) {
1355 filtered_decl_iterator tmp(*this);
1360 friend bool operator==(const filtered_decl_iterator& x,
1361 const filtered_decl_iterator& y) {
1362 return x.Current == y.Current;
1365 friend bool operator!=(const filtered_decl_iterator& x,
1366 const filtered_decl_iterator& y) {
1367 return x.Current != y.Current;
1371 /// @brief Add the declaration D into this context.
1373 /// This routine should be invoked when the declaration D has first
1374 /// been declared, to place D into the context where it was
1375 /// (lexically) defined. Every declaration must be added to one
1376 /// (and only one!) context, where it can be visited via
1377 /// [decls_begin(), decls_end()). Once a declaration has been added
1378 /// to its lexical context, the corresponding DeclContext owns the
1381 /// If D is also a NamedDecl, it will be made visible within its
1382 /// semantic context via makeDeclVisibleInContext.
1383 void addDecl(Decl *D);
1385 /// @brief Add the declaration D into this context, but suppress
1386 /// searches for external declarations with the same name.
1388 /// Although analogous in function to addDecl, this removes an
1389 /// important check. This is only useful if the Decl is being
1390 /// added in response to an external search; in all other cases,
1391 /// addDecl() is the right function to use.
1392 /// See the ASTImporter for use cases.
1393 void addDeclInternal(Decl *D);
1395 /// @brief Add the declaration D to this context without modifying
1396 /// any lookup tables.
1398 /// This is useful for some operations in dependent contexts where
1399 /// the semantic context might not be dependent; this basically
1400 /// only happens with friends.
1401 void addHiddenDecl(Decl *D);
1403 /// @brief Removes a declaration from this context.
1404 void removeDecl(Decl *D);
1406 /// lookup_iterator - An iterator that provides access to the results
1407 /// of looking up a name within this context.
1408 typedef NamedDecl **lookup_iterator;
1410 /// lookup_const_iterator - An iterator that provides non-mutable
1411 /// access to the results of lookup up a name within this context.
1412 typedef NamedDecl * const * lookup_const_iterator;
1414 typedef DeclContextLookupResult lookup_result;
1415 typedef DeclContextLookupConstResult lookup_const_result;
1417 /// lookup - Find the declarations (if any) with the given Name in
1418 /// this context. Returns a range of iterators that contains all of
1419 /// the declarations with this name, with object, function, member,
1420 /// and enumerator names preceding any tag name. Note that this
1421 /// routine will not look into parent contexts.
1422 lookup_result lookup(DeclarationName Name);
1423 lookup_const_result lookup(DeclarationName Name) const {
1424 return const_cast<DeclContext*>(this)->lookup(Name);
1427 /// \brief A simplistic name lookup mechanism that performs name lookup
1428 /// into this declaration context without consulting the external source.
1430 /// This function should almost never be used, because it subverts the
1431 /// usual relationship between a DeclContext and the external source.
1432 /// See the ASTImporter for the (few, but important) use cases.
1433 void localUncachedLookup(DeclarationName Name,
1434 SmallVectorImpl<NamedDecl *> &Results);
1436 /// @brief Makes a declaration visible within this context.
1438 /// This routine makes the declaration D visible to name lookup
1439 /// within this context and, if this is a transparent context,
1440 /// within its parent contexts up to the first enclosing
1441 /// non-transparent context. Making a declaration visible within a
1442 /// context does not transfer ownership of a declaration, and a
1443 /// declaration can be visible in many contexts that aren't its
1444 /// lexical context.
1446 /// If D is a redeclaration of an existing declaration that is
1447 /// visible from this context, as determined by
1448 /// NamedDecl::declarationReplaces, the previous declaration will be
1449 /// replaced with D.
1450 void makeDeclVisibleInContext(NamedDecl *D);
1452 /// all_lookups_iterator - An iterator that provides a view over the results
1453 /// of looking up every possible name.
1454 class all_lookups_iterator;
1456 all_lookups_iterator lookups_begin() const;
1458 all_lookups_iterator lookups_end() const;
1460 /// udir_iterator - Iterates through the using-directives stored
1461 /// within this context.
1462 typedef UsingDirectiveDecl * const * udir_iterator;
1464 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
1466 udir_iterator_range getUsingDirectives() const;
1468 udir_iterator using_directives_begin() const {
1469 return getUsingDirectives().first;
1472 udir_iterator using_directives_end() const {
1473 return getUsingDirectives().second;
1476 // These are all defined in DependentDiagnostic.h.
1477 class ddiag_iterator;
1478 inline ddiag_iterator ddiag_begin() const;
1479 inline ddiag_iterator ddiag_end() const;
1481 // Low-level accessors
1483 /// \brief Mark the lookup table as needing to be built. This should be
1484 /// used only if setHasExternalLexicalStorage() has been called on any
1485 /// decl context for which this is the primary context.
1486 void setMustBuildLookupTable() {
1487 LookupPtr.setInt(true);
1490 /// \brief Retrieve the internal representation of the lookup structure.
1491 /// This may omit some names if we are lazily building the structure.
1492 StoredDeclsMap *getLookupPtr() const { return LookupPtr.getPointer(); }
1494 /// \brief Ensure the lookup structure is fully-built and return it.
1495 StoredDeclsMap *buildLookup();
1497 /// \brief Whether this DeclContext has external storage containing
1498 /// additional declarations that are lexically in this context.
1499 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
1501 /// \brief State whether this DeclContext has external storage for
1502 /// declarations lexically in this context.
1503 void setHasExternalLexicalStorage(bool ES = true) {
1504 ExternalLexicalStorage = ES;
1507 /// \brief Whether this DeclContext has external storage containing
1508 /// additional declarations that are visible in this context.
1509 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
1511 /// \brief State whether this DeclContext has external storage for
1512 /// declarations visible in this context.
1513 void setHasExternalVisibleStorage(bool ES = true) {
1514 ExternalVisibleStorage = ES;
1515 if (ES && LookupPtr.getPointer())
1516 NeedToReconcileExternalVisibleStorage = true;
1519 /// \brief Determine whether the given declaration is stored in the list of
1520 /// declarations lexically within this context.
1521 bool isDeclInLexicalTraversal(const Decl *D) const {
1522 return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl ||
1526 static bool classof(const Decl *D);
1527 static bool classof(const DeclContext *D) { return true; }
1529 LLVM_ATTRIBUTE_USED void dumpDeclContext() const;
1532 void reconcileExternalVisibleStorage();
1533 void LoadLexicalDeclsFromExternalStorage() const;
1535 /// @brief Makes a declaration visible within this context, but
1536 /// suppresses searches for external declarations with the same
1539 /// Analogous to makeDeclVisibleInContext, but for the exclusive
1540 /// use of addDeclInternal().
1541 void makeDeclVisibleInContextInternal(NamedDecl *D);
1543 friend class DependentDiagnostic;
1544 StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
1546 void buildLookupImpl(DeclContext *DCtx);
1547 void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1548 bool Rediscoverable);
1549 void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal);
1552 inline bool Decl::isTemplateParameter() const {
1553 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
1554 getKind() == TemplateTemplateParm;
1557 // Specialization selected when ToTy is not a known subclass of DeclContext.
1558 template <class ToTy,
1559 bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value>
1560 struct cast_convert_decl_context {
1561 static const ToTy *doit(const DeclContext *Val) {
1562 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
1565 static ToTy *doit(DeclContext *Val) {
1566 return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
1570 // Specialization selected when ToTy is a known subclass of DeclContext.
1571 template <class ToTy>
1572 struct cast_convert_decl_context<ToTy, true> {
1573 static const ToTy *doit(const DeclContext *Val) {
1574 return static_cast<const ToTy*>(Val);
1577 static ToTy *doit(DeclContext *Val) {
1578 return static_cast<ToTy*>(Val);
1587 /// isa<T>(DeclContext*)
1588 template <typename To>
1589 struct isa_impl<To, ::clang::DeclContext> {
1590 static bool doit(const ::clang::DeclContext &Val) {
1591 return To::classofKind(Val.getDeclKind());
1595 /// cast<T>(DeclContext*)
1596 template<class ToTy>
1597 struct cast_convert_val<ToTy,
1598 const ::clang::DeclContext,const ::clang::DeclContext> {
1599 static const ToTy &doit(const ::clang::DeclContext &Val) {
1600 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1603 template<class ToTy>
1604 struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
1605 static ToTy &doit(::clang::DeclContext &Val) {
1606 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1609 template<class ToTy>
1610 struct cast_convert_val<ToTy,
1611 const ::clang::DeclContext*, const ::clang::DeclContext*> {
1612 static const ToTy *doit(const ::clang::DeclContext *Val) {
1613 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1616 template<class ToTy>
1617 struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
1618 static ToTy *doit(::clang::DeclContext *Val) {
1619 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1623 /// Implement cast_convert_val for Decl -> DeclContext conversions.
1624 template<class FromTy>
1625 struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
1626 static ::clang::DeclContext &doit(const FromTy &Val) {
1627 return *FromTy::castToDeclContext(&Val);
1631 template<class FromTy>
1632 struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
1633 static ::clang::DeclContext *doit(const FromTy *Val) {
1634 return FromTy::castToDeclContext(Val);
1638 template<class FromTy>
1639 struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
1640 static const ::clang::DeclContext &doit(const FromTy &Val) {
1641 return *FromTy::castToDeclContext(&Val);
1645 template<class FromTy>
1646 struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
1647 static const ::clang::DeclContext *doit(const FromTy *Val) {
1648 return FromTy::castToDeclContext(Val);
1652 } // end namespace llvm