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