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/Attr.h"
18 #include "clang/AST/DeclarationName.h"
19 #include "clang/AST/Type.h"
20 #include "clang/Basic/Specifiers.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/PrettyStackTrace.h"
27 class TranslationUnitDecl;
29 class UsingDirectiveDecl;
35 class ObjCContainerDecl;
36 class ObjCInterfaceDecl;
37 class ObjCCategoryDecl;
38 class ObjCProtocolDecl;
39 class ObjCImplementationDecl;
40 class ObjCCategoryImplDecl;
42 class LinkageSpecDecl;
44 class DeclarationName;
47 class DependentDiagnostic;
48 class ASTMutationListener;
52 // DeclContext* is only 4-byte aligned on 32-bit systems.
54 class PointerLikeTypeTraits<clang::DeclContext*> {
55 typedef clang::DeclContext* PT;
57 static inline void *getAsVoidPointer(PT P) { return P; }
58 static inline PT getFromVoidPointer(void *P) {
59 return static_cast<PT>(P);
61 enum { NumLowBitsAvailable = 2 };
67 /// \brief Captures the result of checking the availability of a
69 enum AvailabilityResult {
76 /// Decl - This represents one declaration (or definition), e.g. a variable,
77 /// typedef, function, struct, etc.
81 /// \brief Lists the kind of concrete classes of Decl.
83 #define DECL(DERIVED, BASE) DERIVED,
84 #define ABSTRACT_DECL(DECL)
85 #define DECL_RANGE(BASE, START, END) \
86 first##BASE = START, last##BASE = END,
87 #define LAST_DECL_RANGE(BASE, START, END) \
88 first##BASE = START, last##BASE = END
89 #include "clang/AST/DeclNodes.inc"
92 /// \brief A placeholder type used to construct an empty shell of a
93 /// decl-derived type that will be filled in later (e.g., by some
94 /// deserialization method).
95 struct EmptyShell { };
97 /// IdentifierNamespace - The different namespaces in which
98 /// declarations may appear. According to C99 6.2.3, there are
99 /// four namespaces, labels, tags, members and ordinary
100 /// identifiers. C++ describes lookup completely differently:
101 /// certain lookups merely "ignore" certain kinds of declarations,
102 /// usually based on whether the declaration is of a type, etc.
104 /// These are meant as bitmasks, so that searches in
105 /// C++ can look into the "tag" namespace during ordinary lookup.
107 /// Decl currently provides 15 bits of IDNS bits.
108 enum IdentifierNamespace {
109 /// Labels, declared with 'x:' and referenced with 'goto x'.
112 /// Tags, declared with 'struct foo;' and referenced with
113 /// 'struct foo'. All tags are also types. This is what
114 /// elaborated-type-specifiers look for in C.
117 /// Types, declared with 'struct foo', typedefs, etc.
118 /// This is what elaborated-type-specifiers look for in C++,
119 /// but note that it's ill-formed to find a non-tag.
122 /// Members, declared with object declarations within tag
123 /// definitions. In C, these can only be found by "qualified"
124 /// lookup in member expressions. In C++, they're found by
126 IDNS_Member = 0x0008,
128 /// Namespaces, declared with 'namespace foo {}'.
129 /// Lookup for nested-name-specifiers find these.
130 IDNS_Namespace = 0x0010,
132 /// Ordinary names. In C, everything that's not a label, tag,
133 /// or member ends up here.
134 IDNS_Ordinary = 0x0020,
136 /// Objective C @protocol.
137 IDNS_ObjCProtocol = 0x0040,
139 /// This declaration is a friend function. A friend function
140 /// declaration is always in this namespace but may also be in
141 /// IDNS_Ordinary if it was previously declared.
142 IDNS_OrdinaryFriend = 0x0080,
144 /// This declaration is a friend class. A friend class
145 /// declaration is always in this namespace but may also be in
146 /// IDNS_Tag|IDNS_Type if it was previously declared.
147 IDNS_TagFriend = 0x0100,
149 /// This declaration is a using declaration. A using declaration
150 /// *introduces* a number of other declarations into the current
151 /// scope, and those declarations use the IDNS of their targets,
152 /// but the actual using declarations go in this namespace.
155 /// This declaration is a C++ operator declared in a non-class
156 /// context. All such operators are also in IDNS_Ordinary.
157 /// C++ lexical operator lookup looks for these.
158 IDNS_NonMemberOperator = 0x0400
161 /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
162 /// parameter types in method declarations. Other than remembering
163 /// them and mangling them into the method's signature string, these
164 /// are ignored by the compiler; they are consumed by certain
165 /// remote-messaging frameworks.
167 /// in, inout, and out are mutually exclusive and apply only to
168 /// method parameters. bycopy and byref are mutually exclusive and
169 /// apply only to method parameters (?). oneway applies only to
170 /// results. All of these expect their corresponding parameter to
171 /// have a particular type. None of this is currently enforced by
174 /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
175 enum ObjCDeclQualifier {
180 OBJC_TQ_Bycopy = 0x8,
181 OBJC_TQ_Byref = 0x10,
182 OBJC_TQ_Oneway = 0x20
186 // Enumeration values used in the bits stored in NextInContextAndBits.
188 /// \brief Whether this declaration is a top-level declaration (function,
189 /// global variable, etc.) that is lexically inside an objc container
191 TopLevelDeclInObjCContainerFlag = 0x01,
193 /// \brief Whether this declaration is private to the module in which it was
195 ModulePrivateFlag = 0x02
198 /// \brief The next declaration within the same lexical
199 /// DeclContext. These pointers form the linked list that is
200 /// traversed via DeclContext's decls_begin()/decls_end().
202 /// The extra two bits are used for the TopLevelDeclInObjCContainer and
203 /// ModulePrivate bits.
204 llvm::PointerIntPair<Decl *, 2, unsigned> NextInContextAndBits;
207 friend class DeclContext;
210 DeclContext *SemanticDC;
211 DeclContext *LexicalDC;
215 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
216 /// For declarations that don't contain C++ scope specifiers, it contains
217 /// the DeclContext where the Decl was declared.
218 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
219 /// with the context where it semantically belongs (SemanticDC) and the
220 /// context where it was lexically declared (LexicalDC).
224 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
226 /// void A::f(); // SemanticDC == namespace 'A'
227 /// // LexicalDC == global namespace
228 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
230 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
231 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
232 inline MultipleDC *getMultipleDC() const {
233 return DeclCtx.get<MultipleDC*>();
235 inline DeclContext *getSemanticDC() const {
236 return DeclCtx.get<DeclContext*>();
239 /// Loc - The location of this decl.
242 /// DeclKind - This indicates which class this is.
243 unsigned DeclKind : 8;
245 /// InvalidDecl - This indicates a semantic error occurred.
246 unsigned InvalidDecl : 1;
248 /// HasAttrs - This indicates whether the decl has attributes or not.
249 unsigned HasAttrs : 1;
251 /// Implicit - Whether this declaration was implicitly generated by
252 /// the implementation rather than explicitly written by the user.
253 unsigned Implicit : 1;
255 /// \brief Whether this declaration was "used", meaning that a definition is
259 /// \brief Whether this declaration was "referenced".
260 /// The difference with 'Used' is whether the reference appears in a
261 /// evaluated context or not, e.g. functions used in uninstantiated templates
262 /// are regarded as "referenced" but not "used".
263 unsigned Referenced : 1;
265 /// \brief Whether statistic collection is enabled.
266 static bool StatisticsEnabled;
269 /// Access - Used by C++ decls for the access specifier.
270 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
272 friend class CXXClassMemberWrapper;
274 /// \brief Whether this declaration was loaded from an AST file.
275 unsigned FromASTFile : 1;
277 /// \brief Whether this declaration is hidden from normal name lookup, e.g.,
278 /// because it is was loaded from an AST file is either module-private or
279 /// because its submodule has not been made visible.
282 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
283 unsigned IdentifierNamespace : 12;
285 /// \brief Whether the \c CachedLinkage field is active.
287 /// This field is only valid for NamedDecls subclasses.
288 mutable unsigned HasCachedLinkage : 1;
290 /// \brief If \c HasCachedLinkage, the linkage of this declaration.
292 /// This field is only valid for NamedDecls subclasses.
293 mutable unsigned CachedLinkage : 2;
295 friend class ASTDeclWriter;
296 friend class ASTDeclReader;
297 friend class ASTReader;
300 void CheckAccessDeclContext() const;
304 Decl(Kind DK, DeclContext *DC, SourceLocation L)
305 : NextInContextAndBits(), DeclCtx(DC),
306 Loc(L), DeclKind(DK), InvalidDecl(0),
307 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
308 Access(AS_none), FromASTFile(0), Hidden(0),
309 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
312 if (StatisticsEnabled) add(DK);
315 Decl(Kind DK, EmptyShell Empty)
316 : NextInContextAndBits(), DeclKind(DK), InvalidDecl(0),
317 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
318 Access(AS_none), FromASTFile(0), Hidden(0),
319 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
322 if (StatisticsEnabled) add(DK);
327 /// \brief Allocate memory for a deserialized declaration.
329 /// This routine must be used to allocate memory for any declaration that is
330 /// deserialized from a module file.
332 /// \param Context The context in which we will allocate memory.
333 /// \param ID The global ID of the deserialized declaration.
334 /// \param Size The size of the allocated object.
335 static void *AllocateDeserializedDecl(const ASTContext &Context,
341 /// \brief Source range that this declaration covers.
342 virtual SourceRange getSourceRange() const LLVM_READONLY {
343 return SourceRange(getLocation(), getLocation());
345 SourceLocation getLocStart() const LLVM_READONLY {
346 return getSourceRange().getBegin();
348 SourceLocation getLocEnd() const LLVM_READONLY {
349 return getSourceRange().getEnd();
352 SourceLocation getLocation() const { return Loc; }
353 void setLocation(SourceLocation L) { Loc = L; }
355 Kind getKind() const { return static_cast<Kind>(DeclKind); }
356 const char *getDeclKindName() const;
358 Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); }
359 const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();}
361 DeclContext *getDeclContext() {
363 return getSemanticDC();
364 return getMultipleDC()->SemanticDC;
366 const DeclContext *getDeclContext() const {
367 return const_cast<Decl*>(this)->getDeclContext();
370 /// Finds the innermost non-closure context of this declaration.
371 /// That is, walk out the DeclContext chain, skipping any blocks.
372 DeclContext *getNonClosureContext();
373 const DeclContext *getNonClosureContext() const {
374 return const_cast<Decl*>(this)->getNonClosureContext();
377 TranslationUnitDecl *getTranslationUnitDecl();
378 const TranslationUnitDecl *getTranslationUnitDecl() const {
379 return const_cast<Decl*>(this)->getTranslationUnitDecl();
382 bool isInAnonymousNamespace() const;
384 ASTContext &getASTContext() const LLVM_READONLY;
386 void setAccess(AccessSpecifier AS) {
389 CheckAccessDeclContext();
393 AccessSpecifier getAccess() const {
395 CheckAccessDeclContext();
397 return AccessSpecifier(Access);
400 bool hasAttrs() const { return HasAttrs; }
401 void setAttrs(const AttrVec& Attrs) {
402 return setAttrsImpl(Attrs, getASTContext());
404 AttrVec &getAttrs() {
405 return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
407 const AttrVec &getAttrs() const;
408 void swapAttrs(Decl *D);
411 void addAttr(Attr *A) {
413 getAttrs().push_back(A);
415 setAttrs(AttrVec(1, A));
418 typedef AttrVec::const_iterator attr_iterator;
420 // FIXME: Do not rely on iterators having comparable singular values.
421 // Note that this should error out if they do not.
422 attr_iterator attr_begin() const {
423 return hasAttrs() ? getAttrs().begin() : 0;
425 attr_iterator attr_end() const {
426 return hasAttrs() ? getAttrs().end() : 0;
429 template <typename T>
431 if (!HasAttrs) return;
433 AttrVec &Vec = getAttrs();
434 Vec.erase(std::remove_if(Vec.begin(), Vec.end(), isa<T, Attr*>), Vec.end());
440 template <typename T>
441 specific_attr_iterator<T> specific_attr_begin() const {
442 return specific_attr_iterator<T>(attr_begin());
444 template <typename T>
445 specific_attr_iterator<T> specific_attr_end() const {
446 return specific_attr_iterator<T>(attr_end());
449 template<typename T> T *getAttr() const {
450 return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : 0;
452 template<typename T> bool hasAttr() const {
453 return hasAttrs() && hasSpecificAttr<T>(getAttrs());
456 /// getMaxAlignment - return the maximum alignment specified by attributes
457 /// on this decl, 0 if there are none.
458 unsigned getMaxAlignment() const {
459 return hasAttrs() ? getMaxAttrAlignment(getAttrs(), getASTContext()) : 0;
462 /// setInvalidDecl - Indicates the Decl had a semantic error. This
463 /// allows for graceful error recovery.
464 void setInvalidDecl(bool Invalid = true);
465 bool isInvalidDecl() const { return (bool) InvalidDecl; }
467 /// isImplicit - Indicates whether the declaration was implicitly
468 /// generated by the implementation. If false, this declaration
469 /// was written explicitly in the source code.
470 bool isImplicit() const { return Implicit; }
471 void setImplicit(bool I = true) { Implicit = I; }
473 /// \brief Whether this declaration was used, meaning that a definition
476 /// \param CheckUsedAttr When true, also consider the "used" attribute
477 /// (in addition to the "used" bit set by \c setUsed()) when determining
478 /// whether the function is used.
479 bool isUsed(bool CheckUsedAttr = true) const;
481 void setUsed(bool U = true) { Used = U; }
483 /// \brief Whether this declaration was referenced.
484 bool isReferenced() const;
486 void setReferenced(bool R = true) { Referenced = R; }
488 /// \brief Whether this declaration is a top-level declaration (function,
489 /// global variable, etc.) that is lexically inside an objc container
491 bool isTopLevelDeclInObjCContainer() const {
492 return NextInContextAndBits.getInt() & TopLevelDeclInObjCContainerFlag;
495 void setTopLevelDeclInObjCContainer(bool V = true) {
496 unsigned Bits = NextInContextAndBits.getInt();
498 Bits |= TopLevelDeclInObjCContainerFlag;
500 Bits &= ~TopLevelDeclInObjCContainerFlag;
501 NextInContextAndBits.setInt(Bits);
505 /// \brief Whether this declaration was marked as being private to the
506 /// module in which it was defined.
507 bool isModulePrivate() const {
508 return NextInContextAndBits.getInt() & ModulePrivateFlag;
511 /// \brief Specify whether this declaration was marked as being private
512 /// to the module in which it was defined.
513 void setModulePrivate(bool MP = true) {
514 unsigned Bits = NextInContextAndBits.getInt();
516 Bits |= ModulePrivateFlag;
518 Bits &= ~ModulePrivateFlag;
519 NextInContextAndBits.setInt(Bits);
522 /// \brief Set the owning module ID.
523 void setOwningModuleID(unsigned ID) {
524 assert(isFromASTFile() && "Only works on a deserialized declaration");
525 *((unsigned*)this - 2) = ID;
530 /// \brief Determine the availability of the given declaration.
532 /// This routine will determine the most restrictive availability of
533 /// the given declaration (e.g., preferring 'unavailable' to
536 /// \param Message If non-NULL and the result is not \c
537 /// AR_Available, will be set to a (possibly empty) message
538 /// describing why the declaration has not been introduced, is
539 /// deprecated, or is unavailable.
540 AvailabilityResult getAvailability(std::string *Message = 0) const;
542 /// \brief Determine whether this declaration is marked 'deprecated'.
544 /// \param Message If non-NULL and the declaration is deprecated,
545 /// this will be set to the message describing why the declaration
546 /// was deprecated (which may be empty).
547 bool isDeprecated(std::string *Message = 0) const {
548 return getAvailability(Message) == AR_Deprecated;
551 /// \brief Determine whether this declaration is marked 'unavailable'.
553 /// \param Message If non-NULL and the declaration is unavailable,
554 /// this will be set to the message describing why the declaration
555 /// was made unavailable (which may be empty).
556 bool isUnavailable(std::string *Message = 0) const {
557 return getAvailability(Message) == AR_Unavailable;
560 /// \brief Determine whether this is a weak-imported symbol.
562 /// Weak-imported symbols are typically marked with the
563 /// 'weak_import' attribute, but may also be marked with an
564 /// 'availability' attribute where we're targing a platform prior to
565 /// the introduction of this feature.
566 bool isWeakImported() const;
568 /// \brief Determines whether this symbol can be weak-imported,
569 /// e.g., whether it would be well-formed to add the weak_import
572 /// \param IsDefinition Set to \c true to indicate that this
573 /// declaration cannot be weak-imported because it has a definition.
574 bool canBeWeakImported(bool &IsDefinition) const;
576 /// \brief Determine whether this declaration came from an AST file (such as
577 /// a precompiled header or module) rather than having been parsed.
578 bool isFromASTFile() const { return FromASTFile; }
580 /// \brief Retrieve the global declaration ID associated with this
581 /// declaration, which specifies where in the
582 unsigned getGlobalID() const {
584 return *((const unsigned*)this - 1);
588 /// \brief Retrieve the global ID of the module that owns this particular
590 unsigned getOwningModuleID() const {
592 return *((const unsigned*)this - 2);
597 unsigned getIdentifierNamespace() const {
598 return IdentifierNamespace;
600 bool isInIdentifierNamespace(unsigned NS) const {
601 return getIdentifierNamespace() & NS;
603 static unsigned getIdentifierNamespaceForKind(Kind DK);
605 bool hasTagIdentifierNamespace() const {
606 return isTagIdentifierNamespace(getIdentifierNamespace());
608 static bool isTagIdentifierNamespace(unsigned NS) {
609 // TagDecls have Tag and Type set and may also have TagFriend.
610 return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
613 /// getLexicalDeclContext - The declaration context where this Decl was
614 /// lexically declared (LexicalDC). May be different from
615 /// getDeclContext() (SemanticDC).
619 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
621 /// void A::f(); // SemanticDC == namespace 'A'
622 /// // LexicalDC == global namespace
623 DeclContext *getLexicalDeclContext() {
625 return getSemanticDC();
626 return getMultipleDC()->LexicalDC;
628 const DeclContext *getLexicalDeclContext() const {
629 return const_cast<Decl*>(this)->getLexicalDeclContext();
632 virtual bool isOutOfLine() const {
633 return getLexicalDeclContext() != getDeclContext();
636 /// setDeclContext - Set both the semantic and lexical DeclContext
638 void setDeclContext(DeclContext *DC);
640 void setLexicalDeclContext(DeclContext *DC);
642 /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
643 /// scoped decl is defined outside the current function or method. This is
644 /// roughly global variables and functions, but also handles enums (which
645 /// could be defined inside or outside a function etc).
646 bool isDefinedOutsideFunctionOrMethod() const {
647 return getParentFunctionOrMethod() == 0;
650 /// \brief If this decl is defined inside a function/method/block it returns
651 /// the corresponding DeclContext, otherwise it returns null.
652 const DeclContext *getParentFunctionOrMethod() const;
653 DeclContext *getParentFunctionOrMethod() {
654 return const_cast<DeclContext*>(
655 const_cast<const Decl*>(this)->getParentFunctionOrMethod());
658 /// \brief Retrieves the "canonical" declaration of the given declaration.
659 virtual Decl *getCanonicalDecl() { return this; }
660 const Decl *getCanonicalDecl() const {
661 return const_cast<Decl*>(this)->getCanonicalDecl();
664 /// \brief Whether this particular Decl is a canonical one.
665 bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
668 /// \brief Returns the next redeclaration or itself if this is the only decl.
670 /// Decl subclasses that can be redeclared should override this method so that
671 /// Decl::redecl_iterator can iterate over them.
672 virtual Decl *getNextRedeclaration() { return this; }
674 /// \brief Implementation of getPreviousDecl(), to be overridden by any
675 /// subclass that has a redeclaration chain.
676 virtual Decl *getPreviousDeclImpl() { return 0; }
678 /// \brief Implementation of getMostRecentDecl(), to be overridden by any
679 /// subclass that has a redeclaration chain.
680 virtual Decl *getMostRecentDeclImpl() { return this; }
683 /// \brief Iterates through all the redeclarations of the same decl.
684 class redecl_iterator {
685 /// Current - The current declaration.
690 typedef Decl *value_type;
691 typedef const value_type &reference;
692 typedef const value_type *pointer;
693 typedef std::forward_iterator_tag iterator_category;
694 typedef std::ptrdiff_t difference_type;
696 redecl_iterator() : Current(0) { }
697 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
699 reference operator*() const { return Current; }
700 value_type operator->() const { return Current; }
702 redecl_iterator& operator++() {
703 assert(Current && "Advancing while iterator has reached end");
704 // Get either previous decl or latest decl.
705 Decl *Next = Current->getNextRedeclaration();
706 assert(Next && "Should return next redeclaration or itself, never null!");
707 Current = (Next != Starter ? Next : 0);
711 redecl_iterator operator++(int) {
712 redecl_iterator tmp(*this);
717 friend bool operator==(redecl_iterator x, redecl_iterator y) {
718 return x.Current == y.Current;
720 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
721 return x.Current != y.Current;
725 /// \brief Returns iterator for all the redeclarations of the same decl.
726 /// It will iterate at least once (when this decl is the only one).
727 redecl_iterator redecls_begin() const {
728 return redecl_iterator(const_cast<Decl*>(this));
730 redecl_iterator redecls_end() const { return redecl_iterator(); }
732 /// \brief Retrieve the previous declaration that declares the same entity
733 /// as this declaration, or NULL if there is no previous declaration.
734 Decl *getPreviousDecl() { return getPreviousDeclImpl(); }
736 /// \brief Retrieve the most recent declaration that declares the same entity
737 /// as this declaration, or NULL if there is no previous declaration.
738 const Decl *getPreviousDecl() const {
739 return const_cast<Decl *>(this)->getPreviousDeclImpl();
742 /// \brief Retrieve the most recent declaration that declares the same entity
743 /// as this declaration (which may be this declaration).
744 Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); }
746 /// \brief Retrieve the most recent declaration that declares the same entity
747 /// as this declaration (which may be this declaration).
748 const Decl *getMostRecentDecl() const {
749 return const_cast<Decl *>(this)->getMostRecentDeclImpl();
752 /// getBody - If this Decl represents a declaration for a body of code,
753 /// such as a function or method definition, this method returns the
754 /// top-level Stmt* of that body. Otherwise this method returns null.
755 virtual Stmt* getBody() const { return 0; }
757 /// \brief Returns true if this Decl represents a declaration for a body of
758 /// code, such as a function or method definition.
759 virtual bool hasBody() const { return getBody() != 0; }
761 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
762 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
763 SourceLocation getBodyRBrace() const;
765 // global temp stats (until we have a per-module visitor)
766 static void add(Kind k);
767 static void EnableStatistics();
768 static void PrintStats();
770 /// isTemplateParameter - Determines whether this declaration is a
771 /// template parameter.
772 bool isTemplateParameter() const;
774 /// isTemplateParameter - Determines whether this declaration is a
775 /// template parameter pack.
776 bool isTemplateParameterPack() const;
778 /// \brief Whether this declaration is a parameter pack.
779 bool isParameterPack() const;
781 /// \brief returns true if this declaration is a template
782 bool isTemplateDecl() const;
784 /// \brief Whether this declaration is a function or function template.
785 bool isFunctionOrFunctionTemplate() const;
787 /// \brief Changes the namespace of this declaration to reflect that it's
788 /// the object of a friend declaration.
790 /// These declarations appear in the lexical context of the friending
791 /// class, but in the semantic context of the actual entity. This property
792 /// applies only to a specific decl object; other redeclarations of the
793 /// same entity may not (and probably don't) share this property.
794 void setObjectOfFriendDecl(bool PreviouslyDeclared) {
795 unsigned OldNS = IdentifierNamespace;
796 assert((OldNS & (IDNS_Tag | IDNS_Ordinary |
797 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
798 "namespace includes neither ordinary nor tag");
799 assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |
800 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
801 "namespace includes other than ordinary or tag");
803 IdentifierNamespace = 0;
804 if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
805 IdentifierNamespace |= IDNS_TagFriend;
806 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Tag | IDNS_Type;
809 if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend)) {
810 IdentifierNamespace |= IDNS_OrdinaryFriend;
811 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Ordinary;
815 enum FriendObjectKind {
816 FOK_None, // not a friend object
817 FOK_Declared, // a friend of a previously-declared entity
818 FOK_Undeclared // a friend of a previously-undeclared entity
821 /// \brief Determines whether this declaration is the object of a
822 /// friend declaration and, if so, what kind.
824 /// There is currently no direct way to find the associated FriendDecl.
825 FriendObjectKind getFriendObjectKind() const {
827 = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
828 if (!mask) return FOK_None;
829 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ?
830 FOK_Declared : FOK_Undeclared);
833 /// Specifies that this declaration is a C++ overloaded non-member.
834 void setNonMemberOperator() {
835 assert(getKind() == Function || getKind() == FunctionTemplate);
836 assert((IdentifierNamespace & IDNS_Ordinary) &&
837 "visible non-member operators should be in ordinary namespace");
838 IdentifierNamespace |= IDNS_NonMemberOperator;
841 static bool classofKind(Kind K) { return true; }
842 static DeclContext *castToDeclContext(const Decl *);
843 static Decl *castFromDeclContext(const DeclContext *);
845 void print(raw_ostream &Out, unsigned Indentation = 0,
846 bool PrintInstantiation = false) const;
847 void print(raw_ostream &Out, const PrintingPolicy &Policy,
848 unsigned Indentation = 0, bool PrintInstantiation = false) const;
849 static void printGroup(Decl** Begin, unsigned NumDecls,
850 raw_ostream &Out, const PrintingPolicy &Policy,
851 unsigned Indentation = 0);
852 // Debuggers don't usually respect default arguments.
853 LLVM_ATTRIBUTE_USED void dump() const;
854 void dump(raw_ostream &Out) const;
855 // Debuggers don't usually respect default arguments.
856 LLVM_ATTRIBUTE_USED void dumpXML() const;
857 void dumpXML(raw_ostream &OS) const;
860 void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
861 void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
865 ASTMutationListener *getASTMutationListener() const;
868 /// \brief Determine whether two declarations declare the same entity.
869 inline bool declaresSameEntity(const Decl *D1, const Decl *D2) {
876 return D1->getCanonicalDecl() == D2->getCanonicalDecl();
879 /// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
880 /// doing something to a specific decl.
881 class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
887 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
888 SourceManager &sm, const char *Msg)
889 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
891 virtual void print(raw_ostream &OS) const;
894 class DeclContextLookupResult
895 : public std::pair<NamedDecl**,NamedDecl**> {
897 DeclContextLookupResult(NamedDecl **I, NamedDecl **E)
898 : std::pair<NamedDecl**,NamedDecl**>(I, E) {}
899 DeclContextLookupResult()
900 : std::pair<NamedDecl**,NamedDecl**>() {}
902 using std::pair<NamedDecl**,NamedDecl**>::operator=;
905 class DeclContextLookupConstResult
906 : public std::pair<NamedDecl*const*, NamedDecl*const*> {
908 DeclContextLookupConstResult(std::pair<NamedDecl**,NamedDecl**> R)
909 : std::pair<NamedDecl*const*, NamedDecl*const*>(R) {}
910 DeclContextLookupConstResult(NamedDecl * const *I, NamedDecl * const *E)
911 : std::pair<NamedDecl*const*, NamedDecl*const*>(I, E) {}
912 DeclContextLookupConstResult()
913 : std::pair<NamedDecl*const*, NamedDecl*const*>() {}
915 using std::pair<NamedDecl*const*,NamedDecl*const*>::operator=;
918 /// DeclContext - This is used only as base class of specific decl types that
919 /// can act as declaration contexts. These decls are (only the top classes
920 /// that directly derive from DeclContext are mentioned, not their subclasses):
922 /// TranslationUnitDecl
927 /// ObjCContainerDecl
932 /// DeclKind - This indicates which class this is.
933 unsigned DeclKind : 8;
935 /// \brief Whether this declaration context also has some external
936 /// storage that contains additional declarations that are lexically
937 /// part of this context.
938 mutable unsigned ExternalLexicalStorage : 1;
940 /// \brief Whether this declaration context also has some external
941 /// storage that contains additional declarations that are visible
943 mutable unsigned ExternalVisibleStorage : 1;
945 /// \brief Pointer to the data structure used to lookup declarations
946 /// within this context (or a DependentStoredDeclsMap if this is a
947 /// dependent context), and a bool indicating whether we have lazily
948 /// omitted any declarations from the map. We maintain the invariant
949 /// that, if the map contains an entry for a DeclarationName, then it
950 /// contains all relevant entries for that name.
951 mutable llvm::PointerIntPair<StoredDeclsMap*, 1, bool> LookupPtr;
954 /// FirstDecl - The first declaration stored within this declaration
956 mutable Decl *FirstDecl;
958 /// LastDecl - The last declaration stored within this declaration
959 /// context. FIXME: We could probably cache this value somewhere
960 /// outside of the DeclContext, to reduce the size of DeclContext by
962 mutable Decl *LastDecl;
964 friend class ExternalASTSource;
965 friend class ASTWriter;
967 /// \brief Build up a chain of declarations.
969 /// \returns the first/last pair of declarations.
970 static std::pair<Decl *, Decl *>
971 BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded);
973 DeclContext(Decl::Kind K)
974 : DeclKind(K), ExternalLexicalStorage(false),
975 ExternalVisibleStorage(false), LookupPtr(0, false), FirstDecl(0),
981 Decl::Kind getDeclKind() const {
982 return static_cast<Decl::Kind>(DeclKind);
984 const char *getDeclKindName() const;
986 /// getParent - Returns the containing DeclContext.
987 DeclContext *getParent() {
988 return cast<Decl>(this)->getDeclContext();
990 const DeclContext *getParent() const {
991 return const_cast<DeclContext*>(this)->getParent();
994 /// getLexicalParent - Returns the containing lexical DeclContext. May be
995 /// different from getParent, e.g.:
1000 /// struct A::S {}; // getParent() == namespace 'A'
1001 /// // getLexicalParent() == translation unit
1003 DeclContext *getLexicalParent() {
1004 return cast<Decl>(this)->getLexicalDeclContext();
1006 const DeclContext *getLexicalParent() const {
1007 return const_cast<DeclContext*>(this)->getLexicalParent();
1010 DeclContext *getLookupParent();
1012 const DeclContext *getLookupParent() const {
1013 return const_cast<DeclContext*>(this)->getLookupParent();
1016 ASTContext &getParentASTContext() const {
1017 return cast<Decl>(this)->getASTContext();
1020 bool isClosure() const {
1021 return DeclKind == Decl::Block;
1024 bool isObjCContainer() const {
1026 case Decl::ObjCCategory:
1027 case Decl::ObjCCategoryImpl:
1028 case Decl::ObjCImplementation:
1029 case Decl::ObjCInterface:
1030 case Decl::ObjCProtocol:
1036 bool isFunctionOrMethod() const {
1039 case Decl::ObjCMethod:
1042 return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction;
1046 bool isFileContext() const {
1047 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
1050 bool isTranslationUnit() const {
1051 return DeclKind == Decl::TranslationUnit;
1054 bool isRecord() const {
1055 return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord;
1058 bool isNamespace() const {
1059 return DeclKind == Decl::Namespace;
1062 bool isInlineNamespace() const;
1064 /// \brief Determines whether this context is dependent on a
1065 /// template parameter.
1066 bool isDependentContext() const;
1068 /// isTransparentContext - Determines whether this context is a
1069 /// "transparent" context, meaning that the members declared in this
1070 /// context are semantically declared in the nearest enclosing
1071 /// non-transparent (opaque) context but are lexically declared in
1072 /// this context. For example, consider the enumerators of an
1073 /// enumeration type:
1079 /// Here, E is a transparent context, so its enumerator (Val1) will
1080 /// appear (semantically) that it is in the same context of E.
1081 /// Examples of transparent contexts include: enumerations (except for
1082 /// C++0x scoped enums), and C++ linkage specifications.
1083 bool isTransparentContext() const;
1085 /// \brief Determines whether this context is, or is nested within,
1086 /// a C++ extern "C" linkage spec.
1087 bool isExternCContext() const;
1089 /// \brief Determine whether this declaration context is equivalent
1090 /// to the declaration context DC.
1091 bool Equals(const DeclContext *DC) const {
1092 return DC && this->getPrimaryContext() == DC->getPrimaryContext();
1095 /// \brief Determine whether this declaration context encloses the
1096 /// declaration context DC.
1097 bool Encloses(const DeclContext *DC) const;
1099 /// \brief Find the nearest non-closure ancestor of this context,
1100 /// i.e. the innermost semantic parent of this context which is not
1101 /// a closure. A context may be its own non-closure ancestor.
1102 DeclContext *getNonClosureAncestor();
1103 const DeclContext *getNonClosureAncestor() const {
1104 return const_cast<DeclContext*>(this)->getNonClosureAncestor();
1107 /// getPrimaryContext - There may be many different
1108 /// declarations of the same entity (including forward declarations
1109 /// of classes, multiple definitions of namespaces, etc.), each with
1110 /// a different set of declarations. This routine returns the
1111 /// "primary" DeclContext structure, which will contain the
1112 /// information needed to perform name lookup into this context.
1113 DeclContext *getPrimaryContext();
1114 const DeclContext *getPrimaryContext() const {
1115 return const_cast<DeclContext*>(this)->getPrimaryContext();
1118 /// getRedeclContext - Retrieve the context in which an entity conflicts with
1119 /// other entities of the same name, or where it is a redeclaration if the
1120 /// two entities are compatible. This skips through transparent contexts.
1121 DeclContext *getRedeclContext();
1122 const DeclContext *getRedeclContext() const {
1123 return const_cast<DeclContext *>(this)->getRedeclContext();
1126 /// \brief Retrieve the nearest enclosing namespace context.
1127 DeclContext *getEnclosingNamespaceContext();
1128 const DeclContext *getEnclosingNamespaceContext() const {
1129 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
1132 /// \brief Test if this context is part of the enclosing namespace set of
1133 /// the context NS, as defined in C++0x [namespace.def]p9. If either context
1134 /// isn't a namespace, this is equivalent to Equals().
1136 /// The enclosing namespace set of a namespace is the namespace and, if it is
1137 /// inline, its enclosing namespace, recursively.
1138 bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
1140 /// \brief Collects all of the declaration contexts that are semantically
1141 /// connected to this declaration context.
1143 /// For declaration contexts that have multiple semantically connected but
1144 /// syntactically distinct contexts, such as C++ namespaces, this routine
1145 /// retrieves the complete set of such declaration contexts in source order.
1146 /// For example, given:
1157 /// The \c Contexts parameter will contain both definitions of N.
1159 /// \param Contexts Will be cleared and set to the set of declaration
1160 /// contexts that are semanticaly connected to this declaration context,
1161 /// in source order, including this context (which may be the only result,
1162 /// for non-namespace contexts).
1163 void collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts);
1165 /// decl_iterator - Iterates through the declarations stored
1166 /// within this context.
1167 class decl_iterator {
1168 /// Current - The current declaration.
1172 typedef Decl *value_type;
1173 typedef const value_type &reference;
1174 typedef const value_type *pointer;
1175 typedef std::forward_iterator_tag iterator_category;
1176 typedef std::ptrdiff_t difference_type;
1178 decl_iterator() : Current(0) { }
1179 explicit decl_iterator(Decl *C) : Current(C) { }
1181 reference operator*() const { return Current; }
1182 // This doesn't meet the iterator requirements, but it's convenient
1183 value_type operator->() const { return Current; }
1185 decl_iterator& operator++() {
1186 Current = Current->getNextDeclInContext();
1190 decl_iterator operator++(int) {
1191 decl_iterator tmp(*this);
1196 friend bool operator==(decl_iterator x, decl_iterator y) {
1197 return x.Current == y.Current;
1199 friend bool operator!=(decl_iterator x, decl_iterator y) {
1200 return x.Current != y.Current;
1204 /// decls_begin/decls_end - Iterate over the declarations stored in
1206 decl_iterator decls_begin() const;
1207 decl_iterator decls_end() const { return decl_iterator(); }
1208 bool decls_empty() const;
1210 /// noload_decls_begin/end - Iterate over the declarations stored in this
1211 /// context that are currently loaded; don't attempt to retrieve anything
1212 /// from an external source.
1213 decl_iterator noload_decls_begin() const;
1214 decl_iterator noload_decls_end() const { return decl_iterator(); }
1216 /// specific_decl_iterator - Iterates over a subrange of
1217 /// declarations stored in a DeclContext, providing only those that
1218 /// are of type SpecificDecl (or a class derived from it). This
1219 /// iterator is used, for example, to provide iteration over just
1220 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
1221 template<typename SpecificDecl>
1222 class specific_decl_iterator {
1223 /// Current - The current, underlying declaration iterator, which
1224 /// will either be NULL or will point to a declaration of
1225 /// type SpecificDecl.
1226 DeclContext::decl_iterator Current;
1228 /// SkipToNextDecl - Advances the current position up to the next
1229 /// declaration of type SpecificDecl that also meets the criteria
1230 /// required by Acceptable.
1231 void SkipToNextDecl() {
1232 while (*Current && !isa<SpecificDecl>(*Current))
1237 typedef SpecificDecl *value_type;
1238 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1239 // type) if we ever have a need for them.
1240 typedef void reference;
1241 typedef void pointer;
1242 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1244 typedef std::forward_iterator_tag iterator_category;
1246 specific_decl_iterator() : Current() { }
1248 /// specific_decl_iterator - Construct a new iterator over a
1249 /// subset of the declarations the range [C,
1250 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1251 /// member function of SpecificDecl that should return true for
1252 /// all of the SpecificDecl instances that will be in the subset
1253 /// of iterators. For example, if you want Objective-C instance
1254 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1255 /// &ObjCMethodDecl::isInstanceMethod.
1256 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1260 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1261 // This doesn't meet the iterator requirements, but it's convenient
1262 value_type operator->() const { return **this; }
1264 specific_decl_iterator& operator++() {
1270 specific_decl_iterator operator++(int) {
1271 specific_decl_iterator tmp(*this);
1276 friend bool operator==(const specific_decl_iterator& x,
1277 const specific_decl_iterator& y) {
1278 return x.Current == y.Current;
1281 friend bool operator!=(const specific_decl_iterator& x,
1282 const specific_decl_iterator& y) {
1283 return x.Current != y.Current;
1287 /// \brief Iterates over a filtered subrange of declarations stored
1288 /// in a DeclContext.
1290 /// This iterator visits only those declarations that are of type
1291 /// SpecificDecl (or a class derived from it) and that meet some
1292 /// additional run-time criteria. This iterator is used, for
1293 /// example, to provide access to the instance methods within an
1294 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
1295 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
1296 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
1297 class filtered_decl_iterator {
1298 /// Current - The current, underlying declaration iterator, which
1299 /// will either be NULL or will point to a declaration of
1300 /// type SpecificDecl.
1301 DeclContext::decl_iterator Current;
1303 /// SkipToNextDecl - Advances the current position up to the next
1304 /// declaration of type SpecificDecl that also meets the criteria
1305 /// required by Acceptable.
1306 void SkipToNextDecl() {
1308 (!isa<SpecificDecl>(*Current) ||
1309 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
1314 typedef SpecificDecl *value_type;
1315 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1316 // type) if we ever have a need for them.
1317 typedef void reference;
1318 typedef void pointer;
1319 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1321 typedef std::forward_iterator_tag iterator_category;
1323 filtered_decl_iterator() : Current() { }
1325 /// filtered_decl_iterator - Construct a new iterator over a
1326 /// subset of the declarations the range [C,
1327 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1328 /// member function of SpecificDecl that should return true for
1329 /// all of the SpecificDecl instances that will be in the subset
1330 /// of iterators. For example, if you want Objective-C instance
1331 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1332 /// &ObjCMethodDecl::isInstanceMethod.
1333 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1337 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1338 value_type operator->() const { return cast<SpecificDecl>(*Current); }
1340 filtered_decl_iterator& operator++() {
1346 filtered_decl_iterator operator++(int) {
1347 filtered_decl_iterator tmp(*this);
1352 friend bool operator==(const filtered_decl_iterator& x,
1353 const filtered_decl_iterator& y) {
1354 return x.Current == y.Current;
1357 friend bool operator!=(const filtered_decl_iterator& x,
1358 const filtered_decl_iterator& y) {
1359 return x.Current != y.Current;
1363 /// @brief Add the declaration D into this context.
1365 /// This routine should be invoked when the declaration D has first
1366 /// been declared, to place D into the context where it was
1367 /// (lexically) defined. Every declaration must be added to one
1368 /// (and only one!) context, where it can be visited via
1369 /// [decls_begin(), decls_end()). Once a declaration has been added
1370 /// to its lexical context, the corresponding DeclContext owns the
1373 /// If D is also a NamedDecl, it will be made visible within its
1374 /// semantic context via makeDeclVisibleInContext.
1375 void addDecl(Decl *D);
1377 /// @brief Add the declaration D into this context, but suppress
1378 /// searches for external declarations with the same name.
1380 /// Although analogous in function to addDecl, this removes an
1381 /// important check. This is only useful if the Decl is being
1382 /// added in response to an external search; in all other cases,
1383 /// addDecl() is the right function to use.
1384 /// See the ASTImporter for use cases.
1385 void addDeclInternal(Decl *D);
1387 /// @brief Add the declaration D to this context without modifying
1388 /// any lookup tables.
1390 /// This is useful for some operations in dependent contexts where
1391 /// the semantic context might not be dependent; this basically
1392 /// only happens with friends.
1393 void addHiddenDecl(Decl *D);
1395 /// @brief Removes a declaration from this context.
1396 void removeDecl(Decl *D);
1398 /// lookup_iterator - An iterator that provides access to the results
1399 /// of looking up a name within this context.
1400 typedef NamedDecl **lookup_iterator;
1402 /// lookup_const_iterator - An iterator that provides non-mutable
1403 /// access to the results of lookup up a name within this context.
1404 typedef NamedDecl * const * lookup_const_iterator;
1406 typedef DeclContextLookupResult lookup_result;
1407 typedef DeclContextLookupConstResult lookup_const_result;
1409 /// lookup - Find the declarations (if any) with the given Name in
1410 /// this context. Returns a range of iterators that contains all of
1411 /// the declarations with this name, with object, function, member,
1412 /// and enumerator names preceding any tag name. Note that this
1413 /// routine will not look into parent contexts.
1414 lookup_result lookup(DeclarationName Name);
1415 lookup_const_result lookup(DeclarationName Name) const {
1416 return const_cast<DeclContext*>(this)->lookup(Name);
1419 /// \brief A simplistic name lookup mechanism that performs name lookup
1420 /// into this declaration context without consulting the external source.
1422 /// This function should almost never be used, because it subverts the
1423 /// usual relationship between a DeclContext and the external source.
1424 /// See the ASTImporter for the (few, but important) use cases.
1425 void localUncachedLookup(DeclarationName Name,
1426 llvm::SmallVectorImpl<NamedDecl *> &Results);
1428 /// @brief Makes a declaration visible within this context.
1430 /// This routine makes the declaration D visible to name lookup
1431 /// within this context and, if this is a transparent context,
1432 /// within its parent contexts up to the first enclosing
1433 /// non-transparent context. Making a declaration visible within a
1434 /// context does not transfer ownership of a declaration, and a
1435 /// declaration can be visible in many contexts that aren't its
1436 /// lexical context.
1438 /// If D is a redeclaration of an existing declaration that is
1439 /// visible from this context, as determined by
1440 /// NamedDecl::declarationReplaces, the previous declaration will be
1441 /// replaced with D.
1442 void makeDeclVisibleInContext(NamedDecl *D);
1444 /// all_lookups_iterator - An iterator that provides a view over the results
1445 /// of looking up every possible name.
1446 class all_lookups_iterator;
1448 all_lookups_iterator lookups_begin() const;
1450 all_lookups_iterator lookups_end() const;
1452 /// udir_iterator - Iterates through the using-directives stored
1453 /// within this context.
1454 typedef UsingDirectiveDecl * const * udir_iterator;
1456 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
1458 udir_iterator_range getUsingDirectives() const;
1460 udir_iterator using_directives_begin() const {
1461 return getUsingDirectives().first;
1464 udir_iterator using_directives_end() const {
1465 return getUsingDirectives().second;
1468 // These are all defined in DependentDiagnostic.h.
1469 class ddiag_iterator;
1470 inline ddiag_iterator ddiag_begin() const;
1471 inline ddiag_iterator ddiag_end() const;
1473 // Low-level accessors
1475 /// \brief Mark the lookup table as needing to be built. This should be
1476 /// used only if setHasExternalLexicalStorage() has been called.
1477 void setMustBuildLookupTable() {
1478 assert(ExternalLexicalStorage && "Requires external lexical storage");
1479 LookupPtr.setInt(true);
1482 /// \brief Retrieve the internal representation of the lookup structure.
1483 /// This may omit some names if we are lazily building the structure.
1484 StoredDeclsMap *getLookupPtr() const { return LookupPtr.getPointer(); }
1486 /// \brief Ensure the lookup structure is fully-built and return it.
1487 StoredDeclsMap *buildLookup();
1489 /// \brief Whether this DeclContext has external storage containing
1490 /// additional declarations that are lexically in this context.
1491 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
1493 /// \brief State whether this DeclContext has external storage for
1494 /// declarations lexically in this context.
1495 void setHasExternalLexicalStorage(bool ES = true) {
1496 ExternalLexicalStorage = ES;
1499 /// \brief Whether this DeclContext has external storage containing
1500 /// additional declarations that are visible in this context.
1501 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
1503 /// \brief State whether this DeclContext has external storage for
1504 /// declarations visible in this context.
1505 void setHasExternalVisibleStorage(bool ES = true) {
1506 ExternalVisibleStorage = ES;
1509 /// \brief Determine whether the given declaration is stored in the list of
1510 /// declarations lexically within this context.
1511 bool isDeclInLexicalTraversal(const Decl *D) const {
1512 return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl ||
1516 static bool classof(const Decl *D);
1517 static bool classof(const DeclContext *D) { return true; }
1519 LLVM_ATTRIBUTE_USED void dumpDeclContext() const;
1522 void LoadLexicalDeclsFromExternalStorage() const;
1524 /// @brief Makes a declaration visible within this context, but
1525 /// suppresses searches for external declarations with the same
1528 /// Analogous to makeDeclVisibleInContext, but for the exclusive
1529 /// use of addDeclInternal().
1530 void makeDeclVisibleInContextInternal(NamedDecl *D);
1532 friend class DependentDiagnostic;
1533 StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
1535 void buildLookupImpl(DeclContext *DCtx);
1536 void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1537 bool Rediscoverable);
1538 void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal);
1541 inline bool Decl::isTemplateParameter() const {
1542 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
1543 getKind() == TemplateTemplateParm;
1546 // Specialization selected when ToTy is not a known subclass of DeclContext.
1547 template <class ToTy,
1548 bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value>
1549 struct cast_convert_decl_context {
1550 static const ToTy *doit(const DeclContext *Val) {
1551 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
1554 static ToTy *doit(DeclContext *Val) {
1555 return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
1559 // Specialization selected when ToTy is a known subclass of DeclContext.
1560 template <class ToTy>
1561 struct cast_convert_decl_context<ToTy, true> {
1562 static const ToTy *doit(const DeclContext *Val) {
1563 return static_cast<const ToTy*>(Val);
1566 static ToTy *doit(DeclContext *Val) {
1567 return static_cast<ToTy*>(Val);
1576 /// isa<T>(DeclContext*)
1577 template <typename To>
1578 struct isa_impl<To, ::clang::DeclContext> {
1579 static bool doit(const ::clang::DeclContext &Val) {
1580 return To::classofKind(Val.getDeclKind());
1584 /// cast<T>(DeclContext*)
1585 template<class ToTy>
1586 struct cast_convert_val<ToTy,
1587 const ::clang::DeclContext,const ::clang::DeclContext> {
1588 static const ToTy &doit(const ::clang::DeclContext &Val) {
1589 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1592 template<class ToTy>
1593 struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
1594 static ToTy &doit(::clang::DeclContext &Val) {
1595 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1598 template<class ToTy>
1599 struct cast_convert_val<ToTy,
1600 const ::clang::DeclContext*, const ::clang::DeclContext*> {
1601 static const ToTy *doit(const ::clang::DeclContext *Val) {
1602 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1605 template<class ToTy>
1606 struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
1607 static ToTy *doit(::clang::DeclContext *Val) {
1608 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1612 /// Implement cast_convert_val for Decl -> DeclContext conversions.
1613 template<class FromTy>
1614 struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
1615 static ::clang::DeclContext &doit(const FromTy &Val) {
1616 return *FromTy::castToDeclContext(&Val);
1620 template<class FromTy>
1621 struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
1622 static ::clang::DeclContext *doit(const FromTy *Val) {
1623 return FromTy::castToDeclContext(Val);
1627 template<class FromTy>
1628 struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
1629 static const ::clang::DeclContext &doit(const FromTy &Val) {
1630 return *FromTy::castToDeclContext(&Val);
1634 template<class FromTy>
1635 struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
1636 static const ::clang::DeclContext *doit(const FromTy *Val) {
1637 return FromTy::castToDeclContext(Val);
1641 } // end namespace llvm