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/Type.h"
19 #include "clang/Basic/Specifiers.h"
20 #include "llvm/Support/PrettyStackTrace.h"
21 #include "llvm/ADT/PointerUnion.h"
25 class TranslationUnitDecl;
27 class UsingDirectiveDecl;
33 class ObjCContainerDecl;
34 class ObjCInterfaceDecl;
35 class ObjCCategoryDecl;
36 class ObjCProtocolDecl;
37 class ObjCImplementationDecl;
38 class ObjCCategoryImplDecl;
40 class LinkageSpecDecl;
42 class DeclarationName;
47 // DeclContext* is only 4-byte aligned on 32-bit systems.
49 class PointerLikeTypeTraits<clang::DeclContext*> {
50 typedef clang::DeclContext* PT;
52 static inline void *getAsVoidPointer(PT P) { return P; }
53 static inline PT getFromVoidPointer(void *P) {
54 return static_cast<PT>(P);
56 enum { NumLowBitsAvailable = 2 };
62 /// Decl - This represents one declaration (or definition), e.g. a variable,
63 /// typedef, function, struct, etc.
67 /// \brief Lists the kind of concrete classes of Decl.
69 #define DECL(Derived, Base) Derived,
70 #define DECL_RANGE(CommonBase, Start, End) \
71 CommonBase##First = Start, CommonBase##Last = End,
72 #define LAST_DECL_RANGE(CommonBase, Start, End) \
73 CommonBase##First = Start, CommonBase##Last = End
74 #include "clang/AST/DeclNodes.def"
77 /// IdentifierNamespace - According to C99 6.2.3, there are four
78 /// namespaces, labels, tags, members and ordinary
79 /// identifiers. These are meant as bitmasks, so that searches in
80 /// C++ can look into the "tag" namespace during ordinary lookup. We
81 /// use additional namespaces for Objective-C entities. We also put
82 /// C++ friend declarations (of previously-undeclared entities) in
83 /// shadow namespaces, and 'using' declarations (as opposed to their
84 /// implicit shadow declarations) can be found in their own
86 enum IdentifierNamespace {
91 IDNS_ObjCProtocol = 0x10,
92 IDNS_ObjCImplementation = 0x20,
93 IDNS_ObjCCategoryName = 0x40,
94 IDNS_OrdinaryFriend = 0x80,
95 IDNS_TagFriend = 0x100,
99 /// ObjCDeclQualifier - Qualifier used on types in method declarations
100 /// for remote messaging. They are meant for the arguments though and
101 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
102 enum ObjCDeclQualifier {
107 OBJC_TQ_Bycopy = 0x8,
108 OBJC_TQ_Byref = 0x10,
109 OBJC_TQ_Oneway = 0x20
113 /// NextDeclInContext - The next declaration within the same lexical
114 /// DeclContext. These pointers form the linked list that is
115 /// traversed via DeclContext's decls_begin()/decls_end().
116 Decl *NextDeclInContext;
118 friend class DeclContext;
121 DeclContext *SemanticDC;
122 DeclContext *LexicalDC;
126 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
127 /// For declarations that don't contain C++ scope specifiers, it contains
128 /// the DeclContext where the Decl was declared.
129 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
130 /// with the context where it semantically belongs (SemanticDC) and the
131 /// context where it was lexically declared (LexicalDC).
135 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
137 /// void A::f(); // SemanticDC == namespace 'A'
138 /// // LexicalDC == global namespace
139 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
141 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
142 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
143 inline MultipleDC *getMultipleDC() const {
144 return DeclCtx.get<MultipleDC*>();
146 inline DeclContext *getSemanticDC() const {
147 return DeclCtx.get<DeclContext*>();
150 /// Loc - The location that this decl.
153 /// DeclKind - This indicates which class this is.
156 /// InvalidDecl - This indicates a semantic error occurred.
157 unsigned int InvalidDecl : 1;
159 /// HasAttrs - This indicates whether the decl has attributes or not.
160 unsigned int HasAttrs : 1;
162 /// Implicit - Whether this declaration was implicitly generated by
163 /// the implementation rather than explicitly written by the user.
166 /// \brief Whether this declaration was "used", meaning that a definition is
171 /// Access - Used by C++ decls for the access specifier.
172 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
174 friend class CXXClassMemberWrapper;
176 // PCHLevel - the "level" of precompiled header/AST file from which this
177 // declaration was built.
178 unsigned PCHLevel : 2;
180 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
181 unsigned IdentifierNamespace : 16;
185 void CheckAccessDeclContext() const;
187 void CheckAccessDeclContext() const { }
192 Decl(Kind DK, DeclContext *DC, SourceLocation L)
193 : NextDeclInContext(0), DeclCtx(DC),
194 Loc(L), DeclKind(DK), InvalidDecl(0),
195 HasAttrs(false), Implicit(false), Used(false),
196 Access(AS_none), PCHLevel(0),
197 IdentifierNamespace(getIdentifierNamespaceForKind(DK)) {
198 if (Decl::CollectingStats()) addDeclKind(DK);
205 /// \brief Source range that this declaration covers.
206 virtual SourceRange getSourceRange() const {
207 return SourceRange(getLocation(), getLocation());
209 SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
210 SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
212 SourceLocation getLocation() const { return Loc; }
213 void setLocation(SourceLocation L) { Loc = L; }
215 Kind getKind() const { return DeclKind; }
216 const char *getDeclKindName() const;
218 Decl *getNextDeclInContext() { return NextDeclInContext; }
219 const Decl *getNextDeclInContext() const { return NextDeclInContext; }
221 DeclContext *getDeclContext() {
223 return getSemanticDC();
224 return getMultipleDC()->SemanticDC;
226 const DeclContext *getDeclContext() const {
227 return const_cast<Decl*>(this)->getDeclContext();
230 TranslationUnitDecl *getTranslationUnitDecl();
231 const TranslationUnitDecl *getTranslationUnitDecl() const {
232 return const_cast<Decl*>(this)->getTranslationUnitDecl();
235 bool isInAnonymousNamespace() const;
237 ASTContext &getASTContext() const;
239 void setAccess(AccessSpecifier AS) {
241 CheckAccessDeclContext();
244 AccessSpecifier getAccess() const {
245 CheckAccessDeclContext();
246 return AccessSpecifier(Access);
249 bool hasAttrs() const { return HasAttrs; }
250 void addAttr(Attr *attr);
251 const Attr *getAttrs() const {
252 if (!HasAttrs) return 0; // common case, no attributes.
253 return getAttrsImpl(); // Uncommon case, out of line hash lookup.
255 void swapAttrs(Decl *D);
256 void invalidateAttrs();
258 template<typename T> const T *getAttr() const {
259 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())
260 if (const T *V = dyn_cast<T>(attr))
265 template<typename T> bool hasAttr() const {
266 return getAttr<T>() != 0;
269 /// setInvalidDecl - Indicates the Decl had a semantic error. This
270 /// allows for graceful error recovery.
271 void setInvalidDecl(bool Invalid = true) { InvalidDecl = Invalid; }
272 bool isInvalidDecl() const { return (bool) InvalidDecl; }
274 /// isImplicit - Indicates whether the declaration was implicitly
275 /// generated by the implementation. If false, this declaration
276 /// was written explicitly in the source code.
277 bool isImplicit() const { return Implicit; }
278 void setImplicit(bool I = true) { Implicit = I; }
280 /// \brief Whether this declaration was used, meaning that a definition
284 void setUsed(bool U = true) { Used = U; }
286 /// \brief Retrieve the level of precompiled header from which this
287 /// declaration was generated.
289 /// The PCH level of a declaration describes where the declaration originated
290 /// from. A PCH level of 0 indicates that the declaration was not from a
291 /// precompiled header. A PCH level of 1 indicates that the declaration was
292 /// from a top-level precompiled header; 2 indicates that the declaration
293 /// comes from a precompiled header on which the top-level precompiled header
294 /// depends, and so on.
295 unsigned getPCHLevel() const { return PCHLevel; }
297 /// \brief The maximum PCH level that any declaration may have.
298 static const unsigned MaxPCHLevel = 3;
300 /// \brief Set the PCH level of this declaration.
301 void setPCHLevel(unsigned Level) {
302 assert(Level < MaxPCHLevel && "PCH level exceeds the maximum");
306 unsigned getIdentifierNamespace() const {
307 return IdentifierNamespace;
309 bool isInIdentifierNamespace(unsigned NS) const {
310 return getIdentifierNamespace() & NS;
312 static unsigned getIdentifierNamespaceForKind(Kind DK);
315 /// getLexicalDeclContext - The declaration context where this Decl was
316 /// lexically declared (LexicalDC). May be different from
317 /// getDeclContext() (SemanticDC).
321 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
323 /// void A::f(); // SemanticDC == namespace 'A'
324 /// // LexicalDC == global namespace
325 DeclContext *getLexicalDeclContext() {
327 return getSemanticDC();
328 return getMultipleDC()->LexicalDC;
330 const DeclContext *getLexicalDeclContext() const {
331 return const_cast<Decl*>(this)->getLexicalDeclContext();
334 virtual bool isOutOfLine() const {
335 return getLexicalDeclContext() != getDeclContext();
338 /// setDeclContext - Set both the semantic and lexical DeclContext
340 void setDeclContext(DeclContext *DC);
342 void setLexicalDeclContext(DeclContext *DC);
344 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
345 // scoped decl is defined outside the current function or method. This is
346 // roughly global variables and functions, but also handles enums (which could
347 // be defined inside or outside a function etc).
348 bool isDefinedOutsideFunctionOrMethod() const;
350 /// \brief Retrieves the "canonical" declaration of the given declaration.
351 virtual Decl *getCanonicalDecl() { return this; }
352 const Decl *getCanonicalDecl() const {
353 return const_cast<Decl*>(this)->getCanonicalDecl();
356 /// \brief Whether this particular Decl is a canonical one.
357 bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
360 /// \brief Returns the next redeclaration or itself if this is the only decl.
362 /// Decl subclasses that can be redeclared should override this method so that
363 /// Decl::redecl_iterator can iterate over them.
364 virtual Decl *getNextRedeclaration() { return this; }
367 /// \brief Iterates through all the redeclarations of the same decl.
368 class redecl_iterator {
369 /// Current - The current declaration.
374 typedef Decl* value_type;
375 typedef Decl* reference;
376 typedef Decl* pointer;
377 typedef std::forward_iterator_tag iterator_category;
378 typedef std::ptrdiff_t difference_type;
380 redecl_iterator() : Current(0) { }
381 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
383 reference operator*() const { return Current; }
384 pointer operator->() const { return Current; }
386 redecl_iterator& operator++() {
387 assert(Current && "Advancing while iterator has reached end");
388 // Get either previous decl or latest decl.
389 Decl *Next = Current->getNextRedeclaration();
390 assert(Next && "Should return next redeclaration or itself, never null!");
391 Current = (Next != Starter ? Next : 0);
395 redecl_iterator operator++(int) {
396 redecl_iterator tmp(*this);
401 friend bool operator==(redecl_iterator x, redecl_iterator y) {
402 return x.Current == y.Current;
404 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
405 return x.Current != y.Current;
409 /// \brief Returns iterator for all the redeclarations of the same decl.
410 /// It will iterate at least once (when this decl is the only one).
411 redecl_iterator redecls_begin() const {
412 return redecl_iterator(const_cast<Decl*>(this));
414 redecl_iterator redecls_end() const { return redecl_iterator(); }
416 /// getBody - If this Decl represents a declaration for a body of code,
417 /// such as a function or method definition, this method returns the
418 /// top-level Stmt* of that body. Otherwise this method returns null.
419 virtual Stmt* getBody() const { return 0; }
421 /// getCompoundBody - Returns getBody(), dyn_casted to a CompoundStmt.
422 CompoundStmt* getCompoundBody() const;
424 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
425 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
426 SourceLocation getBodyRBrace() const;
428 // global temp stats (until we have a per-module visitor)
429 static void addDeclKind(Kind k);
430 static bool CollectingStats(bool Enable = false);
431 static void PrintStats();
433 /// isTemplateParameter - Determines whether this declaration is a
434 /// template parameter.
435 bool isTemplateParameter() const;
437 /// isTemplateParameter - Determines whether this declaration is a
438 /// template parameter pack.
439 bool isTemplateParameterPack() const;
441 /// \brief Whether this declaration is a function or function template.
442 bool isFunctionOrFunctionTemplate() const;
444 /// \brief Changes the namespace of this declaration to reflect that it's
445 /// the object of a friend declaration.
447 /// These declarations appear in the lexical context of the friending
448 /// class, but in the semantic context of the actual entity. This property
449 /// applies only to a specific decl object; other redeclarations of the
450 /// same entity may not (and probably don't) share this property.
451 void setObjectOfFriendDecl(bool PreviouslyDeclared) {
452 unsigned OldNS = IdentifierNamespace;
453 assert((OldNS == IDNS_Tag || OldNS == IDNS_Ordinary ||
454 OldNS == (IDNS_Tag | IDNS_Ordinary))
455 && "unsupported namespace for undeclared friend");
456 if (!PreviouslyDeclared) IdentifierNamespace = 0;
458 if (OldNS == IDNS_Tag)
459 IdentifierNamespace |= IDNS_TagFriend;
461 IdentifierNamespace |= IDNS_OrdinaryFriend;
464 enum FriendObjectKind {
465 FOK_None, // not a friend object
466 FOK_Declared, // a friend of a previously-declared entity
467 FOK_Undeclared // a friend of a previously-undeclared entity
470 /// \brief Determines whether this declaration is the object of a
471 /// friend declaration and, if so, what kind.
473 /// There is currently no direct way to find the associated FriendDecl.
474 FriendObjectKind getFriendObjectKind() const {
476 = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
477 if (!mask) return FOK_None;
478 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ?
479 FOK_Declared : FOK_Undeclared);
482 // Implement isa/cast/dyncast/etc.
483 static bool classof(const Decl *) { return true; }
484 static bool classofKind(Kind K) { return true; }
485 static DeclContext *castToDeclContext(const Decl *);
486 static Decl *castFromDeclContext(const DeclContext *);
488 /// Destroy - Call destructors and release memory.
489 virtual void Destroy(ASTContext& C);
491 void print(llvm::raw_ostream &Out, unsigned Indentation = 0) const;
492 void print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
493 unsigned Indentation = 0) const;
494 static void printGroup(Decl** Begin, unsigned NumDecls,
495 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
496 unsigned Indentation = 0);
500 const Attr *getAttrsImpl() const;
504 /// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
505 /// doing something to a specific decl.
506 class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
512 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
513 SourceManager &sm, const char *Msg)
514 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
516 virtual void print(llvm::raw_ostream &OS) const;
520 /// DeclContext - This is used only as base class of specific decl types that
521 /// can act as declaration contexts. These decls are (only the top classes
522 /// that directly derive from DeclContext are mentioned, not their subclasses):
524 /// TranslationUnitDecl
529 /// ObjCContainerDecl
534 /// DeclKind - This indicates which class this is.
535 Decl::Kind DeclKind : 8;
537 /// \brief Whether this declaration context also has some external
538 /// storage that contains additional declarations that are lexically
539 /// part of this context.
540 mutable bool ExternalLexicalStorage : 1;
542 /// \brief Whether this declaration context also has some external
543 /// storage that contains additional declarations that are visible
545 mutable bool ExternalVisibleStorage : 1;
547 /// \brief Pointer to the data structure used to lookup declarations
548 /// within this context, which is a DenseMap<DeclarationName,
549 /// StoredDeclsList>.
550 mutable void* LookupPtr;
552 /// FirstDecl - The first declaration stored within this declaration
554 mutable Decl *FirstDecl;
556 /// LastDecl - The last declaration stored within this declaration
557 /// context. FIXME: We could probably cache this value somewhere
558 /// outside of the DeclContext, to reduce the size of DeclContext by
560 mutable Decl *LastDecl;
563 DeclContext(Decl::Kind K)
564 : DeclKind(K), ExternalLexicalStorage(false),
565 ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0),
568 void DestroyDecls(ASTContext &C);
573 Decl::Kind getDeclKind() const {
576 const char *getDeclKindName() const;
578 /// getParent - Returns the containing DeclContext.
579 DeclContext *getParent() {
580 return cast<Decl>(this)->getDeclContext();
582 const DeclContext *getParent() const {
583 return const_cast<DeclContext*>(this)->getParent();
586 /// getLexicalParent - Returns the containing lexical DeclContext. May be
587 /// different from getParent, e.g.:
592 /// struct A::S {}; // getParent() == namespace 'A'
593 /// // getLexicalParent() == translation unit
595 DeclContext *getLexicalParent() {
596 return cast<Decl>(this)->getLexicalDeclContext();
598 const DeclContext *getLexicalParent() const {
599 return const_cast<DeclContext*>(this)->getLexicalParent();
602 DeclContext *getLookupParent();
604 const DeclContext *getLookupParent() const {
605 return const_cast<DeclContext*>(this)->getLookupParent();
608 ASTContext &getParentASTContext() const {
609 return cast<Decl>(this)->getASTContext();
612 bool isFunctionOrMethod() const {
615 case Decl::ObjCMethod:
618 return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast;
622 bool isFileContext() const {
623 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
626 bool isTranslationUnit() const {
627 return DeclKind == Decl::TranslationUnit;
630 bool isRecord() const {
631 return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast;
634 bool isNamespace() const {
635 return DeclKind == Decl::Namespace;
638 /// \brief Determines whether this context is dependent on a
639 /// template parameter.
640 bool isDependentContext() const;
642 /// isTransparentContext - Determines whether this context is a
643 /// "transparent" context, meaning that the members declared in this
644 /// context are semantically declared in the nearest enclosing
645 /// non-transparent (opaque) context but are lexically declared in
646 /// this context. For example, consider the enumerators of an
647 /// enumeration type:
653 /// Here, E is a transparent context, so its enumerator (Val1) will
654 /// appear (semantically) that it is in the same context of E.
655 /// Examples of transparent contexts include: enumerations (except for
656 /// C++0x scoped enums), C++ linkage specifications, and C++0x
657 /// inline namespaces.
658 bool isTransparentContext() const;
660 /// \brief Determine whether this declaration context is equivalent
661 /// to the declaration context DC.
662 bool Equals(DeclContext *DC) {
663 return this->getPrimaryContext() == DC->getPrimaryContext();
666 /// \brief Determine whether this declaration context encloses the
667 /// declaration context DC.
668 bool Encloses(DeclContext *DC);
670 /// getPrimaryContext - There may be many different
671 /// declarations of the same entity (including forward declarations
672 /// of classes, multiple definitions of namespaces, etc.), each with
673 /// a different set of declarations. This routine returns the
674 /// "primary" DeclContext structure, which will contain the
675 /// information needed to perform name lookup into this context.
676 DeclContext *getPrimaryContext();
678 /// getLookupContext - Retrieve the innermost non-transparent
679 /// context of this context, which corresponds to the innermost
680 /// location from which name lookup can find the entities in this
682 DeclContext *getLookupContext();
683 const DeclContext *getLookupContext() const {
684 return const_cast<DeclContext *>(this)->getLookupContext();
687 /// \brief Retrieve the nearest enclosing namespace context.
688 DeclContext *getEnclosingNamespaceContext();
689 const DeclContext *getEnclosingNamespaceContext() const {
690 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
693 /// getNextContext - If this is a DeclContext that may have other
694 /// DeclContexts that are semantically connected but syntactically
695 /// different, such as C++ namespaces, this routine retrieves the
696 /// next DeclContext in the link. Iteration through the chain of
697 /// DeclContexts should begin at the primary DeclContext and
698 /// continue until this function returns NULL. For example, given:
707 /// The first occurrence of namespace N will be the primary
708 /// DeclContext. Its getNextContext will return the second
709 /// occurrence of namespace N.
710 DeclContext *getNextContext();
712 /// decl_iterator - Iterates through the declarations stored
713 /// within this context.
714 class decl_iterator {
715 /// Current - The current declaration.
719 typedef Decl* value_type;
720 typedef Decl* reference;
721 typedef Decl* pointer;
722 typedef std::forward_iterator_tag iterator_category;
723 typedef std::ptrdiff_t difference_type;
725 decl_iterator() : Current(0) { }
726 explicit decl_iterator(Decl *C) : Current(C) { }
728 reference operator*() const { return Current; }
729 pointer operator->() const { return Current; }
731 decl_iterator& operator++() {
732 Current = Current->getNextDeclInContext();
736 decl_iterator operator++(int) {
737 decl_iterator tmp(*this);
742 friend bool operator==(decl_iterator x, decl_iterator y) {
743 return x.Current == y.Current;
745 friend bool operator!=(decl_iterator x, decl_iterator y) {
746 return x.Current != y.Current;
750 /// decls_begin/decls_end - Iterate over the declarations stored in
752 decl_iterator decls_begin() const;
753 decl_iterator decls_end() const;
754 bool decls_empty() const;
756 /// specific_decl_iterator - Iterates over a subrange of
757 /// declarations stored in a DeclContext, providing only those that
758 /// are of type SpecificDecl (or a class derived from it). This
759 /// iterator is used, for example, to provide iteration over just
760 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
761 template<typename SpecificDecl>
762 class specific_decl_iterator {
763 /// Current - The current, underlying declaration iterator, which
764 /// will either be NULL or will point to a declaration of
765 /// type SpecificDecl.
766 DeclContext::decl_iterator Current;
768 /// SkipToNextDecl - Advances the current position up to the next
769 /// declaration of type SpecificDecl that also meets the criteria
770 /// required by Acceptable.
771 void SkipToNextDecl() {
772 while (*Current && !isa<SpecificDecl>(*Current))
777 typedef SpecificDecl* value_type;
778 typedef SpecificDecl* reference;
779 typedef SpecificDecl* pointer;
780 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
782 typedef std::forward_iterator_tag iterator_category;
784 specific_decl_iterator() : Current() { }
786 /// specific_decl_iterator - Construct a new iterator over a
787 /// subset of the declarations the range [C,
788 /// end-of-declarations). If A is non-NULL, it is a pointer to a
789 /// member function of SpecificDecl that should return true for
790 /// all of the SpecificDecl instances that will be in the subset
791 /// of iterators. For example, if you want Objective-C instance
792 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
793 /// &ObjCMethodDecl::isInstanceMethod.
794 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
798 reference operator*() const { return cast<SpecificDecl>(*Current); }
799 pointer operator->() const { return cast<SpecificDecl>(*Current); }
801 specific_decl_iterator& operator++() {
807 specific_decl_iterator operator++(int) {
808 specific_decl_iterator tmp(*this);
814 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
815 return x.Current == y.Current;
819 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
820 return x.Current != y.Current;
824 /// \brief Iterates over a filtered subrange of declarations stored
825 /// in a DeclContext.
827 /// This iterator visits only those declarations that are of type
828 /// SpecificDecl (or a class derived from it) and that meet some
829 /// additional run-time criteria. This iterator is used, for
830 /// example, to provide access to the instance methods within an
831 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
832 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
833 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
834 class filtered_decl_iterator {
835 /// Current - The current, underlying declaration iterator, which
836 /// will either be NULL or will point to a declaration of
837 /// type SpecificDecl.
838 DeclContext::decl_iterator Current;
840 /// SkipToNextDecl - Advances the current position up to the next
841 /// declaration of type SpecificDecl that also meets the criteria
842 /// required by Acceptable.
843 void SkipToNextDecl() {
845 (!isa<SpecificDecl>(*Current) ||
846 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
851 typedef SpecificDecl* value_type;
852 typedef SpecificDecl* reference;
853 typedef SpecificDecl* pointer;
854 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
856 typedef std::forward_iterator_tag iterator_category;
858 filtered_decl_iterator() : Current() { }
860 /// specific_decl_iterator - Construct a new iterator over a
861 /// subset of the declarations the range [C,
862 /// end-of-declarations). If A is non-NULL, it is a pointer to a
863 /// member function of SpecificDecl that should return true for
864 /// all of the SpecificDecl instances that will be in the subset
865 /// of iterators. For example, if you want Objective-C instance
866 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
867 /// &ObjCMethodDecl::isInstanceMethod.
868 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
872 reference operator*() const { return cast<SpecificDecl>(*Current); }
873 pointer operator->() const { return cast<SpecificDecl>(*Current); }
875 filtered_decl_iterator& operator++() {
881 filtered_decl_iterator operator++(int) {
882 filtered_decl_iterator tmp(*this);
888 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
889 return x.Current == y.Current;
893 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
894 return x.Current != y.Current;
898 /// @brief Add the declaration D into this context.
900 /// This routine should be invoked when the declaration D has first
901 /// been declared, to place D into the context where it was
902 /// (lexically) defined. Every declaration must be added to one
903 /// (and only one!) context, where it can be visited via
904 /// [decls_begin(), decls_end()). Once a declaration has been added
905 /// to its lexical context, the corresponding DeclContext owns the
908 /// If D is also a NamedDecl, it will be made visible within its
909 /// semantic context via makeDeclVisibleInContext.
910 void addDecl(Decl *D);
912 /// @brief Add the declaration D to this context without modifying
913 /// any lookup tables.
915 /// This is useful for some operations in dependent contexts where
916 /// the semantic context might not be dependent; this basically
917 /// only happens with friends.
918 void addHiddenDecl(Decl *D);
920 /// @brief Removes a declaration from this context.
921 void removeDecl(Decl *D);
923 /// lookup_iterator - An iterator that provides access to the results
924 /// of looking up a name within this context.
925 typedef NamedDecl **lookup_iterator;
927 /// lookup_const_iterator - An iterator that provides non-mutable
928 /// access to the results of lookup up a name within this context.
929 typedef NamedDecl * const * lookup_const_iterator;
931 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result;
932 typedef std::pair<lookup_const_iterator, lookup_const_iterator>
935 /// lookup - Find the declarations (if any) with the given Name in
936 /// this context. Returns a range of iterators that contains all of
937 /// the declarations with this name, with object, function, member,
938 /// and enumerator names preceding any tag name. Note that this
939 /// routine will not look into parent contexts.
940 lookup_result lookup(DeclarationName Name);
941 lookup_const_result lookup(DeclarationName Name) const;
943 /// @brief Makes a declaration visible within this context.
945 /// This routine makes the declaration D visible to name lookup
946 /// within this context and, if this is a transparent context,
947 /// within its parent contexts up to the first enclosing
948 /// non-transparent context. Making a declaration visible within a
949 /// context does not transfer ownership of a declaration, and a
950 /// declaration can be visible in many contexts that aren't its
953 /// If D is a redeclaration of an existing declaration that is
954 /// visible from this context, as determined by
955 /// NamedDecl::declarationReplaces, the previous declaration will be
958 /// @param Recoverable true if it's okay to not add this decl to
959 /// the lookup tables because it can be easily recovered by walking
960 /// the declaration chains.
961 void makeDeclVisibleInContext(NamedDecl *D, bool Recoverable = true);
963 /// udir_iterator - Iterates through the using-directives stored
964 /// within this context.
965 typedef UsingDirectiveDecl * const * udir_iterator;
967 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
969 udir_iterator_range getUsingDirectives() const;
971 udir_iterator using_directives_begin() const {
972 return getUsingDirectives().first;
975 udir_iterator using_directives_end() const {
976 return getUsingDirectives().second;
979 // Low-level accessors
981 /// \brief Retrieve the internal representation of the lookup structure.
982 void* getLookupPtr() const { return LookupPtr; }
984 /// \brief Whether this DeclContext has external storage containing
985 /// additional declarations that are lexically in this context.
986 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
988 /// \brief State whether this DeclContext has external storage for
989 /// declarations lexically in this context.
990 void setHasExternalLexicalStorage(bool ES = true) {
991 ExternalLexicalStorage = ES;
994 /// \brief Whether this DeclContext has external storage containing
995 /// additional declarations that are visible in this context.
996 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
998 /// \brief State whether this DeclContext has external storage for
999 /// declarations visible in this context.
1000 void setHasExternalVisibleStorage(bool ES = true) {
1001 ExternalVisibleStorage = ES;
1004 static bool classof(const Decl *D);
1005 static bool classof(const DeclContext *D) { return true; }
1006 #define DECL_CONTEXT(Name) \
1007 static bool classof(const Name##Decl *D) { return true; }
1008 #include "clang/AST/DeclNodes.def"
1010 void dumpDeclContext() const;
1013 void LoadLexicalDeclsFromExternalStorage() const;
1014 void LoadVisibleDeclsFromExternalStorage() const;
1016 void buildLookup(DeclContext *DCtx);
1017 void makeDeclVisibleInContextImpl(NamedDecl *D);
1020 inline bool Decl::isTemplateParameter() const {
1021 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
1022 getKind() == TemplateTemplateParm;
1026 // Specialization selected when ToTy is not a known subclass of DeclContext.
1027 template <class ToTy,
1028 bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value>
1029 struct cast_convert_decl_context {
1030 static const ToTy *doit(const DeclContext *Val) {
1031 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
1034 static ToTy *doit(DeclContext *Val) {
1035 return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
1039 // Specialization selected when ToTy is a known subclass of DeclContext.
1040 template <class ToTy>
1041 struct cast_convert_decl_context<ToTy, true> {
1042 static const ToTy *doit(const DeclContext *Val) {
1043 return static_cast<const ToTy*>(Val);
1046 static ToTy *doit(DeclContext *Val) {
1047 return static_cast<ToTy*>(Val);
1056 /// isa<T>(DeclContext*)
1057 template<class ToTy>
1058 struct isa_impl_wrap<ToTy,
1059 const ::clang::DeclContext,const ::clang::DeclContext> {
1060 static bool doit(const ::clang::DeclContext &Val) {
1061 return ToTy::classofKind(Val.getDeclKind());
1064 template<class ToTy>
1065 struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext>
1066 : public isa_impl_wrap<ToTy,
1067 const ::clang::DeclContext,const ::clang::DeclContext> {};
1069 /// cast<T>(DeclContext*)
1070 template<class ToTy>
1071 struct cast_convert_val<ToTy,
1072 const ::clang::DeclContext,const ::clang::DeclContext> {
1073 static const ToTy &doit(const ::clang::DeclContext &Val) {
1074 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1077 template<class ToTy>
1078 struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
1079 static ToTy &doit(::clang::DeclContext &Val) {
1080 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1083 template<class ToTy>
1084 struct cast_convert_val<ToTy,
1085 const ::clang::DeclContext*, const ::clang::DeclContext*> {
1086 static const ToTy *doit(const ::clang::DeclContext *Val) {
1087 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1090 template<class ToTy>
1091 struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
1092 static ToTy *doit(::clang::DeclContext *Val) {
1093 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1097 /// Implement cast_convert_val for Decl -> DeclContext conversions.
1098 template<class FromTy>
1099 struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
1100 static ::clang::DeclContext &doit(const FromTy &Val) {
1101 return *FromTy::castToDeclContext(&Val);
1105 template<class FromTy>
1106 struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
1107 static ::clang::DeclContext *doit(const FromTy *Val) {
1108 return FromTy::castToDeclContext(Val);
1112 template<class FromTy>
1113 struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
1114 static const ::clang::DeclContext &doit(const FromTy &Val) {
1115 return *FromTy::castToDeclContext(&Val);
1119 template<class FromTy>
1120 struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
1121 static const ::clang::DeclContext *doit(const FromTy *Val) {
1122 return FromTy::castToDeclContext(Val);
1126 } // end namespace llvm