Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1 | //===-- DeclBase.h - Base Classes for representing declarations -*- C++ -*-===// |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 10 | // This file defines the Decl and DeclContext interfaces. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CLANG_AST_DECLBASE_H |
| 15 | #define LLVM_CLANG_AST_DECLBASE_H |
| 16 | |
| 17 | #include "clang/AST/Attr.h" |
| 18 | #include "clang/AST/Type.h" |
John McCall | ad2b804 | 2010-01-18 23:21:37 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Specifiers.h" |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 20 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/PointerUnion.h" |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 22 | |
| 23 | namespace clang { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 24 | class DeclContext; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 25 | class TranslationUnitDecl; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 26 | class NamespaceDecl; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 27 | class UsingDirectiveDecl; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 28 | class NamedDecl; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 29 | class FunctionDecl; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 30 | class CXXRecordDecl; |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 31 | class EnumDecl; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 32 | class ObjCMethodDecl; |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 33 | class ObjCContainerDecl; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 34 | class ObjCInterfaceDecl; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 35 | class ObjCCategoryDecl; |
| 36 | class ObjCProtocolDecl; |
| 37 | class ObjCImplementationDecl; |
| 38 | class ObjCCategoryImplDecl; |
Argyrios Kyrtzidis | bfb498d | 2009-07-17 01:19:49 +0000 | [diff] [blame] | 39 | class ObjCImplDecl; |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 40 | class LinkageSpecDecl; |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 41 | class BlockDecl; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 42 | class DeclarationName; |
Ted Kremenek | eaab206 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 43 | class CompoundStmt; |
Chris Lattner | 0eda3b3 | 2009-03-29 04:32:54 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | namespace llvm { |
| 47 | // DeclContext* is only 4-byte aligned on 32-bit systems. |
| 48 | template<> |
| 49 | class PointerLikeTypeTraits<clang::DeclContext*> { |
| 50 | typedef clang::DeclContext* PT; |
| 51 | public: |
| 52 | static inline void *getAsVoidPointer(PT P) { return P; } |
| 53 | static inline PT getFromVoidPointer(void *P) { |
| 54 | return static_cast<PT>(P); |
| 55 | } |
| 56 | enum { NumLowBitsAvailable = 2 }; |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | namespace clang { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 61 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | /// Decl - This represents one declaration (or definition), e.g. a variable, |
| 63 | /// typedef, function, struct, etc. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 64 | /// |
| 65 | class Decl { |
| 66 | public: |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 67 | /// \brief Lists the kind of concrete classes of Decl. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 68 | enum Kind { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 69 | #define DECL(Derived, Base) Derived, |
| 70 | #define DECL_RANGE(CommonBase, Start, End) \ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | CommonBase##First = Start, CommonBase##Last = End, |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 72 | #define LAST_DECL_RANGE(CommonBase, Start, End) \ |
| 73 | CommonBase##First = Start, CommonBase##Last = End |
| 74 | #include "clang/AST/DeclNodes.def" |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 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 |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 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 |
| 85 | /// namespace. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 86 | enum IdentifierNamespace { |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 87 | IDNS_Label = 0x1, |
| 88 | IDNS_Tag = 0x2, |
| 89 | IDNS_Member = 0x4, |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 90 | IDNS_Ordinary = 0x8, |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 91 | IDNS_ObjCProtocol = 0x10, |
| 92 | IDNS_ObjCImplementation = 0x20, |
Fariborz Jahanian | 737061f | 2009-12-11 00:26:36 +0000 | [diff] [blame] | 93 | IDNS_ObjCCategoryName = 0x40, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 94 | IDNS_OrdinaryFriend = 0x80, |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 95 | IDNS_TagFriend = 0x100, |
| 96 | IDNS_Using = 0x200 |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 97 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 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 { |
| 103 | OBJC_TQ_None = 0x0, |
| 104 | OBJC_TQ_In = 0x1, |
| 105 | OBJC_TQ_Inout = 0x2, |
| 106 | OBJC_TQ_Out = 0x4, |
| 107 | OBJC_TQ_Bycopy = 0x8, |
| 108 | OBJC_TQ_Byref = 0x10, |
| 109 | OBJC_TQ_Oneway = 0x20 |
| 110 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 112 | private: |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 113 | /// NextDeclInContext - The next declaration within the same lexical |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 114 | /// DeclContext. These pointers form the linked list that is |
| 115 | /// traversed via DeclContext's decls_begin()/decls_end(). |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 116 | Decl *NextDeclInContext; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 117 | |
| 118 | friend class DeclContext; |
| 119 | |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 120 | struct MultipleDC { |
| 121 | DeclContext *SemanticDC; |
| 122 | DeclContext *LexicalDC; |
| 123 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
| 125 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 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). |
| 132 | /// e.g.: |
| 133 | /// |
| 134 | /// namespace A { |
| 135 | /// void f(); // SemanticDC == LexicalDC == 'namespace A' |
| 136 | /// } |
| 137 | /// void A::f(); // SemanticDC == namespace 'A' |
| 138 | /// // LexicalDC == global namespace |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 139 | llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 140 | |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 141 | inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); } |
| 142 | inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 143 | inline MultipleDC *getMultipleDC() const { |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 144 | return DeclCtx.get<MultipleDC*>(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 146 | inline DeclContext *getSemanticDC() const { |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 147 | return DeclCtx.get<DeclContext*>(); |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 148 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Daniel Dunbar | 39d7650 | 2009-03-04 02:26:41 +0000 | [diff] [blame] | 150 | /// Loc - The location that this decl. |
| 151 | SourceLocation Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 153 | /// DeclKind - This indicates which class this is. |
| 154 | Kind DeclKind : 8; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 156 | /// InvalidDecl - This indicates a semantic error occurred. |
| 157 | unsigned int InvalidDecl : 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 159 | /// HasAttrs - This indicates whether the decl has attributes or not. |
| 160 | unsigned int HasAttrs : 1; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 161 | |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 162 | /// Implicit - Whether this declaration was implicitly generated by |
| 163 | /// the implementation rather than explicitly written by the user. |
| 164 | bool Implicit : 1; |
| 165 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 166 | /// \brief Whether this declaration was "used", meaning that a definition is |
| 167 | /// required. |
| 168 | bool Used : 1; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 169 | |
| 170 | protected: |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 171 | /// Access - Used by C++ decls for the access specifier. |
| 172 | // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum |
| 173 | unsigned Access : 2; |
| 174 | friend class CXXClassMemberWrapper; |
| 175 | |
| 176 | // PCHLevel - the "level" of precompiled header/AST file from which this |
| 177 | // declaration was built. |
| 178 | unsigned PCHLevel : 2; |
| 179 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 180 | /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 181 | unsigned IdentifierNamespace : 16; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 183 | private: |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 184 | #ifndef NDEBUG |
| 185 | void CheckAccessDeclContext() const; |
| 186 | #else |
| 187 | void CheckAccessDeclContext() const { } |
| 188 | #endif |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 190 | protected: |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 191 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | Decl(Kind DK, DeclContext *DC, SourceLocation L) |
| 193 | : NextDeclInContext(0), DeclCtx(DC), |
Daniel Dunbar | 39d7650 | 2009-03-04 02:26:41 +0000 | [diff] [blame] | 194 | Loc(L), DeclKind(DK), InvalidDecl(0), |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 195 | HasAttrs(false), Implicit(false), Used(false), |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 196 | Access(AS_none), PCHLevel(0), |
| 197 | IdentifierNamespace(getIdentifierNamespaceForKind(DK)) { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 198 | if (Decl::CollectingStats()) addDeclKind(DK); |
| 199 | } |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 201 | virtual ~Decl(); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 202 | |
| 203 | public: |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 204 | |
| 205 | /// \brief Source range that this declaration covers. |
| 206 | virtual SourceRange getSourceRange() const { |
| 207 | return SourceRange(getLocation(), getLocation()); |
| 208 | } |
| 209 | SourceLocation getLocStart() const { return getSourceRange().getBegin(); } |
| 210 | SourceLocation getLocEnd() const { return getSourceRange().getEnd(); } |
| 211 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 212 | SourceLocation getLocation() const { return Loc; } |
| 213 | void setLocation(SourceLocation L) { Loc = L; } |
| 214 | |
| 215 | Kind getKind() const { return DeclKind; } |
| 216 | const char *getDeclKindName() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 218 | Decl *getNextDeclInContext() { return NextDeclInContext; } |
| 219 | const Decl *getNextDeclInContext() const { return NextDeclInContext; } |
Chris Lattner | 96f4468 | 2009-03-28 05:59:45 +0000 | [diff] [blame] | 220 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 221 | DeclContext *getDeclContext() { |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 222 | if (isInSemaDC()) |
| 223 | return getSemanticDC(); |
| 224 | return getMultipleDC()->SemanticDC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 225 | } |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 226 | const DeclContext *getDeclContext() const { |
| 227 | return const_cast<Decl*>(this)->getDeclContext(); |
| 228 | } |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 229 | |
| 230 | TranslationUnitDecl *getTranslationUnitDecl(); |
| 231 | const TranslationUnitDecl *getTranslationUnitDecl() const { |
| 232 | return const_cast<Decl*>(this)->getTranslationUnitDecl(); |
| 233 | } |
| 234 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 235 | bool isInAnonymousNamespace() const; |
| 236 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 237 | ASTContext &getASTContext() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 239 | void setAccess(AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | Access = AS; |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 241 | CheckAccessDeclContext(); |
Anders Carlsson | b8547e8 | 2009-03-25 20:19:57 +0000 | [diff] [blame] | 242 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
| 244 | AccessSpecifier getAccess() const { |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 245 | CheckAccessDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | return AccessSpecifier(Access); |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 247 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 249 | bool hasAttrs() const { return HasAttrs; } |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 250 | void addAttr(Attr *attr); |
| 251 | const Attr *getAttrs() const { |
Chris Lattner | 81abbdd | 2009-03-21 06:27:31 +0000 | [diff] [blame] | 252 | if (!HasAttrs) return 0; // common case, no attributes. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 253 | return getAttrsImpl(); // Uncommon case, out of line hash lookup. |
Chris Lattner | 81abbdd | 2009-03-21 06:27:31 +0000 | [diff] [blame] | 254 | } |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 255 | void swapAttrs(Decl *D); |
| 256 | void invalidateAttrs(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 257 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 258 | template<typename T> const T *getAttr() const { |
| 259 | for (const Attr *attr = getAttrs(); attr; attr = attr->getNext()) |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 260 | if (const T *V = dyn_cast<T>(attr)) |
| 261 | return V; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 262 | return 0; |
| 263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 265 | template<typename T> bool hasAttr() const { |
| 266 | return getAttr<T>() != 0; |
Chris Lattner | 115cafc | 2009-04-12 20:07:59 +0000 | [diff] [blame] | 267 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 269 | /// setInvalidDecl - Indicates the Decl had a semantic error. This |
| 270 | /// allows for graceful error recovery. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 271 | void setInvalidDecl(bool Invalid = true) { InvalidDecl = Invalid; } |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 272 | bool isInvalidDecl() const { return (bool) InvalidDecl; } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 273 | |
| 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; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 280 | /// \brief Whether this declaration was used, meaning that a definition |
| 281 | /// is required. |
| 282 | bool isUsed() const { return Used; } |
| 283 | void setUsed(bool U = true) { Used = U; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 285 | /// \brief Retrieve the level of precompiled header from which this |
| 286 | /// declaration was generated. |
| 287 | /// |
| 288 | /// The PCH level of a declaration describes where the declaration originated |
| 289 | /// from. A PCH level of 0 indicates that the declaration was not from a |
| 290 | /// precompiled header. A PCH level of 1 indicates that the declaration was |
| 291 | /// from a top-level precompiled header; 2 indicates that the declaration |
| 292 | /// comes from a precompiled header on which the top-level precompiled header |
| 293 | /// depends, and so on. |
| 294 | unsigned getPCHLevel() const { return PCHLevel; } |
| 295 | |
| 296 | /// \brief The maximum PCH level that any declaration may have. |
| 297 | static const unsigned MaxPCHLevel = 3; |
| 298 | |
| 299 | /// \brief Set the PCH level of this declaration. |
| 300 | void setPCHLevel(unsigned Level) { |
| 301 | assert(Level < MaxPCHLevel && "PCH level exceeds the maximum"); |
| 302 | PCHLevel = Level; |
| 303 | } |
| 304 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 305 | unsigned getIdentifierNamespace() const { |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 306 | return IdentifierNamespace; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 307 | } |
Chris Lattner | d62fdc4 | 2009-01-06 07:16:40 +0000 | [diff] [blame] | 308 | bool isInIdentifierNamespace(unsigned NS) const { |
| 309 | return getIdentifierNamespace() & NS; |
| 310 | } |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 311 | static unsigned getIdentifierNamespaceForKind(Kind DK); |
| 312 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 314 | /// getLexicalDeclContext - The declaration context where this Decl was |
| 315 | /// lexically declared (LexicalDC). May be different from |
| 316 | /// getDeclContext() (SemanticDC). |
| 317 | /// e.g.: |
| 318 | /// |
| 319 | /// namespace A { |
| 320 | /// void f(); // SemanticDC == LexicalDC == 'namespace A' |
| 321 | /// } |
| 322 | /// void A::f(); // SemanticDC == namespace 'A' |
| 323 | /// // LexicalDC == global namespace |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 324 | DeclContext *getLexicalDeclContext() { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 325 | if (isInSemaDC()) |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 326 | return getSemanticDC(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 327 | return getMultipleDC()->LexicalDC; |
| 328 | } |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 329 | const DeclContext *getLexicalDeclContext() const { |
| 330 | return const_cast<Decl*>(this)->getLexicalDeclContext(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 331 | } |
Argyrios Kyrtzidis | f5cecfb | 2009-06-17 22:49:50 +0000 | [diff] [blame] | 332 | |
| 333 | bool isOutOfLine() const { |
| 334 | return getLexicalDeclContext() != getDeclContext(); |
| 335 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 337 | /// setDeclContext - Set both the semantic and lexical DeclContext |
| 338 | /// to DC. |
| 339 | void setDeclContext(DeclContext *DC); |
| 340 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 341 | void setLexicalDeclContext(DeclContext *DC); |
| 342 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 343 | // isDefinedOutsideFunctionOrMethod - This predicate returns true if this |
| 344 | // scoped decl is defined outside the current function or method. This is |
| 345 | // roughly global variables and functions, but also handles enums (which could |
| 346 | // be defined inside or outside a function etc). |
| 347 | bool isDefinedOutsideFunctionOrMethod() const; |
| 348 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 349 | /// \brief Retrieves the "canonical" declaration of the given declaration. |
| 350 | virtual Decl *getCanonicalDecl() { return this; } |
| 351 | const Decl *getCanonicalDecl() const { |
| 352 | return const_cast<Decl*>(this)->getCanonicalDecl(); |
| 353 | } |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 354 | |
Argyrios Kyrtzidis | ac4e379 | 2009-07-18 08:50:48 +0000 | [diff] [blame] | 355 | /// \brief Whether this particular Decl is a canonical one. |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 356 | bool isCanonicalDecl() const { return getCanonicalDecl() == this; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 358 | protected: |
| 359 | /// \brief Returns the next redeclaration or itself if this is the only decl. |
| 360 | /// |
| 361 | /// Decl subclasses that can be redeclared should override this method so that |
| 362 | /// Decl::redecl_iterator can iterate over them. |
| 363 | virtual Decl *getNextRedeclaration() { return this; } |
| 364 | |
| 365 | public: |
| 366 | /// \brief Iterates through all the redeclarations of the same decl. |
| 367 | class redecl_iterator { |
| 368 | /// Current - The current declaration. |
| 369 | Decl *Current; |
| 370 | Decl *Starter; |
| 371 | |
| 372 | public: |
| 373 | typedef Decl* value_type; |
| 374 | typedef Decl* reference; |
| 375 | typedef Decl* pointer; |
| 376 | typedef std::forward_iterator_tag iterator_category; |
| 377 | typedef std::ptrdiff_t difference_type; |
| 378 | |
| 379 | redecl_iterator() : Current(0) { } |
| 380 | explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { } |
| 381 | |
| 382 | reference operator*() const { return Current; } |
| 383 | pointer operator->() const { return Current; } |
| 384 | |
| 385 | redecl_iterator& operator++() { |
| 386 | assert(Current && "Advancing while iterator has reached end"); |
| 387 | // Get either previous decl or latest decl. |
| 388 | Decl *Next = Current->getNextRedeclaration(); |
Argyrios Kyrtzidis | 22cbd2b | 2009-07-21 00:06:27 +0000 | [diff] [blame] | 389 | assert(Next && "Should return next redeclaration or itself, never null!"); |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 390 | Current = (Next != Starter ? Next : 0); |
| 391 | return *this; |
| 392 | } |
| 393 | |
| 394 | redecl_iterator operator++(int) { |
| 395 | redecl_iterator tmp(*this); |
| 396 | ++(*this); |
| 397 | return tmp; |
| 398 | } |
| 399 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | friend bool operator==(redecl_iterator x, redecl_iterator y) { |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 401 | return x.Current == y.Current; |
| 402 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | friend bool operator!=(redecl_iterator x, redecl_iterator y) { |
Argyrios Kyrtzidis | 1e4bc09 | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 404 | return x.Current != y.Current; |
| 405 | } |
| 406 | }; |
| 407 | |
| 408 | /// \brief Returns iterator for all the redeclarations of the same decl. |
| 409 | /// It will iterate at least once (when this decl is the only one). |
| 410 | redecl_iterator redecls_begin() const { |
| 411 | return redecl_iterator(const_cast<Decl*>(this)); |
| 412 | } |
| 413 | redecl_iterator redecls_end() const { return redecl_iterator(); } |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 414 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 415 | /// getBody - If this Decl represents a declaration for a body of code, |
| 416 | /// such as a function or method definition, this method returns the |
| 417 | /// top-level Stmt* of that body. Otherwise this method returns null. |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 418 | virtual Stmt* getBody() const { return 0; } |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 419 | |
| 420 | /// getCompoundBody - Returns getBody(), dyn_casted to a CompoundStmt. |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 421 | CompoundStmt* getCompoundBody() const; |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 422 | |
| 423 | /// getBodyRBrace - Gets the right brace of the body, if a body exists. |
| 424 | /// This works whether the body is a CompoundStmt or a CXXTryStmt. |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 425 | SourceLocation getBodyRBrace() const; |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 427 | // global temp stats (until we have a per-module visitor) |
| 428 | static void addDeclKind(Kind k); |
| 429 | static bool CollectingStats(bool Enable = false); |
| 430 | static void PrintStats(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 432 | /// isTemplateParameter - Determines whether this declaration is a |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 433 | /// template parameter. |
| 434 | bool isTemplateParameter() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 436 | /// isTemplateParameter - Determines whether this declaration is a |
| 437 | /// template parameter pack. |
| 438 | bool isTemplateParameterPack() const; |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 439 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 440 | /// \brief Whether this declaration is a function or function template. |
| 441 | bool isFunctionOrFunctionTemplate() const; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 442 | |
| 443 | /// \brief Changes the namespace of this declaration to reflect that it's |
| 444 | /// the object of a friend declaration. |
| 445 | /// |
| 446 | /// These declarations appear in the lexical context of the friending |
| 447 | /// class, but in the semantic context of the actual entity. This property |
| 448 | /// applies only to a specific decl object; other redeclarations of the |
| 449 | /// same entity may not (and probably don't) share this property. |
| 450 | void setObjectOfFriendDecl(bool PreviouslyDeclared) { |
| 451 | unsigned OldNS = IdentifierNamespace; |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 452 | assert((OldNS == IDNS_Tag || OldNS == IDNS_Ordinary || |
| 453 | OldNS == (IDNS_Tag | IDNS_Ordinary)) |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 454 | && "unsupported namespace for undeclared friend"); |
| 455 | if (!PreviouslyDeclared) IdentifierNamespace = 0; |
| 456 | |
| 457 | if (OldNS == IDNS_Tag) |
| 458 | IdentifierNamespace |= IDNS_TagFriend; |
| 459 | else |
| 460 | IdentifierNamespace |= IDNS_OrdinaryFriend; |
| 461 | } |
| 462 | |
| 463 | enum FriendObjectKind { |
| 464 | FOK_None, // not a friend object |
| 465 | FOK_Declared, // a friend of a previously-declared entity |
| 466 | FOK_Undeclared // a friend of a previously-undeclared entity |
| 467 | }; |
| 468 | |
| 469 | /// \brief Determines whether this declaration is the object of a |
| 470 | /// friend declaration and, if so, what kind. |
| 471 | /// |
| 472 | /// There is currently no direct way to find the associated FriendDecl. |
| 473 | FriendObjectKind getFriendObjectKind() const { |
| 474 | unsigned mask |
| 475 | = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend)); |
| 476 | if (!mask) return FOK_None; |
Anders Carlsson | 255d641 | 2009-09-13 23:59:13 +0000 | [diff] [blame] | 477 | return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? |
| 478 | FOK_Declared : FOK_Undeclared); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 479 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 481 | // Implement isa/cast/dyncast/etc. |
| 482 | static bool classof(const Decl *) { return true; } |
John McCall | 80cd64a | 2010-01-29 01:45:37 +0000 | [diff] [blame^] | 483 | static bool classofKind(Kind K) { return true; } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 484 | static DeclContext *castToDeclContext(const Decl *); |
| 485 | static Decl *castFromDeclContext(const DeclContext *); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 487 | /// Destroy - Call destructors and release memory. |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 488 | virtual void Destroy(ASTContext& C); |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 489 | |
Anders Carlsson | f88df86 | 2009-09-26 21:58:53 +0000 | [diff] [blame] | 490 | void print(llvm::raw_ostream &Out, unsigned Indentation = 0) const; |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 491 | void print(llvm::raw_ostream &Out, const PrintingPolicy &Policy, |
Anders Carlsson | f88df86 | 2009-09-26 21:58:53 +0000 | [diff] [blame] | 492 | unsigned Indentation = 0) const; |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 493 | static void printGroup(Decl** Begin, unsigned NumDecls, |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 494 | llvm::raw_ostream &Out, const PrintingPolicy &Policy, |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 495 | unsigned Indentation = 0); |
Anders Carlsson | f88df86 | 2009-09-26 21:58:53 +0000 | [diff] [blame] | 496 | void dump() const; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 81abbdd | 2009-03-21 06:27:31 +0000 | [diff] [blame] | 498 | private: |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 499 | const Attr *getAttrsImpl() const; |
Chris Lattner | 81abbdd | 2009-03-21 06:27:31 +0000 | [diff] [blame] | 500 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 501 | }; |
| 502 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 503 | /// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when |
| 504 | /// doing something to a specific decl. |
| 505 | class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { |
Daniel Dunbar | c523656 | 2009-11-21 09:05:59 +0000 | [diff] [blame] | 506 | const Decl *TheDecl; |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 507 | SourceLocation Loc; |
| 508 | SourceManager &SM; |
| 509 | const char *Message; |
| 510 | public: |
Daniel Dunbar | c523656 | 2009-11-21 09:05:59 +0000 | [diff] [blame] | 511 | PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L, |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 512 | SourceManager &sm, const char *Msg) |
| 513 | : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 514 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 515 | virtual void print(llvm::raw_ostream &OS) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | }; |
| 517 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 518 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 519 | /// DeclContext - This is used only as base class of specific decl types that |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 520 | /// can act as declaration contexts. These decls are (only the top classes |
| 521 | /// that directly derive from DeclContext are mentioned, not their subclasses): |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 522 | /// |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 523 | /// TranslationUnitDecl |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 524 | /// NamespaceDecl |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 525 | /// FunctionDecl |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 526 | /// TagDecl |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 527 | /// ObjCMethodDecl |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 528 | /// ObjCContainerDecl |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 529 | /// LinkageSpecDecl |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 530 | /// BlockDecl |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 531 | /// |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 532 | class DeclContext { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 533 | /// DeclKind - This indicates which class this is. |
| 534 | Decl::Kind DeclKind : 8; |
| 535 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 536 | /// \brief Whether this declaration context also has some external |
| 537 | /// storage that contains additional declarations that are lexically |
| 538 | /// part of this context. |
| 539 | mutable bool ExternalLexicalStorage : 1; |
| 540 | |
| 541 | /// \brief Whether this declaration context also has some external |
| 542 | /// storage that contains additional declarations that are visible |
| 543 | /// in this context. |
| 544 | mutable bool ExternalVisibleStorage : 1; |
| 545 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 546 | /// \brief Pointer to the data structure used to lookup declarations |
| 547 | /// within this context, which is a DenseMap<DeclarationName, |
| 548 | /// StoredDeclsList>. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 549 | mutable void* LookupPtr; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 550 | |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 551 | /// FirstDecl - The first declaration stored within this declaration |
| 552 | /// context. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 553 | mutable Decl *FirstDecl; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 554 | |
| 555 | /// LastDecl - The last declaration stored within this declaration |
| 556 | /// context. FIXME: We could probably cache this value somewhere |
| 557 | /// outside of the DeclContext, to reduce the size of DeclContext by |
| 558 | /// another pointer. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 559 | mutable Decl *LastDecl; |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 560 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 561 | protected: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | DeclContext(Decl::Kind K) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 563 | : DeclKind(K), ExternalLexicalStorage(false), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0), |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 565 | LastDecl(0) { } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 566 | |
| 567 | void DestroyDecls(ASTContext &C); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 568 | |
| 569 | public: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 570 | ~DeclContext(); |
| 571 | |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 572 | Decl::Kind getDeclKind() const { |
| 573 | return DeclKind; |
| 574 | } |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 575 | const char *getDeclKindName() const; |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 576 | |
Argyrios Kyrtzidis | 305ec42 | 2009-02-17 20:26:05 +0000 | [diff] [blame] | 577 | /// getParent - Returns the containing DeclContext. |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 578 | DeclContext *getParent() { |
Argyrios Kyrtzidis | 305ec42 | 2009-02-17 20:26:05 +0000 | [diff] [blame] | 579 | return cast<Decl>(this)->getDeclContext(); |
| 580 | } |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 581 | const DeclContext *getParent() const { |
| 582 | return const_cast<DeclContext*>(this)->getParent(); |
Argyrios Kyrtzidis | d2595ec | 2008-10-12 18:40:01 +0000 | [diff] [blame] | 583 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 585 | /// getLexicalParent - Returns the containing lexical DeclContext. May be |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 586 | /// different from getParent, e.g.: |
| 587 | /// |
| 588 | /// namespace A { |
| 589 | /// struct S; |
| 590 | /// } |
| 591 | /// struct A::S {}; // getParent() == namespace 'A' |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 592 | /// // getLexicalParent() == translation unit |
| 593 | /// |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 594 | DeclContext *getLexicalParent() { |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 595 | return cast<Decl>(this)->getLexicalDeclContext(); |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 596 | } |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 597 | const DeclContext *getLexicalParent() const { |
| 598 | return const_cast<DeclContext*>(this)->getLexicalParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | } |
Argyrios Kyrtzidis | 048f30a | 2009-06-30 02:35:38 +0000 | [diff] [blame] | 600 | |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 601 | DeclContext *getLookupParent(); |
| 602 | |
| 603 | const DeclContext *getLookupParent() const { |
| 604 | return const_cast<DeclContext*>(this)->getLookupParent(); |
| 605 | } |
| 606 | |
Argyrios Kyrtzidis | 048f30a | 2009-06-30 02:35:38 +0000 | [diff] [blame] | 607 | ASTContext &getParentASTContext() const { |
| 608 | return cast<Decl>(this)->getASTContext(); |
| 609 | } |
| 610 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 611 | bool isFunctionOrMethod() const { |
| 612 | switch (DeclKind) { |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 613 | case Decl::Block: |
| 614 | case Decl::ObjCMethod: |
| 615 | return true; |
| 616 | default: |
| 617 | return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 621 | bool isFileContext() const { |
| 622 | return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; |
| 623 | } |
| 624 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 625 | bool isTranslationUnit() const { |
| 626 | return DeclKind == Decl::TranslationUnit; |
| 627 | } |
| 628 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 629 | bool isRecord() const { |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 630 | return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast; |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 633 | bool isNamespace() const { |
| 634 | return DeclKind == Decl::Namespace; |
| 635 | } |
| 636 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 637 | /// \brief Determines whether this context is dependent on a |
| 638 | /// template parameter. |
| 639 | bool isDependentContext() const; |
| 640 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 641 | /// isTransparentContext - Determines whether this context is a |
| 642 | /// "transparent" context, meaning that the members declared in this |
| 643 | /// context are semantically declared in the nearest enclosing |
| 644 | /// non-transparent (opaque) context but are lexically declared in |
| 645 | /// this context. For example, consider the enumerators of an |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | /// enumeration type: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 647 | /// @code |
| 648 | /// enum E { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | /// Val1 |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 650 | /// }; |
| 651 | /// @endcode |
| 652 | /// Here, E is a transparent context, so its enumerator (Val1) will |
| 653 | /// appear (semantically) that it is in the same context of E. |
| 654 | /// Examples of transparent contexts include: enumerations (except for |
| 655 | /// C++0x scoped enums), C++ linkage specifications, and C++0x |
| 656 | /// inline namespaces. |
| 657 | bool isTransparentContext() const; |
| 658 | |
Douglas Gregor | 61481da | 2009-09-01 17:22:34 +0000 | [diff] [blame] | 659 | /// \brief Determine whether this declaration context is equivalent |
| 660 | /// to the declaration context DC. |
| 661 | bool Equals(DeclContext *DC) { |
| 662 | return this->getPrimaryContext() == DC->getPrimaryContext(); |
| 663 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 665 | /// \brief Determine whether this declaration context encloses the |
| 666 | /// declaration context DC. |
| 667 | bool Encloses(DeclContext *DC); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 668 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 669 | /// getPrimaryContext - There may be many different |
| 670 | /// declarations of the same entity (including forward declarations |
| 671 | /// of classes, multiple definitions of namespaces, etc.), each with |
| 672 | /// a different set of declarations. This routine returns the |
| 673 | /// "primary" DeclContext structure, which will contain the |
| 674 | /// information needed to perform name lookup into this context. |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 675 | DeclContext *getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 676 | |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 677 | /// getLookupContext - Retrieve the innermost non-transparent |
| 678 | /// context of this context, which corresponds to the innermost |
| 679 | /// location from which name lookup can find the entities in this |
| 680 | /// context. |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 681 | DeclContext *getLookupContext(); |
| 682 | const DeclContext *getLookupContext() const { |
| 683 | return const_cast<DeclContext *>(this)->getLookupContext(); |
Douglas Gregor | 17a9b9e | 2009-01-07 02:48:43 +0000 | [diff] [blame] | 684 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 686 | /// \brief Retrieve the nearest enclosing namespace context. |
| 687 | DeclContext *getEnclosingNamespaceContext(); |
| 688 | const DeclContext *getEnclosingNamespaceContext() const { |
| 689 | return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); |
| 690 | } |
| 691 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 692 | /// getNextContext - If this is a DeclContext that may have other |
| 693 | /// DeclContexts that are semantically connected but syntactically |
| 694 | /// different, such as C++ namespaces, this routine retrieves the |
| 695 | /// next DeclContext in the link. Iteration through the chain of |
| 696 | /// DeclContexts should begin at the primary DeclContext and |
| 697 | /// continue until this function returns NULL. For example, given: |
| 698 | /// @code |
| 699 | /// namespace N { |
| 700 | /// int x; |
| 701 | /// } |
| 702 | /// namespace N { |
| 703 | /// int y; |
| 704 | /// } |
| 705 | /// @endcode |
| 706 | /// The first occurrence of namespace N will be the primary |
| 707 | /// DeclContext. Its getNextContext will return the second |
| 708 | /// occurrence of namespace N. |
| 709 | DeclContext *getNextContext(); |
| 710 | |
| 711 | /// decl_iterator - Iterates through the declarations stored |
| 712 | /// within this context. |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 713 | class decl_iterator { |
| 714 | /// Current - The current declaration. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 715 | Decl *Current; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 716 | |
| 717 | public: |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 718 | typedef Decl* value_type; |
| 719 | typedef Decl* reference; |
| 720 | typedef Decl* pointer; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 721 | typedef std::forward_iterator_tag iterator_category; |
| 722 | typedef std::ptrdiff_t difference_type; |
| 723 | |
| 724 | decl_iterator() : Current(0) { } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 725 | explicit decl_iterator(Decl *C) : Current(C) { } |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 726 | |
| 727 | reference operator*() const { return Current; } |
| 728 | pointer operator->() const { return Current; } |
| 729 | |
Chris Lattner | 96f4468 | 2009-03-28 05:59:45 +0000 | [diff] [blame] | 730 | decl_iterator& operator++() { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 731 | Current = Current->getNextDeclInContext(); |
Chris Lattner | 96f4468 | 2009-03-28 05:59:45 +0000 | [diff] [blame] | 732 | return *this; |
| 733 | } |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 734 | |
| 735 | decl_iterator operator++(int) { |
| 736 | decl_iterator tmp(*this); |
| 737 | ++(*this); |
| 738 | return tmp; |
| 739 | } |
| 740 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | friend bool operator==(decl_iterator x, decl_iterator y) { |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 742 | return x.Current == y.Current; |
| 743 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | friend bool operator!=(decl_iterator x, decl_iterator y) { |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 745 | return x.Current != y.Current; |
| 746 | } |
| 747 | }; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 748 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 749 | /// decls_begin/decls_end - Iterate over the declarations stored in |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | /// this context. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 751 | decl_iterator decls_begin() const; |
| 752 | decl_iterator decls_end() const; |
| 753 | bool decls_empty() const; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 754 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 755 | /// specific_decl_iterator - Iterates over a subrange of |
| 756 | /// declarations stored in a DeclContext, providing only those that |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 757 | /// are of type SpecificDecl (or a class derived from it). This |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 758 | /// iterator is used, for example, to provide iteration over just |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 759 | /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 760 | template<typename SpecificDecl> |
| 761 | class specific_decl_iterator { |
| 762 | /// Current - The current, underlying declaration iterator, which |
Douglas Gregor | d6f0b4e | 2009-02-02 17:56:05 +0000 | [diff] [blame] | 763 | /// will either be NULL or will point to a declaration of |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 764 | /// type SpecificDecl. |
| 765 | DeclContext::decl_iterator Current; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 767 | /// SkipToNextDecl - Advances the current position up to the next |
| 768 | /// declaration of type SpecificDecl that also meets the criteria |
| 769 | /// required by Acceptable. |
| 770 | void SkipToNextDecl() { |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 771 | while (*Current && !isa<SpecificDecl>(*Current)) |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 772 | ++Current; |
| 773 | } |
| 774 | |
| 775 | public: |
| 776 | typedef SpecificDecl* value_type; |
| 777 | typedef SpecificDecl* reference; |
| 778 | typedef SpecificDecl* pointer; |
| 779 | typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type |
| 780 | difference_type; |
| 781 | typedef std::forward_iterator_tag iterator_category; |
| 782 | |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 783 | specific_decl_iterator() : Current() { } |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 784 | |
| 785 | /// specific_decl_iterator - Construct a new iterator over a |
Douglas Gregor | d6f0b4e | 2009-02-02 17:56:05 +0000 | [diff] [blame] | 786 | /// subset of the declarations the range [C, |
| 787 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
| 788 | /// member function of SpecificDecl that should return true for |
| 789 | /// all of the SpecificDecl instances that will be in the subset |
| 790 | /// of iterators. For example, if you want Objective-C instance |
| 791 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
| 792 | /// &ObjCMethodDecl::isInstanceMethod. |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 793 | explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 794 | SkipToNextDecl(); |
| 795 | } |
| 796 | |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 797 | reference operator*() const { return cast<SpecificDecl>(*Current); } |
| 798 | pointer operator->() const { return cast<SpecificDecl>(*Current); } |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 799 | |
| 800 | specific_decl_iterator& operator++() { |
| 801 | ++Current; |
| 802 | SkipToNextDecl(); |
| 803 | return *this; |
| 804 | } |
| 805 | |
| 806 | specific_decl_iterator operator++(int) { |
| 807 | specific_decl_iterator tmp(*this); |
| 808 | ++(*this); |
| 809 | return tmp; |
| 810 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 812 | friend bool |
| 813 | operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) { |
| 814 | return x.Current == y.Current; |
| 815 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
| 817 | friend bool |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 818 | operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) { |
| 819 | return x.Current != y.Current; |
| 820 | } |
| 821 | }; |
| 822 | |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 823 | /// \brief Iterates over a filtered subrange of declarations stored |
| 824 | /// in a DeclContext. |
| 825 | /// |
| 826 | /// This iterator visits only those declarations that are of type |
| 827 | /// SpecificDecl (or a class derived from it) and that meet some |
| 828 | /// additional run-time criteria. This iterator is used, for |
| 829 | /// example, to provide access to the instance methods within an |
| 830 | /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and |
| 831 | /// Acceptable = ObjCMethodDecl::isInstanceMethod). |
| 832 | template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> |
| 833 | class filtered_decl_iterator { |
| 834 | /// Current - The current, underlying declaration iterator, which |
| 835 | /// will either be NULL or will point to a declaration of |
| 836 | /// type SpecificDecl. |
| 837 | DeclContext::decl_iterator Current; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 839 | /// SkipToNextDecl - Advances the current position up to the next |
| 840 | /// declaration of type SpecificDecl that also meets the criteria |
| 841 | /// required by Acceptable. |
| 842 | void SkipToNextDecl() { |
| 843 | while (*Current && |
| 844 | (!isa<SpecificDecl>(*Current) || |
| 845 | (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) |
| 846 | ++Current; |
| 847 | } |
| 848 | |
| 849 | public: |
| 850 | typedef SpecificDecl* value_type; |
| 851 | typedef SpecificDecl* reference; |
| 852 | typedef SpecificDecl* pointer; |
| 853 | typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type |
| 854 | difference_type; |
| 855 | typedef std::forward_iterator_tag iterator_category; |
| 856 | |
| 857 | filtered_decl_iterator() : Current() { } |
| 858 | |
| 859 | /// specific_decl_iterator - Construct a new iterator over a |
| 860 | /// subset of the declarations the range [C, |
| 861 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
| 862 | /// member function of SpecificDecl that should return true for |
| 863 | /// all of the SpecificDecl instances that will be in the subset |
| 864 | /// of iterators. For example, if you want Objective-C instance |
| 865 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
| 866 | /// &ObjCMethodDecl::isInstanceMethod. |
| 867 | explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
| 868 | SkipToNextDecl(); |
| 869 | } |
| 870 | |
| 871 | reference operator*() const { return cast<SpecificDecl>(*Current); } |
| 872 | pointer operator->() const { return cast<SpecificDecl>(*Current); } |
| 873 | |
| 874 | filtered_decl_iterator& operator++() { |
| 875 | ++Current; |
| 876 | SkipToNextDecl(); |
| 877 | return *this; |
| 878 | } |
| 879 | |
| 880 | filtered_decl_iterator operator++(int) { |
| 881 | filtered_decl_iterator tmp(*this); |
| 882 | ++(*this); |
| 883 | return tmp; |
| 884 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 885 | |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 886 | friend bool |
| 887 | operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { |
| 888 | return x.Current == y.Current; |
| 889 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | |
| 891 | friend bool |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 892 | operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { |
| 893 | return x.Current != y.Current; |
| 894 | } |
| 895 | }; |
| 896 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 897 | /// @brief Add the declaration D into this context. |
| 898 | /// |
| 899 | /// This routine should be invoked when the declaration D has first |
| 900 | /// been declared, to place D into the context where it was |
| 901 | /// (lexically) defined. Every declaration must be added to one |
| 902 | /// (and only one!) context, where it can be visited via |
| 903 | /// [decls_begin(), decls_end()). Once a declaration has been added |
| 904 | /// to its lexical context, the corresponding DeclContext owns the |
| 905 | /// declaration. |
| 906 | /// |
| 907 | /// If D is also a NamedDecl, it will be made visible within its |
| 908 | /// semantic context via makeDeclVisibleInContext. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 909 | void addDecl(Decl *D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 910 | |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 911 | /// @brief Add the declaration D to this context without modifying |
| 912 | /// any lookup tables. |
| 913 | /// |
| 914 | /// This is useful for some operations in dependent contexts where |
| 915 | /// the semantic context might not be dependent; this basically |
| 916 | /// only happens with friends. |
| 917 | void addHiddenDecl(Decl *D); |
| 918 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 919 | /// @brief Removes a declaration from this context. |
| 920 | void removeDecl(Decl *D); |
| 921 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 922 | /// lookup_iterator - An iterator that provides access to the results |
| 923 | /// of looking up a name within this context. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 924 | typedef NamedDecl **lookup_iterator; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 925 | |
| 926 | /// lookup_const_iterator - An iterator that provides non-mutable |
| 927 | /// access to the results of lookup up a name within this context. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 928 | typedef NamedDecl * const * lookup_const_iterator; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 929 | |
| 930 | typedef std::pair<lookup_iterator, lookup_iterator> lookup_result; |
| 931 | typedef std::pair<lookup_const_iterator, lookup_const_iterator> |
| 932 | lookup_const_result; |
| 933 | |
| 934 | /// lookup - Find the declarations (if any) with the given Name in |
| 935 | /// this context. Returns a range of iterators that contains all of |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 936 | /// the declarations with this name, with object, function, member, |
| 937 | /// and enumerator names preceding any tag name. Note that this |
| 938 | /// routine will not look into parent contexts. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 939 | lookup_result lookup(DeclarationName Name); |
| 940 | lookup_const_result lookup(DeclarationName Name) const; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 941 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 942 | /// @brief Makes a declaration visible within this context. |
| 943 | /// |
| 944 | /// This routine makes the declaration D visible to name lookup |
| 945 | /// within this context and, if this is a transparent context, |
| 946 | /// within its parent contexts up to the first enclosing |
| 947 | /// non-transparent context. Making a declaration visible within a |
| 948 | /// context does not transfer ownership of a declaration, and a |
| 949 | /// declaration can be visible in many contexts that aren't its |
| 950 | /// lexical context. |
| 951 | /// |
| 952 | /// If D is a redeclaration of an existing declaration that is |
| 953 | /// visible from this context, as determined by |
| 954 | /// NamedDecl::declarationReplaces, the previous declaration will be |
| 955 | /// replaced with D. |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 956 | /// |
| 957 | /// @param Recoverable true if it's okay to not add this decl to |
| 958 | /// the lookup tables because it can be easily recovered by walking |
| 959 | /// the declaration chains. |
| 960 | void makeDeclVisibleInContext(NamedDecl *D, bool Recoverable = true); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 961 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 962 | /// udir_iterator - Iterates through the using-directives stored |
| 963 | /// within this context. |
| 964 | typedef UsingDirectiveDecl * const * udir_iterator; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 966 | typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range; |
| 967 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 968 | udir_iterator_range getUsingDirectives() const; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 969 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 970 | udir_iterator using_directives_begin() const { |
| 971 | return getUsingDirectives().first; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 974 | udir_iterator using_directives_end() const { |
| 975 | return getUsingDirectives().second; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 978 | // Low-level accessors |
| 979 | |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 980 | /// \brief Retrieve the internal representation of the lookup structure. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 981 | void* getLookupPtr() const { return LookupPtr; } |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 982 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 983 | /// \brief Whether this DeclContext has external storage containing |
| 984 | /// additional declarations that are lexically in this context. |
| 985 | bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; } |
| 986 | |
| 987 | /// \brief State whether this DeclContext has external storage for |
| 988 | /// declarations lexically in this context. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | void setHasExternalLexicalStorage(bool ES = true) { |
| 990 | ExternalLexicalStorage = ES; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /// \brief Whether this DeclContext has external storage containing |
| 994 | /// additional declarations that are visible in this context. |
| 995 | bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; } |
| 996 | |
| 997 | /// \brief State whether this DeclContext has external storage for |
| 998 | /// declarations visible in this context. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | void setHasExternalVisibleStorage(bool ES = true) { |
| 1000 | ExternalVisibleStorage = ES; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 1003 | static bool classof(const Decl *D); |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1004 | static bool classof(const DeclContext *D) { return true; } |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 1005 | #define DECL_CONTEXT(Name) \ |
| 1006 | static bool classof(const Name##Decl *D) { return true; } |
| 1007 | #include "clang/AST/DeclNodes.def" |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1008 | |
Anders Carlsson | 8483443 | 2009-12-14 00:51:04 +0000 | [diff] [blame] | 1009 | void dumpDeclContext() const; |
Anders Carlsson | 2b7d8dd | 2009-12-09 17:27:46 +0000 | [diff] [blame] | 1010 | |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 1011 | private: |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1012 | void LoadLexicalDeclsFromExternalStorage() const; |
| 1013 | void LoadVisibleDeclsFromExternalStorage() const; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1014 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1015 | void buildLookup(DeclContext *DCtx); |
| 1016 | void makeDeclVisibleInContextImpl(NamedDecl *D); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1017 | }; |
| 1018 | |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 1019 | inline bool Decl::isTemplateParameter() const { |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1020 | return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm || |
| 1021 | getKind() == TemplateTemplateParm; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
John McCall | 80cd64a | 2010-01-29 01:45:37 +0000 | [diff] [blame^] | 1024 | |
| 1025 | // Specialization selected when ToTy is not a known subclass of DeclContext. |
| 1026 | template <class ToTy, |
| 1027 | bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value> |
| 1028 | struct cast_convert_decl_context { |
| 1029 | static const ToTy *doit(const DeclContext *Val) { |
| 1030 | return static_cast<const ToTy*>(Decl::castFromDeclContext(Val)); |
| 1031 | } |
| 1032 | |
| 1033 | static ToTy *doit(DeclContext *Val) { |
| 1034 | return static_cast<ToTy*>(Decl::castFromDeclContext(Val)); |
| 1035 | } |
| 1036 | }; |
| 1037 | |
| 1038 | // Specialization selected when ToTy is a known subclass of DeclContext. |
| 1039 | template <class ToTy> |
| 1040 | struct cast_convert_decl_context<ToTy, true> { |
| 1041 | static const ToTy *doit(const DeclContext *Val) { |
| 1042 | return static_cast<const ToTy*>(Val); |
| 1043 | } |
| 1044 | |
| 1045 | static ToTy *doit(DeclContext *Val) { |
| 1046 | return static_cast<ToTy*>(Val); |
| 1047 | } |
| 1048 | }; |
| 1049 | |
| 1050 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1051 | } // end clang. |
| 1052 | |
| 1053 | namespace llvm { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1054 | |
John McCall | 80cd64a | 2010-01-29 01:45:37 +0000 | [diff] [blame^] | 1055 | /// isa<T>(DeclContext*) |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1056 | template<class ToTy> |
| 1057 | struct isa_impl_wrap<ToTy, |
| 1058 | const ::clang::DeclContext,const ::clang::DeclContext> { |
| 1059 | static bool doit(const ::clang::DeclContext &Val) { |
John McCall | 80cd64a | 2010-01-29 01:45:37 +0000 | [diff] [blame^] | 1060 | return ToTy::classofKind(Val.getDeclKind()); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1061 | } |
| 1062 | }; |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1063 | template<class ToTy> |
| 1064 | struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> |
| 1065 | : public isa_impl_wrap<ToTy, |
| 1066 | const ::clang::DeclContext,const ::clang::DeclContext> {}; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1067 | |
John McCall | 80cd64a | 2010-01-29 01:45:37 +0000 | [diff] [blame^] | 1068 | /// cast<T>(DeclContext*) |
| 1069 | template<class ToTy> |
| 1070 | struct cast_convert_val<ToTy, |
| 1071 | const ::clang::DeclContext,const ::clang::DeclContext> { |
| 1072 | static const ToTy &doit(const ::clang::DeclContext &Val) { |
| 1073 | return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); |
| 1074 | } |
| 1075 | }; |
| 1076 | template<class ToTy> |
| 1077 | struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> { |
| 1078 | static ToTy &doit(::clang::DeclContext &Val) { |
| 1079 | return *::clang::cast_convert_decl_context<ToTy>::doit(&Val); |
| 1080 | } |
| 1081 | }; |
| 1082 | template<class ToTy> |
| 1083 | struct cast_convert_val<ToTy, |
| 1084 | const ::clang::DeclContext*, const ::clang::DeclContext*> { |
| 1085 | static const ToTy *doit(const ::clang::DeclContext *Val) { |
| 1086 | return ::clang::cast_convert_decl_context<ToTy>::doit(Val); |
| 1087 | } |
| 1088 | }; |
| 1089 | template<class ToTy> |
| 1090 | struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> { |
| 1091 | static ToTy *doit(::clang::DeclContext *Val) { |
| 1092 | return ::clang::cast_convert_decl_context<ToTy>::doit(Val); |
| 1093 | } |
| 1094 | }; |
| 1095 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1096 | /// Implement cast_convert_val for Decl -> DeclContext conversions. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1097 | template<class FromTy> |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1098 | struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1099 | static ::clang::DeclContext &doit(const FromTy &Val) { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1100 | return *FromTy::castToDeclContext(&Val); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1101 | } |
| 1102 | }; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1103 | |
| 1104 | template<class FromTy> |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1105 | struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1106 | static ::clang::DeclContext *doit(const FromTy *Val) { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1107 | return FromTy::castToDeclContext(Val); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1108 | } |
| 1109 | }; |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 1110 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1111 | template<class FromTy> |
| 1112 | struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { |
| 1113 | static const ::clang::DeclContext &doit(const FromTy &Val) { |
| 1114 | return *FromTy::castToDeclContext(&Val); |
| 1115 | } |
| 1116 | }; |
| 1117 | |
| 1118 | template<class FromTy> |
| 1119 | struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { |
| 1120 | static const ::clang::DeclContext *doit(const FromTy *Val) { |
| 1121 | return FromTy::castToDeclContext(Val); |
| 1122 | } |
| 1123 | }; |
| 1124 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1125 | } // end namespace llvm |
| 1126 | |
| 1127 | #endif |