blob: cee7ddad02277d442e5c8ad6798cfd6a1cc4b1a5 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattnere4650482008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Anders Carlsson3f70c542008-02-15 07:04:12 +000016#include "clang/AST/Attr.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Anders Carlsson484eb5f2008-02-15 23:30:50 +000018#include "llvm/ADT/DenseMap.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Chris Lattnera8344c32008-03-15 05:43:15 +000021//===----------------------------------------------------------------------===//
22// Statistics
23//===----------------------------------------------------------------------===//
24
Chris Lattner4b009652007-07-25 00:24:17 +000025// temporary statistics gathering
26static unsigned nFuncs = 0;
Steve Naroff72a6ebc2008-04-15 22:42:06 +000027static unsigned nVars = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000028static unsigned nParmVars = 0;
29static unsigned nSUC = 0;
30static unsigned nEnumConst = 0;
31static unsigned nEnumDecls = 0;
32static unsigned nTypedef = 0;
33static unsigned nFieldDecls = 0;
Steve Naroff81f1bba2007-09-06 21:24:23 +000034static unsigned nInterfaceDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000035static unsigned nClassDecls = 0;
36static unsigned nMethodDecls = 0;
37static unsigned nProtocolDecls = 0;
Fariborz Jahanianc716c942007-09-21 15:40:54 +000038static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000039static unsigned nCategoryDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000040static unsigned nIvarDecls = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000041static unsigned nObjCImplementationDecls = 0;
42static unsigned nObjCCategoryImpl = 0;
43static unsigned nObjCCompatibleAlias = 0;
44static unsigned nObjCPropertyDecl = 0;
Chris Lattner806a5f52008-01-12 07:05:38 +000045static unsigned nLinkageSpecDecl = 0;
Anders Carlsson4f7f4412008-02-08 00:33:21 +000046static unsigned nFileScopeAsmDecl = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000047
Chris Lattner4b009652007-07-25 00:24:17 +000048static bool StatSwitch = false;
49
Anders Carlsson484eb5f2008-02-15 23:30:50 +000050// This keeps track of all decl attributes. Since so few decls have attrs, we
51// keep them in a hash map instead of wasting space in the Decl class.
52typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
53
54static DeclAttrMapTy *DeclAttrs = 0;
55
Steve Naroff590aba82007-09-17 14:49:06 +000056const char *Decl::getDeclKindName() const {
Steve Narofff0c31dd2007-09-16 16:16:00 +000057 switch (DeclKind) {
58 default: assert(0 && "Unknown decl kind!");
Chris Lattnera8344c32008-03-15 05:43:15 +000059 case Typedef: return "Typedef";
60 case Function: return "Function";
Steve Naroff72a6ebc2008-04-15 22:42:06 +000061 case Var: return "Var";
Chris Lattnera8344c32008-03-15 05:43:15 +000062 case ParmVar: return "ParmVar";
63 case EnumConstant: return "EnumConstant";
64 case ObjCInterface: return "ObjCInterface";
65 case ObjCClass: return "ObjCClass";
66 case ObjCMethod: return "ObjCMethod";
67 case ObjCProtocol: return "ObjCProtocol";
68 case ObjCForwardProtocol: return "ObjCForwardProtocol";
69 case Struct: return "Struct";
70 case Union: return "Union";
71 case Class: return "Class";
72 case Enum: return "Enum";
Steve Narofff0c31dd2007-09-16 16:16:00 +000073 }
74}
75
Chris Lattnera8344c32008-03-15 05:43:15 +000076bool Decl::CollectingStats(bool Enable) {
77 if (Enable)
78 StatSwitch = true;
79 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000080}
81
82void Decl::PrintStats() {
83 fprintf(stderr, "*** Decl Stats:\n");
84 fprintf(stderr, " %d decls total.\n",
Steve Naroff72a6ebc2008-04-15 22:42:06 +000085 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+
Chris Lattner43b885f2008-02-25 21:04:36 +000086 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
87 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattner4b009652007-07-25 00:24:17 +000088 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000089 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Steve Naroff72a6ebc2008-04-15 22:42:06 +000090 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
91 nVars, (int)sizeof(VarDecl),
92 int(nVars*sizeof(VarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000093 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000094 nParmVars, (int)sizeof(ParmVarDecl),
95 int(nParmVars*sizeof(ParmVarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000096 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000097 nFieldDecls, (int)sizeof(FieldDecl),
98 int(nFieldDecls*sizeof(FieldDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000099 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000100 nSUC, (int)sizeof(RecordDecl),
101 int(nSUC*sizeof(RecordDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000102 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000103 nEnumDecls, (int)sizeof(EnumDecl),
104 int(nEnumDecls*sizeof(EnumDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000105 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000106 nEnumConst, (int)sizeof(EnumConstantDecl),
107 int(nEnumConst*sizeof(EnumConstantDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000108 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000109 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000110 // Objective-C decls...
111 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000112 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
113 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000114 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000115 nIvarDecls, (int)sizeof(ObjCIvarDecl),
116 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000117 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000118 nClassDecls, (int)sizeof(ObjCClassDecl),
119 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000120 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000121 nMethodDecls, (int)sizeof(ObjCMethodDecl),
122 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000123 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000124 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
125 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000126 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000127 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
128 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000129 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000130 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
131 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000132
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000133 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000134 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
135 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000136
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000137 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000138 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
139 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000140
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000141 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000142 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
143 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000144
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000145 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000146 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
147 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000148
Chris Lattner4b009652007-07-25 00:24:17 +0000149 fprintf(stderr, "Total bytes = %d\n",
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000150 int(nFuncs*sizeof(FunctionDecl)+
151 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
Chris Lattner43b885f2008-02-25 21:04:36 +0000152 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
153 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
154 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000155 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
156 nIvarDecls*sizeof(ObjCIvarDecl)+
157 nClassDecls*sizeof(ObjCClassDecl)+
158 nMethodDecls*sizeof(ObjCMethodDecl)+
159 nProtocolDecls*sizeof(ObjCProtocolDecl)+
160 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
161 nCategoryDecls*sizeof(ObjCCategoryDecl)+
162 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
163 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
164 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
165 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000166 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
167 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)));
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000168
Chris Lattner4b009652007-07-25 00:24:17 +0000169}
170
Chris Lattnera8344c32008-03-15 05:43:15 +0000171void Decl::addDeclKind(Kind k) {
Chris Lattner4b009652007-07-25 00:24:17 +0000172 switch (k) {
Chris Lattnera8344c32008-03-15 05:43:15 +0000173 case Typedef: nTypedef++; break;
174 case Function: nFuncs++; break;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000175 case Var: nVars++; break;
Chris Lattnera8344c32008-03-15 05:43:15 +0000176 case ParmVar: nParmVars++; break;
177 case EnumConstant: nEnumConst++; break;
178 case Field: nFieldDecls++; break;
179 case Struct: case Union: case Class: nSUC++; break;
180 case Enum: nEnumDecls++; break;
181 case ObjCInterface: nInterfaceDecls++; break;
182 case ObjCClass: nClassDecls++; break;
183 case ObjCMethod: nMethodDecls++; break;
184 case ObjCProtocol: nProtocolDecls++; break;
185 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
186 case ObjCCategory: nCategoryDecls++; break;
187 case ObjCIvar: nIvarDecls++; break;
188 case ObjCImplementation: nObjCImplementationDecls++; break;
189 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000190 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
Sam Bishopbe9c27a2008-04-08 20:49:25 +0000191 case ObjCProperty: nObjCPropertyDecl++; break;
Chris Lattnera8344c32008-03-15 05:43:15 +0000192 case LinkageSpec: nLinkageSpecDecl++; break;
193 case FileScopeAsm: nFileScopeAsmDecl++; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000194 }
195}
196
Chris Lattnera8344c32008-03-15 05:43:15 +0000197//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +0000198// Decl Allocation/Deallocation Method Implementations
199//===----------------------------------------------------------------------===//
200
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000201VarDecl *VarDecl::Create(ASTContext &C, DeclContext *CD,
202 SourceLocation L,
203 IdentifierInfo *Id, QualType T,
204 StorageClass S, ScopedDecl *PrevDecl) {
205 void *Mem = C.getAllocator().Allocate<VarDecl>();
206 return new (Mem) VarDecl(Var, CD, L, Id, T, S, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +0000207}
208
209
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000210ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattnereee57c02008-04-04 06:12:32 +0000211 SourceLocation L, IdentifierInfo *Id,
212 QualType T, StorageClass S,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000213 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner48d225c2008-03-15 21:10:16 +0000214 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattner3e254fb2008-04-08 04:40:51 +0000215 return new (Mem) ParmVarDecl(CD, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +0000216}
217
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000218FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattnereee57c02008-04-04 06:12:32 +0000219 SourceLocation L,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000220 IdentifierInfo *Id, QualType T,
221 StorageClass S, bool isInline,
222 ScopedDecl *PrevDecl) {
223 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +0000224 return new (Mem) FunctionDecl(CD, L, Id, T, S, isInline, PrevDecl);
Chris Lattner4c7802b2008-03-15 21:24:04 +0000225}
226
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000227FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000228 IdentifierInfo *Id, QualType T, Expr *BW) {
229 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000230 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner81db64a2008-03-16 00:16:02 +0000231}
232
Chris Lattner4c7802b2008-03-15 21:24:04 +0000233
Chris Lattnereee57c02008-04-04 06:12:32 +0000234EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
235 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000236 IdentifierInfo *Id, QualType T,
237 Expr *E, const llvm::APSInt &V,
238 ScopedDecl *PrevDecl){
Chris Lattnere4650482008-03-15 06:12:44 +0000239 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +0000240 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000241}
242
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000243TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattnereee57c02008-04-04 06:12:32 +0000244 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000245 IdentifierInfo *Id, QualType T,
246 ScopedDecl *PD) {
Chris Lattnere4650482008-03-15 06:12:44 +0000247 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +0000248 return new (Mem) TypedefDecl(CD, L, Id, T, PD);
Chris Lattnere4650482008-03-15 06:12:44 +0000249}
250
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000251EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *CD, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000252 IdentifierInfo *Id,
Chris Lattner58114f02008-03-15 21:32:50 +0000253 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000254 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +0000255 return new (Mem) EnumDecl(CD, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000256}
257
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000258RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *CD,
Chris Lattnereee57c02008-04-04 06:12:32 +0000259 SourceLocation L, IdentifierInfo *Id,
260 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000261 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +0000262 return new (Mem) RecordDecl(DK, CD, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000263}
264
Chris Lattnereee57c02008-04-04 06:12:32 +0000265FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
266 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000267 StringLiteral *Str) {
268 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
269 return new (Mem) FileScopeAsmDecl(L, Str);
270}
271
Chris Lattnereee57c02008-04-04 06:12:32 +0000272LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
273 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000274 LanguageIDs Lang, Decl *D) {
275 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
276 return new (Mem) LinkageSpecDecl(L, Lang, D);
277}
Chris Lattnere4650482008-03-15 06:12:44 +0000278
279//===----------------------------------------------------------------------===//
Chris Lattnera8344c32008-03-15 05:43:15 +0000280// Decl Implementation
281//===----------------------------------------------------------------------===//
282
Chris Lattner4b009652007-07-25 00:24:17 +0000283// Out-of-line virtual method providing a home for Decl.
284Decl::~Decl() {
Anders Carlssonb610a992008-02-16 03:37:41 +0000285 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000286 return;
287
288 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssonb610a992008-02-16 03:37:41 +0000289 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
290
291 delete it->second;
292 DeclAttrs->erase(it);
293 if (DeclAttrs->empty()) {
294 delete DeclAttrs;
295 DeclAttrs = 0;
296 }
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000297}
298
Chris Lattnera8344c32008-03-15 05:43:15 +0000299void Decl::addAttr(Attr *NewAttr) {
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000300 if (!DeclAttrs)
Chris Lattner10318b82008-03-16 00:19:01 +0000301 DeclAttrs = new DeclAttrMapTy();
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000302
Chris Lattnera8344c32008-03-15 05:43:15 +0000303 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000304
Chris Lattnera8344c32008-03-15 05:43:15 +0000305 NewAttr->setNext(ExistingAttr);
306 ExistingAttr = NewAttr;
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000307
308 HasAttrs = true;
309}
310
Chris Lattnera8344c32008-03-15 05:43:15 +0000311const Attr *Decl::getAttrs() const {
Anders Carlssonb610a992008-02-16 03:37:41 +0000312 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000313 return 0;
314
Anders Carlssonb610a992008-02-16 03:37:41 +0000315 return (*DeclAttrs)[this];
Chris Lattner4b009652007-07-25 00:24:17 +0000316}
317
Sam Bishop2ea14c52008-04-13 04:32:18 +0000318#define CASE(KIND) \
319 case KIND: \
320 static_cast<KIND##Decl *>(const_cast<Decl *>(this))->~KIND##Decl(); \
321 break
Sam Bishop21d4e812008-04-11 18:04:39 +0000322
Sam Bishop274366f2008-04-11 15:01:25 +0000323void Decl::Destroy(ASTContext& C) const {
Sam Bishop21d4e812008-04-11 18:04:39 +0000324 switch (getKind()) {
325 CASE(Field);
326 CASE(ObjCIvar);
327 CASE(ObjCCategory);
328 CASE(ObjCCategoryImpl);
329 CASE(ObjCImplementation);
330 CASE(ObjCProtocol);
331 CASE(ObjCProperty);
332 CASE(Typedef);
333 CASE(Enum);
334 CASE(EnumConstant);
335 CASE(Function);
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000336 CASE(Var);
Sam Bishop21d4e812008-04-11 18:04:39 +0000337 CASE(ParmVar);
338 CASE(ObjCInterface);
339 CASE(ObjCCompatibleAlias);
340 CASE(ObjCMethod);
341 CASE(ObjCClass);
342 CASE(ObjCForwardProtocol);
343 CASE(LinkageSpec);
344
345 case Struct: case Union: case Class:
Sam Bishop2ea14c52008-04-13 04:32:18 +0000346 static_cast<RecordDecl *>(const_cast<Decl *>(this))->~RecordDecl();
Sam Bishop21d4e812008-04-11 18:04:39 +0000347 break;
348
349 default: assert(0 && "Unknown decl kind!");
350 }
351
Sam Bishop274366f2008-04-11 15:01:25 +0000352 C.getAllocator().Deallocate((void *)this);
353}
354
Sam Bishop21d4e812008-04-11 18:04:39 +0000355#undef CASE
356
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000357//===----------------------------------------------------------------------===//
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000358// DeclContext Implementation
Chris Lattnereee57c02008-04-04 06:12:32 +0000359//===----------------------------------------------------------------------===//
360
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000361DeclContext *DeclContext::getParent() const {
Chris Lattnereee57c02008-04-04 06:12:32 +0000362 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000363 return SD->getDeclContext();
Chris Lattnereee57c02008-04-04 06:12:32 +0000364 else
365 return NULL;
366}
367
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000368Decl *DeclContext::ToDecl (const DeclContext *D) {
Chris Lattnereee57c02008-04-04 06:12:32 +0000369 return CastTo<Decl>(D);
370}
371
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000372DeclContext *DeclContext::FromDecl (const Decl *D) {
373 return CastTo<DeclContext>(D);
Chris Lattnereee57c02008-04-04 06:12:32 +0000374}
375
376//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000377// NamedDecl Implementation
378//===----------------------------------------------------------------------===//
379
Chris Lattner910435b2007-10-06 22:53:46 +0000380const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000381 if (const IdentifierInfo *II = getIdentifier())
382 return II->getName();
383 return "";
384}
385
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000386//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000387// FunctionDecl Implementation
388//===----------------------------------------------------------------------===//
389
Chris Lattner4b009652007-07-25 00:24:17 +0000390FunctionDecl::~FunctionDecl() {
391 delete[] ParamInfo;
Sam Bishop7a7445e2008-04-03 05:01:04 +0000392 delete Body;
Chris Lattner4b009652007-07-25 00:24:17 +0000393}
394
395unsigned FunctionDecl::getNumParams() const {
Chris Lattnere8733592008-04-06 23:09:52 +0000396 const FunctionType *FT = getType()->getAsFunctionType();
397 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000398 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000399 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000400}
401
402void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
403 assert(ParamInfo == 0 && "Already has param info!");
404 assert(NumParams == getNumParams() && "Parameter count mismatch!");
405
406 // Zero params -> null pointer.
407 if (NumParams) {
408 ParamInfo = new ParmVarDecl*[NumParams];
409 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
410 }
411}
412
Chris Lattner97316c02008-04-10 02:22:51 +0000413/// getMinRequiredArguments - Returns the minimum number of arguments
414/// needed to call this function. This may be fewer than the number of
415/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000416/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000417unsigned FunctionDecl::getMinRequiredArguments() const {
418 unsigned NumRequiredArgs = getNumParams();
419 while (NumRequiredArgs > 0
420 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
421 --NumRequiredArgs;
422
423 return NumRequiredArgs;
424}
425
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000426//===----------------------------------------------------------------------===//
427// RecordDecl Implementation
428//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000429
430/// defineBody - When created, RecordDecl's correspond to a forward declared
431/// record. This method is used to mark the decl as being defined, with the
432/// specified contents.
433void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
434 assert(!isDefinition() && "Cannot redefine record!");
435 setDefinition(true);
436 NumMembers = numMembers;
437 if (numMembers) {
438 Members = new FieldDecl*[numMembers];
439 memcpy(Members, members, numMembers*sizeof(Decl*));
440 }
441}
442
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000443FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Chris Lattner4b009652007-07-25 00:24:17 +0000444 if (Members == 0 || NumMembers < 0)
445 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000446
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000447 // Linear search. When C++ classes come along, will likely need to revisit.
448 for (int i = 0; i != NumMembers; ++i)
449 if (Members[i]->getIdentifier() == II)
Chris Lattner4b009652007-07-25 00:24:17 +0000450 return Members[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000451 return 0;
452}