blob: 908bab4277334db5095797ab35df4e545f4146a2 [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"
Chris Lattnerc7229c32007-10-07 08:58:51 +000016#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017using 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;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +000038static unsigned nObjcCompatibleAlias = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000039
Reid Spencer5f016e22007-07-11 17:01:13 +000040static bool StatSwitch = false;
41
Steve Naroffe5ea3802007-09-17 14:49:06 +000042const char *Decl::getDeclKindName() const {
Steve Naroff8c9f13e2007-09-16 16:16:00 +000043 switch (DeclKind) {
44 default: assert(0 && "Unknown decl kind!");
45 case Typedef:
46 return "Typedef";
47 case Function:
48 return "Function";
Chris Lattneraa9fc462007-10-08 21:37:32 +000049 case BlockVar:
50 return "BlockVar";
51 case FileVar:
52 return "FileVar";
53 case ParmVar:
54 return "ParmVar";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000055 case EnumConstant:
56 return "EnumConstant";
57 case ObjcInterface:
58 return "ObjcInterface";
59 case ObjcClass:
60 return "ObjcClass";
61 case ObjcMethod:
62 return "ObjcMethod";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000063 case ObjcProtocol:
64 return "ObjcProtocol";
Fariborz Jahanian894c57f2007-09-21 15:40:54 +000065 case ObjcForwardProtocol:
66 return "ObjcForwardProtocol";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000067 case Struct:
68 return "Struct";
69 case Union:
70 return "Union";
71 case Class:
72 return "Class";
73 case Enum:
74 return "Enum";
75 }
76}
77
Reid Spencer5f016e22007-07-11 17:01:13 +000078bool Decl::CollectingStats(bool enable) {
79 if (enable) StatSwitch = true;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +000080 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
82
83void Decl::PrintStats() {
84 fprintf(stderr, "*** Decl Stats:\n");
85 fprintf(stderr, " %d decls total.\n",
86 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
Steve Naroff3f128ad2007-09-17 14:16:13 +000087 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000088 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Reid Spencer5f016e22007-07-11 17:01:13 +000089 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
90 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
91 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
92 nBlockVars, (int)sizeof(BlockVarDecl),
93 int(nBlockVars*sizeof(BlockVarDecl)));
94 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
95 nFileVars, (int)sizeof(FileVarDecl),
96 int(nFileVars*sizeof(FileVarDecl)));
97 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
98 nParmVars, (int)sizeof(ParmVarDecl),
99 int(nParmVars*sizeof(ParmVarDecl)));
100 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
101 nFieldDecls, (int)sizeof(FieldDecl),
102 int(nFieldDecls*sizeof(FieldDecl)));
103 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
104 nSUC, (int)sizeof(RecordDecl),
105 int(nSUC*sizeof(RecordDecl)));
106 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
107 nEnumDecls, (int)sizeof(EnumDecl),
108 int(nEnumDecls*sizeof(EnumDecl)));
109 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
110 nEnumConst, (int)sizeof(EnumConstantDecl),
111 int(nEnumConst*sizeof(EnumConstantDecl)));
112 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
113 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000114 // Objective-C decls...
115 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
116 nInterfaceDecls, (int)sizeof(ObjcInterfaceDecl),
117 int(nInterfaceDecls*sizeof(ObjcInterfaceDecl)));
118 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
119 nIvarDecls, (int)sizeof(ObjcIvarDecl),
120 int(nIvarDecls*sizeof(ObjcIvarDecl)));
121 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
122 nClassDecls, (int)sizeof(ObjcClassDecl),
123 int(nClassDecls*sizeof(ObjcClassDecl)));
124 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
125 nMethodDecls, (int)sizeof(ObjcMethodDecl),
126 int(nMethodDecls*sizeof(ObjcMethodDecl)));
127 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
128 nProtocolDecls, (int)sizeof(ObjcProtocolDecl),
129 int(nProtocolDecls*sizeof(ObjcProtocolDecl)));
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000130 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
131 nForwardProtocolDecls, (int)sizeof(ObjcForwardProtocolDecl),
132 int(nForwardProtocolDecls*sizeof(ObjcForwardProtocolDecl)));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000133 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
134 nCategoryDecls, (int)sizeof(ObjcCategoryDecl),
135 int(nCategoryDecls*sizeof(ObjcCategoryDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000136
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000137 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
138 nObjcImplementationDecls, (int)sizeof(ObjcImplementationDecl),
139 int(nObjcImplementationDecls*sizeof(ObjcImplementationDecl)));
140
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000141 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
142 nObjcCategoryImpl, (int)sizeof(ObjcCategoryImplDecl),
143 int(nObjcCategoryImpl*sizeof(ObjcCategoryImplDecl)));
144
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000145 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
146 nObjcCompatibleAlias, (int)sizeof(ObjcCompatibleAliasDecl),
147 int(nObjcCompatibleAlias*sizeof(ObjcCompatibleAliasDecl)));
148
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 fprintf(stderr, "Total bytes = %d\n",
150 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
151 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
152 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
153 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
Steve Naroff3f128ad2007-09-17 14:16:13 +0000154 nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */);
Reid Spencer5f016e22007-07-11 17:01:13 +0000155}
156
157void Decl::addDeclKind(const Kind k) {
158 switch (k) {
159 case Typedef:
160 nTypedef++;
161 break;
162 case Function:
163 nFuncs++;
164 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000165 case BlockVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 nBlockVars++;
167 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000168 case FileVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 nFileVars++;
170 break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000171 case ParmVar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 nParmVars++;
173 break;
174 case EnumConstant:
175 nEnumConst++;
176 break;
177 case Field:
178 nFieldDecls++;
179 break;
180 case Struct:
181 case Union:
182 case Class:
183 nSUC++;
184 break;
185 case Enum:
186 nEnumDecls++;
187 break;
Steve Naroff3536b442007-09-06 21:24:23 +0000188 case ObjcInterface:
189 nInterfaceDecls++;
190 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000191 case ObjcClass:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000192 nClassDecls++;
193 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000194 case ObjcMethod:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000195 nMethodDecls++;
196 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000197 case ObjcProtocol:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000198 nProtocolDecls++;
199 break;
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000200 case ObjcForwardProtocol:
201 nForwardProtocolDecls++;
202 break;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000203 case ObjcCategory:
204 nCategoryDecls++;
205 break;
Chris Lattner7341c332007-09-16 19:23:04 +0000206 case ObjcIvar:
Steve Naroff3f128ad2007-09-17 14:16:13 +0000207 nIvarDecls++;
Chris Lattner7341c332007-09-16 19:23:04 +0000208 break;
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000209 case ObjcImplementation:
210 nObjcImplementationDecls++;
211 break;
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000212 case ObjcCategoryImpl:
213 nObjcCategoryImpl++;
214 break;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000215 case CompatibleAlias:
216 nObjcCompatibleAlias++;
217 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 }
219}
220
221// Out-of-line virtual method providing a home for Decl.
222Decl::~Decl() {
223}
224
Chris Lattnerfd5de472007-10-06 22:53:46 +0000225const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 if (const IdentifierInfo *II = getIdentifier())
227 return II->getName();
228 return "";
229}
230
231
232FunctionDecl::~FunctionDecl() {
233 delete[] ParamInfo;
234}
235
236unsigned FunctionDecl::getNumParams() const {
237 return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
238}
239
240void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
241 assert(ParamInfo == 0 && "Already has param info!");
242 assert(NumParams == getNumParams() && "Parameter count mismatch!");
243
244 // Zero params -> null pointer.
245 if (NumParams) {
246 ParamInfo = new ParmVarDecl*[NumParams];
247 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
248 }
249}
250
251
252/// defineBody - When created, RecordDecl's correspond to a forward declared
253/// record. This method is used to mark the decl as being defined, with the
254/// specified contents.
255void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
256 assert(!isDefinition() && "Cannot redefine record!");
257 setDefinition(true);
258 NumMembers = numMembers;
259 if (numMembers) {
260 Members = new FieldDecl*[numMembers];
261 memcpy(Members, members, numMembers*sizeof(Decl*));
262 }
263}
264
265FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
266 if (Members == 0 || NumMembers < 0)
267 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000268
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 // linear search. When C++ classes come along, will likely need to revisit.
270 for (int i = 0; i < NumMembers; ++i) {
271 if (Members[i]->getIdentifier() == name)
272 return Members[i];
273 }
274 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000275}
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000276
Fariborz Jahanian59519652007-10-04 17:06:28 +0000277void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
278 unsigned NumParams) {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000279 assert(ParamInfo == 0 && "Already has param info!");
280
281 // Zero params -> null pointer.
282 if (NumParams) {
283 ParamInfo = new ParmVarDecl*[NumParams];
284 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
285 NumMethodParams = NumParams;
286 }
287}
288
289ObjcMethodDecl::~ObjcMethodDecl() {
290 delete[] ParamInfo;
291}
292
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000293/// ObjcAddInstanceVariablesToClass - Inserts instance variables
294/// into ObjcInterfaceDecl's fields.
295///
296void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars,
Fariborz Jahanian59519652007-10-04 17:06:28 +0000297 unsigned numIvars) {
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000298 NumIvars = numIvars;
299 if (numIvars) {
300 Ivars = new ObjcIvarDecl*[numIvars];
301 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
302 }
303}
304
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000305/// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance
306/// Variables (Ivars) relative to what declared in @implementation;s class.
307/// Ivars into ObjcImplementationDecl's fields.
308///
309void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl(
Fariborz Jahanian59519652007-10-04 17:06:28 +0000310 ObjcIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000311 NumIvars = numIvars;
312 if (numIvars) {
313 Ivars = new ObjcIvarDecl*[numIvars];
314 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
315 }
316}
317
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000318/// addObjcMethods - Insert instance and methods declarations into
319/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
320///
321void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods,
Fariborz Jahanian59519652007-10-04 17:06:28 +0000322 unsigned numInsMembers,
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000323 ObjcMethodDecl **clsMethods,
324 unsigned numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000325 NumInstanceMethods = numInsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000326 if (numInsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000327 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
328 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000329 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000330 NumClassMethods = numClsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000331 if (numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000332 ClassMethods = new ObjcMethodDecl*[numClsMembers];
333 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000334 }
335}
336
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000337/// ObjcAddProtoMethods - Insert instance and methods declarations into
338/// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
339///
340void ObjcProtocolDecl::ObjcAddProtoMethods(ObjcMethodDecl **insMethods,
Fariborz Jahanian59519652007-10-04 17:06:28 +0000341 unsigned numInsMembers,
342 ObjcMethodDecl **clsMethods,
343 unsigned numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000344 NumInstanceMethods = numInsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000345 if (numInsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000346 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
347 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000348 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000349 NumClassMethods = numClsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000350 if (numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000351 ClassMethods = new ObjcMethodDecl*[numClsMembers];
352 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000353 }
354}
355
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000356/// ObjcAddCat - Insert instance and methods declarations into
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000357/// ObjcCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000358///
359void ObjcCategoryDecl::ObjcAddCatMethods(ObjcMethodDecl **insMethods,
Fariborz Jahanian59519652007-10-04 17:06:28 +0000360 unsigned numInsMembers,
361 ObjcMethodDecl **clsMethods,
362 unsigned numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000363 NumInstanceMethods = numInsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000364 if (numInsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000365 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
366 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000367 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000368 NumClassMethods = numClsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000369 if (numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000370 ClassMethods = new ObjcMethodDecl*[numClsMembers];
371 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000372 }
373}
374
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000375/// ObjcAddCatImplMethods - Insert instance and methods declarations into
376/// ObjcCategoryImplDecl's CatInsMethods and CatClsMethods fields.
377///
378void ObjcCategoryImplDecl::ObjcAddCatImplMethods(ObjcMethodDecl **insMethods,
Fariborz Jahanian59519652007-10-04 17:06:28 +0000379 unsigned numInsMembers,
380 ObjcMethodDecl **clsMethods,
381 unsigned numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000382 NumInstanceMethods = numInsMembers;
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000383 if (numInsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000384 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
385 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000386 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000387 NumClassMethods = numClsMembers;
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000388 if (numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000389 ClassMethods = new ObjcMethodDecl*[numClsMembers];
390 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000391 }
392}
393
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000394/// ObjcAddImplMethods - Insert instance and methods declarations into
395/// ObjcImplementationDecl's InsMethods and ClsMethods fields.
396///
397void ObjcImplementationDecl::ObjcAddImplMethods(ObjcMethodDecl **insMethods,
Fariborz Jahanian59519652007-10-04 17:06:28 +0000398 unsigned numInsMembers,
399 ObjcMethodDecl **clsMethods,
400 unsigned numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000401 NumInstanceMethods = numInsMembers;
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000402 if (numInsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000403 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
404 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000405 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000406 NumClassMethods = numClsMembers;
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000407 if (numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000408 ClassMethods = new ObjcMethodDecl*[numClsMembers];
409 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000410 }
411}
412
Steve Naroff3d581382007-10-14 18:27:41 +0000413// lookupInstanceMethod - This method returns an instance method by looking in
414// the class, it's categories, and it's super classes (using a linear search).
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000415ObjcMethodDecl *ObjcInterfaceDecl::lookupInstanceMethod(Selector &Sel) {
416 ObjcInterfaceDecl* ClassDecl = this;
417 while (ClassDecl != NULL) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000418 ObjcMethodDecl **methods = ClassDecl->getInstanceMethods();
419 int methodCount = ClassDecl->getNumInstanceMethods();
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000420 for (int i = 0; i < methodCount; ++i) {
421 if (methods[i]->getSelector() == Sel) {
422 return methods[i];
423 }
424 }
Steve Naroff3d581382007-10-14 18:27:41 +0000425 // Didn't find one yet - now look through categories.
426 ObjcCategoryDecl *CatDecl = this->getCategoryList();
427 while (CatDecl) {
428 ObjcMethodDecl **methods = CatDecl->getInstanceMethods();
429 int methodCount = CatDecl->getNumInstanceMethods();
430 for (int i = 0; i < methodCount; ++i) {
431 if (methods[i]->getSelector() == Sel) {
432 return methods[i];
433 }
434 }
435 CatDecl = CatDecl->getNextClassCategory();
436 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000437 ClassDecl = ClassDecl->getSuperClass();
438 }
439 return NULL;
440}
441
Steve Naroff3d581382007-10-14 18:27:41 +0000442// lookupClassMethod - This method returns a class method by looking in the
443// class, it's categories, and it's super classes (using a linear search).
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000444ObjcMethodDecl *ObjcInterfaceDecl::lookupClassMethod(Selector &Sel) {
445 ObjcInterfaceDecl* ClassDecl = this;
446 while (ClassDecl != NULL) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000447 ObjcMethodDecl **methods = ClassDecl->getClassMethods();
448 int methodCount = ClassDecl->getNumClassMethods();
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000449 for (int i = 0; i < methodCount; ++i) {
450 if (methods[i]->getSelector() == Sel) {
451 return methods[i];
452 }
453 }
Steve Naroff3d581382007-10-14 18:27:41 +0000454 // Didn't find one yet - now look through categories.
455 ObjcCategoryDecl *CatDecl = this->getCategoryList();
456 while (CatDecl) {
457 ObjcMethodDecl **methods = CatDecl->getClassMethods();
458 int methodCount = CatDecl->getNumClassMethods();
459 for (int i = 0; i < methodCount; ++i) {
460 if (methods[i]->getSelector() == Sel) {
461 return methods[i];
462 }
463 }
464 CatDecl = CatDecl->getNextClassCategory();
465 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000466 ClassDecl = ClassDecl->getSuperClass();
467 }
468 return NULL;
469}
470
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000471