blob: 5a3c730a766f9f456d19fc0ccab969bc8d7e682a [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"
Daniel Dunbarde300732008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Chris Lattner10318b82008-03-16 00:19:01 +000017using namespace clang;
18
Chris Lattner114add62008-03-16 00:49:28 +000019//===----------------------------------------------------------------------===//
Chris Lattner217eb8c2009-02-20 21:16:26 +000020// ObjCListBase
21//===----------------------------------------------------------------------===//
22
23void ObjCListBase::Destroy() {
24 delete[] List;
25 NumElts = 0;
26 List = 0;
27}
28
29void ObjCListBase::set(void *const* InList, unsigned Elts) {
30 assert(List == 0 && "Elements already set!");
31 if (Elts == 0) return; // Setting to an empty list is a noop.
32
33 List = new void*[Elts];
34 NumElts = Elts;
35 memcpy(List, InList, sizeof(void*)*Elts);
36}
37
38
39//===----------------------------------------------------------------------===//
Chris Lattner13f63602009-02-20 20:59:54 +000040// ObjCInterfaceDecl
Chris Lattner114add62008-03-16 00:49:28 +000041//===----------------------------------------------------------------------===//
42
Steve Naroffab63fd62009-01-08 17:28:14 +000043// Get the local instance method declared in this interface.
44// FIXME: handle overloading, instance & class methods can have the same name.
45ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
46 lookup_const_result MethodResult = lookup(Sel);
47 if (MethodResult.first)
48 return const_cast<ObjCMethodDecl*>(
Chris Lattner13f63602009-02-20 20:59:54 +000049 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
Steve Naroffab63fd62009-01-08 17:28:14 +000050 return 0;
51}
52
53// Get the local class method declared in this interface.
54ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
55 lookup_const_result MethodResult = lookup(Sel);
56 if (MethodResult.first)
57 return const_cast<ObjCMethodDecl*>(
Chris Lattner13f63602009-02-20 20:59:54 +000058 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
Steve Naroffab63fd62009-01-08 17:28:14 +000059 return 0;
60}
61
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000062/// FindPropertyDeclaration - Finds declaration of the property given its name
63/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroffdcf1e842009-01-11 12:47:58 +000064/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000065///
66ObjCPropertyDecl *
Steve Naroff451f83c2009-01-09 15:36:25 +000067ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Chris Lattner13f63602009-02-20 20:59:54 +000068 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
69 if ((*I)->getIdentifier() == PropertyId)
70 return *I;
71
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000072 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
73 if (PID) {
Chris Lattner13f63602009-02-20 20:59:54 +000074 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
75 E = PID->protocol_end(); I != E; ++I)
76 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
77 return P;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000078 }
79
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +000080 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff451f83c2009-01-09 15:36:25 +000081 // Look through categories.
82 for (ObjCCategoryDecl *Category = OID->getCategoryList();
83 Category; Category = Category->getNextClassCategory()) {
Chris Lattner13f63602009-02-20 20:59:54 +000084 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
85 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +000086 }
87 // Look through protocols.
88 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
89 E = OID->protocol_end(); I != E; ++I) {
Chris Lattner13f63602009-02-20 20:59:54 +000090 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
91 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +000092 }
93 if (OID->getSuperClass())
94 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattner13f63602009-02-20 20:59:54 +000095 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +000096 // Look through protocols.
97 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
98 E = OCD->protocol_end(); I != E; ++I) {
Chris Lattner13f63602009-02-20 20:59:54 +000099 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
100 return P;
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000101 }
102 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000103 return 0;
104}
105
Chris Lattner10318b82008-03-16 00:19:01 +0000106ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
107 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
108 ObjCInterfaceDecl* ClassDecl = this;
109 while (ClassDecl != NULL) {
110 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
111 I != E; ++I) {
112 if ((*I)->getIdentifier() == ID) {
113 clsDeclared = ClassDecl;
114 return *I;
115 }
116 }
117 ClassDecl = ClassDecl->getSuperClass();
118 }
119 return NULL;
120}
121
122/// lookupInstanceMethod - This method returns an instance method by looking in
123/// the class, its categories, and its super classes (using a linear search).
124ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
125 ObjCInterfaceDecl* ClassDecl = this;
126 ObjCMethodDecl *MethodDecl = 0;
127
128 while (ClassDecl != NULL) {
129 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
130 return MethodDecl;
131
132 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000133 const ObjCList<ObjCProtocolDecl> &Protocols =
134 ClassDecl->getReferencedProtocols();
135 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
136 E = Protocols.end(); I != E; ++I)
137 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000138 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000139
Chris Lattner10318b82008-03-16 00:19:01 +0000140 // Didn't find one yet - now look through categories.
141 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
142 while (CatDecl) {
143 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
144 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000145
146 // Didn't find one yet - look through protocols.
147 const ObjCList<ObjCProtocolDecl> &Protocols =
148 CatDecl->getReferencedProtocols();
149 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
150 E = Protocols.end(); I != E; ++I)
151 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
152 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000153 CatDecl = CatDecl->getNextClassCategory();
154 }
155 ClassDecl = ClassDecl->getSuperClass();
156 }
157 return NULL;
158}
159
160// lookupClassMethod - This method returns a class method by looking in the
161// class, its categories, and its super classes (using a linear search).
162ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
163 ObjCInterfaceDecl* ClassDecl = this;
164 ObjCMethodDecl *MethodDecl = 0;
165
166 while (ClassDecl != NULL) {
167 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
168 return MethodDecl;
169
170 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000171 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
172 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner8bcb5252008-07-21 18:19:38 +0000173 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000174 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000175
Chris Lattner10318b82008-03-16 00:19:01 +0000176 // Didn't find one yet - now look through categories.
177 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
178 while (CatDecl) {
179 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
180 return MethodDecl;
181 CatDecl = CatDecl->getNextClassCategory();
182 }
183 ClassDecl = ClassDecl->getSuperClass();
184 }
185 return NULL;
186}
187
Chris Lattner13f63602009-02-20 20:59:54 +0000188
189
190//===----------------------------------------------------------------------===//
191// ObjCMethodDecl
192//===----------------------------------------------------------------------===//
193
194ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
195 SourceLocation beginLoc,
196 SourceLocation endLoc,
197 Selector SelInfo, QualType T,
198 DeclContext *contextDecl,
199 bool isInstance,
200 bool isVariadic,
201 bool isSynthesized,
202 ImplementationControl impControl) {
203 return new (C) ObjCMethodDecl(beginLoc, endLoc,
204 SelInfo, T, contextDecl,
205 isInstance,
206 isVariadic, isSynthesized, impControl);
Chris Lattner10318b82008-03-16 00:19:01 +0000207}
208
Chris Lattner13f63602009-02-20 20:59:54 +0000209void ObjCMethodDecl::Destroy(ASTContext& C) {
210 if (Body) Body->Destroy(C);
211 if (SelfDecl) SelfDecl->Destroy(C);
212
213 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
214 if (*I) (*I)->Destroy(C);
215
Chris Lattnerc3e72682009-02-20 21:02:11 +0000216 ParamInfo.Destroy();
Chris Lattner13f63602009-02-20 20:59:54 +0000217
218 Decl::Destroy(C);
Chris Lattner10318b82008-03-16 00:19:01 +0000219}
220
Chris Lattner13f63602009-02-20 20:59:54 +0000221void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
222 const ObjCInterfaceDecl *OID) {
223 QualType selfTy;
224 if (isInstanceMethod()) {
225 // There may be no interface context due to error in declaration
226 // of the interface (which has been reported). Recover gracefully.
227 if (OID) {
228 selfTy =Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(OID));
229 selfTy = Context.getPointerType(selfTy);
230 } else {
231 selfTy = Context.getObjCIdType();
232 }
233 } else // we have a factory method.
234 selfTy = Context.getObjCClassType();
235
236 SelfDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
237 &Context.Idents.get("self"), selfTy);
238
239 CmdDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
240 &Context.Idents.get("_cmd"),
241 Context.getObjCSelType());
242}
243
244
245
246/// getSynthesizedMethodSize - Compute size of synthesized method name
247/// as done be the rewrite.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000248///
Chris Lattner13f63602009-02-20 20:59:54 +0000249unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
250 // syntesized method name is a concatenation of -/+[class-name selector]
251 // Get length of this name.
252 unsigned length = 3; // _I_ or _C_
253 length += getClassInterface()->getNameAsString().size()+1; // extra for _
254 if (const ObjCCategoryImplDecl *CID =
255 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
256 length += CID->getNameAsString().size()+1;
257 length += getSelector().getAsString().size(); // selector name
258 return length;
259}
260
261ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
262 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
263 return ID;
264 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
265 return CD->getClassInterface();
266 if (ObjCImplementationDecl *IMD =
267 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
268 return IMD->getClassInterface();
269 if (ObjCCategoryImplDecl *CID =
270 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
271 return CID->getClassInterface();
272 assert(false && "unknown method context");
Fariborz Jahanian68342282008-12-05 22:32:48 +0000273 return 0;
274}
275
Chris Lattner13f63602009-02-20 20:59:54 +0000276//===----------------------------------------------------------------------===//
277// ObjCInterfaceDecl
278//===----------------------------------------------------------------------===//
279
280ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
281 DeclContext *DC,
282 SourceLocation atLoc,
283 IdentifierInfo *Id,
284 SourceLocation ClassLoc,
285 bool ForwardDecl, bool isInternal){
286 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
287 isInternal);
288}
289
290ObjCInterfaceDecl::
291ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
292 SourceLocation CLoc, bool FD, bool isInternal)
293 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
294 TypeForDecl(0), SuperClass(0),
295 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
296 ClassLoc(CLoc) {
297}
298
299void ObjCInterfaceDecl::Destroy(ASTContext &C) {
300 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
301 if (*I) (*I)->Destroy(C);
302
Chris Lattnerc3e72682009-02-20 21:02:11 +0000303 IVars.Destroy();
Chris Lattner13f63602009-02-20 20:59:54 +0000304 // FIXME: CategoryList?
305
306 // FIXME: Because there is no clear ownership
307 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
308 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
309 Decl::Destroy(C);
310}
311
312
313/// FindCategoryDeclaration - Finds category declaration in the list of
314/// categories for this class and returns it. Name of the category is passed
315/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000316///
Chris Lattner13f63602009-02-20 20:59:54 +0000317ObjCCategoryDecl *
318ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
319 for (ObjCCategoryDecl *Category = getCategoryList();
320 Category; Category = Category->getNextClassCategory())
321 if (Category->getIdentifier() == CategoryId)
322 return Category;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000323 return 0;
324}
325
Chris Lattner13f63602009-02-20 20:59:54 +0000326/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
327/// storage which matches this 'ivar'.
328///
329FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
330 const ObjCIvarDecl *IVar) {
331 const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
332 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
333 DeclarationName Member = IVar->getDeclName();
334 DeclContext::lookup_result Lookup =
335 (const_cast< RecordDecl *>(RecordForDecl))->lookup(Member);
336 assert((Lookup.first != Lookup.second) && "field decl not found");
337 FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first);
338 assert(MemberDecl && "field decl not found");
339 return MemberDecl;
340}
341
342//===----------------------------------------------------------------------===//
343// ObjCIvarDecl
344//===----------------------------------------------------------------------===//
345
346ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
347 SourceLocation L, IdentifierInfo *Id,
348 QualType T, AccessControl ac, Expr *BW) {
349 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
350}
351
352
353
354//===----------------------------------------------------------------------===//
355// ObjCAtDefsFieldDecl
356//===----------------------------------------------------------------------===//
357
358ObjCAtDefsFieldDecl
359*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
360 IdentifierInfo *Id, QualType T, Expr *BW) {
361 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
362}
363
364void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
365 this->~ObjCAtDefsFieldDecl();
366 C.Deallocate((void *)this);
367}
368
369//===----------------------------------------------------------------------===//
370// ObjCProtocolDecl
371//===----------------------------------------------------------------------===//
372
373ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
374 SourceLocation L,
375 IdentifierInfo *Id) {
376 return new (C) ObjCProtocolDecl(DC, L, Id);
377}
378
379void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerc3e72682009-02-20 21:02:11 +0000380 ReferencedProtocols.Destroy();
Chris Lattner13f63602009-02-20 20:59:54 +0000381 ObjCContainerDecl::Destroy(C);
382}
383
384// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
385// it inherited.
386ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
387 ObjCMethodDecl *MethodDecl = NULL;
388
389 if ((MethodDecl = getInstanceMethod(Sel)))
390 return MethodDecl;
391
392 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
393 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
394 return MethodDecl;
395 return NULL;
396}
397
398// lookupInstanceMethod - Lookup a class method in the protocol and protocols
399// it inherited.
400ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
401 ObjCMethodDecl *MethodDecl = NULL;
402
403 if ((MethodDecl = getClassMethod(Sel)))
404 return MethodDecl;
405
406 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
407 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
408 return MethodDecl;
409 return NULL;
410}
411
412//===----------------------------------------------------------------------===//
413// ObjCClassDecl
414//===----------------------------------------------------------------------===//
415
416ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
417 SourceLocation L,
418 ObjCInterfaceDecl *const *Elts,
419 unsigned nElts) {
420 return new (C) ObjCClassDecl(DC, L, Elts, nElts);
421}
422
423void ObjCClassDecl::Destroy(ASTContext &C) {
424
425 // FIXME: There is no clear ownership policy now for referenced
426 // ObjCInterfaceDecls. Some of them can be forward declarations that
427 // are never later defined (in which case the ObjCClassDecl owns them)
428 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
429 // we should have separate objects for forward declarations and definitions,
430 // obviating this problem. Because of this situation, referenced
431 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
432
Chris Lattnerc3e72682009-02-20 21:02:11 +0000433 ForwardDecls.Destroy();
Chris Lattner13f63602009-02-20 20:59:54 +0000434 Decl::Destroy(C);
435}
436
437//===----------------------------------------------------------------------===//
438// ObjCForwardProtocolDecl
439//===----------------------------------------------------------------------===//
440
441ObjCForwardProtocolDecl *
442ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
443 SourceLocation L,
444 ObjCProtocolDecl *const *Elts,
445 unsigned NumElts) {
446 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts);
447}
448
449ObjCForwardProtocolDecl::
450ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
451 ObjCProtocolDecl *const *Elts, unsigned nElts)
452 : Decl(ObjCForwardProtocol, DC, L) {
453 ReferencedProtocols.set(Elts, nElts);
454}
455
456void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerc3e72682009-02-20 21:02:11 +0000457 ReferencedProtocols.Destroy();
Chris Lattner13f63602009-02-20 20:59:54 +0000458 Decl::Destroy(C);
459}
460
461//===----------------------------------------------------------------------===//
462// ObjCCategoryDecl
463//===----------------------------------------------------------------------===//
464
465ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
466 SourceLocation L,
467 IdentifierInfo *Id) {
468 return new (C) ObjCCategoryDecl(DC, L, Id);
469}
470
471//===----------------------------------------------------------------------===//
472// ObjCCategoryImplDecl
473//===----------------------------------------------------------------------===//
474
475ObjCCategoryImplDecl *
476ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
477 SourceLocation L,IdentifierInfo *Id,
478 ObjCInterfaceDecl *ClassInterface) {
479 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
480}
481
482
Fariborz Jahanian68342282008-12-05 22:32:48 +0000483/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattner715e8482009-02-16 19:24:31 +0000484/// properties implemented in this category @implementation block and returns
485/// the implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000486///
Chris Lattner715e8482009-02-16 19:24:31 +0000487ObjCPropertyImplDecl *ObjCCategoryImplDecl::
488FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
489 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000490 ObjCPropertyImplDecl *PID = *i;
491 if (PID->getPropertyIvarDecl() &&
492 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
493 return PID;
494 }
495 return 0;
496}
497
498/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
499/// added to the list of those properties @synthesized/@dynamic in this
500/// category @implementation block.
501///
Chris Lattner715e8482009-02-16 19:24:31 +0000502ObjCPropertyImplDecl *ObjCCategoryImplDecl::
503FindPropertyImplDecl(IdentifierInfo *Id) const {
504 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000505 ObjCPropertyImplDecl *PID = *i;
506 if (PID->getPropertyDecl()->getIdentifier() == Id)
507 return PID;
508 }
509 return 0;
510}
511
Chris Lattner10318b82008-03-16 00:19:01 +0000512// lookupInstanceMethod - This method returns an instance method by looking in
513// the class implementation. Unlike interfaces, we don't look outside the
514// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000515ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000516 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
517 if ((*I)->getSelector() == Sel)
518 return *I;
519 return NULL;
520}
521
522// lookupClassMethod - This method returns an instance method by looking in
523// the class implementation. Unlike interfaces, we don't look outside the
524// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000525ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000526 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
527 I != E; ++I)
528 if ((*I)->getSelector() == Sel)
529 return *I;
530 return NULL;
531}
532
Chris Lattner13f63602009-02-20 20:59:54 +0000533//===----------------------------------------------------------------------===//
534// ObjCImplementationDecl
535//===----------------------------------------------------------------------===//
536
537ObjCImplementationDecl *
538ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
539 SourceLocation L,
540 ObjCInterfaceDecl *ClassInterface,
541 ObjCInterfaceDecl *SuperDecl) {
542 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
543}
544
545/// Destroy - Call destructors and release memory.
546void ObjCImplementationDecl::Destroy(ASTContext& C) {
Chris Lattnerc3e72682009-02-20 21:02:11 +0000547 IVars.Destroy();
Chris Lattner13f63602009-02-20 20:59:54 +0000548 Decl::Destroy(C);
549}
550
551/// getInstanceMethod - This method returns an instance method by
552/// looking in the class implementation. Unlike interfaces, we don't
553/// look outside the implementation.
554ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
555 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
556 if ((*I)->getSelector() == Sel)
557 return *I;
Chris Lattner10318b82008-03-16 00:19:01 +0000558 return NULL;
559}
560
Chris Lattner13f63602009-02-20 20:59:54 +0000561/// getClassMethod - This method returns a class method by looking in
562/// the class implementation. Unlike interfaces, we don't look outside
563/// the implementation.
564ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
565 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
566 I != E; ++I)
567 if ((*I)->getSelector() == Sel)
568 return *I;
Chris Lattner10318b82008-03-16 00:19:01 +0000569 return NULL;
570}
571
Chris Lattner13f63602009-02-20 20:59:54 +0000572/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
573/// added to the list of those properties @synthesized/@dynamic in this
574/// @implementation block.
Chris Lattner10318b82008-03-16 00:19:01 +0000575///
Chris Lattner13f63602009-02-20 20:59:54 +0000576ObjCPropertyImplDecl *ObjCImplementationDecl::
577FindPropertyImplDecl(IdentifierInfo *Id) const {
578 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
579 ObjCPropertyImplDecl *PID = *i;
580 if (PID->getPropertyDecl()->getIdentifier() == Id)
581 return PID;
582 }
Chris Lattner10318b82008-03-16 00:19:01 +0000583 return 0;
584}
Chris Lattner44859612008-03-17 01:19:02 +0000585
Chris Lattner13f63602009-02-20 20:59:54 +0000586/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
587/// properties implemented in this @implementation block and returns the
588/// implemented property that uses it.
589///
590ObjCPropertyImplDecl *ObjCImplementationDecl::
591FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
592 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
593 ObjCPropertyImplDecl *PID = *i;
594 if (PID->getPropertyIvarDecl() &&
595 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
596 return PID;
597 }
598 return 0;
599}
600
601//===----------------------------------------------------------------------===//
602// ObjCCompatibleAliasDecl
603//===----------------------------------------------------------------------===//
604
605ObjCCompatibleAliasDecl *
606ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
607 SourceLocation L,
608 IdentifierInfo *Id,
609 ObjCInterfaceDecl* AliasedClass) {
610 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
611}
612
613//===----------------------------------------------------------------------===//
614// ObjCPropertyDecl
615//===----------------------------------------------------------------------===//
616
617ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
618 SourceLocation L,
619 IdentifierInfo *Id,
620 QualType T,
621 PropertyControl propControl) {
622 return new (C) ObjCPropertyDecl(DC, L, Id, T);
623}
624
625
626//===----------------------------------------------------------------------===//
627// ObjCPropertyImplDecl
628//===----------------------------------------------------------------------===//
629
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000630ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000631 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000632 SourceLocation atLoc,
633 SourceLocation L,
634 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000635 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000636 ObjCIvarDecl *ivar) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000637 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000638}
Chris Lattner44859612008-03-17 01:19:02 +0000639
Chris Lattnereee57c02008-04-04 06:12:32 +0000640