1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the Decl class and subclasses.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/Basic/IdentifierTable.h"
using namespace clang;
// temporary statistics gathering
static unsigned nFuncs = 0;
static unsigned nBlockVars = 0;
static unsigned nFileVars = 0;
static unsigned nParmVars = 0;
static unsigned nSUC = 0;
static unsigned nEnumConst = 0;
static unsigned nEnumDecls = 0;
static unsigned nTypedef = 0;
static unsigned nFieldDecls = 0;
static unsigned nInterfaceDecls = 0;
static unsigned nClassDecls = 0;
static unsigned nMethodDecls = 0;
static unsigned nProtocolDecls = 0;
static unsigned nForwardProtocolDecls = 0;
static unsigned nCategoryDecls = 0;
static unsigned nIvarDecls = 0;
static unsigned nObjcImplementationDecls = 0;
static unsigned nObjcCategoryImpl = 0;
static bool StatSwitch = false;
const char *Decl::getDeclKindName() const {
switch (DeclKind) {
default: assert(0 && "Unknown decl kind!");
case Typedef:
return "Typedef";
case Function:
return "Function";
case BlockVar:
return "BlockVar";
case FileVar:
return "FileVar";
case ParmVar:
return "ParmVar";
case EnumConstant:
return "EnumConstant";
case ObjcInterface:
return "ObjcInterface";
case ObjcClass:
return "ObjcClass";
case ObjcMethod:
return "ObjcMethod";
case ObjcProtocol:
return "ObjcProtocol";
case ObjcForwardProtocol:
return "ObjcForwardProtocol";
case Struct:
return "Struct";
case Union:
return "Union";
case Class:
return "Class";
case Enum:
return "Enum";
}
}
bool Decl::CollectingStats(bool enable) {
if (enable) StatSwitch = true;
return StatSwitch;
}
void Decl::PrintStats() {
fprintf(stderr, "*** Decl Stats:\n");
fprintf(stderr, " %d decls total.\n",
int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
nBlockVars, (int)sizeof(BlockVarDecl),
int(nBlockVars*sizeof(BlockVarDecl)));
fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
nFileVars, (int)sizeof(FileVarDecl),
int(nFileVars*sizeof(FileVarDecl)));
fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
nParmVars, (int)sizeof(ParmVarDecl),
int(nParmVars*sizeof(ParmVarDecl)));
fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
nFieldDecls, (int)sizeof(FieldDecl),
int(nFieldDecls*sizeof(FieldDecl)));
fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
nSUC, (int)sizeof(RecordDecl),
int(nSUC*sizeof(RecordDecl)));
fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
nEnumDecls, (int)sizeof(EnumDecl),
int(nEnumDecls*sizeof(EnumDecl)));
fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
nEnumConst, (int)sizeof(EnumConstantDecl),
int(nEnumConst*sizeof(EnumConstantDecl)));
fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
// Objective-C decls...
fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
nInterfaceDecls, (int)sizeof(ObjcInterfaceDecl),
int(nInterfaceDecls*sizeof(ObjcInterfaceDecl)));
fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
nIvarDecls, (int)sizeof(ObjcIvarDecl),
int(nIvarDecls*sizeof(ObjcIvarDecl)));
fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
nClassDecls, (int)sizeof(ObjcClassDecl),
int(nClassDecls*sizeof(ObjcClassDecl)));
fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
nMethodDecls, (int)sizeof(ObjcMethodDecl),
int(nMethodDecls*sizeof(ObjcMethodDecl)));
fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
nProtocolDecls, (int)sizeof(ObjcProtocolDecl),
int(nProtocolDecls*sizeof(ObjcProtocolDecl)));
fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
nForwardProtocolDecls, (int)sizeof(ObjcForwardProtocolDecl),
int(nForwardProtocolDecls*sizeof(ObjcForwardProtocolDecl)));
fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
nCategoryDecls, (int)sizeof(ObjcCategoryDecl),
int(nCategoryDecls*sizeof(ObjcCategoryDecl)));
fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
nObjcImplementationDecls, (int)sizeof(ObjcImplementationDecl),
int(nObjcImplementationDecls*sizeof(ObjcImplementationDecl)));
fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
nObjcCategoryImpl, (int)sizeof(ObjcCategoryImplDecl),
int(nObjcCategoryImpl*sizeof(ObjcCategoryImplDecl)));
fprintf(stderr, "Total bytes = %d\n",
int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */);
}
void Decl::addDeclKind(const Kind k) {
switch (k) {
case Typedef:
nTypedef++;
break;
case Function:
nFuncs++;
break;
case BlockVar:
nBlockVars++;
break;
case FileVar:
nFileVars++;
break;
case ParmVar:
nParmVars++;
break;
case EnumConstant:
nEnumConst++;
break;
case Field:
nFieldDecls++;
break;
case Struct:
case Union:
case Class:
nSUC++;
break;
case Enum:
nEnumDecls++;
break;
case ObjcInterface:
nInterfaceDecls++;
break;
case ObjcClass:
nClassDecls++;
break;
case ObjcMethod:
nMethodDecls++;
break;
case ObjcProtocol:
nProtocolDecls++;
break;
case ObjcForwardProtocol:
nForwardProtocolDecls++;
break;
case ObjcCategory:
nCategoryDecls++;
break;
case ObjcIvar:
nIvarDecls++;
break;
case ObjcImplementation:
nObjcImplementationDecls++;
break;
case ObjcCategoryImpl:
nObjcCategoryImpl++;
break;
}
}
// Out-of-line virtual method providing a home for Decl.
Decl::~Decl() {
}
const char *NamedDecl::getName() const {
if (const IdentifierInfo *II = getIdentifier())
return II->getName();
return "";
}
FunctionDecl::~FunctionDecl() {
delete[] ParamInfo;
}
unsigned FunctionDecl::getNumParams() const {
return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
}
void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
assert(ParamInfo == 0 && "Already has param info!");
assert(NumParams == getNumParams() && "Parameter count mismatch!");
// Zero params -> null pointer.
if (NumParams) {
ParamInfo = new ParmVarDecl*[NumParams];
memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
}
}
/// defineBody - When created, RecordDecl's correspond to a forward declared
/// record. This method is used to mark the decl as being defined, with the
/// specified contents.
void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
assert(!isDefinition() && "Cannot redefine record!");
setDefinition(true);
NumMembers = numMembers;
if (numMembers) {
Members = new FieldDecl*[numMembers];
memcpy(Members, members, numMembers*sizeof(Decl*));
}
}
FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
if (Members == 0 || NumMembers < 0)
return 0;
// linear search. When C++ classes come along, will likely need to revisit.
for (int i = 0; i < NumMembers; ++i) {
if (Members[i]->getIdentifier() == name)
return Members[i];
}
return 0;
}
void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
unsigned NumParams) {
assert(ParamInfo == 0 && "Already has param info!");
// Zero params -> null pointer.
if (NumParams) {
ParamInfo = new ParmVarDecl*[NumParams];
memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
NumMethodParams = NumParams;
}
}
ObjcMethodDecl::~ObjcMethodDecl() {
delete[] ParamInfo;
}
/// ObjcAddInstanceVariablesToClass - Inserts instance variables
/// into ObjcInterfaceDecl's fields.
///
void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars,
unsigned numIvars) {
NumIvars = numIvars;
if (numIvars) {
Ivars = new ObjcIvarDecl*[numIvars];
memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
}
}
/// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance
/// Variables (Ivars) relative to what declared in @implementation;s class.
/// Ivars into ObjcImplementationDecl's fields.
///
void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl(
ObjcIvarDecl **ivars, unsigned numIvars) {
NumIvars = numIvars;
if (numIvars) {
Ivars = new ObjcIvarDecl*[numIvars];
memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
}
}
/// addObjcMethods - Insert instance and methods declarations into
/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
///
void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods,
unsigned numInsMembers,
ObjcMethodDecl **clsMethods,
unsigned numClsMembers) {
NumInstanceMethods = numInsMembers;
if (numInsMembers) {
InstanceMethods = new ObjcMethodDecl*[numInsMembers];
memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
}
NumClassMethods = numClsMembers;
if (numClsMembers) {
ClassMethods = new ObjcMethodDecl*[numClsMembers];
memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
}
}
/// ObjcAddProtoMethods - Insert instance and methods declarations into
/// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
///
void ObjcProtocolDecl::ObjcAddProtoMethods(ObjcMethodDecl **insMethods,
unsigned numInsMembers,
ObjcMethodDecl **clsMethods,
unsigned numClsMembers) {
NumInstanceMethods = numInsMembers;
if (numInsMembers) {
InstanceMethods = new ObjcMethodDecl*[numInsMembers];
memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
}
NumClassMethods = numClsMembers;
if (numClsMembers) {
ClassMethods = new ObjcMethodDecl*[numClsMembers];
memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
}
}
/// ObjcAddCat - Insert instance and methods declarations into
/// ObjcCategoryDecl's CatInsMethods and CatClsMethods fields.
///
void ObjcCategoryDecl::ObjcAddCatMethods(ObjcMethodDecl **insMethods,
unsigned numInsMembers,
ObjcMethodDecl **clsMethods,
unsigned numClsMembers) {
NumInstanceMethods = numInsMembers;
if (numInsMembers) {
InstanceMethods = new ObjcMethodDecl*[numInsMembers];
memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
}
NumClassMethods = numClsMembers;
if (numClsMembers) {
ClassMethods = new ObjcMethodDecl*[numClsMembers];
memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
}
}
/// ObjcAddCatImplMethods - Insert instance and methods declarations into
/// ObjcCategoryImplDecl's CatInsMethods and CatClsMethods fields.
///
void ObjcCategoryImplDecl::ObjcAddCatImplMethods(ObjcMethodDecl **insMethods,
unsigned numInsMembers,
ObjcMethodDecl **clsMethods,
unsigned numClsMembers) {
NumInstanceMethods = numInsMembers;
if (numInsMembers) {
InstanceMethods = new ObjcMethodDecl*[numInsMembers];
memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
}
NumClassMethods = numClsMembers;
if (numClsMembers) {
ClassMethods = new ObjcMethodDecl*[numClsMembers];
memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
}
}
/// ObjcAddImplMethods - Insert instance and methods declarations into
/// ObjcImplementationDecl's InsMethods and ClsMethods fields.
///
void ObjcImplementationDecl::ObjcAddImplMethods(ObjcMethodDecl **insMethods,
unsigned numInsMembers,
ObjcMethodDecl **clsMethods,
unsigned numClsMembers) {
NumInstanceMethods = numInsMembers;
if (numInsMembers) {
InstanceMethods = new ObjcMethodDecl*[numInsMembers];
memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
}
NumClassMethods = numClsMembers;
if (numClsMembers) {
ClassMethods = new ObjcMethodDecl*[numClsMembers];
memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
}
}
// FIXME: look through categories...
ObjcMethodDecl *ObjcInterfaceDecl::lookupInstanceMethod(Selector &Sel) {
ObjcInterfaceDecl* ClassDecl = this;
while (ClassDecl != NULL) {
ObjcMethodDecl **methods = ClassDecl->getInstanceMethods();
int methodCount = ClassDecl->getNumInstanceMethods();
for (int i = 0; i < methodCount; ++i) {
if (methods[i]->getSelector() == Sel) {
return methods[i];
}
}
ClassDecl = ClassDecl->getSuperClass();
}
return NULL;
}
// FIXME: look through categories...
ObjcMethodDecl *ObjcInterfaceDecl::lookupClassMethod(Selector &Sel) {
ObjcInterfaceDecl* ClassDecl = this;
while (ClassDecl != NULL) {
ObjcMethodDecl **methods = ClassDecl->getClassMethods();
int methodCount = ClassDecl->getNumClassMethods();
for (int i = 0; i < methodCount; ++i) {
if (methods[i]->getSelector() == Sel) {
return methods[i];
}
}
ClassDecl = ClassDecl->getSuperClass();
}
return NULL;
}
|