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" |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 19 | // FIXME: Layering violation |
| 20 | #include "clang/Parse/AccessSpecifier.h" |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 21 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/PointerUnion.h" |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 23 | |
| 24 | namespace clang { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 25 | class DeclContext; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 26 | class TranslationUnitDecl; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 27 | class NamespaceDecl; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 28 | class UsingDirectiveDecl; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 29 | class NamedDecl; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 30 | class FunctionDecl; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 31 | class CXXRecordDecl; |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 32 | class EnumDecl; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 33 | class ObjCMethodDecl; |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 34 | class ObjCContainerDecl; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 35 | class ObjCInterfaceDecl; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 36 | class ObjCCategoryDecl; |
| 37 | class ObjCProtocolDecl; |
| 38 | class ObjCImplementationDecl; |
| 39 | class ObjCCategoryImplDecl; |
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 | |
| 62 | /// Decl - This represents one declaration (or definition), e.g. a variable, |
| 63 | /// typedef, function, struct, etc. |
| 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) \ |
| 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" |
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 |
| 81 | /// use additional namespaces for Objective-C entities. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 82 | enum IdentifierNamespace { |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 83 | IDNS_Label = 0x1, |
| 84 | IDNS_Tag = 0x2, |
| 85 | IDNS_Member = 0x4, |
Douglas Gregor | 7dda67d | 2009-02-05 19:25:20 +0000 | [diff] [blame] | 86 | IDNS_Ordinary = 0x8, |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 87 | IDNS_ObjCProtocol = 0x10, |
| 88 | IDNS_ObjCImplementation = 0x20, |
| 89 | IDNS_ObjCCategoryImpl = 0x40 |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | /// ObjCDeclQualifier - Qualifier used on types in method declarations |
| 93 | /// for remote messaging. They are meant for the arguments though and |
| 94 | /// applied to the Decls (ObjCMethodDecl and ParmVarDecl). |
| 95 | enum ObjCDeclQualifier { |
| 96 | OBJC_TQ_None = 0x0, |
| 97 | OBJC_TQ_In = 0x1, |
| 98 | OBJC_TQ_Inout = 0x2, |
| 99 | OBJC_TQ_Out = 0x4, |
| 100 | OBJC_TQ_Bycopy = 0x8, |
| 101 | OBJC_TQ_Byref = 0x10, |
| 102 | OBJC_TQ_Oneway = 0x20 |
| 103 | }; |
| 104 | |
| 105 | private: |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 106 | /// NextDeclInContext - The next declaration within the same lexical |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 107 | /// DeclContext. These pointers form the linked list that is |
| 108 | /// traversed via DeclContext's decls_begin()/decls_end(). |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 109 | Decl *NextDeclInContext; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 110 | |
| 111 | friend class DeclContext; |
| 112 | |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 113 | struct MultipleDC { |
| 114 | DeclContext *SemanticDC; |
| 115 | DeclContext *LexicalDC; |
| 116 | }; |
| 117 | |
| 118 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 119 | /// DeclCtx - Holds either a DeclContext* or a MultipleDC*. |
| 120 | /// For declarations that don't contain C++ scope specifiers, it contains |
| 121 | /// the DeclContext where the Decl was declared. |
| 122 | /// For declarations with C++ scope specifiers, it contains a MultipleDC* |
| 123 | /// with the context where it semantically belongs (SemanticDC) and the |
| 124 | /// context where it was lexically declared (LexicalDC). |
| 125 | /// e.g.: |
| 126 | /// |
| 127 | /// namespace A { |
| 128 | /// void f(); // SemanticDC == LexicalDC == 'namespace A' |
| 129 | /// } |
| 130 | /// void A::f(); // SemanticDC == namespace 'A' |
| 131 | /// // LexicalDC == global namespace |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 132 | llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 133 | |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 134 | inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); } |
| 135 | inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 136 | inline MultipleDC *getMultipleDC() const { |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 137 | return DeclCtx.get<MultipleDC*>(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 138 | } |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 139 | inline DeclContext *getSemanticDC() const { |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 140 | return DeclCtx.get<DeclContext*>(); |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Daniel Dunbar | 39d7650 | 2009-03-04 02:26:41 +0000 | [diff] [blame] | 143 | /// Loc - The location that this decl. |
| 144 | SourceLocation Loc; |
| 145 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 146 | /// DeclKind - This indicates which class this is. |
| 147 | Kind DeclKind : 8; |
| 148 | |
| 149 | /// InvalidDecl - This indicates a semantic error occurred. |
| 150 | unsigned int InvalidDecl : 1; |
| 151 | |
| 152 | /// HasAttrs - This indicates whether the decl has attributes or not. |
| 153 | unsigned int HasAttrs : 1; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 154 | |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 155 | /// Implicit - Whether this declaration was implicitly generated by |
| 156 | /// the implementation rather than explicitly written by the user. |
| 157 | bool Implicit : 1; |
| 158 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 159 | /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in. |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 160 | unsigned IdentifierNamespace : 8; |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 161 | |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 162 | #ifndef NDEBUG |
| 163 | void CheckAccessDeclContext() const; |
| 164 | #else |
| 165 | void CheckAccessDeclContext() const { } |
| 166 | #endif |
| 167 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 168 | protected: |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 169 | /// Access - Used by C++ decls for the access specifier. |
| 170 | // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum |
| 171 | unsigned Access : 2; |
| 172 | friend class CXXClassMemberWrapper; |
| 173 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 174 | Decl(Kind DK, DeclContext *DC, SourceLocation L) |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 175 | : NextDeclInContext(0), DeclCtx(DC), |
Daniel Dunbar | 39d7650 | 2009-03-04 02:26:41 +0000 | [diff] [blame] | 176 | Loc(L), DeclKind(DK), InvalidDecl(0), |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 177 | HasAttrs(false), Implicit(false), |
| 178 | IdentifierNamespace(getIdentifierNamespaceForKind(DK)), Access(AS_none) { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 179 | if (Decl::CollectingStats()) addDeclKind(DK); |
| 180 | } |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 182 | virtual ~Decl(); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 183 | |
| 184 | public: |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 185 | SourceLocation getLocation() const { return Loc; } |
| 186 | void setLocation(SourceLocation L) { Loc = L; } |
| 187 | |
| 188 | Kind getKind() const { return DeclKind; } |
| 189 | const char *getDeclKindName() const; |
| 190 | |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 191 | Decl *getNextDeclInContext() { return NextDeclInContext; } |
| 192 | const Decl *getNextDeclInContext() const { return NextDeclInContext; } |
Chris Lattner | 96f4468 | 2009-03-28 05:59:45 +0000 | [diff] [blame] | 193 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 194 | DeclContext *getDeclContext() { |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 195 | if (isInSemaDC()) |
| 196 | return getSemanticDC(); |
| 197 | return getMultipleDC()->SemanticDC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 198 | } |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 199 | const DeclContext *getDeclContext() const { |
| 200 | return const_cast<Decl*>(this)->getDeclContext(); |
| 201 | } |
| 202 | |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 203 | void setAccess(AccessSpecifier AS) { |
Anders Carlsson | b8547e8 | 2009-03-25 20:19:57 +0000 | [diff] [blame] | 204 | Access = AS; |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 205 | CheckAccessDeclContext(); |
Anders Carlsson | b8547e8 | 2009-03-25 20:19:57 +0000 | [diff] [blame] | 206 | } |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 207 | |
| 208 | AccessSpecifier getAccess() const { |
| 209 | CheckAccessDeclContext(); |
| 210 | return AccessSpecifier(Access); |
| 211 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 213 | bool hasAttrs() const { return HasAttrs; } |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 214 | void addAttr(Attr *attr); |
Chris Lattner | 81abbdd | 2009-03-21 06:27:31 +0000 | [diff] [blame] | 215 | const Attr *getAttrs() const { |
| 216 | if (!HasAttrs) return 0; // common case, no attributes. |
| 217 | return getAttrsImpl(); // Uncommon case, out of line hash lookup. |
| 218 | } |
Chris Lattner | a212c56 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 219 | void swapAttrs(Decl *D); |
Nuno Lopes | 9141bee | 2008-06-01 22:53:53 +0000 | [diff] [blame] | 220 | void invalidateAttrs(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 221 | |
| 222 | template<typename T> const T *getAttr() const { |
| 223 | for (const Attr *attr = getAttrs(); attr; attr = attr->getNext()) |
| 224 | if (const T *V = dyn_cast<T>(attr)) |
| 225 | return V; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
Chris Lattner | 115cafc | 2009-04-12 20:07:59 +0000 | [diff] [blame] | 229 | template<typename T> bool hasAttr() const { |
| 230 | return getAttr<T>() != 0; |
| 231 | } |
| 232 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 233 | /// setInvalidDecl - Indicates the Decl had a semantic error. This |
| 234 | /// allows for graceful error recovery. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 235 | void setInvalidDecl(bool Invalid = true) { InvalidDecl = Invalid; } |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 236 | bool isInvalidDecl() const { return (bool) InvalidDecl; } |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 237 | |
| 238 | /// isImplicit - Indicates whether the declaration was implicitly |
| 239 | /// generated by the implementation. If false, this declaration |
| 240 | /// was written explicitly in the source code. |
| 241 | bool isImplicit() const { return Implicit; } |
| 242 | void setImplicit(bool I = true) { Implicit = I; } |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 243 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 244 | unsigned getIdentifierNamespace() const { |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 245 | return IdentifierNamespace; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 246 | } |
Chris Lattner | d62fdc4 | 2009-01-06 07:16:40 +0000 | [diff] [blame] | 247 | bool isInIdentifierNamespace(unsigned NS) const { |
| 248 | return getIdentifierNamespace() & NS; |
| 249 | } |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 250 | static unsigned getIdentifierNamespaceForKind(Kind DK); |
| 251 | |
Chris Lattner | d62fdc4 | 2009-01-06 07:16:40 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 253 | /// getLexicalDeclContext - The declaration context where this Decl was |
| 254 | /// lexically declared (LexicalDC). May be different from |
| 255 | /// getDeclContext() (SemanticDC). |
| 256 | /// e.g.: |
| 257 | /// |
| 258 | /// namespace A { |
| 259 | /// void f(); // SemanticDC == LexicalDC == 'namespace A' |
| 260 | /// } |
| 261 | /// void A::f(); // SemanticDC == namespace 'A' |
| 262 | /// // LexicalDC == global namespace |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 263 | DeclContext *getLexicalDeclContext() { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 264 | if (isInSemaDC()) |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 265 | return getSemanticDC(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 266 | return getMultipleDC()->LexicalDC; |
| 267 | } |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 268 | const DeclContext *getLexicalDeclContext() const { |
| 269 | return const_cast<Decl*>(this)->getLexicalDeclContext(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 270 | } |
Chris Lattner | 10d8379 | 2009-03-27 18:46:15 +0000 | [diff] [blame] | 271 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 272 | /// setDeclContext - Set both the semantic and lexical DeclContext |
| 273 | /// to DC. |
| 274 | void setDeclContext(DeclContext *DC); |
| 275 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 276 | void setLexicalDeclContext(DeclContext *DC); |
| 277 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 278 | // isDefinedOutsideFunctionOrMethod - This predicate returns true if this |
| 279 | // scoped decl is defined outside the current function or method. This is |
| 280 | // roughly global variables and functions, but also handles enums (which could |
| 281 | // be defined inside or outside a function etc). |
| 282 | bool isDefinedOutsideFunctionOrMethod() const; |
| 283 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 284 | /// getBody - If this Decl represents a declaration for a body of code, |
| 285 | /// such as a function or method definition, this method returns the |
| 286 | /// top-level Stmt* of that body. Otherwise this method returns null. |
| 287 | virtual Stmt* getBody(ASTContext &Context) const { return 0; } |
| 288 | |
| 289 | /// getCompoundBody - Returns getBody(), dyn_casted to a CompoundStmt. |
| 290 | CompoundStmt* getCompoundBody(ASTContext &Context) const; |
| 291 | |
| 292 | /// getBodyRBrace - Gets the right brace of the body, if a body exists. |
| 293 | /// This works whether the body is a CompoundStmt or a CXXTryStmt. |
| 294 | SourceLocation getBodyRBrace(ASTContext &Context) const; |
| 295 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 296 | // global temp stats (until we have a per-module visitor) |
| 297 | static void addDeclKind(Kind k); |
| 298 | static bool CollectingStats(bool Enable = false); |
| 299 | static void PrintStats(); |
| 300 | |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 301 | /// isTemplateParameter - Determines whether this declartion is a |
| 302 | /// template parameter. |
| 303 | bool isTemplateParameter() const; |
| 304 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 305 | // Implement isa/cast/dyncast/etc. |
| 306 | static bool classof(const Decl *) { return true; } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 307 | static DeclContext *castToDeclContext(const Decl *); |
| 308 | static Decl *castFromDeclContext(const DeclContext *); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 309 | |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 310 | /// Destroy - Call destructors and release memory. |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 311 | virtual void Destroy(ASTContext& C); |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 81abbdd | 2009-03-21 06:27:31 +0000 | [diff] [blame] | 313 | private: |
| 314 | const Attr *getAttrsImpl() const; |
| 315 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 316 | }; |
| 317 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 318 | /// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when |
| 319 | /// doing something to a specific decl. |
| 320 | class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry { |
| 321 | Decl *TheDecl; |
| 322 | SourceLocation Loc; |
| 323 | SourceManager &SM; |
| 324 | const char *Message; |
| 325 | public: |
| 326 | PrettyStackTraceDecl(Decl *theDecl, SourceLocation L, |
| 327 | SourceManager &sm, const char *Msg) |
| 328 | : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {} |
| 329 | |
| 330 | virtual void print(llvm::raw_ostream &OS) const; |
| 331 | }; |
| 332 | |
| 333 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 334 | /// 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] | 335 | /// can act as declaration contexts. These decls are (only the top classes |
| 336 | /// that directly derive from DeclContext are mentioned, not their subclasses): |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 337 | /// |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 338 | /// TranslationUnitDecl |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 339 | /// NamespaceDecl |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 340 | /// FunctionDecl |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 341 | /// TagDecl |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 342 | /// ObjCMethodDecl |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 343 | /// ObjCContainerDecl |
| 344 | /// ObjCCategoryImplDecl |
| 345 | /// ObjCImplementationDecl |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 346 | /// LinkageSpecDecl |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 347 | /// BlockDecl |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 348 | /// |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 349 | class DeclContext { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 350 | /// DeclKind - This indicates which class this is. |
| 351 | Decl::Kind DeclKind : 8; |
| 352 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 353 | /// \brief Whether this declaration context also has some external |
| 354 | /// storage that contains additional declarations that are lexically |
| 355 | /// part of this context. |
| 356 | mutable bool ExternalLexicalStorage : 1; |
| 357 | |
| 358 | /// \brief Whether this declaration context also has some external |
| 359 | /// storage that contains additional declarations that are visible |
| 360 | /// in this context. |
| 361 | mutable bool ExternalVisibleStorage : 1; |
| 362 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 363 | /// \brief Pointer to the data structure used to lookup declarations |
| 364 | /// within this context, which is a DenseMap<DeclarationName, |
| 365 | /// StoredDeclsList>. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 366 | mutable void* LookupPtr; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 367 | |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 368 | /// FirstDecl - The first declaration stored within this declaration |
| 369 | /// context. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 370 | mutable Decl *FirstDecl; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 371 | |
| 372 | /// LastDecl - The last declaration stored within this declaration |
| 373 | /// context. FIXME: We could probably cache this value somewhere |
| 374 | /// outside of the DeclContext, to reduce the size of DeclContext by |
| 375 | /// another pointer. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 376 | mutable Decl *LastDecl; |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 378 | protected: |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 379 | DeclContext(Decl::Kind K) |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 380 | : DeclKind(K), ExternalLexicalStorage(false), |
| 381 | ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0), |
| 382 | LastDecl(0) { } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 383 | |
| 384 | void DestroyDecls(ASTContext &C); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 385 | |
| 386 | public: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 387 | ~DeclContext(); |
| 388 | |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 389 | Decl::Kind getDeclKind() const { |
| 390 | return DeclKind; |
| 391 | } |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 392 | const char *getDeclKindName() const; |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 393 | |
Argyrios Kyrtzidis | 305ec42 | 2009-02-17 20:26:05 +0000 | [diff] [blame] | 394 | /// getParent - Returns the containing DeclContext. |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 395 | DeclContext *getParent() { |
Argyrios Kyrtzidis | 305ec42 | 2009-02-17 20:26:05 +0000 | [diff] [blame] | 396 | return cast<Decl>(this)->getDeclContext(); |
| 397 | } |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 398 | const DeclContext *getParent() const { |
| 399 | return const_cast<DeclContext*>(this)->getParent(); |
Argyrios Kyrtzidis | d2595ec | 2008-10-12 18:40:01 +0000 | [diff] [blame] | 400 | } |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 401 | |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 402 | /// getLexicalParent - Returns the containing lexical DeclContext. May be |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 403 | /// different from getParent, e.g.: |
| 404 | /// |
| 405 | /// namespace A { |
| 406 | /// struct S; |
| 407 | /// } |
| 408 | /// struct A::S {}; // getParent() == namespace 'A' |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 409 | /// // getLexicalParent() == translation unit |
| 410 | /// |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 411 | DeclContext *getLexicalParent() { |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 412 | return cast<Decl>(this)->getLexicalDeclContext(); |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 413 | } |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 414 | const DeclContext *getLexicalParent() const { |
| 415 | return const_cast<DeclContext*>(this)->getLexicalParent(); |
| 416 | } |
| 417 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 418 | bool isFunctionOrMethod() const { |
| 419 | switch (DeclKind) { |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 420 | case Decl::Block: |
| 421 | case Decl::ObjCMethod: |
| 422 | return true; |
| 423 | default: |
| 424 | return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 428 | bool isFileContext() const { |
| 429 | return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace; |
| 430 | } |
| 431 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 432 | bool isTranslationUnit() const { |
| 433 | return DeclKind == Decl::TranslationUnit; |
| 434 | } |
| 435 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 436 | bool isRecord() const { |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 437 | return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast; |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 440 | bool isNamespace() const { |
| 441 | return DeclKind == Decl::Namespace; |
| 442 | } |
| 443 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame^] | 444 | /// \brief Determines whether this context is dependent on a |
| 445 | /// template parameter. |
| 446 | bool isDependentContext() const; |
| 447 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 448 | /// isTransparentContext - Determines whether this context is a |
| 449 | /// "transparent" context, meaning that the members declared in this |
| 450 | /// context are semantically declared in the nearest enclosing |
| 451 | /// non-transparent (opaque) context but are lexically declared in |
| 452 | /// this context. For example, consider the enumerators of an |
| 453 | /// enumeration type: |
| 454 | /// @code |
| 455 | /// enum E { |
| 456 | /// Val1 |
| 457 | /// }; |
| 458 | /// @endcode |
| 459 | /// Here, E is a transparent context, so its enumerator (Val1) will |
| 460 | /// appear (semantically) that it is in the same context of E. |
| 461 | /// Examples of transparent contexts include: enumerations (except for |
| 462 | /// C++0x scoped enums), C++ linkage specifications, and C++0x |
| 463 | /// inline namespaces. |
| 464 | bool isTransparentContext() const; |
| 465 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 466 | bool Encloses(DeclContext *DC) const { |
| 467 | for (; DC; DC = DC->getParent()) |
| 468 | if (DC == this) |
| 469 | return true; |
| 470 | return false; |
| 471 | } |
| 472 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 473 | /// getPrimaryContext - There may be many different |
| 474 | /// declarations of the same entity (including forward declarations |
| 475 | /// of classes, multiple definitions of namespaces, etc.), each with |
| 476 | /// a different set of declarations. This routine returns the |
| 477 | /// "primary" DeclContext structure, which will contain the |
| 478 | /// information needed to perform name lookup into this context. |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 479 | DeclContext *getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 480 | |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 481 | /// getLookupContext - Retrieve the innermost non-transparent |
| 482 | /// context of this context, which corresponds to the innermost |
| 483 | /// location from which name lookup can find the entities in this |
| 484 | /// context. |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 485 | DeclContext *getLookupContext(); |
| 486 | const DeclContext *getLookupContext() const { |
| 487 | return const_cast<DeclContext *>(this)->getLookupContext(); |
Douglas Gregor | 17a9b9e | 2009-01-07 02:48:43 +0000 | [diff] [blame] | 488 | } |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 489 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 490 | /// \brief Retrieve the nearest enclosing namespace context. |
| 491 | DeclContext *getEnclosingNamespaceContext(); |
| 492 | const DeclContext *getEnclosingNamespaceContext() const { |
| 493 | return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext(); |
| 494 | } |
| 495 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 496 | /// getNextContext - If this is a DeclContext that may have other |
| 497 | /// DeclContexts that are semantically connected but syntactically |
| 498 | /// different, such as C++ namespaces, this routine retrieves the |
| 499 | /// next DeclContext in the link. Iteration through the chain of |
| 500 | /// DeclContexts should begin at the primary DeclContext and |
| 501 | /// continue until this function returns NULL. For example, given: |
| 502 | /// @code |
| 503 | /// namespace N { |
| 504 | /// int x; |
| 505 | /// } |
| 506 | /// namespace N { |
| 507 | /// int y; |
| 508 | /// } |
| 509 | /// @endcode |
| 510 | /// The first occurrence of namespace N will be the primary |
| 511 | /// DeclContext. Its getNextContext will return the second |
| 512 | /// occurrence of namespace N. |
| 513 | DeclContext *getNextContext(); |
| 514 | |
| 515 | /// decl_iterator - Iterates through the declarations stored |
| 516 | /// within this context. |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 517 | class decl_iterator { |
| 518 | /// Current - The current declaration. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 519 | Decl *Current; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 520 | |
| 521 | public: |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 522 | typedef Decl* value_type; |
| 523 | typedef Decl* reference; |
| 524 | typedef Decl* pointer; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 525 | typedef std::forward_iterator_tag iterator_category; |
| 526 | typedef std::ptrdiff_t difference_type; |
| 527 | |
| 528 | decl_iterator() : Current(0) { } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 529 | explicit decl_iterator(Decl *C) : Current(C) { } |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 530 | |
| 531 | reference operator*() const { return Current; } |
| 532 | pointer operator->() const { return Current; } |
| 533 | |
Chris Lattner | 96f4468 | 2009-03-28 05:59:45 +0000 | [diff] [blame] | 534 | decl_iterator& operator++() { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 535 | Current = Current->getNextDeclInContext(); |
Chris Lattner | 96f4468 | 2009-03-28 05:59:45 +0000 | [diff] [blame] | 536 | return *this; |
| 537 | } |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 538 | |
| 539 | decl_iterator operator++(int) { |
| 540 | decl_iterator tmp(*this); |
| 541 | ++(*this); |
| 542 | return tmp; |
| 543 | } |
| 544 | |
| 545 | friend bool operator==(decl_iterator x, decl_iterator y) { |
| 546 | return x.Current == y.Current; |
| 547 | } |
| 548 | friend bool operator!=(decl_iterator x, decl_iterator y) { |
| 549 | return x.Current != y.Current; |
| 550 | } |
| 551 | }; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 553 | /// decls_begin/decls_end - Iterate over the declarations stored in |
| 554 | /// this context. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 555 | decl_iterator decls_begin(ASTContext &Context) const; |
| 556 | decl_iterator decls_end(ASTContext &Context) const; |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 557 | bool decls_empty(ASTContext &Context) const; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 558 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 559 | /// specific_decl_iterator - Iterates over a subrange of |
| 560 | /// declarations stored in a DeclContext, providing only those that |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 561 | /// are of type SpecificDecl (or a class derived from it). This |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 562 | /// iterator is used, for example, to provide iteration over just |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 563 | /// the fields within a RecordDecl (with SpecificDecl = FieldDecl). |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 564 | template<typename SpecificDecl> |
| 565 | class specific_decl_iterator { |
| 566 | /// Current - The current, underlying declaration iterator, which |
Douglas Gregor | d6f0b4e | 2009-02-02 17:56:05 +0000 | [diff] [blame] | 567 | /// will either be NULL or will point to a declaration of |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 568 | /// type SpecificDecl. |
| 569 | DeclContext::decl_iterator Current; |
| 570 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 571 | /// SkipToNextDecl - Advances the current position up to the next |
| 572 | /// declaration of type SpecificDecl that also meets the criteria |
| 573 | /// required by Acceptable. |
| 574 | void SkipToNextDecl() { |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 575 | while (*Current && !isa<SpecificDecl>(*Current)) |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 576 | ++Current; |
| 577 | } |
| 578 | |
| 579 | public: |
| 580 | typedef SpecificDecl* value_type; |
| 581 | typedef SpecificDecl* reference; |
| 582 | typedef SpecificDecl* pointer; |
| 583 | typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type |
| 584 | difference_type; |
| 585 | typedef std::forward_iterator_tag iterator_category; |
| 586 | |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 587 | specific_decl_iterator() : Current() { } |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 588 | |
| 589 | /// specific_decl_iterator - Construct a new iterator over a |
Douglas Gregor | d6f0b4e | 2009-02-02 17:56:05 +0000 | [diff] [blame] | 590 | /// subset of the declarations the range [C, |
| 591 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
| 592 | /// member function of SpecificDecl that should return true for |
| 593 | /// all of the SpecificDecl instances that will be in the subset |
| 594 | /// of iterators. For example, if you want Objective-C instance |
| 595 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
| 596 | /// &ObjCMethodDecl::isInstanceMethod. |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 597 | explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 598 | SkipToNextDecl(); |
| 599 | } |
| 600 | |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 601 | reference operator*() const { return cast<SpecificDecl>(*Current); } |
| 602 | pointer operator->() const { return cast<SpecificDecl>(*Current); } |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 603 | |
| 604 | specific_decl_iterator& operator++() { |
| 605 | ++Current; |
| 606 | SkipToNextDecl(); |
| 607 | return *this; |
| 608 | } |
| 609 | |
| 610 | specific_decl_iterator operator++(int) { |
| 611 | specific_decl_iterator tmp(*this); |
| 612 | ++(*this); |
| 613 | return tmp; |
| 614 | } |
| 615 | |
| 616 | friend bool |
| 617 | operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) { |
| 618 | return x.Current == y.Current; |
| 619 | } |
| 620 | |
| 621 | friend bool |
| 622 | operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) { |
| 623 | return x.Current != y.Current; |
| 624 | } |
| 625 | }; |
| 626 | |
Douglas Gregor | 669c9a2 | 2009-02-02 18:25:48 +0000 | [diff] [blame] | 627 | /// \brief Iterates over a filtered subrange of declarations stored |
| 628 | /// in a DeclContext. |
| 629 | /// |
| 630 | /// This iterator visits only those declarations that are of type |
| 631 | /// SpecificDecl (or a class derived from it) and that meet some |
| 632 | /// additional run-time criteria. This iterator is used, for |
| 633 | /// example, to provide access to the instance methods within an |
| 634 | /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and |
| 635 | /// Acceptable = ObjCMethodDecl::isInstanceMethod). |
| 636 | template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const> |
| 637 | class filtered_decl_iterator { |
| 638 | /// Current - The current, underlying declaration iterator, which |
| 639 | /// will either be NULL or will point to a declaration of |
| 640 | /// type SpecificDecl. |
| 641 | DeclContext::decl_iterator Current; |
| 642 | |
| 643 | /// SkipToNextDecl - Advances the current position up to the next |
| 644 | /// declaration of type SpecificDecl that also meets the criteria |
| 645 | /// required by Acceptable. |
| 646 | void SkipToNextDecl() { |
| 647 | while (*Current && |
| 648 | (!isa<SpecificDecl>(*Current) || |
| 649 | (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)()))) |
| 650 | ++Current; |
| 651 | } |
| 652 | |
| 653 | public: |
| 654 | typedef SpecificDecl* value_type; |
| 655 | typedef SpecificDecl* reference; |
| 656 | typedef SpecificDecl* pointer; |
| 657 | typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type |
| 658 | difference_type; |
| 659 | typedef std::forward_iterator_tag iterator_category; |
| 660 | |
| 661 | filtered_decl_iterator() : Current() { } |
| 662 | |
| 663 | /// specific_decl_iterator - Construct a new iterator over a |
| 664 | /// subset of the declarations the range [C, |
| 665 | /// end-of-declarations). If A is non-NULL, it is a pointer to a |
| 666 | /// member function of SpecificDecl that should return true for |
| 667 | /// all of the SpecificDecl instances that will be in the subset |
| 668 | /// of iterators. For example, if you want Objective-C instance |
| 669 | /// methods, SpecificDecl will be ObjCMethodDecl and A will be |
| 670 | /// &ObjCMethodDecl::isInstanceMethod. |
| 671 | explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) { |
| 672 | SkipToNextDecl(); |
| 673 | } |
| 674 | |
| 675 | reference operator*() const { return cast<SpecificDecl>(*Current); } |
| 676 | pointer operator->() const { return cast<SpecificDecl>(*Current); } |
| 677 | |
| 678 | filtered_decl_iterator& operator++() { |
| 679 | ++Current; |
| 680 | SkipToNextDecl(); |
| 681 | return *this; |
| 682 | } |
| 683 | |
| 684 | filtered_decl_iterator operator++(int) { |
| 685 | filtered_decl_iterator tmp(*this); |
| 686 | ++(*this); |
| 687 | return tmp; |
| 688 | } |
| 689 | |
| 690 | friend bool |
| 691 | operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { |
| 692 | return x.Current == y.Current; |
| 693 | } |
| 694 | |
| 695 | friend bool |
| 696 | operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) { |
| 697 | return x.Current != y.Current; |
| 698 | } |
| 699 | }; |
| 700 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 701 | /// @brief Add the declaration D into this context. |
| 702 | /// |
| 703 | /// This routine should be invoked when the declaration D has first |
| 704 | /// been declared, to place D into the context where it was |
| 705 | /// (lexically) defined. Every declaration must be added to one |
| 706 | /// (and only one!) context, where it can be visited via |
| 707 | /// [decls_begin(), decls_end()). Once a declaration has been added |
| 708 | /// to its lexical context, the corresponding DeclContext owns the |
| 709 | /// declaration. |
| 710 | /// |
| 711 | /// If D is also a NamedDecl, it will be made visible within its |
| 712 | /// semantic context via makeDeclVisibleInContext. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 713 | void addDecl(ASTContext &Context, Decl *D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 715 | /// lookup_iterator - An iterator that provides access to the results |
| 716 | /// of looking up a name within this context. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 717 | typedef NamedDecl **lookup_iterator; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 718 | |
| 719 | /// lookup_const_iterator - An iterator that provides non-mutable |
| 720 | /// access to the results of lookup up a name within this context. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 721 | typedef NamedDecl * const * lookup_const_iterator; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 722 | |
| 723 | typedef std::pair<lookup_iterator, lookup_iterator> lookup_result; |
| 724 | typedef std::pair<lookup_const_iterator, lookup_const_iterator> |
| 725 | lookup_const_result; |
| 726 | |
| 727 | /// lookup - Find the declarations (if any) with the given Name in |
| 728 | /// this context. Returns a range of iterators that contains all of |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 729 | /// the declarations with this name, with object, function, member, |
| 730 | /// and enumerator names preceding any tag name. Note that this |
| 731 | /// routine will not look into parent contexts. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 732 | lookup_result lookup(ASTContext &Context, DeclarationName Name); |
| 733 | lookup_const_result lookup(ASTContext &Context, DeclarationName Name) const; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 734 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 735 | /// @brief Makes a declaration visible within this context. |
| 736 | /// |
| 737 | /// This routine makes the declaration D visible to name lookup |
| 738 | /// within this context and, if this is a transparent context, |
| 739 | /// within its parent contexts up to the first enclosing |
| 740 | /// non-transparent context. Making a declaration visible within a |
| 741 | /// context does not transfer ownership of a declaration, and a |
| 742 | /// declaration can be visible in many contexts that aren't its |
| 743 | /// lexical context. |
| 744 | /// |
| 745 | /// If D is a redeclaration of an existing declaration that is |
| 746 | /// visible from this context, as determined by |
| 747 | /// NamedDecl::declarationReplaces, the previous declaration will be |
| 748 | /// replaced with D. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 749 | void makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D); |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 751 | /// udir_iterator - Iterates through the using-directives stored |
| 752 | /// within this context. |
| 753 | typedef UsingDirectiveDecl * const * udir_iterator; |
| 754 | |
| 755 | typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range; |
| 756 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 757 | udir_iterator_range getUsingDirectives(ASTContext &Context) const; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 758 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 759 | udir_iterator using_directives_begin(ASTContext &Context) const { |
| 760 | return getUsingDirectives(Context).first; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 763 | udir_iterator using_directives_end(ASTContext &Context) const { |
| 764 | return getUsingDirectives(Context).second; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 767 | // Low-level accessors |
| 768 | |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 769 | /// \brief Retrieve the internal representation of the lookup structure. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 770 | void* getLookupPtr() const { return LookupPtr; } |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 771 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 772 | /// \brief Whether this DeclContext has external storage containing |
| 773 | /// additional declarations that are lexically in this context. |
| 774 | bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; } |
| 775 | |
| 776 | /// \brief State whether this DeclContext has external storage for |
| 777 | /// declarations lexically in this context. |
| 778 | void setHasExternalLexicalStorage(bool ES = true) { |
| 779 | ExternalLexicalStorage = ES; |
| 780 | } |
| 781 | |
| 782 | /// \brief Whether this DeclContext has external storage containing |
| 783 | /// additional declarations that are visible in this context. |
| 784 | bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; } |
| 785 | |
| 786 | /// \brief State whether this DeclContext has external storage for |
| 787 | /// declarations visible in this context. |
| 788 | void setHasExternalVisibleStorage(bool ES = true) { |
| 789 | ExternalVisibleStorage = ES; |
| 790 | } |
| 791 | |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 792 | static bool classof(const Decl *D); |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 793 | static bool classof(const DeclContext *D) { return true; } |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 794 | #define DECL_CONTEXT(Name) \ |
| 795 | static bool classof(const Name##Decl *D) { return true; } |
| 796 | #include "clang/AST/DeclNodes.def" |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 797 | |
| 798 | private: |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 799 | void LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const; |
| 800 | void LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const; |
| 801 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 802 | void buildLookup(ASTContext &Context, DeclContext *DCtx); |
| 803 | void makeDeclVisibleInContextImpl(ASTContext &Context, NamedDecl *D); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 804 | }; |
| 805 | |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 806 | inline bool Decl::isTemplateParameter() const { |
| 807 | return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm; |
| 808 | } |
| 809 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 810 | inline bool Decl::isDefinedOutsideFunctionOrMethod() const { |
| 811 | if (getDeclContext()) |
| 812 | return !getDeclContext()->getLookupContext()->isFunctionOrMethod(); |
Chris Lattner | 96f4468 | 2009-03-28 05:59:45 +0000 | [diff] [blame] | 813 | return true; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 816 | } // end clang. |
| 817 | |
| 818 | namespace llvm { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 819 | |
| 820 | /// Implement a isa_impl_wrap specialization to check whether a DeclContext is |
| 821 | /// a specific Decl. |
| 822 | template<class ToTy> |
| 823 | struct isa_impl_wrap<ToTy, |
| 824 | const ::clang::DeclContext,const ::clang::DeclContext> { |
| 825 | static bool doit(const ::clang::DeclContext &Val) { |
| 826 | return ToTy::classof(::clang::Decl::castFromDeclContext(&Val)); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 827 | } |
| 828 | }; |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 829 | template<class ToTy> |
| 830 | struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> |
| 831 | : public isa_impl_wrap<ToTy, |
| 832 | const ::clang::DeclContext,const ::clang::DeclContext> {}; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 833 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 834 | /// Implement cast_convert_val for Decl -> DeclContext conversions. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 835 | template<class FromTy> |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 836 | struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 837 | static ::clang::DeclContext &doit(const FromTy &Val) { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 838 | return *FromTy::castToDeclContext(&Val); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 839 | } |
| 840 | }; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 841 | |
| 842 | template<class FromTy> |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 843 | struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 844 | static ::clang::DeclContext *doit(const FromTy *Val) { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 845 | return FromTy::castToDeclContext(Val); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 846 | } |
| 847 | }; |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 848 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 849 | template<class FromTy> |
| 850 | struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> { |
| 851 | static const ::clang::DeclContext &doit(const FromTy &Val) { |
| 852 | return *FromTy::castToDeclContext(&Val); |
| 853 | } |
| 854 | }; |
| 855 | |
| 856 | template<class FromTy> |
| 857 | struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> { |
| 858 | static const ::clang::DeclContext *doit(const FromTy *Val) { |
| 859 | return FromTy::castToDeclContext(Val); |
| 860 | } |
| 861 | }; |
| 862 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 863 | /// Implement cast_convert_val for DeclContext -> Decl conversions. |
| 864 | template<class ToTy> |
| 865 | struct cast_convert_val<ToTy, |
| 866 | const ::clang::DeclContext,const ::clang::DeclContext> { |
| 867 | static ToTy &doit(const ::clang::DeclContext &Val) { |
| 868 | return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val)); |
| 869 | } |
| 870 | }; |
| 871 | template<class ToTy> |
| 872 | struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> |
| 873 | : public cast_convert_val<ToTy, |
| 874 | const ::clang::DeclContext,const ::clang::DeclContext> {}; |
| 875 | |
| 876 | template<class ToTy> |
| 877 | struct cast_convert_val<ToTy, |
| 878 | const ::clang::DeclContext*, const ::clang::DeclContext*> { |
| 879 | static ToTy *doit(const ::clang::DeclContext *Val) { |
| 880 | return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val)); |
| 881 | } |
| 882 | }; |
| 883 | template<class ToTy> |
| 884 | struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> |
| 885 | : public cast_convert_val<ToTy, |
| 886 | const ::clang::DeclContext*,const ::clang::DeclContext*> {}; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 887 | |
| 888 | } // end namespace llvm |
| 889 | |
| 890 | #endif |