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