blob: d3592e1416abd951ef8e13d51afaec4e0cbfe92d [file] [log] [blame]
Chris Lattner1e03a562008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
16using namespace clang;
17
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000018//===----------------------------------------------------------------------===//
19// ObjC Decl Allocation/Deallocation Method Implementations
20//===----------------------------------------------------------------------===//
21
Chris Lattner0ed844b2008-04-04 06:12:32 +000022ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
23 SourceLocation beginLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000024 SourceLocation endLoc,
25 Selector SelInfo, QualType T,
26 Decl *contextDecl,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000027 AttributeList *M, bool isInstance,
28 bool isVariadic,
Chris Lattnerb06fa3b2008-03-16 00:58:16 +000029 ImplementationControl impControl) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000030 void *Mem = C.getAllocator().Allocate<ObjCMethodDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +000031 return new (Mem) ObjCMethodDecl(beginLoc, endLoc,
32 SelInfo, T, contextDecl,
Chris Lattnerb06fa3b2008-03-16 00:58:16 +000033 M, isInstance,
34 isVariadic, impControl);
Chris Lattner0e77ba02008-03-16 01:15:50 +000035}
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000036
Chris Lattner0ed844b2008-04-04 06:12:32 +000037ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
38 SourceLocation atLoc,
Chris Lattner0e77ba02008-03-16 01:15:50 +000039 unsigned numRefProtos,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000040 IdentifierInfo *Id,
41 SourceLocation ClassLoc,
Chris Lattner0e77ba02008-03-16 01:15:50 +000042 bool ForwardDecl, bool isInternal){
43 void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +000044 return new (Mem) ObjCInterfaceDecl(atLoc, numRefProtos,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000045 Id, ClassLoc, ForwardDecl,
Chris Lattner0e77ba02008-03-16 01:15:50 +000046 isInternal);
47}
48
Chris Lattnerb048c982008-04-06 04:47:34 +000049ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner0e77ba02008-03-16 01:15:50 +000050 IdentifierInfo *Id, QualType T) {
51 void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
Chris Lattnerb048c982008-04-06 04:47:34 +000052 return new (Mem) ObjCIvarDecl(L, Id, T);
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000053}
54
Chris Lattner0ed844b2008-04-04 06:12:32 +000055ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
56 SourceLocation L,
Chris Lattnercca59d72008-03-16 01:23:04 +000057 unsigned numRefProtos,
Chris Lattnerc8581052008-03-16 20:19:15 +000058 IdentifierInfo *Id) {
Chris Lattnercca59d72008-03-16 01:23:04 +000059 void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
Chris Lattnerc8581052008-03-16 20:19:15 +000060 return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id);
Chris Lattnercca59d72008-03-16 01:23:04 +000061}
62
Chris Lattner0ed844b2008-04-04 06:12:32 +000063ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C,
64 SourceLocation L,
Chris Lattner61f9d412008-03-16 20:34:23 +000065 ObjCInterfaceDecl **Elts, unsigned nElts) {
66 void *Mem = C.getAllocator().Allocate<ObjCClassDecl>();
67 return new (Mem) ObjCClassDecl(L, Elts, nElts);
68}
69
70ObjCForwardProtocolDecl *
Chris Lattner0ed844b2008-04-04 06:12:32 +000071ObjCForwardProtocolDecl::Create(ASTContext &C,
72 SourceLocation L,
Chris Lattner61f9d412008-03-16 20:34:23 +000073 ObjCProtocolDecl **Elts, unsigned NumElts) {
74 void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>();
75 return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts);
76}
77
Chris Lattner0ed844b2008-04-04 06:12:32 +000078ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C,
79 SourceLocation L,
Chris Lattner61f9d412008-03-16 20:34:23 +000080 IdentifierInfo *Id) {
81 void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
Chris Lattner68c82cf2008-03-16 20:47:45 +000082 return new (Mem) ObjCCategoryDecl(L, Id);
Chris Lattner61f9d412008-03-16 20:34:23 +000083}
84
Chris Lattner75c9cae2008-03-16 20:53:07 +000085ObjCCategoryImplDecl *
Chris Lattner0ed844b2008-04-04 06:12:32 +000086ObjCCategoryImplDecl::Create(ASTContext &C,
87 SourceLocation L,IdentifierInfo *Id,
Chris Lattner75c9cae2008-03-16 20:53:07 +000088 ObjCInterfaceDecl *ClassInterface) {
89 void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>();
90 return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface);
91}
92
93ObjCImplementationDecl *
Chris Lattner0ed844b2008-04-04 06:12:32 +000094ObjCImplementationDecl::Create(ASTContext &C,
95 SourceLocation L,
Chris Lattner75c9cae2008-03-16 20:53:07 +000096 IdentifierInfo *Id,
97 ObjCInterfaceDecl *ClassInterface,
98 ObjCInterfaceDecl *SuperDecl) {
99 void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>();
100 return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl);
101}
Chris Lattner1e03a562008-03-16 00:19:01 +0000102
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000103ObjCCompatibleAliasDecl *
Chris Lattner0ed844b2008-04-04 06:12:32 +0000104ObjCCompatibleAliasDecl::Create(ASTContext &C,
105 SourceLocation L,
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000106 IdentifierInfo *Id,
107 ObjCInterfaceDecl* AliasedClass) {
108 void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>();
109 return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass);
110}
111
Chris Lattner0ed844b2008-04-04 06:12:32 +0000112ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C,
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000113 SourceLocation L,
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000114 IdentifierInfo *Id,
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000115 QualType T) {
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000116 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000117 return new (Mem) ObjCPropertyDecl(L, Id, T);
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000118}
119
Chris Lattner1e03a562008-03-16 00:19:01 +0000120//===----------------------------------------------------------------------===//
121// Objective-C Decl Implementation
122//===----------------------------------------------------------------------===//
123
124void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
125 unsigned NumParams) {
126 assert(ParamInfo == 0 && "Already has param info!");
127
128 // Zero params -> null pointer.
129 if (NumParams) {
130 ParamInfo = new ParmVarDecl*[NumParams];
131 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
132 NumMethodParams = NumParams;
133 }
134}
135
136ObjCMethodDecl::~ObjCMethodDecl() {
137 delete[] ParamInfo;
138}
139
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000140/// FindPropertyDeclaration - Finds declaration of the property given its name
141/// in 'PropertyId' and returns it. It returns 0, if not found.
142///
143ObjCPropertyDecl *
144 ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
145 for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(),
146 E = classprop_end(); I != E; ++I) {
147 ObjCPropertyDecl *property = *I;
148 if (property->getIdentifier() == PropertyId)
149 return property;
150 }
Fariborz Jahanianc70bee82008-04-21 23:57:08 +0000151 if (getSuperClass())
152 return getSuperClass()->FindPropertyDeclaration(PropertyId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000153 return 0;
154}
155
156/// FindCategoryDeclaration - Finds category declaration in the list of
157/// categories for this class and returns it. Name of the category is passed
158/// in 'CategoryId'. If category not found, return 0;
159///
160ObjCCategoryDecl *
161 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
162 for (ObjCCategoryDecl *Category = getCategoryList();
163 Category; Category = Category->getNextClassCategory())
164 if (Category->getIdentifier() == CategoryId)
165 return Category;
166 return 0;
167}
168
169/// FindIvarDeclaration - Find an Ivar declaration in this class given its
170/// name in 'IvarId'. On failure to find, return 0;
171///
172ObjCIvarDecl *
173 ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const {
174 for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(),
175 IVE = ivar_end(); IVI != IVE; ++IVI) {
176 ObjCIvarDecl* Ivar = (*IVI);
177 if (Ivar->getIdentifier() == IvarId)
178 return Ivar;
179 }
Fariborz Jahanianc70bee82008-04-21 23:57:08 +0000180 if (getSuperClass())
181 return getSuperClass()->FindIvarDeclaration(IvarId);
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000182 return 0;
183}
184
Chris Lattner1e03a562008-03-16 00:19:01 +0000185/// ObjCAddInstanceVariablesToClass - Inserts instance variables
186/// into ObjCInterfaceDecl's fields.
187///
188void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
189 unsigned numIvars,
190 SourceLocation RBrac) {
191 NumIvars = numIvars;
192 if (numIvars) {
193 Ivars = new ObjCIvarDecl*[numIvars];
194 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
195 }
196 setLocEnd(RBrac);
197}
198
199/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
200/// Variables (Ivars) relative to what declared in @implementation;s class.
201/// Ivars into ObjCImplementationDecl's fields.
202///
203void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
204 ObjCIvarDecl **ivars, unsigned numIvars) {
205 NumIvars = numIvars;
206 if (numIvars) {
207 Ivars = new ObjCIvarDecl*[numIvars];
208 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
209 }
210}
211
212/// addMethods - Insert instance and methods declarations into
213/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
214///
215void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
216 unsigned numInsMembers,
217 ObjCMethodDecl **clsMethods,
218 unsigned numClsMembers,
219 SourceLocation endLoc) {
220 NumInstanceMethods = numInsMembers;
221 if (numInsMembers) {
222 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
223 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
224 }
225 NumClassMethods = numClsMembers;
226 if (numClsMembers) {
227 ClassMethods = new ObjCMethodDecl*[numClsMembers];
228 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
229 }
230 AtEndLoc = endLoc;
231}
232
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000233/// addProperties - Insert property declaration AST nodes into
234/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattner55d13b42008-03-16 21:23:50 +0000235///
236void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
237 unsigned NumProperties) {
238 if (NumProperties == 0) return;
239
240 NumPropertyDecl = NumProperties;
241 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
242 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
243}
244
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000245/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000246/// ObjCProtocolDecl's PropertyDecl field.
247///
248void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
249 unsigned NumProperties) {
250 if (NumProperties == 0) return;
251
252 NumPropertyDecl = NumProperties;
253 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
254 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
255}
256
257/// addProperties - Insert property declaration AST nodes into
Fariborz Jahaniand9a3c332008-04-16 21:11:25 +0000258/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000259///
260void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
261 unsigned NumProperties) {
262 if (NumProperties == 0) return;
263
264 NumPropertyDecl = NumProperties;
265 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
266 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
267}
268
Chris Lattner55d13b42008-03-16 21:23:50 +0000269/// addMethods - Insert instance and methods declarations into
Chris Lattner1e03a562008-03-16 00:19:01 +0000270/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
271///
272void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
273 unsigned numInsMembers,
274 ObjCMethodDecl **clsMethods,
275 unsigned numClsMembers,
276 SourceLocation endLoc) {
277 NumInstanceMethods = numInsMembers;
278 if (numInsMembers) {
279 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
280 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
281 }
282 NumClassMethods = numClsMembers;
283 if (numClsMembers) {
284 ClassMethods = new ObjCMethodDecl*[numClsMembers];
285 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
286 }
287 AtEndLoc = endLoc;
288}
289
Chris Lattner68c82cf2008-03-16 20:47:45 +0000290void ObjCCategoryDecl::setReferencedProtocolList(ObjCProtocolDecl **List,
291 unsigned NumRPs) {
292 assert(NumReferencedProtocols == 0 && "Protocol list already set");
293 if (NumRPs == 0) return;
294
295 ReferencedProtocols = new ObjCProtocolDecl*[NumRPs];
296 memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*));
297 NumReferencedProtocols = NumRPs;
298}
299
300
Chris Lattner1e03a562008-03-16 00:19:01 +0000301/// addMethods - Insert instance and methods declarations into
302/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
303///
304void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
305 unsigned numInsMembers,
306 ObjCMethodDecl **clsMethods,
307 unsigned numClsMembers,
308 SourceLocation endLoc) {
309 NumInstanceMethods = numInsMembers;
310 if (numInsMembers) {
311 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
312 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
313 }
314 NumClassMethods = numClsMembers;
315 if (numClsMembers) {
316 ClassMethods = new ObjCMethodDecl*[numClsMembers];
317 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
318 }
319 AtEndLoc = endLoc;
320}
321
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000322/// FindPropertyDeclaration - Finds declaration of the property given its name
323/// in 'PropertyId' and returns it. It returns 0, if not found.
324///
325ObjCPropertyDecl *
326ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
327 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
328 E = classprop_end(); I != E; ++I) {
329 ObjCPropertyDecl *property = *I;
330 if (property->getIdentifier() == PropertyId)
331 return property;
332 }
333 return 0;
334}
335
Chris Lattner1e03a562008-03-16 00:19:01 +0000336ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
337 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
338 ObjCInterfaceDecl* ClassDecl = this;
339 while (ClassDecl != NULL) {
340 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
341 I != E; ++I) {
342 if ((*I)->getIdentifier() == ID) {
343 clsDeclared = ClassDecl;
344 return *I;
345 }
346 }
347 ClassDecl = ClassDecl->getSuperClass();
348 }
349 return NULL;
350}
351
352/// lookupInstanceMethod - This method returns an instance method by looking in
353/// the class, its categories, and its super classes (using a linear search).
354ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
355 ObjCInterfaceDecl* ClassDecl = this;
356 ObjCMethodDecl *MethodDecl = 0;
357
358 while (ClassDecl != NULL) {
359 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
360 return MethodDecl;
361
362 // Didn't find one yet - look through protocols.
363 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
364 int numProtocols = ClassDecl->getNumIntfRefProtocols();
365 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
366 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
367 return MethodDecl;
368 }
369 // Didn't find one yet - now look through categories.
370 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
371 while (CatDecl) {
372 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
373 return MethodDecl;
374 CatDecl = CatDecl->getNextClassCategory();
375 }
376 ClassDecl = ClassDecl->getSuperClass();
377 }
378 return NULL;
379}
380
381// lookupClassMethod - This method returns a class method by looking in the
382// class, its categories, and its super classes (using a linear search).
383ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
384 ObjCInterfaceDecl* ClassDecl = this;
385 ObjCMethodDecl *MethodDecl = 0;
386
387 while (ClassDecl != NULL) {
388 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
389 return MethodDecl;
390
391 // Didn't find one yet - look through protocols.
392 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
393 int numProtocols = ClassDecl->getNumIntfRefProtocols();
394 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
395 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
396 return MethodDecl;
397 }
398 // Didn't find one yet - now look through categories.
399 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
400 while (CatDecl) {
401 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
402 return MethodDecl;
403 CatDecl = CatDecl->getNextClassCategory();
404 }
405 ClassDecl = ClassDecl->getSuperClass();
406 }
407 return NULL;
408}
409
410/// lookupInstanceMethod - This method returns an instance method by looking in
411/// the class implementation. Unlike interfaces, we don't look outside the
412/// implementation.
413ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
414 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
415 if ((*I)->getSelector() == Sel)
416 return *I;
417 return NULL;
418}
419
420/// lookupClassMethod - This method returns a class method by looking in
421/// the class implementation. Unlike interfaces, we don't look outside the
422/// implementation.
423ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
424 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
425 I != E; ++I)
426 if ((*I)->getSelector() == Sel)
427 return *I;
428 return NULL;
429}
430
431// lookupInstanceMethod - This method returns an instance method by looking in
432// the class implementation. Unlike interfaces, we don't look outside the
433// implementation.
434ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
435 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
436 if ((*I)->getSelector() == Sel)
437 return *I;
438 return NULL;
439}
440
441// lookupClassMethod - This method returns an instance method by looking in
442// the class implementation. Unlike interfaces, we don't look outside the
443// implementation.
444ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
445 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
446 I != E; ++I)
447 if ((*I)->getSelector() == Sel)
448 return *I;
449 return NULL;
450}
451
452// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
453// it inherited.
454ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
455 ObjCMethodDecl *MethodDecl = NULL;
456
457 if ((MethodDecl = getInstanceMethod(Sel)))
458 return MethodDecl;
459
460 if (getNumReferencedProtocols() > 0) {
461 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
462
463 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
464 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
465 return MethodDecl;
466 }
467 }
468 return NULL;
469}
470
471// lookupInstanceMethod - Lookup a class method in the protocol and protocols
472// it inherited.
473ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
474 ObjCMethodDecl *MethodDecl = NULL;
475
476 if ((MethodDecl = getClassMethod(Sel)))
477 return MethodDecl;
478
479 if (getNumReferencedProtocols() > 0) {
480 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
481
482 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
483 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
484 return MethodDecl;
485 }
486 }
487 return NULL;
488}
489
490/// getSynthesizedMethodSize - Compute size of synthesized method name
491/// as done be the rewrite.
492///
493unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
494 // syntesized method name is a concatenation of -/+[class-name selector]
495 // Get length of this name.
496 unsigned length = 3; // _I_ or _C_
497 length += strlen(getClassInterface()->getName()) +1; // extra for _
498 NamedDecl *MethodContext = getMethodContext();
499 if (ObjCCategoryImplDecl *CID =
500 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
501 length += strlen(CID->getName()) +1;
502 length += getSelector().getName().size(); // selector name
503 return length;
504}
505
Chris Lattner56196882008-04-06 05:25:03 +0000506ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner1e03a562008-03-16 00:19:01 +0000507 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
508 return ID;
509 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
510 return CD->getClassInterface();
511 if (ObjCImplementationDecl *IMD =
Chris Lattner56196882008-04-06 05:25:03 +0000512 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner1e03a562008-03-16 00:19:01 +0000513 return IMD->getClassInterface();
Chris Lattner56196882008-04-06 05:25:03 +0000514 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner1e03a562008-03-16 00:19:01 +0000515 return CID->getClassInterface();
516 assert(false && "unknown method context");
517 return 0;
518}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000519
Chris Lattnerf4af5152008-03-17 01:19:02 +0000520
Chris Lattner0ed844b2008-04-04 06:12:32 +0000521