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