blob: ddf4187a0e3234d47618eba0508ce5ef4c2f2258 [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"
Steve Naroff3fafa102007-10-01 19:00:59 +000015#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Lex/IdentifierTable.h"
17using namespace clang;
18
19// temporary statistics gathering
20static unsigned nFuncs = 0;
21static unsigned nBlockVars = 0;
22static unsigned nFileVars = 0;
23static unsigned nParmVars = 0;
24static unsigned nSUC = 0;
25static unsigned nEnumConst = 0;
26static unsigned nEnumDecls = 0;
27static unsigned nTypedef = 0;
28static unsigned nFieldDecls = 0;
Steve Naroff81f1bba2007-09-06 21:24:23 +000029static unsigned nInterfaceDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000030static unsigned nClassDecls = 0;
31static unsigned nMethodDecls = 0;
32static unsigned nProtocolDecls = 0;
Fariborz Jahanianc716c942007-09-21 15:40:54 +000033static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000034static unsigned nCategoryDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000035static unsigned nIvarDecls = 0;
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +000036static unsigned nObjcImplementationDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000037
Chris Lattner4b009652007-07-25 00:24:17 +000038static bool StatSwitch = false;
39
Steve Naroff590aba82007-09-17 14:49:06 +000040const char *Decl::getDeclKindName() const {
Steve Narofff0c31dd2007-09-16 16:16:00 +000041 switch (DeclKind) {
42 default: assert(0 && "Unknown decl kind!");
43 case Typedef:
44 return "Typedef";
45 case Function:
46 return "Function";
47 case BlockVariable:
48 return "BlockVariable";
49 case FileVariable:
50 return "FileVariable";
51 case ParmVariable:
52 return "ParmVariable";
53 case EnumConstant:
54 return "EnumConstant";
55 case ObjcInterface:
56 return "ObjcInterface";
57 case ObjcClass:
58 return "ObjcClass";
59 case ObjcMethod:
60 return "ObjcMethod";
Steve Narofff0c31dd2007-09-16 16:16:00 +000061 case ObjcProtocol:
62 return "ObjcProtocol";
Fariborz Jahanianc716c942007-09-21 15:40:54 +000063 case ObjcForwardProtocol:
64 return "ObjcForwardProtocol";
Steve Narofff0c31dd2007-09-16 16:16:00 +000065 case Struct:
66 return "Struct";
67 case Union:
68 return "Union";
69 case Class:
70 return "Class";
71 case Enum:
72 return "Enum";
73 }
74}
75
Chris Lattner4b009652007-07-25 00:24:17 +000076bool Decl::CollectingStats(bool enable) {
77 if (enable) StatSwitch = true;
78 return StatSwitch;
79}
80
81void Decl::PrintStats() {
82 fprintf(stderr, "*** Decl Stats:\n");
83 fprintf(stderr, " %d decls total.\n",
84 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff948fd372007-09-17 14:16:13 +000085 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000086 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattner4b009652007-07-25 00:24:17 +000087 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
88 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
89 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
90 nBlockVars, (int)sizeof(BlockVarDecl),
91 int(nBlockVars*sizeof(BlockVarDecl)));
92 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
93 nFileVars, (int)sizeof(FileVarDecl),
94 int(nFileVars*sizeof(FileVarDecl)));
95 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
96 nParmVars, (int)sizeof(ParmVarDecl),
97 int(nParmVars*sizeof(ParmVarDecl)));
98 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
99 nFieldDecls, (int)sizeof(FieldDecl),
100 int(nFieldDecls*sizeof(FieldDecl)));
101 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
102 nSUC, (int)sizeof(RecordDecl),
103 int(nSUC*sizeof(RecordDecl)));
104 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
105 nEnumDecls, (int)sizeof(EnumDecl),
106 int(nEnumDecls*sizeof(EnumDecl)));
107 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
108 nEnumConst, (int)sizeof(EnumConstantDecl),
109 int(nEnumConst*sizeof(EnumConstantDecl)));
110 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
111 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000112 // Objective-C decls...
113 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
114 nInterfaceDecls, (int)sizeof(ObjcInterfaceDecl),
115 int(nInterfaceDecls*sizeof(ObjcInterfaceDecl)));
116 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
117 nIvarDecls, (int)sizeof(ObjcIvarDecl),
118 int(nIvarDecls*sizeof(ObjcIvarDecl)));
119 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
120 nClassDecls, (int)sizeof(ObjcClassDecl),
121 int(nClassDecls*sizeof(ObjcClassDecl)));
122 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
123 nMethodDecls, (int)sizeof(ObjcMethodDecl),
124 int(nMethodDecls*sizeof(ObjcMethodDecl)));
125 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
126 nProtocolDecls, (int)sizeof(ObjcProtocolDecl),
127 int(nProtocolDecls*sizeof(ObjcProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000128 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
129 nForwardProtocolDecls, (int)sizeof(ObjcForwardProtocolDecl),
130 int(nForwardProtocolDecls*sizeof(ObjcForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000131 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
132 nCategoryDecls, (int)sizeof(ObjcCategoryDecl),
133 int(nCategoryDecls*sizeof(ObjcCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000134
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000135 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
136 nObjcImplementationDecls, (int)sizeof(ObjcImplementationDecl),
137 int(nObjcImplementationDecls*sizeof(ObjcImplementationDecl)));
138
Chris Lattner4b009652007-07-25 00:24:17 +0000139 fprintf(stderr, "Total bytes = %d\n",
140 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
141 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
142 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
143 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Steve Naroff948fd372007-09-17 14:16:13 +0000144 nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */);
Chris Lattner4b009652007-07-25 00:24:17 +0000145}
146
147void Decl::addDeclKind(const Kind k) {
148 switch (k) {
149 case Typedef:
150 nTypedef++;
151 break;
152 case Function:
153 nFuncs++;
154 break;
155 case BlockVariable:
156 nBlockVars++;
157 break;
158 case FileVariable:
159 nFileVars++;
160 break;
161 case ParmVariable:
162 nParmVars++;
163 break;
164 case EnumConstant:
165 nEnumConst++;
166 break;
167 case Field:
168 nFieldDecls++;
169 break;
170 case Struct:
171 case Union:
172 case Class:
173 nSUC++;
174 break;
175 case Enum:
176 nEnumDecls++;
177 break;
Steve Naroff81f1bba2007-09-06 21:24:23 +0000178 case ObjcInterface:
179 nInterfaceDecls++;
180 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000181 case ObjcClass:
Steve Naroff948fd372007-09-17 14:16:13 +0000182 nClassDecls++;
183 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000184 case ObjcMethod:
Steve Naroff948fd372007-09-17 14:16:13 +0000185 nMethodDecls++;
186 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000187 case ObjcProtocol:
Steve Naroff948fd372007-09-17 14:16:13 +0000188 nProtocolDecls++;
189 break;
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000190 case ObjcForwardProtocol:
191 nForwardProtocolDecls++;
192 break;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000193 case ObjcCategory:
194 nCategoryDecls++;
195 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000196 case ObjcIvar:
Steve Naroff948fd372007-09-17 14:16:13 +0000197 nIvarDecls++;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000198 break;
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000199 case ObjcImplementation:
200 nObjcImplementationDecls++;
201 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000202 }
203}
204
205// Out-of-line virtual method providing a home for Decl.
206Decl::~Decl() {
207}
208
Steve Naroffcb597472007-09-13 21:41:19 +0000209const char *FieldDecl::getName() const {
210 if (const IdentifierInfo *II = getIdentifier())
211 return II->getName();
212 return "";
213}
214
215const char *ScopedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000216 if (const IdentifierInfo *II = getIdentifier())
217 return II->getName();
218 return "";
219}
220
221
222FunctionDecl::~FunctionDecl() {
223 delete[] ParamInfo;
224}
225
226unsigned FunctionDecl::getNumParams() const {
227 return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
228}
229
230void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
231 assert(ParamInfo == 0 && "Already has param info!");
232 assert(NumParams == getNumParams() && "Parameter count mismatch!");
233
234 // Zero params -> null pointer.
235 if (NumParams) {
236 ParamInfo = new ParmVarDecl*[NumParams];
237 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
238 }
239}
240
241
242/// defineBody - When created, RecordDecl's correspond to a forward declared
243/// record. This method is used to mark the decl as being defined, with the
244/// specified contents.
245void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
246 assert(!isDefinition() && "Cannot redefine record!");
247 setDefinition(true);
248 NumMembers = numMembers;
249 if (numMembers) {
250 Members = new FieldDecl*[numMembers];
251 memcpy(Members, members, numMembers*sizeof(Decl*));
252 }
253}
254
255FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
256 if (Members == 0 || NumMembers < 0)
257 return 0;
258
259 // linear search. When C++ classes come along, will likely need to revisit.
260 for (int i = 0; i < NumMembers; ++i) {
261 if (Members[i]->getIdentifier() == name)
262 return Members[i];
263 }
264 return 0;
265}
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000266
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000267void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanianbec0d562007-10-01 16:53:59 +0000268 unsigned NumParams) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000269 assert(ParamInfo == 0 && "Already has param info!");
270
271 // Zero params -> null pointer.
272 if (NumParams) {
273 ParamInfo = new ParmVarDecl*[NumParams];
274 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
275 NumMethodParams = NumParams;
276 }
277}
278
279ObjcMethodDecl::~ObjcMethodDecl() {
280 delete[] ParamInfo;
281}
282
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000283/// ObjcAddInstanceVariablesToClass - Inserts instance variables
284/// into ObjcInterfaceDecl's fields.
285///
286void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars,
287 unsigned numIvars) {
288 NumIvars = numIvars;
289 if (numIvars) {
290 Ivars = new ObjcIvarDecl*[numIvars];
291 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
292 }
293}
294
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000295/// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance
296/// Variables (Ivars) relative to what declared in @implementation;s class.
297/// Ivars into ObjcImplementationDecl's fields.
298///
299void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl(
300 ObjcIvarDecl **ivars, unsigned numIvars) {
301 NumIvars = numIvars;
302 if (numIvars) {
303 Ivars = new ObjcIvarDecl*[numIvars];
304 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
305 }
306}
307
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000308/// addObjcMethods - Insert instance and methods declarations into
309/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
310///
311void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods,
312 unsigned numInsMembers,
313 ObjcMethodDecl **clsMethods,
314 unsigned numClsMembers) {
315 NumInsMethods = numInsMembers;
316 if (numInsMembers) {
317 InsMethods = new ObjcMethodDecl*[numInsMembers];
318 memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
319 }
320 NumClsMethods = numClsMembers;
321 if (numClsMembers) {
322 ClsMethods = new ObjcMethodDecl*[numClsMembers];
323 memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
324 }
325}
326
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000327/// ObjcAddProtoMethods - Insert instance and methods declarations into
328/// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
329///
330void ObjcProtocolDecl::ObjcAddProtoMethods(ObjcMethodDecl **insMethods,
331 unsigned numInsMembers,
332 ObjcMethodDecl **clsMethods,
333 unsigned numClsMembers) {
334 NumProtoInsMethods = numInsMembers;
335 if (numInsMembers) {
Fariborz Jahanian4a2b0ac2007-09-17 22:36:42 +0000336 ProtoInsMethods = new ObjcMethodDecl*[numInsMembers];
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000337 memcpy(ProtoInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
338 }
339 NumProtoClsMethods = numClsMembers;
340 if (numClsMembers) {
Fariborz Jahanian4a2b0ac2007-09-17 22:36:42 +0000341 ProtoClsMethods = new ObjcMethodDecl*[numClsMembers];
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000342 memcpy(ProtoClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
343 }
344}
345
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000346/// ObjcAddCat - Insert instance and methods declarations into
347/// ObjcProtocolDecl's CatInsMethods and CatClsMethods fields.
348///
349void ObjcCategoryDecl::ObjcAddCatMethods(ObjcMethodDecl **insMethods,
350 unsigned numInsMembers,
351 ObjcMethodDecl **clsMethods,
352 unsigned numClsMembers) {
353 NumCatInsMethods = numInsMembers;
354 if (numInsMembers) {
355 CatInsMethods = new ObjcMethodDecl*[numInsMembers];
356 memcpy(CatInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
357 }
358 NumCatClsMethods = numClsMembers;
359 if (numClsMembers) {
360 CatClsMethods = new ObjcMethodDecl*[numClsMembers];
361 memcpy(CatClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
362 }
363}
364
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000365/// ObjcAddImplMethods - Insert instance and methods declarations into
366/// ObjcImplementationDecl's InsMethods and ClsMethods fields.
367///
368void ObjcImplementationDecl::ObjcAddImplMethods(ObjcMethodDecl **insMethods,
369 unsigned numInsMembers,
370 ObjcMethodDecl **clsMethods,
371 unsigned numClsMembers) {
372 NumInsMethods = numInsMembers;
373 if (numInsMembers) {
374 InsMethods = new ObjcMethodDecl*[numInsMembers];
375 memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
376 }
377 NumClsMethods = numClsMembers;
378 if (numClsMembers) {
379 ClsMethods = new ObjcMethodDecl*[numClsMembers];
380 memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
381 }
382}
383
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000384