blob: 84e822e92107601da4c51b0bf7c20a73997aaaff [file] [log] [blame]
Chris Lattner0ed844b2008-04-04 06:12:32 +00001//===-- 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 Lattnerb048c982008-04-06 04:47:34 +000010// This file defines the Decl and DeclContext interfaces.
Chris Lattner0ed844b2008-04-04 06:12:32 +000011//
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
21namespace clang {
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000022class TranslationUnitDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000023class FunctionDecl;
24class ObjCMethodDecl;
Chris Lattnerb048c982008-04-06 04:47:34 +000025class EnumDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000026class ObjCInterfaceDecl;
27
28/// Decl - This represents one declaration (or definition), e.g. a variable,
29/// typedef, function, struct, etc.
30///
31class Decl {
32public:
33 enum Kind {
34 // This lists the concrete classes of Decl in order of the inheritance
35 // hierarchy. This allows us to do efficient classof tests based on the
36 // enums below. The commented out names are abstract class names.
37
38 // Decl
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000039 TranslationUnit,
Chris Lattner0ed844b2008-04-04 06:12:32 +000040 // NamedDecl
41 Field,
42 ObjCIvar,
43 ObjCCategory,
44 ObjCCategoryImpl,
45 ObjCImplementation,
46 ObjCProtocol,
Sam Bishop670aa9d2008-04-08 20:49:25 +000047 ObjCProperty,
Chris Lattner0ed844b2008-04-04 06:12:32 +000048 // ScopedDecl
49 // TypeDecl
50 Typedef,
51 // TagDecl
52 Enum,
53 // RecordDecl
54 Struct,
55 Union,
56 Class,
57 // ValueDecl
58 EnumConstant,
59 Function,
Steve Naroff248a7532008-04-15 22:42:06 +000060 Var,
Chris Lattner0ed844b2008-04-04 06:12:32 +000061 ParmVar,
62 ObjCInterface,
63 ObjCCompatibleAlias,
64 ObjCMethod,
65 ObjCClass,
66 ObjCForwardProtocol,
Fariborz Jahanian61d46152008-04-16 22:00:24 +000067 ObjCPropertyImpl,
Chris Lattner0ed844b2008-04-04 06:12:32 +000068 LinkageSpec,
69 FileScopeAsm,
70
71 // For each non-leaf class, we now define a mapping to the first/last member
72 // of the class, to allow efficient classof.
73 NamedFirst = Field, NamedLast = ParmVar,
74 FieldFirst = Field, FieldLast = ObjCIvar,
75 ScopedFirst = Typedef, ScopedLast = ParmVar,
76 TypeFirst = Typedef, TypeLast = Class,
77 TagFirst = Enum , TagLast = Class,
78 RecordFirst = Struct , RecordLast = Class,
79 ValueFirst = EnumConstant , ValueLast = ParmVar,
Steve Naroff248a7532008-04-15 22:42:06 +000080 VarFirst = Var , VarLast = ParmVar
Chris Lattner0ed844b2008-04-04 06:12:32 +000081 };
82
83 /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces,
Douglas Gregor2ce52f32008-04-13 21:07:44 +000084 /// labels, tags, members and ordinary identifiers. These are meant
85 /// as bitmasks, so that searches in C++ can look into the "tag" namespace
86 /// during ordinary lookup.
Chris Lattner0ed844b2008-04-04 06:12:32 +000087 enum IdentifierNamespace {
Douglas Gregor2ce52f32008-04-13 21:07:44 +000088 IDNS_Label = 0x1,
89 IDNS_Tag = 0x2,
90 IDNS_Member = 0x4,
91 IDNS_Ordinary = 0x8
Chris Lattner0ed844b2008-04-04 06:12:32 +000092 };
93
94 /// ObjCDeclQualifier - Qualifier used on types in method declarations
95 /// for remote messaging. They are meant for the arguments though and
96 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
97 enum ObjCDeclQualifier {
98 OBJC_TQ_None = 0x0,
99 OBJC_TQ_In = 0x1,
100 OBJC_TQ_Inout = 0x2,
101 OBJC_TQ_Out = 0x4,
102 OBJC_TQ_Bycopy = 0x8,
103 OBJC_TQ_Byref = 0x10,
104 OBJC_TQ_Oneway = 0x20
105 };
106
107private:
108 /// Loc - The location that this decl.
109 SourceLocation Loc;
110
111 /// DeclKind - This indicates which class this is.
112 Kind DeclKind : 8;
113
114 /// InvalidDecl - This indicates a semantic error occurred.
115 unsigned int InvalidDecl : 1;
116
117 /// HasAttrs - This indicates whether the decl has attributes or not.
118 unsigned int HasAttrs : 1;
119protected:
120 Decl(Kind DK, SourceLocation L) : Loc(L), DeclKind(DK), InvalidDecl(0),
121 HasAttrs(false) {
122 if (Decl::CollectingStats()) addDeclKind(DK);
123 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000124
Chris Lattner0ed844b2008-04-04 06:12:32 +0000125 virtual ~Decl();
Sam Bishop1bb19632008-04-11 18:04:39 +0000126
127public:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000128 SourceLocation getLocation() const { return Loc; }
129 void setLocation(SourceLocation L) { Loc = L; }
130
131 Kind getKind() const { return DeclKind; }
132 const char *getDeclKindName() const;
133
134 void addAttr(Attr *attr);
135 const Attr *getAttrs() const;
136
137 template<typename T> const T *getAttr() const {
138 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())
139 if (const T *V = dyn_cast<T>(attr))
140 return V;
141
142 return 0;
143 }
144
145 /// setInvalidDecl - Indicates the Decl had a semantic error. This
146 /// allows for graceful error recovery.
147 void setInvalidDecl() { InvalidDecl = 1; }
148 bool isInvalidDecl() const { return (bool) InvalidDecl; }
149
150 IdentifierNamespace getIdentifierNamespace() const {
151 switch (DeclKind) {
152 default: assert(0 && "Unknown decl kind!");
153 case Typedef:
154 case Function:
Steve Naroff248a7532008-04-15 22:42:06 +0000155 case Var:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000156 case ParmVar:
157 case EnumConstant:
158 case ObjCInterface:
159 case ObjCCompatibleAlias:
160 return IDNS_Ordinary;
161 case Struct:
162 case Union:
163 case Class:
164 case Enum:
165 return IDNS_Tag;
166 }
167 }
168 // global temp stats (until we have a per-module visitor)
169 static void addDeclKind(Kind k);
170 static bool CollectingStats(bool Enable = false);
171 static void PrintStats();
172
173 // Implement isa/cast/dyncast/etc.
174 static bool classof(const Decl *) { return true; }
175
176 /// Emit - Serialize this Decl to Bitcode.
177 void Emit(llvm::Serializer& S) const;
178
179 /// Create - Deserialize a Decl from Bitcode.
Sam Bishope2563ca2008-04-07 21:55:54 +0000180 static Decl* Create(llvm::Deserializer& D, ASTContext& C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000181
Sam Bishopbb45c512008-04-11 15:01:25 +0000182 /// Destroy - Call destructors and release memory.
183 void Destroy(ASTContext& C) const;
184
Chris Lattner0ed844b2008-04-04 06:12:32 +0000185protected:
186 /// EmitImpl - Provides the subclass-specific serialization logic for
187 /// serializing out a decl.
188 virtual void EmitImpl(llvm::Serializer& S) const {
189 // FIXME: This will eventually be a pure virtual function.
190 assert (false && "Not implemented.");
191 }
192
193 void EmitInRec(llvm::Serializer& S) const;
Sam Bishope2563ca2008-04-07 21:55:54 +0000194 void ReadInRec(llvm::Deserializer& D, ASTContext& C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000195};
196
Chris Lattnerb048c982008-04-06 04:47:34 +0000197/// DeclContext - This is used only as base class of specific decl types that
Chris Lattner0ed844b2008-04-04 06:12:32 +0000198/// can act as declaration contexts. These decls are:
199///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000200/// TranslationUnitDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000201/// FunctionDecl
202/// ObjCMethodDecl
Chris Lattnerb048c982008-04-06 04:47:34 +0000203/// EnumDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000204/// ObjCInterfaceDecl
205///
Chris Lattnerb048c982008-04-06 04:47:34 +0000206class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000207 /// DeclKind - This indicates which class this is.
208 Decl::Kind DeclKind : 8;
209
210 // Used in the CastTo template to get the DeclKind
Chris Lattnerb048c982008-04-06 04:47:34 +0000211 // from a Decl or a DeclContext. DeclContext doesn't have a getKind() method
Chris Lattner0ed844b2008-04-04 06:12:32 +0000212 // to avoid 'ambiguous access' compiler errors.
213 template<typename T> struct KindTrait {
214 static Decl::Kind getKind(const T *D) { return D->getKind(); }
215 };
216
217 // Used only by the ToDecl and FromDecl methods
218 template<typename To, typename From>
219 static To *CastTo(const From *D) {
220 Decl::Kind DK = KindTrait<From>::getKind(D);
221 switch(DK) {
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000222 case Decl::TranslationUnit:
223 return static_cast<TranslationUnitDecl*>(const_cast<From*>(D));
Chris Lattner0ed844b2008-04-04 06:12:32 +0000224 case Decl::Function:
225 return static_cast<FunctionDecl*>(const_cast<From*>(D));
226 case Decl::ObjCMethod:
227 return static_cast<ObjCMethodDecl*>(const_cast<From*>(D));
228 case Decl::ObjCInterface:
229 return static_cast<ObjCInterfaceDecl*>(const_cast<From*>(D));
Chris Lattnerb048c982008-04-06 04:47:34 +0000230 case Decl::Enum:
231 return static_cast<EnumDecl*>(const_cast<From*>(D));
Chris Lattner0ed844b2008-04-04 06:12:32 +0000232 default:
Chris Lattnerb048c982008-04-06 04:47:34 +0000233 assert(false && "a decl that inherits DeclContext isn't handled");
Chris Lattner0ed844b2008-04-04 06:12:32 +0000234 return 0;
235 }
236 }
237
238protected:
Chris Lattnerb048c982008-04-06 04:47:34 +0000239 DeclContext(Decl::Kind K) : DeclKind(K) {}
Chris Lattner0ed844b2008-04-04 06:12:32 +0000240
241public:
Chris Lattnerb048c982008-04-06 04:47:34 +0000242 /// getParent - Returns the containing DeclContext if this is a ScopedDecl,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000243 /// else returns NULL.
Chris Lattnerb048c982008-04-06 04:47:34 +0000244 DeclContext *getParent() const;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000245
246 bool isFunctionOrMethod() const {
247 switch (DeclKind) {
248 case Decl::Function:
249 case Decl::ObjCMethod:
250 return true;
251 default:
252 return false;
253 }
254 }
255
Chris Lattnerb048c982008-04-06 04:47:34 +0000256 /// ToDecl and FromDecl make Decl <-> DeclContext castings.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000257 /// They are intended to be used by the simplify_type and cast_convert_val
258 /// templates.
Chris Lattnerb048c982008-04-06 04:47:34 +0000259 static Decl *ToDecl (const DeclContext *D);
260 static DeclContext *FromDecl (const Decl *D);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000261
262 static bool classof(const Decl *D) {
263 switch (D->getKind()) {
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000264 case Decl::TranslationUnit:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000265 case Decl::Function:
266 case Decl::ObjCMethod:
267 case Decl::ObjCInterface:
Chris Lattnerb048c982008-04-06 04:47:34 +0000268 case Decl::Enum:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000269 return true;
270 default:
Chris Lattnerb048c982008-04-06 04:47:34 +0000271 return false;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000272 }
273 }
Chris Lattnerb048c982008-04-06 04:47:34 +0000274 static bool classof(const DeclContext *D) { return true; }
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000275 static bool classof(const TranslationUnitDecl *D) { return true; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000276 static bool classof(const FunctionDecl *D) { return true; }
277 static bool classof(const ObjCMethodDecl *D) { return true; }
Chris Lattnerb048c982008-04-06 04:47:34 +0000278 static bool classof(const EnumDecl *D) { return true; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000279 static bool classof(const ObjCInterfaceDecl *D) { return true; }
280};
281
Chris Lattnerb048c982008-04-06 04:47:34 +0000282template<> struct DeclContext::KindTrait<DeclContext> {
283 static Decl::Kind getKind(const DeclContext *D) { return D->DeclKind; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000284};
285
286} // end clang.
287
288namespace llvm {
Chris Lattnerb048c982008-04-06 04:47:34 +0000289/// Implement simplify_type for DeclContext, so that we can dyn_cast from
290/// DeclContext to a specific Decl class.
291 template<> struct simplify_type<const ::clang::DeclContext*> {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000292 typedef ::clang::Decl* SimpleType;
Chris Lattnerb048c982008-04-06 04:47:34 +0000293 static SimpleType getSimplifiedValue(const ::clang::DeclContext *Val) {
294 return ::clang::DeclContext::ToDecl(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000295 }
296};
Chris Lattnerb048c982008-04-06 04:47:34 +0000297template<> struct simplify_type< ::clang::DeclContext*>
298 : public simplify_type<const ::clang::DeclContext*> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000299
Chris Lattnerb048c982008-04-06 04:47:34 +0000300template<> struct simplify_type<const ::clang::DeclContext> {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000301 typedef ::clang::Decl SimpleType;
Chris Lattnerb048c982008-04-06 04:47:34 +0000302 static SimpleType &getSimplifiedValue(const ::clang::DeclContext &Val) {
303 return *::clang::DeclContext::ToDecl(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000304 }
305};
Chris Lattnerb048c982008-04-06 04:47:34 +0000306template<> struct simplify_type< ::clang::DeclContext>
307 : public simplify_type<const ::clang::DeclContext> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000308
Chris Lattnerb048c982008-04-06 04:47:34 +0000309/// Implement cast_convert_val for DeclContext, so that we can dyn_cast from
310/// a Decl class to DeclContext.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000311template<class FromTy>
Chris Lattnerb048c982008-04-06 04:47:34 +0000312struct cast_convert_val< ::clang::DeclContext,const FromTy,const FromTy> {
313 static ::clang::DeclContext &doit(const FromTy &Val) {
314 return *::clang::DeclContext::FromDecl(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000315 }
316};
317template<class FromTy>
Chris Lattnerb048c982008-04-06 04:47:34 +0000318struct cast_convert_val< ::clang::DeclContext,FromTy,FromTy>
319 : public cast_convert_val< ::clang::DeclContext,const FromTy,const FromTy>
Chris Lattner0ed844b2008-04-04 06:12:32 +0000320 {};
321
322template<class FromTy>
Chris Lattnerb048c982008-04-06 04:47:34 +0000323struct cast_convert_val< ::clang::DeclContext,const FromTy*,const FromTy*> {
324 static ::clang::DeclContext *doit(const FromTy *Val) {
325 return ::clang::DeclContext::FromDecl(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000326 }
327};
328template<class FromTy>
Chris Lattnerb048c982008-04-06 04:47:34 +0000329struct cast_convert_val< ::clang::DeclContext,FromTy*,FromTy*>
330 : public cast_convert_val< ::clang::DeclContext,const FromTy*,const FromTy*>
Chris Lattner0ed844b2008-04-04 06:12:32 +0000331 {};
332
333} // end namespace llvm
334
335#endif