blob: 804e9ccabfef50113974c8cdda7462eb911c4b76 [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///
Steve Naroff60fccee2007-10-29 21:38:07 +0000296void ObjcInterfaceDecl::addInstanceVariablesToClass(ObjcIvarDecl **ivars,
297 unsigned numIvars,
298 SourceLocation RB) {
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000299 NumIvars = numIvars;
300 if (numIvars) {
301 Ivars = new ObjcIvarDecl*[numIvars];
302 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
303 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000304 RBracLoc = RB;
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000305}
306
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000307/// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance
308/// Variables (Ivars) relative to what declared in @implementation;s class.
309/// Ivars into ObjcImplementationDecl's fields.
310///
311void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl(
Fariborz Jahanian59519652007-10-04 17:06:28 +0000312 ObjcIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000313 NumIvars = numIvars;
314 if (numIvars) {
315 Ivars = new ObjcIvarDecl*[numIvars];
316 memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*));
317 }
318}
319
Steve Naroff60fccee2007-10-29 21:38:07 +0000320/// addMethods - Insert instance and methods declarations into
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000321/// ObjcInterfaceDecl's InsMethods and ClsMethods fields.
322///
Steve Naroff60fccee2007-10-29 21:38:07 +0000323void ObjcInterfaceDecl::addMethods(ObjcMethodDecl **insMethods,
324 unsigned numInsMembers,
325 ObjcMethodDecl **clsMethods,
326 unsigned numClsMembers,
327 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000328 NumInstanceMethods = numInsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000329 if (numInsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000330 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
331 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000332 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000333 NumClassMethods = numClsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000334 if (numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000335 ClassMethods = new ObjcMethodDecl*[numClsMembers];
336 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000337 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000338 EndLoc = endLoc;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000339}
340
Steve Naroff60fccee2007-10-29 21:38:07 +0000341/// addMethods - Insert instance and methods declarations into
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000342/// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
343///
Steve Naroff60fccee2007-10-29 21:38:07 +0000344void ObjcProtocolDecl::addMethods(ObjcMethodDecl **insMethods,
345 unsigned numInsMembers,
346 ObjcMethodDecl **clsMethods,
347 unsigned numClsMembers,
348 SourceLocation AtEndLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000349 NumInstanceMethods = numInsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000350 if (numInsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000351 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
352 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000353 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000354 NumClassMethods = numClsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000355 if (numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000356 ClassMethods = new ObjcMethodDecl*[numClsMembers];
357 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000358 }
359}
360
Steve Naroff60fccee2007-10-29 21:38:07 +0000361/// addMethods - Insert instance and methods declarations into
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000362/// ObjcCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000363///
Steve Naroff60fccee2007-10-29 21:38:07 +0000364void ObjcCategoryDecl::addMethods(ObjcMethodDecl **insMethods,
365 unsigned numInsMembers,
366 ObjcMethodDecl **clsMethods,
367 unsigned numClsMembers,
368 SourceLocation AtEndLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000369 NumInstanceMethods = numInsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000370 if (numInsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000371 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
372 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000373 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000374 NumClassMethods = numClsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000375 if (numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000376 ClassMethods = new ObjcMethodDecl*[numClsMembers];
377 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000378 }
379}
380
Steve Naroff60fccee2007-10-29 21:38:07 +0000381/// addMethods - Insert instance and methods declarations into
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000382/// ObjcCategoryImplDecl's CatInsMethods and CatClsMethods fields.
383///
Steve Naroff60fccee2007-10-29 21:38:07 +0000384void ObjcCategoryImplDecl::addMethods(ObjcMethodDecl **insMethods,
385 unsigned numInsMembers,
386 ObjcMethodDecl **clsMethods,
387 unsigned numClsMembers,
388 SourceLocation AtEndLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000389 NumInstanceMethods = numInsMembers;
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000390 if (numInsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000391 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
392 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000393 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000394 NumClassMethods = numClsMembers;
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000395 if (numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000396 ClassMethods = new ObjcMethodDecl*[numClsMembers];
397 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000398 }
399}
400
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000401/// ObjcAddImplMethods - Insert instance and methods declarations into
402/// ObjcImplementationDecl's InsMethods and ClsMethods fields.
403///
Steve Naroff60fccee2007-10-29 21:38:07 +0000404void ObjcImplementationDecl::addMethods(ObjcMethodDecl **insMethods,
405 unsigned numInsMembers,
406 ObjcMethodDecl **clsMethods,
407 unsigned numClsMembers,
408 SourceLocation AtEndLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000409 NumInstanceMethods = numInsMembers;
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000410 if (numInsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000411 InstanceMethods = new ObjcMethodDecl*[numInsMembers];
412 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000413 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000414 NumClassMethods = numClsMembers;
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000415 if (numClsMembers) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000416 ClassMethods = new ObjcMethodDecl*[numClsMembers];
417 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*));
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000418 }
419}
420
Steve Naroff3d581382007-10-14 18:27:41 +0000421// lookupInstanceMethod - This method returns an instance method by looking in
422// the class, it's categories, and it's super classes (using a linear search).
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000423ObjcMethodDecl *ObjcInterfaceDecl::lookupInstanceMethod(Selector &Sel) {
424 ObjcInterfaceDecl* ClassDecl = this;
425 while (ClassDecl != NULL) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000426 ObjcMethodDecl **methods = ClassDecl->getInstanceMethods();
427 int methodCount = ClassDecl->getNumInstanceMethods();
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000428 for (int i = 0; i < methodCount; ++i) {
429 if (methods[i]->getSelector() == Sel) {
430 return methods[i];
431 }
432 }
Steve Naroffff1afdb2007-10-14 23:13:51 +0000433 // Didn't find one yet - look through protocols.
434 ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
435 int numProtocols = ClassDecl->getNumIntfRefProtocols();
436 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
437 ObjcMethodDecl **methods = protocols[pIdx]->getInstanceMethods();
438 int methodCount = protocols[pIdx]->getNumInstanceMethods();
439 for (int i = 0; i < methodCount; ++i) {
440 if (methods[i]->getSelector() == Sel) {
441 return methods[i];
442 }
443 }
444 }
Steve Naroff3d581382007-10-14 18:27:41 +0000445 // Didn't find one yet - now look through categories.
Steve Naroffff1afdb2007-10-14 23:13:51 +0000446 ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroff3d581382007-10-14 18:27:41 +0000447 while (CatDecl) {
448 ObjcMethodDecl **methods = CatDecl->getInstanceMethods();
449 int methodCount = CatDecl->getNumInstanceMethods();
450 for (int i = 0; i < methodCount; ++i) {
451 if (methods[i]->getSelector() == Sel) {
452 return methods[i];
453 }
454 }
455 CatDecl = CatDecl->getNextClassCategory();
456 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000457 ClassDecl = ClassDecl->getSuperClass();
458 }
459 return NULL;
460}
461
Steve Naroff3d581382007-10-14 18:27:41 +0000462// lookupClassMethod - This method returns a class method by looking in the
463// class, it's categories, and it's super classes (using a linear search).
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000464ObjcMethodDecl *ObjcInterfaceDecl::lookupClassMethod(Selector &Sel) {
465 ObjcInterfaceDecl* ClassDecl = this;
466 while (ClassDecl != NULL) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000467 ObjcMethodDecl **methods = ClassDecl->getClassMethods();
468 int methodCount = ClassDecl->getNumClassMethods();
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000469 for (int i = 0; i < methodCount; ++i) {
470 if (methods[i]->getSelector() == Sel) {
471 return methods[i];
472 }
473 }
Steve Naroffff1afdb2007-10-14 23:13:51 +0000474 // Didn't find one yet - look through protocols.
475 ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
476 int numProtocols = ClassDecl->getNumIntfRefProtocols();
477 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
478 ObjcMethodDecl **methods = protocols[pIdx]->getClassMethods();
479 int methodCount = protocols[pIdx]->getNumClassMethods();
480 for (int i = 0; i < methodCount; ++i) {
481 if (methods[i]->getSelector() == Sel) {
482 return methods[i];
483 }
484 }
485 }
Steve Naroff3d581382007-10-14 18:27:41 +0000486 // Didn't find one yet - now look through categories.
Steve Naroffff1afdb2007-10-14 23:13:51 +0000487 ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroff3d581382007-10-14 18:27:41 +0000488 while (CatDecl) {
489 ObjcMethodDecl **methods = CatDecl->getClassMethods();
490 int methodCount = CatDecl->getNumClassMethods();
491 for (int i = 0; i < methodCount; ++i) {
492 if (methods[i]->getSelector() == Sel) {
493 return methods[i];
494 }
495 }
496 CatDecl = CatDecl->getNextClassCategory();
497 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000498 ClassDecl = ClassDecl->getSuperClass();
499 }
500 return NULL;
501}
502
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000503