blob: 83762e946580a49015f9e45d35c0d55fab8dbf74 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
15#include "clang/Lex/IdentifierTable.h"
16using namespace clang;
17
18// temporary statistics gathering
19static unsigned nFuncs = 0;
20static unsigned nBlockVars = 0;
21static unsigned nFileVars = 0;
22static unsigned nParmVars = 0;
23static unsigned nSUC = 0;
24static unsigned nEnumConst = 0;
25static unsigned nEnumDecls = 0;
26static unsigned nTypedef = 0;
27static unsigned nFieldDecls = 0;
Steve Naroff81f1bba2007-09-06 21:24:23 +000028static unsigned nInterfaceDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000029static unsigned nClassDecls = 0;
30static unsigned nMethodDecls = 0;
31static unsigned nProtocolDecls = 0;
Fariborz Jahanianc716c942007-09-21 15:40:54 +000032static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000033static unsigned nCategoryDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000034static unsigned nIvarDecls = 0;
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +000035static unsigned nObjcImplementationDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000036
Chris Lattner4b009652007-07-25 00:24:17 +000037static bool StatSwitch = false;
38
Steve Naroff590aba82007-09-17 14:49:06 +000039const char *Decl::getDeclKindName() const {
Steve Narofff0c31dd2007-09-16 16:16:00 +000040 switch (DeclKind) {
41 default: assert(0 && "Unknown decl kind!");
42 case Typedef:
43 return "Typedef";
44 case Function:
45 return "Function";
46 case BlockVariable:
47 return "BlockVariable";
48 case FileVariable:
49 return "FileVariable";
50 case ParmVariable:
51 return "ParmVariable";
52 case EnumConstant:
53 return "EnumConstant";
54 case ObjcInterface:
55 return "ObjcInterface";
56 case ObjcClass:
57 return "ObjcClass";
58 case ObjcMethod:
59 return "ObjcMethod";
60 case ObjcProtoMethod:
61 return "ObjcProtoMethod";
62 case ObjcProtocol:
63 return "ObjcProtocol";
Fariborz Jahanianc716c942007-09-21 15:40:54 +000064 case ObjcForwardProtocol:
65 return "ObjcForwardProtocol";
Steve Narofff0c31dd2007-09-16 16:16:00 +000066 case Struct:
67 return "Struct";
68 case Union:
69 return "Union";
70 case Class:
71 return "Class";
72 case Enum:
73 return "Enum";
74 }
75}
76
Chris Lattner4b009652007-07-25 00:24:17 +000077bool Decl::CollectingStats(bool enable) {
78 if (enable) StatSwitch = true;
79 return StatSwitch;
80}
81
82void Decl::PrintStats() {
83 fprintf(stderr, "*** Decl Stats:\n");
84 fprintf(stderr, " %d decls total.\n",
85 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff948fd372007-09-17 14:16:13 +000086 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000087 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattner4b009652007-07-25 00:24:17 +000088 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
89 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
90 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
91 nBlockVars, (int)sizeof(BlockVarDecl),
92 int(nBlockVars*sizeof(BlockVarDecl)));
93 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
94 nFileVars, (int)sizeof(FileVarDecl),
95 int(nFileVars*sizeof(FileVarDecl)));
96 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
97 nParmVars, (int)sizeof(ParmVarDecl),
98 int(nParmVars*sizeof(ParmVarDecl)));
99 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
100 nFieldDecls, (int)sizeof(FieldDecl),
101 int(nFieldDecls*sizeof(FieldDecl)));
102 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
103 nSUC, (int)sizeof(RecordDecl),
104 int(nSUC*sizeof(RecordDecl)));
105 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
106 nEnumDecls, (int)sizeof(EnumDecl),
107 int(nEnumDecls*sizeof(EnumDecl)));
108 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
109 nEnumConst, (int)sizeof(EnumConstantDecl),
110 int(nEnumConst*sizeof(EnumConstantDecl)));
111 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
112 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000113 // Objective-C decls...
114 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
115 nInterfaceDecls, (int)sizeof(ObjcInterfaceDecl),
116 int(nInterfaceDecls*sizeof(ObjcInterfaceDecl)));
117 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
118 nIvarDecls, (int)sizeof(ObjcIvarDecl),
119 int(nIvarDecls*sizeof(ObjcIvarDecl)));
120 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
121 nClassDecls, (int)sizeof(ObjcClassDecl),
122 int(nClassDecls*sizeof(ObjcClassDecl)));
123 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
124 nMethodDecls, (int)sizeof(ObjcMethodDecl),
125 int(nMethodDecls*sizeof(ObjcMethodDecl)));
126 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
127 nProtocolDecls, (int)sizeof(ObjcProtocolDecl),
128 int(nProtocolDecls*sizeof(ObjcProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000129 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
130 nForwardProtocolDecls, (int)sizeof(ObjcForwardProtocolDecl),
131 int(nForwardProtocolDecls*sizeof(ObjcForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000132 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
133 nCategoryDecls, (int)sizeof(ObjcCategoryDecl),
134 int(nCategoryDecls*sizeof(ObjcCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000135
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000136 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
137 nObjcImplementationDecls, (int)sizeof(ObjcImplementationDecl),
138 int(nObjcImplementationDecls*sizeof(ObjcImplementationDecl)));
139
Chris Lattner4b009652007-07-25 00:24:17 +0000140 fprintf(stderr, "Total bytes = %d\n",
141 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
142 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
143 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
144 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Steve Naroff948fd372007-09-17 14:16:13 +0000145 nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */);
Chris Lattner4b009652007-07-25 00:24:17 +0000146}
147
148void Decl::addDeclKind(const Kind k) {
149 switch (k) {
150 case Typedef:
151 nTypedef++;
152 break;
153 case Function:
154 nFuncs++;
155 break;
156 case BlockVariable:
157 nBlockVars++;
158 break;
159 case FileVariable:
160 nFileVars++;
161 break;
162 case ParmVariable:
163 nParmVars++;
164 break;
165 case EnumConstant:
166 nEnumConst++;
167 break;
168 case Field:
169 nFieldDecls++;
170 break;
171 case Struct:
172 case Union:
173 case Class:
174 nSUC++;
175 break;
176 case Enum:
177 nEnumDecls++;
178 break;
Steve Naroff81f1bba2007-09-06 21:24:23 +0000179 case ObjcInterface:
180 nInterfaceDecls++;
181 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000182 case ObjcClass:
Steve Naroff948fd372007-09-17 14:16:13 +0000183 nClassDecls++;
184 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000185 case ObjcMethod:
186 case ObjcProtoMethod:
Steve Naroff948fd372007-09-17 14:16:13 +0000187 nMethodDecls++;
188 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000189 case ObjcProtocol:
Steve Naroff948fd372007-09-17 14:16:13 +0000190 nProtocolDecls++;
191 break;
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000192 case ObjcForwardProtocol:
193 nForwardProtocolDecls++;
194 break;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000195 case ObjcCategory:
196 nCategoryDecls++;
197 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000198 case ObjcIvar:
Steve Naroff948fd372007-09-17 14:16:13 +0000199 nIvarDecls++;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000200 break;
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000201 case ObjcImplementation:
202 nObjcImplementationDecls++;
203 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000204 }
205}
206
207// Out-of-line virtual method providing a home for Decl.
208Decl::~Decl() {
209}
210
Steve Naroffcb597472007-09-13 21:41:19 +0000211const char *FieldDecl::getName() const {
212 if (const IdentifierInfo *II = getIdentifier())
213 return II->getName();
214 return "";
215}
216
217const char *ScopedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000218 if (const IdentifierInfo *II = getIdentifier())
219 return II->getName();
220 return "";
221}
222
223
224FunctionDecl::~FunctionDecl() {
225 delete[] ParamInfo;
226}
227
228unsigned FunctionDecl::getNumParams() const {
229 return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
230}
231
232void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
233 assert(ParamInfo == 0 && "Already has param info!");
234 assert(NumParams == getNumParams() && "Parameter count mismatch!");
235
236 // Zero params -> null pointer.
237 if (NumParams) {
238 ParamInfo = new ParmVarDecl*[NumParams];
239 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
240 }
241}
242
243
244/// defineBody - When created, RecordDecl's correspond to a forward declared
245/// record. This method is used to mark the decl as being defined, with the
246/// specified contents.
247void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
248 assert(!isDefinition() && "Cannot redefine record!");
249 setDefinition(true);
250 NumMembers = numMembers;
251 if (numMembers) {
252 Members = new FieldDecl*[numMembers];
253 memcpy(Members, members, numMembers*sizeof(Decl*));
254 }
255}
256
257FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
258 if (Members == 0 || NumMembers < 0)
259 return 0;
260
261 // linear search. When C++ classes come along, will likely need to revisit.
262 for (int i = 0; i < NumMembers; ++i) {
263 if (Members[i]->getIdentifier() == name)
264 return Members[i];
265 }
266 return 0;
267}
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000268
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000269void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
270 unsigned NumParams) {
271 assert(ParamInfo == 0 && "Already has param info!");
272
273 // Zero params -> null pointer.
274 if (NumParams) {
275 ParamInfo = new ParmVarDecl*[NumParams];
276 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
277 NumMethodParams = NumParams;
278 }
279}
280
281ObjcMethodDecl::~ObjcMethodDecl() {
282 delete[] ParamInfo;
283}
284
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000285/// ObjcAddInstanceVariablesToClass - Inserts instance variables
286/// into ObjcInterfaceDecl's fields.
287///
288void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars,
289 unsigned numIvars) {
290 NumIvars = numIvars;
291 if (numIvars) {
292 Ivars = new ObjcIvarDecl*[numIvars];
293 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
294 }
295}
296
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000297/// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance
298/// Variables (Ivars) relative to what declared in @implementation;s class.
299/// Ivars into ObjcImplementationDecl's fields.
300///
301void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl(
302 ObjcIvarDecl **ivars, unsigned numIvars) {
303 NumIvars = numIvars;
304 if (numIvars) {
305 Ivars = new ObjcIvarDecl*[numIvars];
306 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
307 }
308}
309
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000310/// addObjcMethods - Insert instance and methods declarations into
311/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
312///
313void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods,
314 unsigned numInsMembers,
315 ObjcMethodDecl **clsMethods,
316 unsigned numClsMembers) {
317 NumInsMethods = numInsMembers;
318 if (numInsMembers) {
319 InsMethods = new ObjcMethodDecl*[numInsMembers];
320 memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
321 }
322 NumClsMethods = numClsMembers;
323 if (numClsMembers) {
324 ClsMethods = new ObjcMethodDecl*[numClsMembers];
325 memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
326 }
327}
328
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000329/// ObjcAddProtoMethods - Insert instance and methods declarations into
330/// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
331///
332void ObjcProtocolDecl::ObjcAddProtoMethods(ObjcMethodDecl **insMethods,
333 unsigned numInsMembers,
334 ObjcMethodDecl **clsMethods,
335 unsigned numClsMembers) {
336 NumProtoInsMethods = numInsMembers;
337 if (numInsMembers) {
Fariborz Jahanian4a2b0ac2007-09-17 22:36:42 +0000338 ProtoInsMethods = new ObjcMethodDecl*[numInsMembers];
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000339 memcpy(ProtoInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
340 }
341 NumProtoClsMethods = numClsMembers;
342 if (numClsMembers) {
Fariborz Jahanian4a2b0ac2007-09-17 22:36:42 +0000343 ProtoClsMethods = new ObjcMethodDecl*[numClsMembers];
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000344 memcpy(ProtoClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
345 }
346}
347
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000348/// ObjcAddCat - Insert instance and methods declarations into
349/// ObjcProtocolDecl's CatInsMethods and CatClsMethods fields.
350///
351void ObjcCategoryDecl::ObjcAddCatMethods(ObjcMethodDecl **insMethods,
352 unsigned numInsMembers,
353 ObjcMethodDecl **clsMethods,
354 unsigned numClsMembers) {
355 NumCatInsMethods = numInsMembers;
356 if (numInsMembers) {
357 CatInsMethods = new ObjcMethodDecl*[numInsMembers];
358 memcpy(CatInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
359 }
360 NumCatClsMethods = numClsMembers;
361 if (numClsMembers) {
362 CatClsMethods = new ObjcMethodDecl*[numClsMembers];
363 memcpy(CatClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
364 }
365}
366
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000367