blob: d23f3cb6f3496eaea57cf2a62f4dffd565304b02 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff980e5082007-10-01 19:00:59 +000015#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff3536b442007-09-06 21:24:23 +000029static unsigned nInterfaceDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000030static unsigned nClassDecls = 0;
31static unsigned nMethodDecls = 0;
32static unsigned nProtocolDecls = 0;
Fariborz Jahanian894c57f2007-09-21 15:40:54 +000033static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000034static unsigned nCategoryDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000035static unsigned nIvarDecls = 0;
Fariborz Jahanianccb4f312007-09-25 18:38:09 +000036static unsigned nObjcImplementationDecls = 0;
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +000037static unsigned nObjcCategoryImpl = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000038
Reid Spencer5f016e22007-07-11 17:01:13 +000039static bool StatSwitch = false;
40
Steve Naroffe5ea3802007-09-17 14:49:06 +000041const char *Decl::getDeclKindName() const {
Steve Naroff8c9f13e2007-09-16 16:16:00 +000042 switch (DeclKind) {
43 default: assert(0 && "Unknown decl kind!");
44 case Typedef:
45 return "Typedef";
46 case Function:
47 return "Function";
48 case BlockVariable:
49 return "BlockVariable";
50 case FileVariable:
51 return "FileVariable";
52 case ParmVariable:
53 return "ParmVariable";
54 case EnumConstant:
55 return "EnumConstant";
56 case ObjcInterface:
57 return "ObjcInterface";
58 case ObjcClass:
59 return "ObjcClass";
60 case ObjcMethod:
61 return "ObjcMethod";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000062 case ObjcProtocol:
63 return "ObjcProtocol";
Fariborz Jahanian894c57f2007-09-21 15:40:54 +000064 case ObjcForwardProtocol:
65 return "ObjcForwardProtocol";
Steve Naroff8c9f13e2007-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff3f128ad2007-09-17 14:16:13 +000086 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000087 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff3f128ad2007-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 Jahanian894c57f2007-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 Jahanianfd225cc2007-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 Naroff3f128ad2007-09-17 14:16:13 +0000135
Fariborz Jahanianccb4f312007-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
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000140 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
141 nObjcCategoryImpl, (int)sizeof(ObjcCategoryImplDecl),
142 int(nObjcCategoryImpl*sizeof(ObjcCategoryImplDecl)));
143
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 fprintf(stderr, "Total bytes = %d\n",
145 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
146 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
147 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
148 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Steve Naroff3f128ad2007-09-17 14:16:13 +0000149 nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */);
Reid Spencer5f016e22007-07-11 17:01:13 +0000150}
151
152void Decl::addDeclKind(const Kind k) {
153 switch (k) {
154 case Typedef:
155 nTypedef++;
156 break;
157 case Function:
158 nFuncs++;
159 break;
160 case BlockVariable:
161 nBlockVars++;
162 break;
163 case FileVariable:
164 nFileVars++;
165 break;
166 case ParmVariable:
167 nParmVars++;
168 break;
169 case EnumConstant:
170 nEnumConst++;
171 break;
172 case Field:
173 nFieldDecls++;
174 break;
175 case Struct:
176 case Union:
177 case Class:
178 nSUC++;
179 break;
180 case Enum:
181 nEnumDecls++;
182 break;
Steve Naroff3536b442007-09-06 21:24:23 +0000183 case ObjcInterface:
184 nInterfaceDecls++;
185 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000186 case ObjcClass:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000187 nClassDecls++;
188 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000189 case ObjcMethod:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000190 nMethodDecls++;
191 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000192 case ObjcProtocol:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000193 nProtocolDecls++;
194 break;
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000195 case ObjcForwardProtocol:
196 nForwardProtocolDecls++;
197 break;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000198 case ObjcCategory:
199 nCategoryDecls++;
200 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000201 case ObjcIvar:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000202 nIvarDecls++;
Chris Lattner7341c332007-09-16 19:23:04 +0000203 break;
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000204 case ObjcImplementation:
205 nObjcImplementationDecls++;
206 break;
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000207 case ObjcCategoryImpl:
208 nObjcCategoryImpl++;
209 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 }
211}
212
213// Out-of-line virtual method providing a home for Decl.
214Decl::~Decl() {
215}
216
Steve Naroff8e74c932007-09-13 21:41:19 +0000217const char *FieldDecl::getName() const {
218 if (const IdentifierInfo *II = getIdentifier())
219 return II->getName();
220 return "";
221}
222
223const char *ScopedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 if (const IdentifierInfo *II = getIdentifier())
225 return II->getName();
226 return "";
227}
228
229
230FunctionDecl::~FunctionDecl() {
231 delete[] ParamInfo;
232}
233
234unsigned FunctionDecl::getNumParams() const {
235 return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
236}
237
238void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
239 assert(ParamInfo == 0 && "Already has param info!");
240 assert(NumParams == getNumParams() && "Parameter count mismatch!");
241
242 // Zero params -> null pointer.
243 if (NumParams) {
244 ParamInfo = new ParmVarDecl*[NumParams];
245 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
246 }
247}
248
249
250/// defineBody - When created, RecordDecl's correspond to a forward declared
251/// record. This method is used to mark the decl as being defined, with the
252/// specified contents.
253void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
254 assert(!isDefinition() && "Cannot redefine record!");
255 setDefinition(true);
256 NumMembers = numMembers;
257 if (numMembers) {
258 Members = new FieldDecl*[numMembers];
259 memcpy(Members, members, numMembers*sizeof(Decl*));
260 }
261}
262
263FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
264 if (Members == 0 || NumMembers < 0)
265 return 0;
266
267 // linear search. When C++ classes come along, will likely need to revisit.
268 for (int i = 0; i < NumMembers; ++i) {
269 if (Members[i]->getIdentifier() == name)
270 return Members[i];
271 }
272 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000273}
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000274
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000275void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanian89204a12007-10-01 16:53:59 +0000276 unsigned NumParams) {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000277 assert(ParamInfo == 0 && "Already has param info!");
278
279 // Zero params -> null pointer.
280 if (NumParams) {
281 ParamInfo = new ParmVarDecl*[NumParams];
282 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
283 NumMethodParams = NumParams;
284 }
285}
286
287ObjcMethodDecl::~ObjcMethodDecl() {
288 delete[] ParamInfo;
289}
290
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000291/// ObjcAddInstanceVariablesToClass - Inserts instance variables
292/// into ObjcInterfaceDecl's fields.
293///
294void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars,
295 unsigned numIvars) {
296 NumIvars = numIvars;
297 if (numIvars) {
298 Ivars = new ObjcIvarDecl*[numIvars];
299 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
300 }
301}
302
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000303/// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance
304/// Variables (Ivars) relative to what declared in @implementation;s class.
305/// Ivars into ObjcImplementationDecl's fields.
306///
307void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl(
308 ObjcIvarDecl **ivars, unsigned numIvars) {
309 NumIvars = numIvars;
310 if (numIvars) {
311 Ivars = new ObjcIvarDecl*[numIvars];
312 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
313 }
314}
315
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000316/// addObjcMethods - Insert instance and methods declarations into
317/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
318///
319void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods,
320 unsigned numInsMembers,
321 ObjcMethodDecl **clsMethods,
322 unsigned numClsMembers) {
323 NumInsMethods = numInsMembers;
324 if (numInsMembers) {
325 InsMethods = new ObjcMethodDecl*[numInsMembers];
326 memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
327 }
328 NumClsMethods = numClsMembers;
329 if (numClsMembers) {
330 ClsMethods = new ObjcMethodDecl*[numClsMembers];
331 memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
332 }
333}
334
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000335/// ObjcAddProtoMethods - Insert instance and methods declarations into
336/// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
337///
338void ObjcProtocolDecl::ObjcAddProtoMethods(ObjcMethodDecl **insMethods,
339 unsigned numInsMembers,
340 ObjcMethodDecl **clsMethods,
341 unsigned numClsMembers) {
342 NumProtoInsMethods = numInsMembers;
343 if (numInsMembers) {
Fariborz Jahanian146fbb02007-09-17 22:36:42 +0000344 ProtoInsMethods = new ObjcMethodDecl*[numInsMembers];
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000345 memcpy(ProtoInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
346 }
347 NumProtoClsMethods = numClsMembers;
348 if (numClsMembers) {
Fariborz Jahanian146fbb02007-09-17 22:36:42 +0000349 ProtoClsMethods = new ObjcMethodDecl*[numClsMembers];
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000350 memcpy(ProtoClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
351 }
352}
353
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000354/// ObjcAddCat - Insert instance and methods declarations into
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000355/// ObjcCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000356///
357void ObjcCategoryDecl::ObjcAddCatMethods(ObjcMethodDecl **insMethods,
358 unsigned numInsMembers,
359 ObjcMethodDecl **clsMethods,
360 unsigned numClsMembers) {
361 NumCatInsMethods = numInsMembers;
362 if (numInsMembers) {
363 CatInsMethods = new ObjcMethodDecl*[numInsMembers];
364 memcpy(CatInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
365 }
366 NumCatClsMethods = numClsMembers;
367 if (numClsMembers) {
368 CatClsMethods = new ObjcMethodDecl*[numClsMembers];
369 memcpy(CatClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
370 }
371}
372
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000373/// ObjcAddCatImplMethods - Insert instance and methods declarations into
374/// ObjcCategoryImplDecl's CatInsMethods and CatClsMethods fields.
375///
376void ObjcCategoryImplDecl::ObjcAddCatImplMethods(ObjcMethodDecl **insMethods,
377 unsigned numInsMembers,
378 ObjcMethodDecl **clsMethods,
379 unsigned numClsMembers) {
380 NumCatInsMethods = numInsMembers;
381 if (numInsMembers) {
382 CatInsMethods = new ObjcMethodDecl*[numInsMembers];
383 memcpy(CatInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
384 }
385 NumCatClsMethods = numClsMembers;
386 if (numClsMembers) {
387 CatClsMethods = new ObjcMethodDecl*[numClsMembers];
388 memcpy(CatClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
389 }
390}
391
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000392/// ObjcAddImplMethods - Insert instance and methods declarations into
393/// ObjcImplementationDecl's InsMethods and ClsMethods fields.
394///
395void ObjcImplementationDecl::ObjcAddImplMethods(ObjcMethodDecl **insMethods,
396 unsigned numInsMembers,
397 ObjcMethodDecl **clsMethods,
398 unsigned numClsMembers) {
399 NumInsMethods = numInsMembers;
400 if (numInsMembers) {
401 InsMethods = new ObjcMethodDecl*[numInsMembers];
402 memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
403 }
404 NumClsMethods = numClsMembers;
405 if (numClsMembers) {
406 ClsMethods = new ObjcMethodDecl*[numClsMembers];
407 memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
408 }
409}
410
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000411