blob: f3d81a1aa509f08eca4748c34bd08b23c6c173bd [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,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +000029 bool isSynthesized,
Chris Lattnerf7355832008-03-16 00:58:16 +000030 ImplementationControl impControl) {
Chris Lattner114add62008-03-16 00:49:28 +000031 void *Mem = C.getAllocator().Allocate<ObjCMethodDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000032 return new (Mem) ObjCMethodDecl(beginLoc, endLoc,
33 SelInfo, T, contextDecl,
Chris Lattnerf7355832008-03-16 00:58:16 +000034 M, isInstance,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +000035 isVariadic, isSynthesized, impControl);
Chris Lattner0db541b2008-03-16 01:15:50 +000036}
Chris Lattner114add62008-03-16 00:49:28 +000037
Ted Kremenek8c945b12008-06-06 16:45:15 +000038ObjCMethodDecl::~ObjCMethodDecl() {
39 delete [] ParamInfo;
40 //delete [] MethodAttrs; // FIXME: Also destroy the stored Expr*.
41}
42
43void ObjCMethodDecl::Destroy(ASTContext& C) {
44 if (Body) Body->Destroy(C);
45 if (SelfDecl) SelfDecl->Destroy(C);
46
47 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
48 if (*I) (*I)->Destroy(C);
49
50 Decl::Destroy(C);
51}
52
Chris Lattnereee57c02008-04-04 06:12:32 +000053ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
54 SourceLocation atLoc,
Chris Lattner0db541b2008-03-16 01:15:50 +000055 unsigned numRefProtos,
Steve Naroff7c371742008-04-11 19:35:35 +000056 IdentifierInfo *Id,
57 SourceLocation ClassLoc,
Chris Lattner0db541b2008-03-16 01:15:50 +000058 bool ForwardDecl, bool isInternal){
59 void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000060 return new (Mem) ObjCInterfaceDecl(atLoc, numRefProtos,
Steve Naroff7c371742008-04-11 19:35:35 +000061 Id, ClassLoc, ForwardDecl,
Chris Lattner0db541b2008-03-16 01:15:50 +000062 isInternal);
63}
64
Ted Kremenek8c945b12008-06-06 16:45:15 +000065ObjCInterfaceDecl::~ObjCInterfaceDecl() {
66 delete [] Ivars;
67 delete [] InstanceMethods;
68 delete [] ClassMethods;
69 delete [] PropertyDecl;
70 // FIXME: CategoryList?
71}
72
73void ObjCInterfaceDecl::Destroy(ASTContext& C) {
74 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
75 if (*I) (*I)->Destroy(C);
76
77 for (instmeth_iterator I=instmeth_begin(), E=instmeth_end(); I!=E; ++I)
78 if (*I) (*I)->Destroy(C);
79
80 for (classmeth_iterator I=classmeth_begin(), E=classmeth_end(); I!=E; ++I)
81 if (*I) (*I)->Destroy(C);
82
83 // FIXME: Cannot destroy properties right now because the properties of
84 // both the super class and this class are in this array. This can
85 // cause double-deletions.
86 //for (classprop_iterator I=classprop_begin(), E=classprop_end(); I!=E; ++I)
87// if (*I) (*I)->Destroy(C);
88//
89 Decl::Destroy(C);
90}
91
92
Chris Lattnerf3874bc2008-04-06 04:47:34 +000093ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner0db541b2008-03-16 01:15:50 +000094 IdentifierInfo *Id, QualType T) {
95 void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +000096 return new (Mem) ObjCIvarDecl(L, Id, T);
Chris Lattner114add62008-03-16 00:49:28 +000097}
98
Chris Lattnereee57c02008-04-04 06:12:32 +000099ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
100 SourceLocation L,
Chris Lattner180f7e22008-03-16 01:23:04 +0000101 unsigned numRefProtos,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000102 IdentifierInfo *Id) {
Chris Lattner180f7e22008-03-16 01:23:04 +0000103 void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
Chris Lattner7afba9c2008-03-16 20:19:15 +0000104 return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id);
Chris Lattner180f7e22008-03-16 01:23:04 +0000105}
106
Chris Lattnereee57c02008-04-04 06:12:32 +0000107ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C,
108 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000109 ObjCInterfaceDecl **Elts, unsigned nElts) {
110 void *Mem = C.getAllocator().Allocate<ObjCClassDecl>();
111 return new (Mem) ObjCClassDecl(L, Elts, nElts);
112}
113
114ObjCForwardProtocolDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000115ObjCForwardProtocolDecl::Create(ASTContext &C,
116 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000117 ObjCProtocolDecl **Elts, unsigned NumElts) {
118 void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>();
119 return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts);
120}
121
Chris Lattnereee57c02008-04-04 06:12:32 +0000122ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C,
123 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000124 IdentifierInfo *Id) {
125 void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
Chris Lattner321b5d12008-03-16 20:47:45 +0000126 return new (Mem) ObjCCategoryDecl(L, Id);
Chris Lattnere29dc832008-03-16 20:34:23 +0000127}
128
Chris Lattner1b6de332008-03-16 20:53:07 +0000129ObjCCategoryImplDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000130ObjCCategoryImplDecl::Create(ASTContext &C,
131 SourceLocation L,IdentifierInfo *Id,
Chris Lattner1b6de332008-03-16 20:53:07 +0000132 ObjCInterfaceDecl *ClassInterface) {
133 void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>();
134 return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface);
135}
136
137ObjCImplementationDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000138ObjCImplementationDecl::Create(ASTContext &C,
139 SourceLocation L,
Chris Lattner1b6de332008-03-16 20:53:07 +0000140 IdentifierInfo *Id,
141 ObjCInterfaceDecl *ClassInterface,
142 ObjCInterfaceDecl *SuperDecl) {
143 void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>();
144 return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl);
145}
Chris Lattner10318b82008-03-16 00:19:01 +0000146
Chris Lattner2d1c4312008-03-16 21:17:37 +0000147ObjCCompatibleAliasDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000148ObjCCompatibleAliasDecl::Create(ASTContext &C,
149 SourceLocation L,
Chris Lattner2d1c4312008-03-16 21:17:37 +0000150 IdentifierInfo *Id,
151 ObjCInterfaceDecl* AliasedClass) {
152 void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>();
153 return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass);
154}
155
Chris Lattnereee57c02008-04-04 06:12:32 +0000156ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000157 SourceLocation L,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000158 IdentifierInfo *Id,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000159 QualType T,
160 PropertyControl propControl) {
Chris Lattner2d1c4312008-03-16 21:17:37 +0000161 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000162 return new (Mem) ObjCPropertyDecl(L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000163}
164
Chris Lattner10318b82008-03-16 00:19:01 +0000165//===----------------------------------------------------------------------===//
166// Objective-C Decl Implementation
167//===----------------------------------------------------------------------===//
168
169void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
170 unsigned NumParams) {
171 assert(ParamInfo == 0 && "Already has param info!");
172
173 // Zero params -> null pointer.
174 if (NumParams) {
175 ParamInfo = new ParmVarDecl*[NumParams];
176 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
177 NumMethodParams = NumParams;
178 }
179}
180
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000181/// FindPropertyDeclaration - Finds declaration of the property given its name
182/// in 'PropertyId' and returns it. It returns 0, if not found.
183///
184ObjCPropertyDecl *
185 ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
186 for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(),
187 E = classprop_end(); I != E; ++I) {
188 ObjCPropertyDecl *property = *I;
189 if (property->getIdentifier() == PropertyId)
190 return property;
191 }
Steve Naroff30faf472008-06-04 04:46:04 +0000192 // Look through categories.
193 for (ObjCCategoryDecl *Category = getCategoryList();
194 Category; Category = Category->getNextClassCategory()) {
195 ObjCPropertyDecl *property = Category->FindPropertyDeclaration(PropertyId);
196 if (property)
197 return property;
198 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000199 // Look through protocols.
200 for (ObjCInterfaceDecl::protocol_iterator I = protocol_begin(),
201 E = protocol_end(); I != E; ++I) {
202 ObjCProtocolDecl *Protocol = *I;
203 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
204 if (property)
205 return property;
206 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000207 if (getSuperClass())
208 return getSuperClass()->FindPropertyDeclaration(PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000209 return 0;
210}
211
212/// FindCategoryDeclaration - Finds category declaration in the list of
213/// categories for this class and returns it. Name of the category is passed
214/// in 'CategoryId'. If category not found, return 0;
215///
216ObjCCategoryDecl *
217 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
218 for (ObjCCategoryDecl *Category = getCategoryList();
219 Category; Category = Category->getNextClassCategory())
220 if (Category->getIdentifier() == CategoryId)
221 return Category;
222 return 0;
223}
224
225/// FindIvarDeclaration - Find an Ivar declaration in this class given its
226/// name in 'IvarId'. On failure to find, return 0;
227///
228ObjCIvarDecl *
229 ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const {
230 for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(),
231 IVE = ivar_end(); IVI != IVE; ++IVI) {
232 ObjCIvarDecl* Ivar = (*IVI);
233 if (Ivar->getIdentifier() == IvarId)
234 return Ivar;
235 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000236 if (getSuperClass())
237 return getSuperClass()->FindIvarDeclaration(IvarId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000238 return 0;
239}
240
Chris Lattner10318b82008-03-16 00:19:01 +0000241/// ObjCAddInstanceVariablesToClass - Inserts instance variables
242/// into ObjCInterfaceDecl's fields.
243///
244void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
245 unsigned numIvars,
246 SourceLocation RBrac) {
247 NumIvars = numIvars;
248 if (numIvars) {
249 Ivars = new ObjCIvarDecl*[numIvars];
250 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
251 }
252 setLocEnd(RBrac);
253}
254
255/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
256/// Variables (Ivars) relative to what declared in @implementation;s class.
257/// Ivars into ObjCImplementationDecl's fields.
258///
259void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
260 ObjCIvarDecl **ivars, unsigned numIvars) {
261 NumIvars = numIvars;
262 if (numIvars) {
263 Ivars = new ObjCIvarDecl*[numIvars];
264 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
265 }
266}
267
268/// addMethods - Insert instance and methods declarations into
269/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
270///
271void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
272 unsigned numInsMembers,
273 ObjCMethodDecl **clsMethods,
274 unsigned numClsMembers,
275 SourceLocation endLoc) {
276 NumInstanceMethods = numInsMembers;
277 if (numInsMembers) {
278 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
279 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
280 }
281 NumClassMethods = numClsMembers;
282 if (numClsMembers) {
283 ClassMethods = new ObjCMethodDecl*[numClsMembers];
284 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
285 }
286 AtEndLoc = endLoc;
287}
288
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000289/// addProperties - Insert property declaration AST nodes into
290/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattnercffe3662008-03-16 21:23:50 +0000291///
292void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
293 unsigned NumProperties) {
294 if (NumProperties == 0) return;
295
296 NumPropertyDecl = NumProperties;
297 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
298 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
299}
300
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000301/// mergeProperties - Adds properties to the end of list of current properties
302/// for this class.
303
304void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties,
305 unsigned NumNewProperties) {
306 if (NumNewProperties == 0) return;
307
308 if (PropertyDecl) {
309 ObjCPropertyDecl **newPropertyDecl =
310 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
311 ObjCPropertyDecl **buf = newPropertyDecl;
312 // put back original properties in buffer.
313 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
314 // Add new properties to this buffer.
315 memcpy(buf+NumPropertyDecl, Properties,
316 NumNewProperties*sizeof(ObjCPropertyDecl*));
Nuno Lopes1ee78042008-05-10 10:31:54 +0000317 delete[] PropertyDecl;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000318 PropertyDecl = newPropertyDecl;
319 NumPropertyDecl += NumNewProperties;
320 }
321 else {
Nuno Lopes1ee78042008-05-10 10:31:54 +0000322 addProperties(Properties, NumNewProperties);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000323 }
324}
325
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000326/// addPropertyMethods - Goes through list of properties declared in this class
327/// and builds setter/getter method declartions depending on the setter/getter
328/// attributes of the property.
329///
330void ObjCInterfaceDecl::addPropertyMethods(
331 ASTContext &Context,
332 ObjCPropertyDecl *property,
333 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods) {
334 // Find the default getter and if one not found, add one.
335 ObjCMethodDecl *GetterDecl = getInstanceMethod(property->getGetterName());
336 if (GetterDecl) {
337 // An instance method with same name as property getter name found.
338 property->setGetterMethodDecl(GetterDecl);
339 }
340 else {
341 // No instance method of same name as property getter name was found.
342 // Declare a getter method and add it to the list of methods
343 // for this class.
344 QualType resultDeclType = property->getType();
345 ObjCMethodDecl* ObjCMethod =
346 ObjCMethodDecl::Create(Context, property->getLocation(),
347 property->getLocation(),
348 property->getGetterName(), resultDeclType,
349 this, 0,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000350 true, false, true, ObjCMethodDecl::Required);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000351 property->setGetterMethodDecl(ObjCMethod);
352 insMethods.push_back(ObjCMethod);
353 }
354}
355
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000356/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000357/// ObjCProtocolDecl's PropertyDecl field.
358///
359void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
360 unsigned NumProperties) {
361 if (NumProperties == 0) return;
362
363 NumPropertyDecl = NumProperties;
364 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
365 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
366}
367
368/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian5742a0f2008-04-16 21:11:25 +0000369/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000370///
371void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
372 unsigned NumProperties) {
373 if (NumProperties == 0) return;
374
375 NumPropertyDecl = NumProperties;
376 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
377 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
378}
379
Chris Lattnercffe3662008-03-16 21:23:50 +0000380/// addMethods - Insert instance and methods declarations into
Chris Lattner10318b82008-03-16 00:19:01 +0000381/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
382///
383void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
384 unsigned numInsMembers,
385 ObjCMethodDecl **clsMethods,
386 unsigned numClsMembers,
387 SourceLocation endLoc) {
388 NumInstanceMethods = numInsMembers;
389 if (numInsMembers) {
390 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
391 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
392 }
393 NumClassMethods = numClsMembers;
394 if (numClsMembers) {
395 ClassMethods = new ObjCMethodDecl*[numClsMembers];
396 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
397 }
398 AtEndLoc = endLoc;
399}
400
Chris Lattner321b5d12008-03-16 20:47:45 +0000401void ObjCCategoryDecl::setReferencedProtocolList(ObjCProtocolDecl **List,
402 unsigned NumRPs) {
403 assert(NumReferencedProtocols == 0 && "Protocol list already set");
404 if (NumRPs == 0) return;
405
406 ReferencedProtocols = new ObjCProtocolDecl*[NumRPs];
407 memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*));
408 NumReferencedProtocols = NumRPs;
409}
410
411
Chris Lattner10318b82008-03-16 00:19:01 +0000412/// addMethods - Insert instance and methods declarations into
413/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
414///
415void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
416 unsigned numInsMembers,
417 ObjCMethodDecl **clsMethods,
418 unsigned numClsMembers,
419 SourceLocation endLoc) {
420 NumInstanceMethods = numInsMembers;
421 if (numInsMembers) {
422 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
423 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
424 }
425 NumClassMethods = numClsMembers;
426 if (numClsMembers) {
427 ClassMethods = new ObjCMethodDecl*[numClsMembers];
428 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
429 }
430 AtEndLoc = endLoc;
431}
432
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000433/// FindPropertyDeclaration - Finds declaration of the property given its name
434/// in 'PropertyId' and returns it. It returns 0, if not found.
435///
436ObjCPropertyDecl *
437ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
438 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
439 E = classprop_end(); I != E; ++I) {
440 ObjCPropertyDecl *property = *I;
441 if (property->getIdentifier() == PropertyId)
442 return property;
443 }
444 return 0;
445}
446
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000447/// FindPropertyDeclaration - Finds declaration of the property given its name
448/// in 'PropertyId' and returns it. It returns 0, if not found.
449///
450ObjCPropertyDecl *
451ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
452 for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(),
453 E = classprop_end(); I != E; ++I) {
454 ObjCPropertyDecl *property = *I;
455 if (property->getIdentifier() == PropertyId)
456 return property;
457 }
458 return 0;
459}
460
Chris Lattner10318b82008-03-16 00:19:01 +0000461ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
462 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
463 ObjCInterfaceDecl* ClassDecl = this;
464 while (ClassDecl != NULL) {
465 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
466 I != E; ++I) {
467 if ((*I)->getIdentifier() == ID) {
468 clsDeclared = ClassDecl;
469 return *I;
470 }
471 }
472 ClassDecl = ClassDecl->getSuperClass();
473 }
474 return NULL;
475}
476
477/// lookupInstanceMethod - This method returns an instance method by looking in
478/// the class, its categories, and its super classes (using a linear search).
479ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
480 ObjCInterfaceDecl* ClassDecl = this;
481 ObjCMethodDecl *MethodDecl = 0;
482
483 while (ClassDecl != NULL) {
484 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
485 return MethodDecl;
486
487 // Didn't find one yet - look through protocols.
488 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
489 int numProtocols = ClassDecl->getNumIntfRefProtocols();
490 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
491 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
492 return MethodDecl;
493 }
494 // Didn't find one yet - now look through categories.
495 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
496 while (CatDecl) {
497 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
498 return MethodDecl;
499 CatDecl = CatDecl->getNextClassCategory();
500 }
501 ClassDecl = ClassDecl->getSuperClass();
502 }
503 return NULL;
504}
505
506// lookupClassMethod - This method returns a class method by looking in the
507// class, its categories, and its super classes (using a linear search).
508ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
509 ObjCInterfaceDecl* ClassDecl = this;
510 ObjCMethodDecl *MethodDecl = 0;
511
512 while (ClassDecl != NULL) {
513 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
514 return MethodDecl;
515
516 // Didn't find one yet - look through protocols.
517 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
518 int numProtocols = ClassDecl->getNumIntfRefProtocols();
519 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
520 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
521 return MethodDecl;
522 }
523 // Didn't find one yet - now look through categories.
524 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
525 while (CatDecl) {
526 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
527 return MethodDecl;
528 CatDecl = CatDecl->getNextClassCategory();
529 }
530 ClassDecl = ClassDecl->getSuperClass();
531 }
532 return NULL;
533}
534
535/// lookupInstanceMethod - This method returns an instance method by looking in
536/// the class implementation. Unlike interfaces, we don't look outside the
537/// implementation.
538ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
539 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
540 if ((*I)->getSelector() == Sel)
541 return *I;
542 return NULL;
543}
544
545/// lookupClassMethod - This method returns a class method by looking in
546/// the class implementation. Unlike interfaces, we don't look outside the
547/// implementation.
548ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
549 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
550 I != E; ++I)
551 if ((*I)->getSelector() == Sel)
552 return *I;
553 return NULL;
554}
555
556// lookupInstanceMethod - This method returns an instance method by looking in
557// the class implementation. Unlike interfaces, we don't look outside the
558// implementation.
559ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
560 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
561 if ((*I)->getSelector() == Sel)
562 return *I;
563 return NULL;
564}
565
566// lookupClassMethod - This method returns an instance method by looking in
567// the class implementation. Unlike interfaces, we don't look outside the
568// implementation.
569ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
570 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
571 I != E; ++I)
572 if ((*I)->getSelector() == Sel)
573 return *I;
574 return NULL;
575}
576
577// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
578// it inherited.
579ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
580 ObjCMethodDecl *MethodDecl = NULL;
581
582 if ((MethodDecl = getInstanceMethod(Sel)))
583 return MethodDecl;
584
585 if (getNumReferencedProtocols() > 0) {
586 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
587
588 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
589 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
590 return MethodDecl;
591 }
592 }
593 return NULL;
594}
595
596// lookupInstanceMethod - Lookup a class method in the protocol and protocols
597// it inherited.
598ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
599 ObjCMethodDecl *MethodDecl = NULL;
600
601 if ((MethodDecl = getClassMethod(Sel)))
602 return MethodDecl;
603
604 if (getNumReferencedProtocols() > 0) {
605 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
606
607 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
608 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
609 return MethodDecl;
610 }
611 }
612 return NULL;
613}
614
615/// getSynthesizedMethodSize - Compute size of synthesized method name
616/// as done be the rewrite.
617///
618unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
619 // syntesized method name is a concatenation of -/+[class-name selector]
620 // Get length of this name.
621 unsigned length = 3; // _I_ or _C_
622 length += strlen(getClassInterface()->getName()) +1; // extra for _
623 NamedDecl *MethodContext = getMethodContext();
624 if (ObjCCategoryImplDecl *CID =
625 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
626 length += strlen(CID->getName()) +1;
627 length += getSelector().getName().size(); // selector name
628 return length;
629}
630
Chris Lattner052fbcd2008-04-06 05:25:03 +0000631ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner10318b82008-03-16 00:19:01 +0000632 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
633 return ID;
634 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
635 return CD->getClassInterface();
636 if (ObjCImplementationDecl *IMD =
Chris Lattner052fbcd2008-04-06 05:25:03 +0000637 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000638 return IMD->getClassInterface();
Chris Lattner052fbcd2008-04-06 05:25:03 +0000639 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000640 return CID->getClassInterface();
641 assert(false && "unknown method context");
642 return 0;
643}
Chris Lattner44859612008-03-17 01:19:02 +0000644
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000645ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
646 SourceLocation atLoc,
647 SourceLocation L,
648 ObjCPropertyDecl *property,
649 PropertyImplKind kind,
650 ObjCIvarDecl *ivar) {
651 void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>();
652 return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, kind, ivar);
653}
Chris Lattner44859612008-03-17 01:19:02 +0000654
Chris Lattnereee57c02008-04-04 06:12:32 +0000655