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