blob: b8c9275a2638cc00a8295a3f3b3fab3982de5066 [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";
Steve Narofff0c31dd2007-09-16 16:16:00 +000060 case ObjcProtocol:
61 return "ObjcProtocol";
Fariborz Jahanianc716c942007-09-21 15:40:54 +000062 case ObjcForwardProtocol:
63 return "ObjcForwardProtocol";
Steve Narofff0c31dd2007-09-16 16:16:00 +000064 case Struct:
65 return "Struct";
66 case Union:
67 return "Union";
68 case Class:
69 return "Class";
70 case Enum:
71 return "Enum";
72 }
73}
74
Chris Lattner4b009652007-07-25 00:24:17 +000075bool Decl::CollectingStats(bool enable) {
76 if (enable) StatSwitch = true;
77 return StatSwitch;
78}
79
80void Decl::PrintStats() {
81 fprintf(stderr, "*** Decl Stats:\n");
82 fprintf(stderr, " %d decls total.\n",
83 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff948fd372007-09-17 14:16:13 +000084 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000085 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattner4b009652007-07-25 00:24:17 +000086 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
87 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
88 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
89 nBlockVars, (int)sizeof(BlockVarDecl),
90 int(nBlockVars*sizeof(BlockVarDecl)));
91 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
92 nFileVars, (int)sizeof(FileVarDecl),
93 int(nFileVars*sizeof(FileVarDecl)));
94 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
95 nParmVars, (int)sizeof(ParmVarDecl),
96 int(nParmVars*sizeof(ParmVarDecl)));
97 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
98 nFieldDecls, (int)sizeof(FieldDecl),
99 int(nFieldDecls*sizeof(FieldDecl)));
100 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
101 nSUC, (int)sizeof(RecordDecl),
102 int(nSUC*sizeof(RecordDecl)));
103 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
104 nEnumDecls, (int)sizeof(EnumDecl),
105 int(nEnumDecls*sizeof(EnumDecl)));
106 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
107 nEnumConst, (int)sizeof(EnumConstantDecl),
108 int(nEnumConst*sizeof(EnumConstantDecl)));
109 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
110 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000111 // Objective-C decls...
112 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
113 nInterfaceDecls, (int)sizeof(ObjcInterfaceDecl),
114 int(nInterfaceDecls*sizeof(ObjcInterfaceDecl)));
115 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
116 nIvarDecls, (int)sizeof(ObjcIvarDecl),
117 int(nIvarDecls*sizeof(ObjcIvarDecl)));
118 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
119 nClassDecls, (int)sizeof(ObjcClassDecl),
120 int(nClassDecls*sizeof(ObjcClassDecl)));
121 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
122 nMethodDecls, (int)sizeof(ObjcMethodDecl),
123 int(nMethodDecls*sizeof(ObjcMethodDecl)));
124 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
125 nProtocolDecls, (int)sizeof(ObjcProtocolDecl),
126 int(nProtocolDecls*sizeof(ObjcProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000127 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
128 nForwardProtocolDecls, (int)sizeof(ObjcForwardProtocolDecl),
129 int(nForwardProtocolDecls*sizeof(ObjcForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000130 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
131 nCategoryDecls, (int)sizeof(ObjcCategoryDecl),
132 int(nCategoryDecls*sizeof(ObjcCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000133
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000134 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
135 nObjcImplementationDecls, (int)sizeof(ObjcImplementationDecl),
136 int(nObjcImplementationDecls*sizeof(ObjcImplementationDecl)));
137
Chris Lattner4b009652007-07-25 00:24:17 +0000138 fprintf(stderr, "Total bytes = %d\n",
139 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
140 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
141 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
142 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Steve Naroff948fd372007-09-17 14:16:13 +0000143 nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */);
Chris Lattner4b009652007-07-25 00:24:17 +0000144}
145
146void Decl::addDeclKind(const Kind k) {
147 switch (k) {
148 case Typedef:
149 nTypedef++;
150 break;
151 case Function:
152 nFuncs++;
153 break;
154 case BlockVariable:
155 nBlockVars++;
156 break;
157 case FileVariable:
158 nFileVars++;
159 break;
160 case ParmVariable:
161 nParmVars++;
162 break;
163 case EnumConstant:
164 nEnumConst++;
165 break;
166 case Field:
167 nFieldDecls++;
168 break;
169 case Struct:
170 case Union:
171 case Class:
172 nSUC++;
173 break;
174 case Enum:
175 nEnumDecls++;
176 break;
Steve Naroff81f1bba2007-09-06 21:24:23 +0000177 case ObjcInterface:
178 nInterfaceDecls++;
179 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000180 case ObjcClass:
Steve Naroff948fd372007-09-17 14:16:13 +0000181 nClassDecls++;
182 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000183 case ObjcMethod:
Steve Naroff948fd372007-09-17 14:16:13 +0000184 nMethodDecls++;
185 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000186 case ObjcProtocol:
Steve Naroff948fd372007-09-17 14:16:13 +0000187 nProtocolDecls++;
188 break;
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000189 case ObjcForwardProtocol:
190 nForwardProtocolDecls++;
191 break;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000192 case ObjcCategory:
193 nCategoryDecls++;
194 break;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000195 case ObjcIvar:
Steve Naroff948fd372007-09-17 14:16:13 +0000196 nIvarDecls++;
Chris Lattnerb15b4382007-09-16 19:23:04 +0000197 break;
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000198 case ObjcImplementation:
199 nObjcImplementationDecls++;
200 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000201 }
202}
203
204// Out-of-line virtual method providing a home for Decl.
205Decl::~Decl() {
206}
207
Steve Naroffcb597472007-09-13 21:41:19 +0000208const char *FieldDecl::getName() const {
209 if (const IdentifierInfo *II = getIdentifier())
210 return II->getName();
211 return "";
212}
213
214const char *ScopedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000215 if (const IdentifierInfo *II = getIdentifier())
216 return II->getName();
217 return "";
218}
219
220
221FunctionDecl::~FunctionDecl() {
222 delete[] ParamInfo;
223}
224
225unsigned FunctionDecl::getNumParams() const {
226 return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
227}
228
229void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
230 assert(ParamInfo == 0 && "Already has param info!");
231 assert(NumParams == getNumParams() && "Parameter count mismatch!");
232
233 // Zero params -> null pointer.
234 if (NumParams) {
235 ParamInfo = new ParmVarDecl*[NumParams];
236 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
237 }
238}
239
240
241/// defineBody - When created, RecordDecl's correspond to a forward declared
242/// record. This method is used to mark the decl as being defined, with the
243/// specified contents.
244void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
245 assert(!isDefinition() && "Cannot redefine record!");
246 setDefinition(true);
247 NumMembers = numMembers;
248 if (numMembers) {
249 Members = new FieldDecl*[numMembers];
250 memcpy(Members, members, numMembers*sizeof(Decl*));
251 }
252}
253
254FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
255 if (Members == 0 || NumMembers < 0)
256 return 0;
257
258 // linear search. When C++ classes come along, will likely need to revisit.
259 for (int i = 0; i < NumMembers; ++i) {
260 if (Members[i]->getIdentifier() == name)
261 return Members[i];
262 }
263 return 0;
264}
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000265
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000266void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanianbec0d562007-10-01 16:53:59 +0000267 unsigned NumParams) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000268 assert(ParamInfo == 0 && "Already has param info!");
269
270 // Zero params -> null pointer.
271 if (NumParams) {
272 ParamInfo = new ParmVarDecl*[NumParams];
273 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
274 NumMethodParams = NumParams;
275 }
276}
277
278ObjcMethodDecl::~ObjcMethodDecl() {
279 delete[] ParamInfo;
280}
281
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000282/// ObjcAddInstanceVariablesToClass - Inserts instance variables
283/// into ObjcInterfaceDecl's fields.
284///
285void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars,
286 unsigned numIvars) {
287 NumIvars = numIvars;
288 if (numIvars) {
289 Ivars = new ObjcIvarDecl*[numIvars];
290 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
291 }
292}
293
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000294/// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance
295/// Variables (Ivars) relative to what declared in @implementation;s class.
296/// Ivars into ObjcImplementationDecl's fields.
297///
298void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl(
299 ObjcIvarDecl **ivars, unsigned numIvars) {
300 NumIvars = numIvars;
301 if (numIvars) {
302 Ivars = new ObjcIvarDecl*[numIvars];
303 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
304 }
305}
306
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000307/// addObjcMethods - Insert instance and methods declarations into
308/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
309///
310void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods,
311 unsigned numInsMembers,
312 ObjcMethodDecl **clsMethods,
313 unsigned numClsMembers) {
314 NumInsMethods = numInsMembers;
315 if (numInsMembers) {
316 InsMethods = new ObjcMethodDecl*[numInsMembers];
317 memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
318 }
319 NumClsMethods = numClsMembers;
320 if (numClsMembers) {
321 ClsMethods = new ObjcMethodDecl*[numClsMembers];
322 memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
323 }
324}
325
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000326/// ObjcAddProtoMethods - Insert instance and methods declarations into
327/// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
328///
329void ObjcProtocolDecl::ObjcAddProtoMethods(ObjcMethodDecl **insMethods,
330 unsigned numInsMembers,
331 ObjcMethodDecl **clsMethods,
332 unsigned numClsMembers) {
333 NumProtoInsMethods = numInsMembers;
334 if (numInsMembers) {
Fariborz Jahanian4a2b0ac2007-09-17 22:36:42 +0000335 ProtoInsMethods = new ObjcMethodDecl*[numInsMembers];
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000336 memcpy(ProtoInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
337 }
338 NumProtoClsMethods = numClsMembers;
339 if (numClsMembers) {
Fariborz Jahanian4a2b0ac2007-09-17 22:36:42 +0000340 ProtoClsMethods = new ObjcMethodDecl*[numClsMembers];
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000341 memcpy(ProtoClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
342 }
343}
344
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000345/// ObjcAddCat - Insert instance and methods declarations into
346/// ObjcProtocolDecl's CatInsMethods and CatClsMethods fields.
347///
348void ObjcCategoryDecl::ObjcAddCatMethods(ObjcMethodDecl **insMethods,
349 unsigned numInsMembers,
350 ObjcMethodDecl **clsMethods,
351 unsigned numClsMembers) {
352 NumCatInsMethods = numInsMembers;
353 if (numInsMembers) {
354 CatInsMethods = new ObjcMethodDecl*[numInsMembers];
355 memcpy(CatInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
356 }
357 NumCatClsMethods = numClsMembers;
358 if (numClsMembers) {
359 CatClsMethods = new ObjcMethodDecl*[numClsMembers];
360 memcpy(CatClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
361 }
362}
363
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000364/// ObjcAddImplMethods - Insert instance and methods declarations into
365/// ObjcImplementationDecl's InsMethods and ClsMethods fields.
366///
367void ObjcImplementationDecl::ObjcAddImplMethods(ObjcMethodDecl **insMethods,
368 unsigned numInsMembers,
369 ObjcMethodDecl **clsMethods,
370 unsigned numClsMembers) {
371 NumInsMethods = numInsMembers;
372 if (numInsMembers) {
373 InsMethods = new ObjcMethodDecl*[numInsMembers];
374 memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
375 }
376 NumClsMethods = numClsMembers;
377 if (numClsMembers) {
378 ClsMethods = new ObjcMethodDecl*[numClsMembers];
379 memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
380 }
381}
382
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000383