blob: 04e9993101effa614fa4a310d4a5e24b14f54805 [file] [log] [blame]
Chris Lattner10318b82008-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 Lattner114add62008-03-16 00:49:28 +000018//===----------------------------------------------------------------------===//
19// ObjC Decl Allocation/Deallocation Method Implementations
20//===----------------------------------------------------------------------===//
21
Chris Lattnereee57c02008-04-04 06:12:32 +000022ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
23 SourceLocation beginLoc,
Chris Lattner114add62008-03-16 00:49:28 +000024 SourceLocation endLoc,
25 Selector SelInfo, QualType T,
26 Decl *contextDecl,
Chris Lattner114add62008-03-16 00:49:28 +000027 AttributeList *M, bool isInstance,
28 bool isVariadic,
Chris Lattnerf7355832008-03-16 00:58:16 +000029 ImplementationControl impControl) {
Chris Lattner114add62008-03-16 00:49:28 +000030 void *Mem = C.getAllocator().Allocate<ObjCMethodDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000031 return new (Mem) ObjCMethodDecl(beginLoc, endLoc,
32 SelInfo, T, contextDecl,
Chris Lattnerf7355832008-03-16 00:58:16 +000033 M, isInstance,
34 isVariadic, impControl);
Chris Lattner0db541b2008-03-16 01:15:50 +000035}
Chris Lattner114add62008-03-16 00:49:28 +000036
Chris Lattnereee57c02008-04-04 06:12:32 +000037ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
38 SourceLocation atLoc,
Chris Lattner0db541b2008-03-16 01:15:50 +000039 unsigned numRefProtos,
Steve Naroff7c371742008-04-11 19:35:35 +000040 IdentifierInfo *Id,
41 SourceLocation ClassLoc,
Chris Lattner0db541b2008-03-16 01:15:50 +000042 bool ForwardDecl, bool isInternal){
43 void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000044 return new (Mem) ObjCInterfaceDecl(atLoc, numRefProtos,
Steve Naroff7c371742008-04-11 19:35:35 +000045 Id, ClassLoc, ForwardDecl,
Chris Lattner0db541b2008-03-16 01:15:50 +000046 isInternal);
47}
48
Chris Lattnerf3874bc2008-04-06 04:47:34 +000049ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner0db541b2008-03-16 01:15:50 +000050 IdentifierInfo *Id, QualType T) {
51 void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +000052 return new (Mem) ObjCIvarDecl(L, Id, T);
Chris Lattner114add62008-03-16 00:49:28 +000053}
54
Chris Lattnereee57c02008-04-04 06:12:32 +000055ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
56 SourceLocation L,
Chris Lattner180f7e22008-03-16 01:23:04 +000057 unsigned numRefProtos,
Chris Lattner7afba9c2008-03-16 20:19:15 +000058 IdentifierInfo *Id) {
Chris Lattner180f7e22008-03-16 01:23:04 +000059 void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
Chris Lattner7afba9c2008-03-16 20:19:15 +000060 return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id);
Chris Lattner180f7e22008-03-16 01:23:04 +000061}
62
Chris Lattnereee57c02008-04-04 06:12:32 +000063ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C,
64 SourceLocation L,
Chris Lattnere29dc832008-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 Lattnereee57c02008-04-04 06:12:32 +000071ObjCForwardProtocolDecl::Create(ASTContext &C,
72 SourceLocation L,
Chris Lattnere29dc832008-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 Lattnereee57c02008-04-04 06:12:32 +000078ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C,
79 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +000080 IdentifierInfo *Id) {
81 void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
Chris Lattner321b5d12008-03-16 20:47:45 +000082 return new (Mem) ObjCCategoryDecl(L, Id);
Chris Lattnere29dc832008-03-16 20:34:23 +000083}
84
Chris Lattner1b6de332008-03-16 20:53:07 +000085ObjCCategoryImplDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +000086ObjCCategoryImplDecl::Create(ASTContext &C,
87 SourceLocation L,IdentifierInfo *Id,
Chris Lattner1b6de332008-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 Lattnereee57c02008-04-04 06:12:32 +000094ObjCImplementationDecl::Create(ASTContext &C,
95 SourceLocation L,
Chris Lattner1b6de332008-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 Lattner10318b82008-03-16 00:19:01 +0000102
Chris Lattner2d1c4312008-03-16 21:17:37 +0000103ObjCCompatibleAliasDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000104ObjCCompatibleAliasDecl::Create(ASTContext &C,
105 SourceLocation L,
Chris Lattner2d1c4312008-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 Lattnereee57c02008-04-04 06:12:32 +0000112ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000113 SourceLocation L,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000114 IdentifierInfo *Id,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000115 QualType T) {
Chris Lattner2d1c4312008-03-16 21:17:37 +0000116 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000117 return new (Mem) ObjCPropertyDecl(L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000118}
119
Chris Lattner10318b82008-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 Jahanianef8a3df2008-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 }
151 return 0;
152}
153
154/// FindCategoryDeclaration - Finds category declaration in the list of
155/// categories for this class and returns it. Name of the category is passed
156/// in 'CategoryId'. If category not found, return 0;
157///
158ObjCCategoryDecl *
159 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
160 for (ObjCCategoryDecl *Category = getCategoryList();
161 Category; Category = Category->getNextClassCategory())
162 if (Category->getIdentifier() == CategoryId)
163 return Category;
164 return 0;
165}
166
167/// FindIvarDeclaration - Find an Ivar declaration in this class given its
168/// name in 'IvarId'. On failure to find, return 0;
169///
170ObjCIvarDecl *
171 ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const {
172 for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(),
173 IVE = ivar_end(); IVI != IVE; ++IVI) {
174 ObjCIvarDecl* Ivar = (*IVI);
175 if (Ivar->getIdentifier() == IvarId)
176 return Ivar;
177 }
178 return 0;
179}
180
Chris Lattner10318b82008-03-16 00:19:01 +0000181/// ObjCAddInstanceVariablesToClass - Inserts instance variables
182/// into ObjCInterfaceDecl's fields.
183///
184void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
185 unsigned numIvars,
186 SourceLocation RBrac) {
187 NumIvars = numIvars;
188 if (numIvars) {
189 Ivars = new ObjCIvarDecl*[numIvars];
190 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
191 }
192 setLocEnd(RBrac);
193}
194
195/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
196/// Variables (Ivars) relative to what declared in @implementation;s class.
197/// Ivars into ObjCImplementationDecl's fields.
198///
199void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
200 ObjCIvarDecl **ivars, unsigned numIvars) {
201 NumIvars = numIvars;
202 if (numIvars) {
203 Ivars = new ObjCIvarDecl*[numIvars];
204 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
205 }
206}
207
208/// addMethods - Insert instance and methods declarations into
209/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
210///
211void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
212 unsigned numInsMembers,
213 ObjCMethodDecl **clsMethods,
214 unsigned numClsMembers,
215 SourceLocation endLoc) {
216 NumInstanceMethods = numInsMembers;
217 if (numInsMembers) {
218 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
219 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
220 }
221 NumClassMethods = numClsMembers;
222 if (numClsMembers) {
223 ClassMethods = new ObjCMethodDecl*[numClsMembers];
224 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
225 }
226 AtEndLoc = endLoc;
227}
228
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000229/// addProperties - Insert property declaration AST nodes into
230/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattnercffe3662008-03-16 21:23:50 +0000231///
232void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
233 unsigned NumProperties) {
234 if (NumProperties == 0) return;
235
236 NumPropertyDecl = NumProperties;
237 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
238 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
239}
240
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000241/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000242/// ObjCProtocolDecl's PropertyDecl field.
243///
244void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
245 unsigned NumProperties) {
246 if (NumProperties == 0) return;
247
248 NumPropertyDecl = NumProperties;
249 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
250 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
251}
252
253/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian5742a0f2008-04-16 21:11:25 +0000254/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000255///
256void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
257 unsigned NumProperties) {
258 if (NumProperties == 0) return;
259
260 NumPropertyDecl = NumProperties;
261 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
262 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
263}
264
Chris Lattnercffe3662008-03-16 21:23:50 +0000265/// addMethods - Insert instance and methods declarations into
Chris Lattner10318b82008-03-16 00:19:01 +0000266/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
267///
268void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
269 unsigned numInsMembers,
270 ObjCMethodDecl **clsMethods,
271 unsigned numClsMembers,
272 SourceLocation endLoc) {
273 NumInstanceMethods = numInsMembers;
274 if (numInsMembers) {
275 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
276 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
277 }
278 NumClassMethods = numClsMembers;
279 if (numClsMembers) {
280 ClassMethods = new ObjCMethodDecl*[numClsMembers];
281 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
282 }
283 AtEndLoc = endLoc;
284}
285
Chris Lattner321b5d12008-03-16 20:47:45 +0000286void ObjCCategoryDecl::setReferencedProtocolList(ObjCProtocolDecl **List,
287 unsigned NumRPs) {
288 assert(NumReferencedProtocols == 0 && "Protocol list already set");
289 if (NumRPs == 0) return;
290
291 ReferencedProtocols = new ObjCProtocolDecl*[NumRPs];
292 memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*));
293 NumReferencedProtocols = NumRPs;
294}
295
296
Chris Lattner10318b82008-03-16 00:19:01 +0000297/// addMethods - Insert instance and methods declarations into
298/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
299///
300void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
301 unsigned numInsMembers,
302 ObjCMethodDecl **clsMethods,
303 unsigned numClsMembers,
304 SourceLocation endLoc) {
305 NumInstanceMethods = numInsMembers;
306 if (numInsMembers) {
307 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
308 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
309 }
310 NumClassMethods = numClsMembers;
311 if (numClsMembers) {
312 ClassMethods = new ObjCMethodDecl*[numClsMembers];
313 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
314 }
315 AtEndLoc = endLoc;
316}
317
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000318/// FindPropertyDeclaration - Finds declaration of the property given its name
319/// in 'PropertyId' and returns it. It returns 0, if not found.
320///
321ObjCPropertyDecl *
322ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
323 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
324 E = classprop_end(); I != E; ++I) {
325 ObjCPropertyDecl *property = *I;
326 if (property->getIdentifier() == PropertyId)
327 return property;
328 }
329 return 0;
330}
331
Chris Lattner10318b82008-03-16 00:19:01 +0000332ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
333 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
334 ObjCInterfaceDecl* ClassDecl = this;
335 while (ClassDecl != NULL) {
336 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
337 I != E; ++I) {
338 if ((*I)->getIdentifier() == ID) {
339 clsDeclared = ClassDecl;
340 return *I;
341 }
342 }
343 ClassDecl = ClassDecl->getSuperClass();
344 }
345 return NULL;
346}
347
348/// lookupInstanceMethod - This method returns an instance method by looking in
349/// the class, its categories, and its super classes (using a linear search).
350ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
351 ObjCInterfaceDecl* ClassDecl = this;
352 ObjCMethodDecl *MethodDecl = 0;
353
354 while (ClassDecl != NULL) {
355 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
356 return MethodDecl;
357
358 // Didn't find one yet - look through protocols.
359 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
360 int numProtocols = ClassDecl->getNumIntfRefProtocols();
361 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
362 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
363 return MethodDecl;
364 }
365 // Didn't find one yet - now look through categories.
366 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
367 while (CatDecl) {
368 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
369 return MethodDecl;
370 CatDecl = CatDecl->getNextClassCategory();
371 }
372 ClassDecl = ClassDecl->getSuperClass();
373 }
374 return NULL;
375}
376
377// lookupClassMethod - This method returns a class method by looking in the
378// class, its categories, and its super classes (using a linear search).
379ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
380 ObjCInterfaceDecl* ClassDecl = this;
381 ObjCMethodDecl *MethodDecl = 0;
382
383 while (ClassDecl != NULL) {
384 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
385 return MethodDecl;
386
387 // Didn't find one yet - look through protocols.
388 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
389 int numProtocols = ClassDecl->getNumIntfRefProtocols();
390 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
391 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
392 return MethodDecl;
393 }
394 // Didn't find one yet - now look through categories.
395 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
396 while (CatDecl) {
397 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
398 return MethodDecl;
399 CatDecl = CatDecl->getNextClassCategory();
400 }
401 ClassDecl = ClassDecl->getSuperClass();
402 }
403 return NULL;
404}
405
406/// lookupInstanceMethod - This method returns an instance method by looking in
407/// the class implementation. Unlike interfaces, we don't look outside the
408/// implementation.
409ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
410 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
411 if ((*I)->getSelector() == Sel)
412 return *I;
413 return NULL;
414}
415
416/// lookupClassMethod - This method returns a class method by looking in
417/// the class implementation. Unlike interfaces, we don't look outside the
418/// implementation.
419ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
420 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
421 I != E; ++I)
422 if ((*I)->getSelector() == Sel)
423 return *I;
424 return NULL;
425}
426
427// lookupInstanceMethod - This method returns an instance method by looking in
428// the class implementation. Unlike interfaces, we don't look outside the
429// implementation.
430ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
431 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
432 if ((*I)->getSelector() == Sel)
433 return *I;
434 return NULL;
435}
436
437// lookupClassMethod - This method returns an instance method by looking in
438// the class implementation. Unlike interfaces, we don't look outside the
439// implementation.
440ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
441 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
442 I != E; ++I)
443 if ((*I)->getSelector() == Sel)
444 return *I;
445 return NULL;
446}
447
448// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
449// it inherited.
450ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
451 ObjCMethodDecl *MethodDecl = NULL;
452
453 if ((MethodDecl = getInstanceMethod(Sel)))
454 return MethodDecl;
455
456 if (getNumReferencedProtocols() > 0) {
457 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
458
459 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
460 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
461 return MethodDecl;
462 }
463 }
464 return NULL;
465}
466
467// lookupInstanceMethod - Lookup a class method in the protocol and protocols
468// it inherited.
469ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
470 ObjCMethodDecl *MethodDecl = NULL;
471
472 if ((MethodDecl = getClassMethod(Sel)))
473 return MethodDecl;
474
475 if (getNumReferencedProtocols() > 0) {
476 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
477
478 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
479 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
480 return MethodDecl;
481 }
482 }
483 return NULL;
484}
485
486/// getSynthesizedMethodSize - Compute size of synthesized method name
487/// as done be the rewrite.
488///
489unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
490 // syntesized method name is a concatenation of -/+[class-name selector]
491 // Get length of this name.
492 unsigned length = 3; // _I_ or _C_
493 length += strlen(getClassInterface()->getName()) +1; // extra for _
494 NamedDecl *MethodContext = getMethodContext();
495 if (ObjCCategoryImplDecl *CID =
496 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
497 length += strlen(CID->getName()) +1;
498 length += getSelector().getName().size(); // selector name
499 return length;
500}
501
Chris Lattner052fbcd2008-04-06 05:25:03 +0000502ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner10318b82008-03-16 00:19:01 +0000503 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
504 return ID;
505 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
506 return CD->getClassInterface();
507 if (ObjCImplementationDecl *IMD =
Chris Lattner052fbcd2008-04-06 05:25:03 +0000508 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000509 return IMD->getClassInterface();
Chris Lattner052fbcd2008-04-06 05:25:03 +0000510 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000511 return CID->getClassInterface();
512 assert(false && "unknown method context");
513 return 0;
514}
Chris Lattner44859612008-03-17 01:19:02 +0000515
Chris Lattner44859612008-03-17 01:19:02 +0000516
Chris Lattnereee57c02008-04-04 06:12:32 +0000517