blob: 98a72ea7f0b58a46d6a85dbec843d7a4568f6fa5 [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;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000023class NamespaceDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000024class FunctionDecl;
25class ObjCMethodDecl;
Chris Lattnerb048c982008-04-06 04:47:34 +000026class EnumDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000027class ObjCInterfaceDecl;
28
29/// Decl - This represents one declaration (or definition), e.g. a variable,
30/// typedef, function, struct, etc.
31///
32class Decl {
33public:
34 enum Kind {
35 // This lists the concrete classes of Decl in order of the inheritance
36 // hierarchy. This allows us to do efficient classof tests based on the
37 // enums below. The commented out names are abstract class names.
38
39 // Decl
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000040 TranslationUnit,
Chris Lattner0ed844b2008-04-04 06:12:32 +000041 // NamedDecl
42 Field,
43 ObjCIvar,
44 ObjCCategory,
45 ObjCCategoryImpl,
46 ObjCImplementation,
47 ObjCProtocol,
Sam Bishop670aa9d2008-04-08 20:49:25 +000048 ObjCProperty,
Chris Lattner0ed844b2008-04-04 06:12:32 +000049 // ScopedDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000050 Namespace,
Chris Lattner0ed844b2008-04-04 06:12:32 +000051 // TypeDecl
52 Typedef,
53 // TagDecl
54 Enum,
55 // RecordDecl
56 Struct,
57 Union,
58 Class,
59 // ValueDecl
60 EnumConstant,
61 Function,
Steve Naroff248a7532008-04-15 22:42:06 +000062 Var,
Chris Lattner0ed844b2008-04-04 06:12:32 +000063 ParmVar,
64 ObjCInterface,
65 ObjCCompatibleAlias,
66 ObjCMethod,
67 ObjCClass,
68 ObjCForwardProtocol,
Fariborz Jahanian61d46152008-04-16 22:00:24 +000069 ObjCPropertyImpl,
Chris Lattner0ed844b2008-04-04 06:12:32 +000070 LinkageSpec,
71 FileScopeAsm,
72
73 // For each non-leaf class, we now define a mapping to the first/last member
74 // of the class, to allow efficient classof.
75 NamedFirst = Field, NamedLast = ParmVar,
76 FieldFirst = Field, FieldLast = ObjCIvar,
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000077 ScopedFirst = Namespace, ScopedLast = ParmVar,
Chris Lattner0ed844b2008-04-04 06:12:32 +000078 TypeFirst = Typedef, TypeLast = Class,
79 TagFirst = Enum , TagLast = Class,
80 RecordFirst = Struct , RecordLast = Class,
81 ValueFirst = EnumConstant , ValueLast = ParmVar,
Steve Naroff248a7532008-04-15 22:42:06 +000082 VarFirst = Var , VarLast = ParmVar
Chris Lattner0ed844b2008-04-04 06:12:32 +000083 };
84
85 /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces,
Douglas Gregor2ce52f32008-04-13 21:07:44 +000086 /// labels, tags, members and ordinary identifiers. These are meant
87 /// as bitmasks, so that searches in C++ can look into the "tag" namespace
88 /// during ordinary lookup.
Chris Lattner0ed844b2008-04-04 06:12:32 +000089 enum IdentifierNamespace {
Douglas Gregor2ce52f32008-04-13 21:07:44 +000090 IDNS_Label = 0x1,
91 IDNS_Tag = 0x2,
92 IDNS_Member = 0x4,
93 IDNS_Ordinary = 0x8
Chris Lattner0ed844b2008-04-04 06:12:32 +000094 };
95
96 /// ObjCDeclQualifier - Qualifier used on types in method declarations
97 /// for remote messaging. They are meant for the arguments though and
98 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
99 enum ObjCDeclQualifier {
100 OBJC_TQ_None = 0x0,
101 OBJC_TQ_In = 0x1,
102 OBJC_TQ_Inout = 0x2,
103 OBJC_TQ_Out = 0x4,
104 OBJC_TQ_Bycopy = 0x8,
105 OBJC_TQ_Byref = 0x10,
106 OBJC_TQ_Oneway = 0x20
107 };
108
109private:
110 /// Loc - The location that this decl.
111 SourceLocation Loc;
112
113 /// DeclKind - This indicates which class this is.
114 Kind DeclKind : 8;
115
116 /// InvalidDecl - This indicates a semantic error occurred.
117 unsigned int InvalidDecl : 1;
118
119 /// HasAttrs - This indicates whether the decl has attributes or not.
120 unsigned int HasAttrs : 1;
121protected:
122 Decl(Kind DK, SourceLocation L) : Loc(L), DeclKind(DK), InvalidDecl(0),
123 HasAttrs(false) {
124 if (Decl::CollectingStats()) addDeclKind(DK);
125 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000126
Chris Lattner0ed844b2008-04-04 06:12:32 +0000127 virtual ~Decl();
Sam Bishop1bb19632008-04-11 18:04:39 +0000128
129public:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000130 SourceLocation getLocation() const { return Loc; }
131 void setLocation(SourceLocation L) { Loc = L; }
132
133 Kind getKind() const { return DeclKind; }
134 const char *getDeclKindName() const;
135
136 void addAttr(Attr *attr);
137 const Attr *getAttrs() const;
138
139 template<typename T> const T *getAttr() const {
140 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())
141 if (const T *V = dyn_cast<T>(attr))
142 return V;
143
144 return 0;
145 }
146
147 /// setInvalidDecl - Indicates the Decl had a semantic error. This
148 /// allows for graceful error recovery.
149 void setInvalidDecl() { InvalidDecl = 1; }
150 bool isInvalidDecl() const { return (bool) InvalidDecl; }
151
152 IdentifierNamespace getIdentifierNamespace() const {
153 switch (DeclKind) {
154 default: assert(0 && "Unknown decl kind!");
155 case Typedef:
156 case Function:
Steve Naroff248a7532008-04-15 22:42:06 +0000157 case Var:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000158 case ParmVar:
159 case EnumConstant:
160 case ObjCInterface:
161 case ObjCCompatibleAlias:
162 return IDNS_Ordinary;
163 case Struct:
164 case Union:
165 case Class:
166 case Enum:
167 return IDNS_Tag;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000168 case Namespace:
169 return IdentifierNamespace(IDNS_Tag | IDNS_Ordinary);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000170 }
171 }
172 // global temp stats (until we have a per-module visitor)
173 static void addDeclKind(Kind k);
174 static bool CollectingStats(bool Enable = false);
175 static void PrintStats();
176
177 // Implement isa/cast/dyncast/etc.
178 static bool classof(const Decl *) { return true; }
179
180 /// Emit - Serialize this Decl to Bitcode.
181 void Emit(llvm::Serializer& S) const;
182
183 /// Create - Deserialize a Decl from Bitcode.
Sam Bishope2563ca2008-04-07 21:55:54 +0000184 static Decl* Create(llvm::Deserializer& D, ASTContext& C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000185
Sam Bishopbb45c512008-04-11 15:01:25 +0000186 /// Destroy - Call destructors and release memory.
187 void Destroy(ASTContext& C) const;
188
Chris Lattner0ed844b2008-04-04 06:12:32 +0000189protected:
190 /// EmitImpl - Provides the subclass-specific serialization logic for
191 /// serializing out a decl.
192 virtual void EmitImpl(llvm::Serializer& S) const {
193 // FIXME: This will eventually be a pure virtual function.
194 assert (false && "Not implemented.");
195 }
196
197 void EmitInRec(llvm::Serializer& S) const;
Sam Bishope2563ca2008-04-07 21:55:54 +0000198 void ReadInRec(llvm::Deserializer& D, ASTContext& C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000199};
200
Chris Lattnerb048c982008-04-06 04:47:34 +0000201/// DeclContext - This is used only as base class of specific decl types that
Chris Lattner0ed844b2008-04-04 06:12:32 +0000202/// can act as declaration contexts. These decls are:
203///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000204/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000205/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000206/// FunctionDecl
207/// ObjCMethodDecl
Chris Lattnerb048c982008-04-06 04:47:34 +0000208/// EnumDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000209/// ObjCInterfaceDecl
210///
Chris Lattnerb048c982008-04-06 04:47:34 +0000211class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000212 /// DeclKind - This indicates which class this is.
213 Decl::Kind DeclKind : 8;
214
215 // Used in the CastTo template to get the DeclKind
Chris Lattnerb048c982008-04-06 04:47:34 +0000216 // from a Decl or a DeclContext. DeclContext doesn't have a getKind() method
Chris Lattner0ed844b2008-04-04 06:12:32 +0000217 // to avoid 'ambiguous access' compiler errors.
218 template<typename T> struct KindTrait {
219 static Decl::Kind getKind(const T *D) { return D->getKind(); }
220 };
221
222 // Used only by the ToDecl and FromDecl methods
223 template<typename To, typename From>
224 static To *CastTo(const From *D) {
225 Decl::Kind DK = KindTrait<From>::getKind(D);
226 switch(DK) {
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000227 case Decl::TranslationUnit:
228 return static_cast<TranslationUnitDecl*>(const_cast<From*>(D));
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000229 case Decl::Namespace:
230 return static_cast<NamespaceDecl*>(const_cast<From*>(D));
Chris Lattner0ed844b2008-04-04 06:12:32 +0000231 case Decl::Function:
232 return static_cast<FunctionDecl*>(const_cast<From*>(D));
233 case Decl::ObjCMethod:
234 return static_cast<ObjCMethodDecl*>(const_cast<From*>(D));
235 case Decl::ObjCInterface:
236 return static_cast<ObjCInterfaceDecl*>(const_cast<From*>(D));
Chris Lattnerb048c982008-04-06 04:47:34 +0000237 case Decl::Enum:
238 return static_cast<EnumDecl*>(const_cast<From*>(D));
Chris Lattner0ed844b2008-04-04 06:12:32 +0000239 default:
Chris Lattnerb048c982008-04-06 04:47:34 +0000240 assert(false && "a decl that inherits DeclContext isn't handled");
Chris Lattner0ed844b2008-04-04 06:12:32 +0000241 return 0;
242 }
243 }
244
245protected:
Chris Lattnerb048c982008-04-06 04:47:34 +0000246 DeclContext(Decl::Kind K) : DeclKind(K) {}
Chris Lattner0ed844b2008-04-04 06:12:32 +0000247
248public:
Chris Lattnerb048c982008-04-06 04:47:34 +0000249 /// getParent - Returns the containing DeclContext if this is a ScopedDecl,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000250 /// else returns NULL.
Chris Lattnerb048c982008-04-06 04:47:34 +0000251 DeclContext *getParent() const;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000252
253 bool isFunctionOrMethod() const {
254 switch (DeclKind) {
255 case Decl::Function:
256 case Decl::ObjCMethod:
257 return true;
258 default:
259 return false;
260 }
261 }
262
Chris Lattnerb048c982008-04-06 04:47:34 +0000263 /// ToDecl and FromDecl make Decl <-> DeclContext castings.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000264 /// They are intended to be used by the simplify_type and cast_convert_val
265 /// templates.
Chris Lattnerb048c982008-04-06 04:47:34 +0000266 static Decl *ToDecl (const DeclContext *D);
267 static DeclContext *FromDecl (const Decl *D);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000268
269 static bool classof(const Decl *D) {
270 switch (D->getKind()) {
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000271 case Decl::TranslationUnit:
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000272 case Decl::Namespace:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000273 case Decl::Function:
274 case Decl::ObjCMethod:
275 case Decl::ObjCInterface:
Chris Lattnerb048c982008-04-06 04:47:34 +0000276 case Decl::Enum:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000277 return true;
278 default:
Chris Lattnerb048c982008-04-06 04:47:34 +0000279 return false;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000280 }
281 }
Chris Lattnerb048c982008-04-06 04:47:34 +0000282 static bool classof(const DeclContext *D) { return true; }
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000283 static bool classof(const TranslationUnitDecl *D) { return true; }
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000284 static bool classof(const NamespaceDecl *D) { return true; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000285 static bool classof(const FunctionDecl *D) { return true; }
286 static bool classof(const ObjCMethodDecl *D) { return true; }
Chris Lattnerb048c982008-04-06 04:47:34 +0000287 static bool classof(const EnumDecl *D) { return true; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000288 static bool classof(const ObjCInterfaceDecl *D) { return true; }
289};
290
Chris Lattnerb048c982008-04-06 04:47:34 +0000291template<> struct DeclContext::KindTrait<DeclContext> {
292 static Decl::Kind getKind(const DeclContext *D) { return D->DeclKind; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000293};
294
295} // end clang.
296
297namespace llvm {
Chris Lattnerb048c982008-04-06 04:47:34 +0000298/// Implement simplify_type for DeclContext, so that we can dyn_cast from
299/// DeclContext to a specific Decl class.
300 template<> 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 +0000309template<> struct simplify_type<const ::clang::DeclContext> {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000310 typedef ::clang::Decl SimpleType;
Chris Lattnerb048c982008-04-06 04:47:34 +0000311 static SimpleType &getSimplifiedValue(const ::clang::DeclContext &Val) {
312 return *::clang::DeclContext::ToDecl(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000313 }
314};
Chris Lattnerb048c982008-04-06 04:47:34 +0000315template<> struct simplify_type< ::clang::DeclContext>
316 : public simplify_type<const ::clang::DeclContext> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000317
Chris Lattnerb048c982008-04-06 04:47:34 +0000318/// Implement cast_convert_val for DeclContext, so that we can dyn_cast from
319/// a Decl class to DeclContext.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000320template<class FromTy>
Chris Lattnerb048c982008-04-06 04:47:34 +0000321struct cast_convert_val< ::clang::DeclContext,const FromTy,const FromTy> {
322 static ::clang::DeclContext &doit(const FromTy &Val) {
323 return *::clang::DeclContext::FromDecl(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000324 }
325};
326template<class FromTy>
Chris Lattnerb048c982008-04-06 04:47:34 +0000327struct cast_convert_val< ::clang::DeclContext,FromTy,FromTy>
328 : public cast_convert_val< ::clang::DeclContext,const FromTy,const FromTy>
Chris Lattner0ed844b2008-04-04 06:12:32 +0000329 {};
330
331template<class FromTy>
Chris Lattnerb048c982008-04-06 04:47:34 +0000332struct cast_convert_val< ::clang::DeclContext,const FromTy*,const FromTy*> {
333 static ::clang::DeclContext *doit(const FromTy *Val) {
334 return ::clang::DeclContext::FromDecl(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000335 }
336};
337template<class FromTy>
Chris Lattnerb048c982008-04-06 04:47:34 +0000338struct cast_convert_val< ::clang::DeclContext,FromTy*,FromTy*>
339 : public cast_convert_val< ::clang::DeclContext,const FromTy*,const FromTy*>
Chris Lattner0ed844b2008-04-04 06:12:32 +0000340 {};
341
342} // end namespace llvm
343
344#endif