blob: b3fc131330eebb19302228c327425e4c446cede7 [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);
Ted Kremenek22db1602008-06-06 17:21:42 +000082
83 // FIXME: Because there is no clear ownership
84 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
85 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
86
Ted Kremenek8c945b12008-06-06 16:45:15 +000087 Decl::Destroy(C);
88}
89
90
Chris Lattnerf3874bc2008-04-06 04:47:34 +000091ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner0db541b2008-03-16 01:15:50 +000092 IdentifierInfo *Id, QualType T) {
93 void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +000094 return new (Mem) ObjCIvarDecl(L, Id, T);
Chris Lattner114add62008-03-16 00:49:28 +000095}
96
Chris Lattnereee57c02008-04-04 06:12:32 +000097ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C,
98 SourceLocation L,
Chris Lattner180f7e22008-03-16 01:23:04 +000099 unsigned numRefProtos,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000100 IdentifierInfo *Id) {
Chris Lattner180f7e22008-03-16 01:23:04 +0000101 void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
Chris Lattner7afba9c2008-03-16 20:19:15 +0000102 return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id);
Chris Lattner180f7e22008-03-16 01:23:04 +0000103}
104
Chris Lattnereee57c02008-04-04 06:12:32 +0000105ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C,
106 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000107 ObjCInterfaceDecl **Elts, unsigned nElts) {
108 void *Mem = C.getAllocator().Allocate<ObjCClassDecl>();
109 return new (Mem) ObjCClassDecl(L, Elts, nElts);
110}
111
112ObjCForwardProtocolDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000113ObjCForwardProtocolDecl::Create(ASTContext &C,
114 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000115 ObjCProtocolDecl **Elts, unsigned NumElts) {
116 void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>();
117 return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts);
118}
119
Chris Lattnereee57c02008-04-04 06:12:32 +0000120ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C,
121 SourceLocation L,
Chris Lattnere29dc832008-03-16 20:34:23 +0000122 IdentifierInfo *Id) {
123 void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
Chris Lattner321b5d12008-03-16 20:47:45 +0000124 return new (Mem) ObjCCategoryDecl(L, Id);
Chris Lattnere29dc832008-03-16 20:34:23 +0000125}
126
Chris Lattner1b6de332008-03-16 20:53:07 +0000127ObjCCategoryImplDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000128ObjCCategoryImplDecl::Create(ASTContext &C,
129 SourceLocation L,IdentifierInfo *Id,
Chris Lattner1b6de332008-03-16 20:53:07 +0000130 ObjCInterfaceDecl *ClassInterface) {
131 void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>();
132 return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface);
133}
134
135ObjCImplementationDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000136ObjCImplementationDecl::Create(ASTContext &C,
137 SourceLocation L,
Chris Lattner1b6de332008-03-16 20:53:07 +0000138 IdentifierInfo *Id,
139 ObjCInterfaceDecl *ClassInterface,
140 ObjCInterfaceDecl *SuperDecl) {
141 void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>();
142 return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl);
143}
Chris Lattner10318b82008-03-16 00:19:01 +0000144
Chris Lattner2d1c4312008-03-16 21:17:37 +0000145ObjCCompatibleAliasDecl *
Chris Lattnereee57c02008-04-04 06:12:32 +0000146ObjCCompatibleAliasDecl::Create(ASTContext &C,
147 SourceLocation L,
Chris Lattner2d1c4312008-03-16 21:17:37 +0000148 IdentifierInfo *Id,
149 ObjCInterfaceDecl* AliasedClass) {
150 void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>();
151 return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass);
152}
153
Chris Lattnereee57c02008-04-04 06:12:32 +0000154ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C,
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000155 SourceLocation L,
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000156 IdentifierInfo *Id,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000157 QualType T,
158 PropertyControl propControl) {
Chris Lattner2d1c4312008-03-16 21:17:37 +0000159 void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000160 return new (Mem) ObjCPropertyDecl(L, Id, T);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000161}
162
Chris Lattner10318b82008-03-16 00:19:01 +0000163//===----------------------------------------------------------------------===//
164// Objective-C Decl Implementation
165//===----------------------------------------------------------------------===//
166
167void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
168 unsigned NumParams) {
169 assert(ParamInfo == 0 && "Already has param info!");
170
171 // Zero params -> null pointer.
172 if (NumParams) {
173 ParamInfo = new ParmVarDecl*[NumParams];
174 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
175 NumMethodParams = NumParams;
176 }
177}
178
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000179/// FindPropertyDeclaration - Finds declaration of the property given its name
180/// in 'PropertyId' and returns it. It returns 0, if not found.
181///
182ObjCPropertyDecl *
183 ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
184 for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(),
185 E = classprop_end(); I != E; ++I) {
186 ObjCPropertyDecl *property = *I;
187 if (property->getIdentifier() == PropertyId)
188 return property;
189 }
Steve Naroff30faf472008-06-04 04:46:04 +0000190 // Look through categories.
191 for (ObjCCategoryDecl *Category = getCategoryList();
192 Category; Category = Category->getNextClassCategory()) {
193 ObjCPropertyDecl *property = Category->FindPropertyDeclaration(PropertyId);
194 if (property)
195 return property;
196 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000197 // Look through protocols.
198 for (ObjCInterfaceDecl::protocol_iterator I = protocol_begin(),
199 E = protocol_end(); I != E; ++I) {
200 ObjCProtocolDecl *Protocol = *I;
201 ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
202 if (property)
203 return property;
204 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000205 if (getSuperClass())
206 return getSuperClass()->FindPropertyDeclaration(PropertyId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000207 return 0;
208}
209
210/// FindCategoryDeclaration - Finds category declaration in the list of
211/// categories for this class and returns it. Name of the category is passed
212/// in 'CategoryId'. If category not found, return 0;
213///
214ObjCCategoryDecl *
215 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
216 for (ObjCCategoryDecl *Category = getCategoryList();
217 Category; Category = Category->getNextClassCategory())
218 if (Category->getIdentifier() == CategoryId)
219 return Category;
220 return 0;
221}
222
223/// FindIvarDeclaration - Find an Ivar declaration in this class given its
224/// name in 'IvarId'. On failure to find, return 0;
225///
226ObjCIvarDecl *
227 ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const {
228 for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(),
229 IVE = ivar_end(); IVI != IVE; ++IVI) {
230 ObjCIvarDecl* Ivar = (*IVI);
231 if (Ivar->getIdentifier() == IvarId)
232 return Ivar;
233 }
Fariborz Jahanian8acf3352008-04-21 23:57:08 +0000234 if (getSuperClass())
235 return getSuperClass()->FindIvarDeclaration(IvarId);
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000236 return 0;
237}
238
Chris Lattner10318b82008-03-16 00:19:01 +0000239/// ObjCAddInstanceVariablesToClass - Inserts instance variables
240/// into ObjCInterfaceDecl's fields.
241///
242void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
243 unsigned numIvars,
244 SourceLocation RBrac) {
245 NumIvars = numIvars;
246 if (numIvars) {
247 Ivars = new ObjCIvarDecl*[numIvars];
248 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
249 }
250 setLocEnd(RBrac);
251}
252
253/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
254/// Variables (Ivars) relative to what declared in @implementation;s class.
255/// Ivars into ObjCImplementationDecl's fields.
256///
257void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
258 ObjCIvarDecl **ivars, unsigned numIvars) {
259 NumIvars = numIvars;
260 if (numIvars) {
261 Ivars = new ObjCIvarDecl*[numIvars];
262 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
263 }
264}
265
266/// addMethods - Insert instance and methods declarations into
267/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
268///
269void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
270 unsigned numInsMembers,
271 ObjCMethodDecl **clsMethods,
272 unsigned numClsMembers,
273 SourceLocation endLoc) {
274 NumInstanceMethods = numInsMembers;
275 if (numInsMembers) {
276 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
277 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
278 }
279 NumClassMethods = numClsMembers;
280 if (numClsMembers) {
281 ClassMethods = new ObjCMethodDecl*[numClsMembers];
282 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
283 }
284 AtEndLoc = endLoc;
285}
286
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000287/// addProperties - Insert property declaration AST nodes into
288/// ObjCInterfaceDecl's PropertyDecl field.
Chris Lattnercffe3662008-03-16 21:23:50 +0000289///
290void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties,
291 unsigned NumProperties) {
292 if (NumProperties == 0) return;
293
294 NumPropertyDecl = NumProperties;
295 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
296 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
297}
298
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000299/// mergeProperties - Adds properties to the end of list of current properties
300/// for this class.
301
302void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties,
303 unsigned NumNewProperties) {
304 if (NumNewProperties == 0) return;
305
306 if (PropertyDecl) {
307 ObjCPropertyDecl **newPropertyDecl =
308 new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl];
309 ObjCPropertyDecl **buf = newPropertyDecl;
310 // put back original properties in buffer.
311 memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*));
312 // Add new properties to this buffer.
313 memcpy(buf+NumPropertyDecl, Properties,
314 NumNewProperties*sizeof(ObjCPropertyDecl*));
Nuno Lopes1ee78042008-05-10 10:31:54 +0000315 delete[] PropertyDecl;
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000316 PropertyDecl = newPropertyDecl;
317 NumPropertyDecl += NumNewProperties;
318 }
319 else {
Nuno Lopes1ee78042008-05-10 10:31:54 +0000320 addProperties(Properties, NumNewProperties);
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000321 }
322}
323
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000324/// addPropertyMethods - Goes through list of properties declared in this class
325/// and builds setter/getter method declartions depending on the setter/getter
326/// attributes of the property.
327///
328void ObjCInterfaceDecl::addPropertyMethods(
329 ASTContext &Context,
330 ObjCPropertyDecl *property,
331 llvm::SmallVector<ObjCMethodDecl*, 32> &insMethods) {
332 // Find the default getter and if one not found, add one.
333 ObjCMethodDecl *GetterDecl = getInstanceMethod(property->getGetterName());
334 if (GetterDecl) {
335 // An instance method with same name as property getter name found.
336 property->setGetterMethodDecl(GetterDecl);
337 }
338 else {
339 // No instance method of same name as property getter name was found.
340 // Declare a getter method and add it to the list of methods
341 // for this class.
342 QualType resultDeclType = property->getType();
343 ObjCMethodDecl* ObjCMethod =
344 ObjCMethodDecl::Create(Context, property->getLocation(),
345 property->getLocation(),
346 property->getGetterName(), resultDeclType,
347 this, 0,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000348 true, false, true, ObjCMethodDecl::Required);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000349 property->setGetterMethodDecl(ObjCMethod);
350 insMethods.push_back(ObjCMethod);
351 }
352}
353
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000354/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000355/// ObjCProtocolDecl's PropertyDecl field.
356///
357void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties,
358 unsigned NumProperties) {
359 if (NumProperties == 0) return;
360
361 NumPropertyDecl = NumProperties;
362 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
363 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
364}
365
366/// addProperties - Insert property declaration AST nodes into
Fariborz Jahanian5742a0f2008-04-16 21:11:25 +0000367/// ObjCCategoryDecl's PropertyDecl field.
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000368///
369void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties,
370 unsigned NumProperties) {
371 if (NumProperties == 0) return;
372
373 NumPropertyDecl = NumProperties;
374 PropertyDecl = new ObjCPropertyDecl*[NumProperties];
375 memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*));
376}
377
Chris Lattnercffe3662008-03-16 21:23:50 +0000378/// addMethods - Insert instance and methods declarations into
Chris Lattner10318b82008-03-16 00:19:01 +0000379/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
380///
381void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
382 unsigned numInsMembers,
383 ObjCMethodDecl **clsMethods,
384 unsigned numClsMembers,
385 SourceLocation endLoc) {
386 NumInstanceMethods = numInsMembers;
387 if (numInsMembers) {
388 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
389 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
390 }
391 NumClassMethods = numClsMembers;
392 if (numClsMembers) {
393 ClassMethods = new ObjCMethodDecl*[numClsMembers];
394 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
395 }
396 AtEndLoc = endLoc;
397}
398
Chris Lattner321b5d12008-03-16 20:47:45 +0000399void ObjCCategoryDecl::setReferencedProtocolList(ObjCProtocolDecl **List,
400 unsigned NumRPs) {
401 assert(NumReferencedProtocols == 0 && "Protocol list already set");
402 if (NumRPs == 0) return;
403
404 ReferencedProtocols = new ObjCProtocolDecl*[NumRPs];
405 memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*));
406 NumReferencedProtocols = NumRPs;
407}
408
409
Chris Lattner10318b82008-03-16 00:19:01 +0000410/// addMethods - Insert instance and methods declarations into
411/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
412///
413void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
414 unsigned numInsMembers,
415 ObjCMethodDecl **clsMethods,
416 unsigned numClsMembers,
417 SourceLocation endLoc) {
418 NumInstanceMethods = numInsMembers;
419 if (numInsMembers) {
420 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
421 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
422 }
423 NumClassMethods = numClsMembers;
424 if (numClsMembers) {
425 ClassMethods = new ObjCMethodDecl*[numClsMembers];
426 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
427 }
428 AtEndLoc = endLoc;
429}
430
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000431/// FindPropertyDeclaration - Finds declaration of the property given its name
432/// in 'PropertyId' and returns it. It returns 0, if not found.
433///
434ObjCPropertyDecl *
435ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
436 for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(),
437 E = classprop_end(); I != E; ++I) {
438 ObjCPropertyDecl *property = *I;
439 if (property->getIdentifier() == PropertyId)
440 return property;
441 }
442 return 0;
443}
444
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000445/// FindPropertyDeclaration - Finds declaration of the property given its name
446/// in 'PropertyId' and returns it. It returns 0, if not found.
447///
448ObjCPropertyDecl *
449ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
450 for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(),
451 E = classprop_end(); I != E; ++I) {
452 ObjCPropertyDecl *property = *I;
453 if (property->getIdentifier() == PropertyId)
454 return property;
455 }
456 return 0;
457}
458
Chris Lattner10318b82008-03-16 00:19:01 +0000459ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
460 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
461 ObjCInterfaceDecl* ClassDecl = this;
462 while (ClassDecl != NULL) {
463 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
464 I != E; ++I) {
465 if ((*I)->getIdentifier() == ID) {
466 clsDeclared = ClassDecl;
467 return *I;
468 }
469 }
470 ClassDecl = ClassDecl->getSuperClass();
471 }
472 return NULL;
473}
474
475/// lookupInstanceMethod - This method returns an instance method by looking in
476/// the class, its categories, and its super classes (using a linear search).
477ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
478 ObjCInterfaceDecl* ClassDecl = this;
479 ObjCMethodDecl *MethodDecl = 0;
480
481 while (ClassDecl != NULL) {
482 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
483 return MethodDecl;
484
485 // Didn't find one yet - look through protocols.
486 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
487 int numProtocols = ClassDecl->getNumIntfRefProtocols();
488 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
489 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
490 return MethodDecl;
491 }
492 // Didn't find one yet - now look through categories.
493 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
494 while (CatDecl) {
495 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
496 return MethodDecl;
497 CatDecl = CatDecl->getNextClassCategory();
498 }
499 ClassDecl = ClassDecl->getSuperClass();
500 }
501 return NULL;
502}
503
504// lookupClassMethod - This method returns a class method by looking in the
505// class, its categories, and its super classes (using a linear search).
506ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
507 ObjCInterfaceDecl* ClassDecl = this;
508 ObjCMethodDecl *MethodDecl = 0;
509
510 while (ClassDecl != NULL) {
511 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
512 return MethodDecl;
513
514 // Didn't find one yet - look through protocols.
515 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
516 int numProtocols = ClassDecl->getNumIntfRefProtocols();
517 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
518 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
519 return MethodDecl;
520 }
521 // Didn't find one yet - now look through categories.
522 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
523 while (CatDecl) {
524 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
525 return MethodDecl;
526 CatDecl = CatDecl->getNextClassCategory();
527 }
528 ClassDecl = ClassDecl->getSuperClass();
529 }
530 return NULL;
531}
532
533/// lookupInstanceMethod - This method returns an instance method by looking in
534/// the class implementation. Unlike interfaces, we don't look outside the
535/// implementation.
536ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
537 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
538 if ((*I)->getSelector() == Sel)
539 return *I;
540 return NULL;
541}
542
543/// lookupClassMethod - This method returns a class method by looking in
544/// the class implementation. Unlike interfaces, we don't look outside the
545/// implementation.
546ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
547 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
548 I != E; ++I)
549 if ((*I)->getSelector() == Sel)
550 return *I;
551 return NULL;
552}
553
554// lookupInstanceMethod - This method returns an instance method by looking in
555// the class implementation. Unlike interfaces, we don't look outside the
556// implementation.
557ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
558 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
559 if ((*I)->getSelector() == Sel)
560 return *I;
561 return NULL;
562}
563
564// lookupClassMethod - This method returns an instance method by looking in
565// the class implementation. Unlike interfaces, we don't look outside the
566// implementation.
567ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
568 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
569 I != E; ++I)
570 if ((*I)->getSelector() == Sel)
571 return *I;
572 return NULL;
573}
574
575// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
576// it inherited.
577ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
578 ObjCMethodDecl *MethodDecl = NULL;
579
580 if ((MethodDecl = getInstanceMethod(Sel)))
581 return MethodDecl;
582
583 if (getNumReferencedProtocols() > 0) {
584 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
585
586 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
587 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
588 return MethodDecl;
589 }
590 }
591 return NULL;
592}
593
594// lookupInstanceMethod - Lookup a class method in the protocol and protocols
595// it inherited.
596ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
597 ObjCMethodDecl *MethodDecl = NULL;
598
599 if ((MethodDecl = getClassMethod(Sel)))
600 return MethodDecl;
601
602 if (getNumReferencedProtocols() > 0) {
603 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
604
605 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
606 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
607 return MethodDecl;
608 }
609 }
610 return NULL;
611}
612
613/// getSynthesizedMethodSize - Compute size of synthesized method name
614/// as done be the rewrite.
615///
616unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
617 // syntesized method name is a concatenation of -/+[class-name selector]
618 // Get length of this name.
619 unsigned length = 3; // _I_ or _C_
620 length += strlen(getClassInterface()->getName()) +1; // extra for _
621 NamedDecl *MethodContext = getMethodContext();
622 if (ObjCCategoryImplDecl *CID =
623 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
624 length += strlen(CID->getName()) +1;
625 length += getSelector().getName().size(); // selector name
626 return length;
627}
628
Chris Lattner052fbcd2008-04-06 05:25:03 +0000629ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Chris Lattner10318b82008-03-16 00:19:01 +0000630 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
631 return ID;
632 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
633 return CD->getClassInterface();
634 if (ObjCImplementationDecl *IMD =
Chris Lattner052fbcd2008-04-06 05:25:03 +0000635 dyn_cast<ObjCImplementationDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000636 return IMD->getClassInterface();
Chris Lattner052fbcd2008-04-06 05:25:03 +0000637 if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Chris Lattner10318b82008-03-16 00:19:01 +0000638 return CID->getClassInterface();
639 assert(false && "unknown method context");
640 return 0;
641}
Chris Lattner44859612008-03-17 01:19:02 +0000642
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000643ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
644 SourceLocation atLoc,
645 SourceLocation L,
646 ObjCPropertyDecl *property,
647 PropertyImplKind kind,
648 ObjCIvarDecl *ivar) {
649 void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>();
650 return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, kind, ivar);
651}
Chris Lattner44859612008-03-17 01:19:02 +0000652
Chris Lattnereee57c02008-04-04 06:12:32 +0000653