blob: 4b330eeeebe6399686bfff6c9c2ac9eba067d8be [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"
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 Naroff3536b442007-09-06 21:24:23 +000028static unsigned nInterfaceDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000029static unsigned nClassDecls = 0;
30static unsigned nMethodDecls = 0;
31static unsigned nProtocolDecls = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000032static unsigned nCategoryDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000033static unsigned nIvarDecls = 0;
34
Reid Spencer5f016e22007-07-11 17:01:13 +000035static bool StatSwitch = false;
36
Steve Naroffe5ea3802007-09-17 14:49:06 +000037const char *Decl::getDeclKindName() const {
Steve Naroff8c9f13e2007-09-16 16:16:00 +000038 switch (DeclKind) {
39 default: assert(0 && "Unknown decl kind!");
40 case Typedef:
41 return "Typedef";
42 case Function:
43 return "Function";
44 case BlockVariable:
45 return "BlockVariable";
46 case FileVariable:
47 return "FileVariable";
48 case ParmVariable:
49 return "ParmVariable";
50 case EnumConstant:
51 return "EnumConstant";
52 case ObjcInterface:
53 return "ObjcInterface";
54 case ObjcClass:
55 return "ObjcClass";
56 case ObjcMethod:
57 return "ObjcMethod";
58 case ObjcProtoMethod:
59 return "ObjcProtoMethod";
60 case ObjcProtocol:
61 return "ObjcProtocol";
62 case Struct:
63 return "Struct";
64 case Union:
65 return "Union";
66 case Class:
67 return "Class";
68 case Enum:
69 return "Enum";
70 }
71}
72
Reid Spencer5f016e22007-07-11 17:01:13 +000073bool Decl::CollectingStats(bool enable) {
74 if (enable) StatSwitch = true;
75 return StatSwitch;
76}
77
78void Decl::PrintStats() {
79 fprintf(stderr, "*** Decl Stats:\n");
80 fprintf(stderr, " %d decls total.\n",
81 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff3f128ad2007-09-17 14:16:13 +000082 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000083 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Reid Spencer5f016e22007-07-11 17:01:13 +000084 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
85 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
86 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
87 nBlockVars, (int)sizeof(BlockVarDecl),
88 int(nBlockVars*sizeof(BlockVarDecl)));
89 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
90 nFileVars, (int)sizeof(FileVarDecl),
91 int(nFileVars*sizeof(FileVarDecl)));
92 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
93 nParmVars, (int)sizeof(ParmVarDecl),
94 int(nParmVars*sizeof(ParmVarDecl)));
95 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
96 nFieldDecls, (int)sizeof(FieldDecl),
97 int(nFieldDecls*sizeof(FieldDecl)));
98 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
99 nSUC, (int)sizeof(RecordDecl),
100 int(nSUC*sizeof(RecordDecl)));
101 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
102 nEnumDecls, (int)sizeof(EnumDecl),
103 int(nEnumDecls*sizeof(EnumDecl)));
104 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
105 nEnumConst, (int)sizeof(EnumConstantDecl),
106 int(nEnumConst*sizeof(EnumConstantDecl)));
107 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
108 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000109 // Objective-C decls...
110 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
111 nInterfaceDecls, (int)sizeof(ObjcInterfaceDecl),
112 int(nInterfaceDecls*sizeof(ObjcInterfaceDecl)));
113 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
114 nIvarDecls, (int)sizeof(ObjcIvarDecl),
115 int(nIvarDecls*sizeof(ObjcIvarDecl)));
116 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
117 nClassDecls, (int)sizeof(ObjcClassDecl),
118 int(nClassDecls*sizeof(ObjcClassDecl)));
119 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
120 nMethodDecls, (int)sizeof(ObjcMethodDecl),
121 int(nMethodDecls*sizeof(ObjcMethodDecl)));
122 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
123 nProtocolDecls, (int)sizeof(ObjcProtocolDecl),
124 int(nProtocolDecls*sizeof(ObjcProtocolDecl)));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000125 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
126 nCategoryDecls, (int)sizeof(ObjcCategoryDecl),
127 int(nCategoryDecls*sizeof(ObjcCategoryDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 fprintf(stderr, "Total bytes = %d\n",
130 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
131 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
132 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
133 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Steve Naroff3f128ad2007-09-17 14:16:13 +0000134 nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */);
Reid Spencer5f016e22007-07-11 17:01:13 +0000135}
136
137void Decl::addDeclKind(const Kind k) {
138 switch (k) {
139 case Typedef:
140 nTypedef++;
141 break;
142 case Function:
143 nFuncs++;
144 break;
145 case BlockVariable:
146 nBlockVars++;
147 break;
148 case FileVariable:
149 nFileVars++;
150 break;
151 case ParmVariable:
152 nParmVars++;
153 break;
154 case EnumConstant:
155 nEnumConst++;
156 break;
157 case Field:
158 nFieldDecls++;
159 break;
160 case Struct:
161 case Union:
162 case Class:
163 nSUC++;
164 break;
165 case Enum:
166 nEnumDecls++;
167 break;
Steve Naroff3536b442007-09-06 21:24:23 +0000168 case ObjcInterface:
169 nInterfaceDecls++;
170 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000171 case ObjcClass:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000172 nClassDecls++;
173 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000174 case ObjcMethod:
175 case ObjcProtoMethod:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000176 nMethodDecls++;
177 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000178 case ObjcProtocol:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000179 nProtocolDecls++;
180 break;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000181 case ObjcCategory:
182 nCategoryDecls++;
183 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000184 case ObjcIvar:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000185 nIvarDecls++;
Chris Lattner7341c332007-09-16 19:23:04 +0000186 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 }
188}
189
190// Out-of-line virtual method providing a home for Decl.
191Decl::~Decl() {
192}
193
Steve Naroff8e74c932007-09-13 21:41:19 +0000194const char *FieldDecl::getName() const {
195 if (const IdentifierInfo *II = getIdentifier())
196 return II->getName();
197 return "";
198}
199
200const char *ScopedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 if (const IdentifierInfo *II = getIdentifier())
202 return II->getName();
203 return "";
204}
205
206
207FunctionDecl::~FunctionDecl() {
208 delete[] ParamInfo;
209}
210
211unsigned FunctionDecl::getNumParams() const {
212 return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
213}
214
215void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
216 assert(ParamInfo == 0 && "Already has param info!");
217 assert(NumParams == getNumParams() && "Parameter count mismatch!");
218
219 // Zero params -> null pointer.
220 if (NumParams) {
221 ParamInfo = new ParmVarDecl*[NumParams];
222 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
223 }
224}
225
226
227/// defineBody - When created, RecordDecl's correspond to a forward declared
228/// record. This method is used to mark the decl as being defined, with the
229/// specified contents.
230void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
231 assert(!isDefinition() && "Cannot redefine record!");
232 setDefinition(true);
233 NumMembers = numMembers;
234 if (numMembers) {
235 Members = new FieldDecl*[numMembers];
236 memcpy(Members, members, numMembers*sizeof(Decl*));
237 }
238}
239
240FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
241 if (Members == 0 || NumMembers < 0)
242 return 0;
243
244 // linear search. When C++ classes come along, will likely need to revisit.
245 for (int i = 0; i < NumMembers; ++i) {
246 if (Members[i]->getIdentifier() == name)
247 return Members[i];
248 }
249 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000250}
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000251
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000252void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
253 unsigned NumParams) {
254 assert(ParamInfo == 0 && "Already has param info!");
255
256 // Zero params -> null pointer.
257 if (NumParams) {
258 ParamInfo = new ParmVarDecl*[NumParams];
259 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
260 NumMethodParams = NumParams;
261 }
262}
263
264ObjcMethodDecl::~ObjcMethodDecl() {
265 delete[] ParamInfo;
266}
267
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000268/// ObjcAddInstanceVariablesToClass - Inserts instance variables
269/// into ObjcInterfaceDecl's fields.
270///
271void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars,
272 unsigned numIvars) {
273 NumIvars = numIvars;
274 if (numIvars) {
275 Ivars = new ObjcIvarDecl*[numIvars];
276 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
277 }
278}
279
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000280/// addObjcMethods - Insert instance and methods declarations into
281/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
282///
283void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods,
284 unsigned numInsMembers,
285 ObjcMethodDecl **clsMethods,
286 unsigned numClsMembers) {
287 NumInsMethods = numInsMembers;
288 if (numInsMembers) {
289 InsMethods = new ObjcMethodDecl*[numInsMembers];
290 memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
291 }
292 NumClsMethods = numClsMembers;
293 if (numClsMembers) {
294 ClsMethods = new ObjcMethodDecl*[numClsMembers];
295 memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
296 }
297}
298
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000299/// ObjcAddProtoMethods - Insert instance and methods declarations into
300/// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
301///
302void ObjcProtocolDecl::ObjcAddProtoMethods(ObjcMethodDecl **insMethods,
303 unsigned numInsMembers,
304 ObjcMethodDecl **clsMethods,
305 unsigned numClsMembers) {
306 NumProtoInsMethods = numInsMembers;
307 if (numInsMembers) {
Fariborz Jahanian146fbb02007-09-17 22:36:42 +0000308 ProtoInsMethods = new ObjcMethodDecl*[numInsMembers];
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000309 memcpy(ProtoInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
310 }
311 NumProtoClsMethods = numClsMembers;
312 if (numClsMembers) {
Fariborz Jahanian146fbb02007-09-17 22:36:42 +0000313 ProtoClsMethods = new ObjcMethodDecl*[numClsMembers];
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000314 memcpy(ProtoClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
315 }
316}
317
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000318/// ObjcAddCat - Insert instance and methods declarations into
319/// ObjcProtocolDecl's CatInsMethods and CatClsMethods fields.
320///
321void ObjcCategoryDecl::ObjcAddCatMethods(ObjcMethodDecl **insMethods,
322 unsigned numInsMembers,
323 ObjcMethodDecl **clsMethods,
324 unsigned numClsMembers) {
325 NumCatInsMethods = numInsMembers;
326 if (numInsMembers) {
327 CatInsMethods = new ObjcMethodDecl*[numInsMembers];
328 memcpy(CatInsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
329 }
330 NumCatClsMethods = numClsMembers;
331 if (numClsMembers) {
332 CatClsMethods = new ObjcMethodDecl*[numClsMembers];
333 memcpy(CatClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
334 }
335}
336
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000337