blob: 3c89a6aa7df20a6d3eecad21795c9438422e6943 [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 Jahaniane4534e72008-05-07 17:43:59 +0000273/// addPropertyMethods - Goes through list of properties declared in this class
274/// and builds setter/getter method declartions depending on the setter/getter
275/// attributes of the property.
276///
277void ObjCInterfaceDecl::addPropertyMethods(
278 ASTContext &Context,
279 ObjCPropertyDecl *property,
280 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods) {
281 // Find the default getter and if one not found, add one.
282 ObjCMethodDecl *GetterDecl = getInstanceMethod(property->getGetterName());
283 if (GetterDecl) {
284 // An instance method with same name as property getter name found.
285 property->setGetterMethodDecl(GetterDecl);
286 }
287 else {
288 // No instance method of same name as property getter name was found.
289 // Declare a getter method and add it to the list of methods
290 // for this class.
291 QualType resultDeclType = property->getType();
292 ObjCMethodDecl* ObjCMethod =
293 ObjCMethodDecl::Create(Context, property->getLocation(),
294 property->getLocation(),
295 property->getGetterName(), resultDeclType,
296 this, 0,
297 true, false, ObjCMethodDecl::Required);
298 property->setGetterMethodDecl(ObjCMethod);
299 insMethods.push_back(ObjCMethod);
300 }
301}
302
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000303/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000304/// ObjCProtocolDecl's PropertyDecl field.
305///
306void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
307 unsigned NumProperties) {
308 if (NumProperties == 0) return;
309
310 NumPropertyDecl = NumProperties;
311 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
312 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
313}
314
315/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian5742a0f2008-04-16 21:11:25 +0000316/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000317///
318void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
319 unsigned NumProperties) {
320 if (NumProperties == 0) return;
321
322 NumPropertyDecl = NumProperties;
323 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
324 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
325}
326
Chris Lattnercffe3662008-03-16 21:23:50 +0000327/// addMethods - Insert instance and methods declarations into
Chris Lattner10318b82008-03-16 00:19:01 +0000328/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
329///
330void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
331 unsigned numInsMembers,
332 ObjCMethodDecl **clsMethods,
333 unsigned numClsMembers,
334 SourceLocation endLoc) {
335 NumInstanceMethods = numInsMembers;
336 if (numInsMembers) {
337 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
338 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
339 }
340 NumClassMethods = numClsMembers;
341 if (numClsMembers) {
342 ClassMethods = new ObjCMethodDecl*[numClsMembers];
343 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
344 }
345 AtEndLoc = endLoc;
346}
347
Chris Lattner321b5d12008-03-16 20:47:45 +0000348void ObjCCategoryDecl::setReferencedProtocolList(ObjCProtocolDecl **List,
349 unsigned NumRPs) {
350 assert(NumReferencedProtocols == 0 && "Protocol list already set");
351 if (NumRPs == 0) return;
352
353 ReferencedProtocols = new ObjCProtocolDecl*[NumRPs];
354 memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*));
355 NumReferencedProtocols = NumRPs;
356}
357
358
Chris Lattner10318b82008-03-16 00:19:01 +0000359/// addMethods - Insert instance and methods declarations into
360/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
361///
362void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
363 unsigned numInsMembers,
364 ObjCMethodDecl **clsMethods,
365 unsigned numClsMembers,
366 SourceLocation endLoc) {
367 NumInstanceMethods = numInsMembers;
368 if (numInsMembers) {
369 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
370 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
371 }
372 NumClassMethods = numClsMembers;
373 if (numClsMembers) {
374 ClassMethods = new ObjCMethodDecl*[numClsMembers];
375 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
376 }
377 AtEndLoc = endLoc;
378}
379
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000380/// FindPropertyDeclaration - Finds declaration of the property given its name
381/// in 'PropertyId' and returns it. It returns 0, if not found.
382///
383ObjCPropertyDecl *
384ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
385 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
386 E = classprop_end(); I != E; ++I) {
387 ObjCPropertyDecl *property = *I;
388 if (property->getIdentifier() == PropertyId)
389 return property;
390 }
391 return 0;
392}
393
Chris Lattner10318b82008-03-16 00:19:01 +0000394ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
395 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
396 ObjCInterfaceDecl* ClassDecl = this;
397 while (ClassDecl != NULL) {
398 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
399 I != E; ++I) {
400 if ((*I)->getIdentifier() == ID) {
401 clsDeclared = ClassDecl;
402 return *I;
403 }
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, its categories, and its super classes (using a linear search).
412ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
413 ObjCInterfaceDecl* ClassDecl = this;
414 ObjCMethodDecl *MethodDecl = 0;
415
416 while (ClassDecl != NULL) {
417 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
418 return MethodDecl;
419
420 // Didn't find one yet - look through protocols.
421 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
422 int numProtocols = ClassDecl->getNumIntfRefProtocols();
423 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
424 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
425 return MethodDecl;
426 }
427 // Didn't find one yet - now look through categories.
428 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
429 while (CatDecl) {
430 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
431 return MethodDecl;
432 CatDecl = CatDecl->getNextClassCategory();
433 }
434 ClassDecl = ClassDecl->getSuperClass();
435 }
436 return NULL;
437}
438
439// lookupClassMethod - This method returns a class method by looking in the
440// class, its categories, and its super classes (using a linear search).
441ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
442 ObjCInterfaceDecl* ClassDecl = this;
443 ObjCMethodDecl *MethodDecl = 0;
444
445 while (ClassDecl != NULL) {
446 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
447 return MethodDecl;
448
449 // Didn't find one yet - look through protocols.
450 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
451 int numProtocols = ClassDecl->getNumIntfRefProtocols();
452 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
453 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
454 return MethodDecl;
455 }
456 // Didn't find one yet - now look through categories.
457 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
458 while (CatDecl) {
459 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
460 return MethodDecl;
461 CatDecl = CatDecl->getNextClassCategory();
462 }
463 ClassDecl = ClassDecl->getSuperClass();
464 }
465 return NULL;
466}
467
468/// lookupInstanceMethod - This method returns an instance method by looking in
469/// the class implementation. Unlike interfaces, we don't look outside the
470/// implementation.
471ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
472 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
473 if ((*I)->getSelector() == Sel)
474 return *I;
475 return NULL;
476}
477
478/// lookupClassMethod - This method returns a class method by looking in
479/// the class implementation. Unlike interfaces, we don't look outside the
480/// implementation.
481ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
482 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
483 I != E; ++I)
484 if ((*I)->getSelector() == Sel)
485 return *I;
486 return NULL;
487}
488
489// lookupInstanceMethod - This method returns an instance method by looking in
490// the class implementation. Unlike interfaces, we don't look outside the
491// implementation.
492ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
493 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
494 if ((*I)->getSelector() == Sel)
495 return *I;
496 return NULL;
497}
498
499// lookupClassMethod - This method returns an instance method by looking in
500// the class implementation. Unlike interfaces, we don't look outside the
501// implementation.
502ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
503 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
504 I != E; ++I)
505 if ((*I)->getSelector() == Sel)
506 return *I;
507 return NULL;
508}
509
510// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
511// it inherited.
512ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
513 ObjCMethodDecl *MethodDecl = NULL;
514
515 if ((MethodDecl = getInstanceMethod(Sel)))
516 return MethodDecl;
517
518 if (getNumReferencedProtocols() > 0) {
519 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
520
521 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
522 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
523 return MethodDecl;
524 }
525 }
526 return NULL;
527}
528
529// lookupInstanceMethod - Lookup a class method in the protocol and protocols
530// it inherited.
531ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
532 ObjCMethodDecl *MethodDecl = NULL;
533
534 if ((MethodDecl = getClassMethod(Sel)))
535 return MethodDecl;
536
537 if (getNumReferencedProtocols() > 0) {
538 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
539
540 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
541 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
542 return MethodDecl;
543 }
544 }
545 return NULL;
546}
547
548/// getSynthesizedMethodSize - Compute size of synthesized method name
549/// as done be the rewrite.
550///
551unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
552 // syntesized method name is a concatenation of -/+[class-name selector]
553 // Get length of this name.
554 unsigned length = 3; // _I_ or _C_
555 length += strlen(getClassInterface()->getName()) +1; // extra for _
556 NamedDecl *MethodContext = getMethodContext();
557 if (ObjCCategoryImplDecl *CID =
558 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
559 length += strlen(CID->getName()) +1;
560 length += getSelector().getName().size(); // selector name
561 return length;
562}
563
Chris Lattner052fbcd2008-04-06 05:25:03 +0000564ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner10318b82008-03-16 00:19:01 +0000565 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
566 return ID;
567 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
568 return CD->getClassInterface();
569 if (ObjCImplementationDecl *IMD =
Chris Lattner052fbcd2008-04-06 05:25:03 +0000570 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000571 return IMD->getClassInterface();
Chris Lattner052fbcd2008-04-06 05:25:03 +0000572 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000573 return CID->getClassInterface();
574 assert(false && "unknown method context");
575 return 0;
576}
Chris Lattner44859612008-03-17 01:19:02 +0000577
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000578ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
579 SourceLocation atLoc,
580 SourceLocation L,
581 ObjCPropertyDecl *property,
582 PropertyImplKind kind,
583 ObjCIvarDecl *ivar) {
584 void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>();
585 return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, kind, ivar);
586}
Chris Lattner44859612008-03-17 01:19:02 +0000587
Chris Lattnereee57c02008-04-04 06:12:32 +0000588