blob: 0cb777d8debf369d052e7d128ab1356412bfe398 [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 Jahanian4aa72a72008-05-05 18:51:55 +0000115 QualType T,
116 PropertyControl propControl) {
Chris Lattner2d1c4312008-03-16 21:17:37 +0000117 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000118 return new (Mem) ObjCPropertyDecl(L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000119}
120
Chris Lattner10318b82008-03-16 00:19:01 +0000121//===----------------------------------------------------------------------===//
122// Objective-C Decl Implementation
123//===----------------------------------------------------------------------===//
124
125void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
126 unsigned NumParams) {
127 assert(ParamInfo == 0 && "Already has param info!");
128
129 // Zero params -> null pointer.
130 if (NumParams) {
131 ParamInfo = new ParmVarDecl*[NumParams];
132 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
133 NumMethodParams = NumParams;
134 }
135}
136
137ObjCMethodDecl::~ObjCMethodDecl() {
138 delete[] ParamInfo;
139}
140
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000141/// FindPropertyDeclaration - Finds declaration of the property given its name
142/// in 'PropertyId' and returns it. It returns 0, if not found.
143///
144ObjCPropertyDecl *
145 ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
146 for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(),
147 E = classprop_end(); I != E; ++I) {
148 ObjCPropertyDecl *property = *I;
149 if (property->getIdentifier() == PropertyId)
150 return property;
151 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000152 if (getSuperClass())
153 return getSuperClass()->FindPropertyDeclaration(PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000154 return 0;
155}
156
157/// FindCategoryDeclaration - Finds category declaration in the list of
158/// categories for this class and returns it. Name of the category is passed
159/// in 'CategoryId'. If category not found, return 0;
160///
161ObjCCategoryDecl *
162 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
163 for (ObjCCategoryDecl *Category = getCategoryList();
164 Category; Category = Category->getNextClassCategory())
165 if (Category->getIdentifier() == CategoryId)
166 return Category;
167 return 0;
168}
169
170/// FindIvarDeclaration - Find an Ivar declaration in this class given its
171/// name in 'IvarId'. On failure to find, return 0;
172///
173ObjCIvarDecl *
174 ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const {
175 for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(),
176 IVE = ivar_end(); IVI != IVE; ++IVI) {
177 ObjCIvarDecl* Ivar = (*IVI);
178 if (Ivar->getIdentifier() == IvarId)
179 return Ivar;
180 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000181 if (getSuperClass())
182 return getSuperClass()->FindIvarDeclaration(IvarId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000183 return 0;
184}
185
Chris Lattner10318b82008-03-16 00:19:01 +0000186/// ObjCAddInstanceVariablesToClass - Inserts instance variables
187/// into ObjCInterfaceDecl's fields.
188///
189void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
190 unsigned numIvars,
191 SourceLocation RBrac) {
192 NumIvars = numIvars;
193 if (numIvars) {
194 Ivars = new ObjCIvarDecl*[numIvars];
195 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
196 }
197 setLocEnd(RBrac);
198}
199
200/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
201/// Variables (Ivars) relative to what declared in @implementation;s class.
202/// Ivars into ObjCImplementationDecl's fields.
203///
204void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
205 ObjCIvarDecl **ivars, unsigned numIvars) {
206 NumIvars = numIvars;
207 if (numIvars) {
208 Ivars = new ObjCIvarDecl*[numIvars];
209 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
210 }
211}
212
213/// addMethods - Insert instance and methods declarations into
214/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
215///
216void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
217 unsigned numInsMembers,
218 ObjCMethodDecl **clsMethods,
219 unsigned numClsMembers,
220 SourceLocation endLoc) {
221 NumInstanceMethods = numInsMembers;
222 if (numInsMembers) {
223 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
224 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
225 }
226 NumClassMethods = numClsMembers;
227 if (numClsMembers) {
228 ClassMethods = new ObjCMethodDecl*[numClsMembers];
229 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
230 }
231 AtEndLoc = endLoc;
232}
233
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000234/// addProperties - Insert property declaration AST nodes into
235/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattnercffe3662008-03-16 21:23:50 +0000236///
237void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
238 unsigned NumProperties) {
239 if (NumProperties == 0) return;
240
241 NumPropertyDecl = NumProperties;
242 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
243 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
244}
245
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000246/// mergeProperties - Adds properties to the end of list of current properties
247/// for this class.
248
249void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties,
250 unsigned NumNewProperties) {
251 if (NumNewProperties == 0) return;
252
253 if (PropertyDecl) {
254 ObjCPropertyDecl **newPropertyDecl =
255 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
256 ObjCPropertyDecl **buf = newPropertyDecl;
257 // put back original properties in buffer.
258 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
259 // Add new properties to this buffer.
260 memcpy(buf+NumPropertyDecl, Properties,
261 NumNewProperties*sizeof(ObjCPropertyDecl*));
262 free(PropertyDecl);
263 PropertyDecl = newPropertyDecl;
264 NumPropertyDecl += NumNewProperties;
265 }
266 else {
267 PropertyDecl = new ObjCPropertyDecl*[NumNewProperties];
268 memcpy(PropertyDecl, Properties, NumNewProperties*sizeof(ObjCPropertyDecl*));
269 NumPropertyDecl = NumNewProperties;
270 }
271}
272
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000273/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000274/// ObjCProtocolDecl's PropertyDecl field.
275///
276void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
277 unsigned NumProperties) {
278 if (NumProperties == 0) return;
279
280 NumPropertyDecl = NumProperties;
281 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
282 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
283}
284
285/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian5742a0f2008-04-16 21:11:25 +0000286/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000287///
288void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
289 unsigned NumProperties) {
290 if (NumProperties == 0) return;
291
292 NumPropertyDecl = NumProperties;
293 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
294 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
295}
296
Chris Lattnercffe3662008-03-16 21:23:50 +0000297/// addMethods - Insert instance and methods declarations into
Chris Lattner10318b82008-03-16 00:19:01 +0000298/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
299///
300void ObjCProtocolDecl::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
Chris Lattner321b5d12008-03-16 20:47:45 +0000318void ObjCCategoryDecl::setReferencedProtocolList(ObjCProtocolDecl **List,
319 unsigned NumRPs) {
320 assert(NumReferencedProtocols == 0 && "Protocol list already set");
321 if (NumRPs == 0) return;
322
323 ReferencedProtocols = new ObjCProtocolDecl*[NumRPs];
324 memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*));
325 NumReferencedProtocols = NumRPs;
326}
327
328
Chris Lattner10318b82008-03-16 00:19:01 +0000329/// addMethods - Insert instance and methods declarations into
330/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
331///
332void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
333 unsigned numInsMembers,
334 ObjCMethodDecl **clsMethods,
335 unsigned numClsMembers,
336 SourceLocation endLoc) {
337 NumInstanceMethods = numInsMembers;
338 if (numInsMembers) {
339 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
340 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
341 }
342 NumClassMethods = numClsMembers;
343 if (numClsMembers) {
344 ClassMethods = new ObjCMethodDecl*[numClsMembers];
345 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
346 }
347 AtEndLoc = endLoc;
348}
349
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000350/// FindPropertyDeclaration - Finds declaration of the property given its name
351/// in 'PropertyId' and returns it. It returns 0, if not found.
352///
353ObjCPropertyDecl *
354ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
355 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
356 E = classprop_end(); I != E; ++I) {
357 ObjCPropertyDecl *property = *I;
358 if (property->getIdentifier() == PropertyId)
359 return property;
360 }
361 return 0;
362}
363
Chris Lattner10318b82008-03-16 00:19:01 +0000364ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
365 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
366 ObjCInterfaceDecl* ClassDecl = this;
367 while (ClassDecl != NULL) {
368 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
369 I != E; ++I) {
370 if ((*I)->getIdentifier() == ID) {
371 clsDeclared = ClassDecl;
372 return *I;
373 }
374 }
375 ClassDecl = ClassDecl->getSuperClass();
376 }
377 return NULL;
378}
379
380/// lookupInstanceMethod - This method returns an instance method by looking in
381/// the class, its categories, and its super classes (using a linear search).
382ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
383 ObjCInterfaceDecl* ClassDecl = this;
384 ObjCMethodDecl *MethodDecl = 0;
385
386 while (ClassDecl != NULL) {
387 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
388 return MethodDecl;
389
390 // Didn't find one yet - look through protocols.
391 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
392 int numProtocols = ClassDecl->getNumIntfRefProtocols();
393 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
394 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
395 return MethodDecl;
396 }
397 // Didn't find one yet - now look through categories.
398 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
399 while (CatDecl) {
400 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
401 return MethodDecl;
402 CatDecl = CatDecl->getNextClassCategory();
403 }
404 ClassDecl = ClassDecl->getSuperClass();
405 }
406 return NULL;
407}
408
409// lookupClassMethod - This method returns a class method by looking in the
410// class, its categories, and its super classes (using a linear search).
411ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
412 ObjCInterfaceDecl* ClassDecl = this;
413 ObjCMethodDecl *MethodDecl = 0;
414
415 while (ClassDecl != NULL) {
416 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
417 return MethodDecl;
418
419 // Didn't find one yet - look through protocols.
420 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
421 int numProtocols = ClassDecl->getNumIntfRefProtocols();
422 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
423 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
424 return MethodDecl;
425 }
426 // Didn't find one yet - now look through categories.
427 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
428 while (CatDecl) {
429 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
430 return MethodDecl;
431 CatDecl = CatDecl->getNextClassCategory();
432 }
433 ClassDecl = ClassDecl->getSuperClass();
434 }
435 return NULL;
436}
437
438/// lookupInstanceMethod - This method returns an instance method by looking in
439/// the class implementation. Unlike interfaces, we don't look outside the
440/// implementation.
441ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
442 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
443 if ((*I)->getSelector() == Sel)
444 return *I;
445 return NULL;
446}
447
448/// lookupClassMethod - This method returns a class method by looking in
449/// the class implementation. Unlike interfaces, we don't look outside the
450/// implementation.
451ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
452 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
453 I != E; ++I)
454 if ((*I)->getSelector() == Sel)
455 return *I;
456 return NULL;
457}
458
459// lookupInstanceMethod - This method returns an instance method by looking in
460// the class implementation. Unlike interfaces, we don't look outside the
461// implementation.
462ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
463 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
464 if ((*I)->getSelector() == Sel)
465 return *I;
466 return NULL;
467}
468
469// lookupClassMethod - This method returns an instance method by looking in
470// the class implementation. Unlike interfaces, we don't look outside the
471// implementation.
472ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
473 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
474 I != E; ++I)
475 if ((*I)->getSelector() == Sel)
476 return *I;
477 return NULL;
478}
479
480// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
481// it inherited.
482ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
483 ObjCMethodDecl *MethodDecl = NULL;
484
485 if ((MethodDecl = getInstanceMethod(Sel)))
486 return MethodDecl;
487
488 if (getNumReferencedProtocols() > 0) {
489 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
490
491 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
492 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
493 return MethodDecl;
494 }
495 }
496 return NULL;
497}
498
499// lookupInstanceMethod - Lookup a class method in the protocol and protocols
500// it inherited.
501ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
502 ObjCMethodDecl *MethodDecl = NULL;
503
504 if ((MethodDecl = getClassMethod(Sel)))
505 return MethodDecl;
506
507 if (getNumReferencedProtocols() > 0) {
508 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
509
510 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
511 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
512 return MethodDecl;
513 }
514 }
515 return NULL;
516}
517
518/// getSynthesizedMethodSize - Compute size of synthesized method name
519/// as done be the rewrite.
520///
521unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
522 // syntesized method name is a concatenation of -/+[class-name selector]
523 // Get length of this name.
524 unsigned length = 3; // _I_ or _C_
525 length += strlen(getClassInterface()->getName()) +1; // extra for _
526 NamedDecl *MethodContext = getMethodContext();
527 if (ObjCCategoryImplDecl *CID =
528 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
529 length += strlen(CID->getName()) +1;
530 length += getSelector().getName().size(); // selector name
531 return length;
532}
533
Chris Lattner052fbcd2008-04-06 05:25:03 +0000534ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner10318b82008-03-16 00:19:01 +0000535 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
536 return ID;
537 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
538 return CD->getClassInterface();
539 if (ObjCImplementationDecl *IMD =
Chris Lattner052fbcd2008-04-06 05:25:03 +0000540 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000541 return IMD->getClassInterface();
Chris Lattner052fbcd2008-04-06 05:25:03 +0000542 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000543 return CID->getClassInterface();
544 assert(false && "unknown method context");
545 return 0;
546}
Chris Lattner44859612008-03-17 01:19:02 +0000547
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000548ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
549 SourceLocation atLoc,
550 SourceLocation L,
551 ObjCPropertyDecl *property,
552 PropertyImplKind kind,
553 ObjCIvarDecl *ivar) {
554 void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>();
555 return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, kind, ivar);
556}
Chris Lattner44859612008-03-17 01:19:02 +0000557
Chris Lattnereee57c02008-04-04 06:12:32 +0000558