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" |
| 19 | #include "clang/Basic/SourceLocation.h" |
| 20 | |
| 21 | namespace clang { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 22 | class DeclContext; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 23 | class TranslationUnitDecl; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 24 | class NamespaceDecl; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 25 | class ScopedDecl; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 26 | class FunctionDecl; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 27 | class CXXRecordDecl; |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 28 | class EnumDecl; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 29 | class ObjCMethodDecl; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 30 | class ObjCInterfaceDecl; |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 31 | class BlockDecl; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 32 | |
| 33 | /// Decl - This represents one declaration (or definition), e.g. a variable, |
| 34 | /// typedef, function, struct, etc. |
| 35 | /// |
| 36 | class Decl { |
| 37 | public: |
| 38 | enum Kind { |
| 39 | // This lists the concrete classes of Decl in order of the inheritance |
| 40 | // hierarchy. This allows us to do efficient classof tests based on the |
| 41 | // enums below. The commented out names are abstract class names. |
Ted Kremenek | a34ea07 | 2008-08-04 22:51:42 +0000 | [diff] [blame] | 42 | // [DeclContext] indicates that the class also inherits from DeclContext. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 43 | |
| 44 | // Decl |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 45 | TranslationUnit, // [DeclContext] |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 46 | // NamedDecl |
| 47 | Field, |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 48 | CXXField, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 49 | ObjCIvar, |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 50 | ObjCAtDefsField, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 51 | OverloadedFunction, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 52 | ObjCCategory, |
| 53 | ObjCCategoryImpl, |
| 54 | ObjCImplementation, |
| 55 | ObjCProtocol, |
Sam Bishop | 670aa9d | 2008-04-08 20:49:25 +0000 | [diff] [blame] | 56 | ObjCProperty, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 57 | // ScopedDecl |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 58 | Namespace, // [DeclContext] |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 59 | // TypeDecl |
| 60 | Typedef, |
| 61 | // TagDecl |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 62 | Enum, // [DeclContext] |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 63 | Record, |
| 64 | CXXRecord, // [DeclContext] |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 65 | // ValueDecl |
| 66 | EnumConstant, |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 67 | Function, // [DeclContext] |
| 68 | CXXMethod, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 69 | CXXConstructor, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 70 | CXXDestructor, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 71 | CXXConversion, |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 72 | Var, |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 73 | ImplicitParam, |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 74 | CXXClassVar, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 75 | ParmVar, |
Daniel Dunbar | 226e04a | 2008-08-15 23:18:35 +0000 | [diff] [blame] | 76 | ObjCInterface, // [DeclContext] |
| 77 | ObjCCompatibleAlias, |
| 78 | ObjCMethod, // [DeclContext] |
| 79 | ObjCClass, |
| 80 | ObjCForwardProtocol, |
| 81 | ObjCPropertyImpl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 82 | LinkageSpec, |
Daniel Dunbar | 226e04a | 2008-08-15 23:18:35 +0000 | [diff] [blame] | 83 | FileScopeAsm, |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 84 | Block, // [DeclContext] |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 85 | |
| 86 | // For each non-leaf class, we now define a mapping to the first/last member |
| 87 | // of the class, to allow efficient classof. |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 88 | NamedFirst = Field , NamedLast = ParmVar, |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 89 | FieldFirst = Field , FieldLast = ObjCAtDefsField, |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 90 | ScopedFirst = Namespace , ScopedLast = ParmVar, |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 91 | TypeFirst = Typedef , TypeLast = CXXRecord, |
| 92 | TagFirst = Enum , TagLast = CXXRecord, |
| 93 | RecordFirst = Record , RecordLast = CXXRecord, |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 94 | ValueFirst = EnumConstant , ValueLast = ParmVar, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 95 | FunctionFirst = Function , FunctionLast = CXXConversion, |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 96 | VarFirst = Var , VarLast = ParmVar |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces, |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 100 | /// labels, tags, members and ordinary identifiers. These are meant |
| 101 | /// as bitmasks, so that searches in C++ can look into the "tag" namespace |
| 102 | /// during ordinary lookup. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 103 | enum IdentifierNamespace { |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 104 | IDNS_Label = 0x1, |
| 105 | IDNS_Tag = 0x2, |
| 106 | IDNS_Member = 0x4, |
| 107 | IDNS_Ordinary = 0x8 |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /// ObjCDeclQualifier - Qualifier used on types in method declarations |
| 111 | /// for remote messaging. They are meant for the arguments though and |
| 112 | /// applied to the Decls (ObjCMethodDecl and ParmVarDecl). |
| 113 | enum ObjCDeclQualifier { |
| 114 | OBJC_TQ_None = 0x0, |
| 115 | OBJC_TQ_In = 0x1, |
| 116 | OBJC_TQ_Inout = 0x2, |
| 117 | OBJC_TQ_Out = 0x4, |
| 118 | OBJC_TQ_Bycopy = 0x8, |
| 119 | OBJC_TQ_Byref = 0x10, |
| 120 | OBJC_TQ_Oneway = 0x20 |
| 121 | }; |
| 122 | |
| 123 | private: |
| 124 | /// Loc - The location that this decl. |
| 125 | SourceLocation Loc; |
| 126 | |
| 127 | /// DeclKind - This indicates which class this is. |
| 128 | Kind DeclKind : 8; |
| 129 | |
| 130 | /// InvalidDecl - This indicates a semantic error occurred. |
| 131 | unsigned int InvalidDecl : 1; |
| 132 | |
| 133 | /// HasAttrs - This indicates whether the decl has attributes or not. |
| 134 | unsigned int HasAttrs : 1; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 135 | |
| 136 | protected: |
| 137 | /// Access - Used by C++ decls for the access specifier. |
| 138 | // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum |
| 139 | unsigned Access : 2; |
| 140 | friend class CXXClassMemberWrapper; |
| 141 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 142 | Decl(Kind DK, SourceLocation L) : Loc(L), DeclKind(DK), InvalidDecl(0), |
| 143 | HasAttrs(false) { |
| 144 | if (Decl::CollectingStats()) addDeclKind(DK); |
| 145 | } |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 147 | virtual ~Decl(); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 148 | |
| 149 | public: |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 150 | SourceLocation getLocation() const { return Loc; } |
| 151 | void setLocation(SourceLocation L) { Loc = L; } |
| 152 | |
| 153 | Kind getKind() const { return DeclKind; } |
| 154 | const char *getDeclKindName() const; |
| 155 | |
| 156 | void addAttr(Attr *attr); |
| 157 | const Attr *getAttrs() const; |
Chris Lattner | a212c56 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 158 | void swapAttrs(Decl *D); |
Nuno Lopes | 9141bee | 2008-06-01 22:53:53 +0000 | [diff] [blame] | 159 | void invalidateAttrs(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 160 | |
| 161 | template<typename T> const T *getAttr() const { |
| 162 | for (const Attr *attr = getAttrs(); attr; attr = attr->getNext()) |
| 163 | if (const T *V = dyn_cast<T>(attr)) |
| 164 | return V; |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /// setInvalidDecl - Indicates the Decl had a semantic error. This |
| 170 | /// allows for graceful error recovery. |
| 171 | void setInvalidDecl() { InvalidDecl = 1; } |
| 172 | bool isInvalidDecl() const { return (bool) InvalidDecl; } |
| 173 | |
| 174 | IdentifierNamespace getIdentifierNamespace() const { |
| 175 | switch (DeclKind) { |
| 176 | default: assert(0 && "Unknown decl kind!"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 177 | case ImplicitParam: |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 178 | case Typedef: |
| 179 | case Function: |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 180 | case Var: |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 181 | case ParmVar: |
| 182 | case EnumConstant: |
| 183 | case ObjCInterface: |
| 184 | case ObjCCompatibleAlias: |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 185 | case OverloadedFunction: |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 186 | case CXXField: |
Argyrios Kyrtzidis | 1ce6ead | 2008-06-24 22:56:42 +0000 | [diff] [blame] | 187 | case CXXMethod: |
Argyrios Kyrtzidis | 09c31b0 | 2008-06-25 14:04:17 +0000 | [diff] [blame] | 188 | case CXXClassVar: |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 189 | return IDNS_Ordinary; |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 190 | case Record: |
| 191 | case CXXRecord: |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 192 | case Enum: |
| 193 | return IDNS_Tag; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 194 | case Namespace: |
| 195 | return IdentifierNamespace(IDNS_Tag | IDNS_Ordinary); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
Ted Kremenek | 792481e | 2008-06-20 21:39:47 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 69c8f0a | 2008-07-31 17:32:12 +0000 | [diff] [blame] | 199 | // getBody - If this Decl represents a declaration for a body of code, |
Ted Kremenek | 792481e | 2008-06-20 21:39:47 +0000 | [diff] [blame] | 200 | // 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] | 201 | // Stmt* of that body. Otherwise this method returns null. |
| 202 | virtual Stmt* getBody() const { return 0; } |
Ted Kremenek | 792481e | 2008-06-20 21:39:47 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 204 | // global temp stats (until we have a per-module visitor) |
| 205 | static void addDeclKind(Kind k); |
| 206 | static bool CollectingStats(bool Enable = false); |
| 207 | static void PrintStats(); |
| 208 | |
| 209 | // Implement isa/cast/dyncast/etc. |
| 210 | static bool classof(const Decl *) { return true; } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 211 | static DeclContext *castToDeclContext(const Decl *); |
| 212 | static Decl *castFromDeclContext(const DeclContext *); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 213 | |
| 214 | /// Emit - Serialize this Decl to Bitcode. |
| 215 | void Emit(llvm::Serializer& S) const; |
| 216 | |
| 217 | /// Create - Deserialize a Decl from Bitcode. |
Sam Bishop | e2563ca | 2008-04-07 21:55:54 +0000 | [diff] [blame] | 218 | static Decl* Create(llvm::Deserializer& D, ASTContext& C); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 219 | |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 220 | /// Destroy - Call destructors and release memory. |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 221 | virtual void Destroy(ASTContext& C); |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 223 | protected: |
| 224 | /// EmitImpl - Provides the subclass-specific serialization logic for |
| 225 | /// serializing out a decl. |
| 226 | virtual void EmitImpl(llvm::Serializer& S) const { |
| 227 | // FIXME: This will eventually be a pure virtual function. |
| 228 | assert (false && "Not implemented."); |
| 229 | } |
| 230 | |
| 231 | void EmitInRec(llvm::Serializer& S) const; |
Sam Bishop | e2563ca | 2008-04-07 21:55:54 +0000 | [diff] [blame] | 232 | void ReadInRec(llvm::Deserializer& D, ASTContext& C); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 235 | /// DeclContext - This is used only as base class of specific decl types that |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 236 | /// can act as declaration contexts. These decls are: |
| 237 | /// |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 238 | /// TranslationUnitDecl |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 239 | /// NamespaceDecl |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 240 | /// FunctionDecl |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 241 | /// CXXRecordDecl |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 242 | /// EnumDecl |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 243 | /// ObjCMethodDecl |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 244 | /// ObjCInterfaceDecl |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 245 | /// BlockDecl |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 246 | /// |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 247 | class DeclContext { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 248 | /// DeclKind - This indicates which class this is. |
| 249 | Decl::Kind DeclKind : 8; |
| 250 | |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 251 | /// DeclChain - Linked list of declarations that are defined inside this |
| 252 | /// declaration context. |
| 253 | ScopedDecl *DeclChain; |
| 254 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 255 | // Used in the CastTo template to get the DeclKind |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 256 | // from a Decl or a DeclContext. DeclContext doesn't have a getKind() method |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 257 | // to avoid 'ambiguous access' compiler errors. |
| 258 | template<typename T> struct KindTrait { |
| 259 | static Decl::Kind getKind(const T *D) { return D->getKind(); } |
| 260 | }; |
| 261 | |
| 262 | // Used only by the ToDecl and FromDecl methods |
| 263 | template<typename To, typename From> |
| 264 | static To *CastTo(const From *D) { |
| 265 | Decl::Kind DK = KindTrait<From>::getKind(D); |
| 266 | switch(DK) { |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 267 | case Decl::Block: |
| 268 | return static_cast<BlockDecl*>(const_cast<From*>(D)); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 269 | case Decl::TranslationUnit: |
| 270 | return static_cast<TranslationUnitDecl*>(const_cast<From*>(D)); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 271 | case Decl::Namespace: |
| 272 | return static_cast<NamespaceDecl*>(const_cast<From*>(D)); |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 273 | case Decl::Enum: |
| 274 | return static_cast<EnumDecl*>(const_cast<From*>(D)); |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 275 | case Decl::CXXRecord: |
| 276 | return static_cast<CXXRecordDecl*>(const_cast<From*>(D)); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 277 | case Decl::ObjCMethod: |
| 278 | return static_cast<ObjCMethodDecl*>(const_cast<From*>(D)); |
| 279 | case Decl::ObjCInterface: |
| 280 | return static_cast<ObjCInterfaceDecl*>(const_cast<From*>(D)); |
| 281 | default: |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 282 | if (DK >= Decl::FunctionFirst && DK <= Decl::FunctionLast) |
| 283 | return static_cast<FunctionDecl*>(const_cast<From*>(D)); |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 284 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 285 | assert(false && "a decl that inherits DeclContext isn't handled"); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | protected: |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 291 | DeclContext(Decl::Kind K) : DeclKind(K), DeclChain(0) {} |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 292 | |
| 293 | public: |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 294 | /// getParent - Returns the containing DeclContext if this is a ScopedDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 295 | /// else returns NULL. |
Argyrios Kyrtzidis | d2595ec | 2008-10-12 18:40:01 +0000 | [diff] [blame] | 296 | DeclContext *getParent(); |
| 297 | const DeclContext *getParent() const { |
| 298 | return const_cast<DeclContext*>(this)->getParent(); |
| 299 | } |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 300 | |
| 301 | bool isFunctionOrMethod() const { |
| 302 | switch (DeclKind) { |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 303 | case Decl::Block: |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 304 | case Decl::Function: |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 305 | case Decl::CXXMethod: |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 306 | case Decl::ObjCMethod: |
| 307 | return true; |
| 308 | default: |
| 309 | return false; |
| 310 | } |
| 311 | } |
| 312 | |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame^] | 313 | bool isCXXRecord() const { |
| 314 | return DeclKind == Decl::CXXRecord; |
| 315 | } |
| 316 | |
Argyrios Kyrtzidis | e2ed203 | 2008-10-12 18:45:56 +0000 | [diff] [blame] | 317 | const ScopedDecl *getDeclChain() const { return DeclChain; } |
| 318 | ScopedDecl *getDeclChain() { return DeclChain; } |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 319 | void setDeclChain(ScopedDecl *D) { DeclChain = D; } |
| 320 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 321 | static bool classof(const Decl *D) { |
| 322 | switch (D->getKind()) { |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 323 | case Decl::TranslationUnit: |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 324 | case Decl::Namespace: |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 325 | case Decl::Enum: |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 326 | case Decl::CXXRecord: |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 327 | case Decl::ObjCMethod: |
| 328 | case Decl::ObjCInterface: |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 329 | case Decl::Block: |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 330 | return true; |
| 331 | default: |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 332 | if (D->getKind() >= Decl::FunctionFirst && |
| 333 | D->getKind() <= Decl::FunctionLast) |
| 334 | return true; |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 335 | return false; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 338 | static bool classof(const DeclContext *D) { return true; } |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 339 | static bool classof(const TranslationUnitDecl *D) { return true; } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 340 | static bool classof(const NamespaceDecl *D) { return true; } |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 341 | static bool classof(const FunctionDecl *D) { return true; } |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 342 | static bool classof(const CXXRecordDecl *D) { return true; } |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 343 | static bool classof(const EnumDecl *D) { return true; } |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 344 | static bool classof(const ObjCMethodDecl *D) { return true; } |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 345 | static bool classof(const ObjCInterfaceDecl *D) { return true; } |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 346 | static bool classof(const BlockDecl *D) { return true; } |
Argyrios Kyrtzidis | 7643536 | 2008-06-10 01:32:09 +0000 | [diff] [blame] | 347 | |
| 348 | private: |
| 349 | void EmitOutRec(llvm::Serializer& S) const; |
| 350 | void ReadOutRec(llvm::Deserializer& D, ASTContext& C); |
| 351 | |
| 352 | friend class Decl; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 353 | }; |
| 354 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 355 | template<> struct DeclContext::KindTrait<DeclContext> { |
| 356 | static Decl::Kind getKind(const DeclContext *D) { return D->DeclKind; } |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | } // end clang. |
| 360 | |
| 361 | namespace llvm { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 362 | |
| 363 | /// Implement a isa_impl_wrap specialization to check whether a DeclContext is |
| 364 | /// a specific Decl. |
| 365 | template<class ToTy> |
| 366 | struct isa_impl_wrap<ToTy, |
| 367 | const ::clang::DeclContext,const ::clang::DeclContext> { |
| 368 | static bool doit(const ::clang::DeclContext &Val) { |
| 369 | return ToTy::classof(::clang::Decl::castFromDeclContext(&Val)); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 370 | } |
| 371 | }; |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 372 | template<class ToTy> |
| 373 | struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext> |
| 374 | : public isa_impl_wrap<ToTy, |
| 375 | const ::clang::DeclContext,const ::clang::DeclContext> {}; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 376 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 377 | /// Implement cast_convert_val for Decl -> DeclContext conversions. |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 378 | template<class FromTy> |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 379 | struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> { |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 380 | static ::clang::DeclContext &doit(const FromTy &Val) { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 381 | return *FromTy::castToDeclContext(&Val); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 382 | } |
| 383 | }; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 384 | |
| 385 | template<class FromTy> |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 386 | struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> { |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 387 | static ::clang::DeclContext *doit(const FromTy *Val) { |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 388 | return FromTy::castToDeclContext(Val); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 389 | } |
| 390 | }; |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 391 | |
| 392 | /// Implement cast_convert_val for DeclContext -> Decl conversions. |
| 393 | template<class ToTy> |
| 394 | struct cast_convert_val<ToTy, |
| 395 | const ::clang::DeclContext,const ::clang::DeclContext> { |
| 396 | static ToTy &doit(const ::clang::DeclContext &Val) { |
| 397 | return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val)); |
| 398 | } |
| 399 | }; |
| 400 | template<class ToTy> |
| 401 | struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> |
| 402 | : public cast_convert_val<ToTy, |
| 403 | const ::clang::DeclContext,const ::clang::DeclContext> {}; |
| 404 | |
| 405 | template<class ToTy> |
| 406 | struct cast_convert_val<ToTy, |
| 407 | const ::clang::DeclContext*, const ::clang::DeclContext*> { |
| 408 | static ToTy *doit(const ::clang::DeclContext *Val) { |
| 409 | return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val)); |
| 410 | } |
| 411 | }; |
| 412 | template<class ToTy> |
| 413 | struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> |
| 414 | : public cast_convert_val<ToTy, |
| 415 | const ::clang::DeclContext*,const ::clang::DeclContext*> {}; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 416 | |
| 417 | } // end namespace llvm |
| 418 | |
| 419 | #endif |