blob: 9e030d771ada37aa55d975b2f074996a3fb4f23d [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 Lattner13f63602009-02-20 20:59:54 +000020// ObjCInterfaceDecl
Chris Lattner114add62008-03-16 00:49:28 +000021//===----------------------------------------------------------------------===//
22
Steve Naroffab63fd62009-01-08 17:28:14 +000023// Get the local instance method declared in this interface.
24// FIXME: handle overloading, instance & class methods can have the same name.
25ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
26 lookup_const_result MethodResult = lookup(Sel);
27 if (MethodResult.first)
28 return const_cast<ObjCMethodDecl*>(
Chris Lattner13f63602009-02-20 20:59:54 +000029 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
Steve Naroffab63fd62009-01-08 17:28:14 +000030 return 0;
31}
32
33// Get the local class method declared in this interface.
34ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
35 lookup_const_result MethodResult = lookup(Sel);
36 if (MethodResult.first)
37 return const_cast<ObjCMethodDecl*>(
Chris Lattner13f63602009-02-20 20:59:54 +000038 dyn_cast<ObjCMethodDecl>(*MethodResult.first));
Steve Naroffab63fd62009-01-08 17:28:14 +000039 return 0;
40}
41
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000042/// FindPropertyDeclaration - Finds declaration of the property given its name
43/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroffdcf1e842009-01-11 12:47:58 +000044/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000045///
46ObjCPropertyDecl *
Steve Naroff451f83c2009-01-09 15:36:25 +000047ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Chris Lattner13f63602009-02-20 20:59:54 +000048 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
49 if ((*I)->getIdentifier() == PropertyId)
50 return *I;
51
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000052 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
53 if (PID) {
Chris Lattner13f63602009-02-20 20:59:54 +000054 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
55 E = PID->protocol_end(); I != E; ++I)
56 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
57 return P;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000058 }
59
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +000060 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff451f83c2009-01-09 15:36:25 +000061 // Look through categories.
62 for (ObjCCategoryDecl *Category = OID->getCategoryList();
63 Category; Category = Category->getNextClassCategory()) {
Chris Lattner13f63602009-02-20 20:59:54 +000064 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
65 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +000066 }
67 // Look through protocols.
68 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
69 E = OID->protocol_end(); I != E; ++I) {
Chris Lattner13f63602009-02-20 20:59:54 +000070 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
71 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +000072 }
73 if (OID->getSuperClass())
74 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattner13f63602009-02-20 20:59:54 +000075 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +000076 // Look through protocols.
77 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
78 E = OCD->protocol_end(); I != E; ++I) {
Chris Lattner13f63602009-02-20 20:59:54 +000079 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
80 return P;
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +000081 }
82 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +000083 return 0;
84}
85
Chris Lattner10318b82008-03-16 00:19:01 +000086ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
87 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
88 ObjCInterfaceDecl* ClassDecl = this;
89 while (ClassDecl != NULL) {
90 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
91 I != E; ++I) {
92 if ((*I)->getIdentifier() == ID) {
93 clsDeclared = ClassDecl;
94 return *I;
95 }
96 }
97 ClassDecl = ClassDecl->getSuperClass();
98 }
99 return NULL;
100}
101
102/// lookupInstanceMethod - This method returns an instance method by looking in
103/// the class, its categories, and its super classes (using a linear search).
104ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
105 ObjCInterfaceDecl* ClassDecl = this;
106 ObjCMethodDecl *MethodDecl = 0;
107
108 while (ClassDecl != NULL) {
109 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
110 return MethodDecl;
111
112 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000113 const ObjCList<ObjCProtocolDecl> &Protocols =
114 ClassDecl->getReferencedProtocols();
115 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
116 E = Protocols.end(); I != E; ++I)
117 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000118 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000119
Chris Lattner10318b82008-03-16 00:19:01 +0000120 // Didn't find one yet - now look through categories.
121 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
122 while (CatDecl) {
123 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
124 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000125
126 // Didn't find one yet - look through protocols.
127 const ObjCList<ObjCProtocolDecl> &Protocols =
128 CatDecl->getReferencedProtocols();
129 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
130 E = Protocols.end(); I != E; ++I)
131 if ((MethodDecl = (*I)->getInstanceMethod(Sel)))
132 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000133 CatDecl = CatDecl->getNextClassCategory();
134 }
135 ClassDecl = ClassDecl->getSuperClass();
136 }
137 return NULL;
138}
139
140// lookupClassMethod - This method returns a class method by looking in the
141// class, its categories, and its super classes (using a linear search).
142ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
143 ObjCInterfaceDecl* ClassDecl = this;
144 ObjCMethodDecl *MethodDecl = 0;
145
146 while (ClassDecl != NULL) {
147 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
148 return MethodDecl;
149
150 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000151 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
152 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattner8bcb5252008-07-21 18:19:38 +0000153 if ((MethodDecl = (*I)->getClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000154 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000155
Chris Lattner10318b82008-03-16 00:19:01 +0000156 // Didn't find one yet - now look through categories.
157 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
158 while (CatDecl) {
159 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
160 return MethodDecl;
161 CatDecl = CatDecl->getNextClassCategory();
162 }
163 ClassDecl = ClassDecl->getSuperClass();
164 }
165 return NULL;
166}
167
Chris Lattner13f63602009-02-20 20:59:54 +0000168
169
170//===----------------------------------------------------------------------===//
171// ObjCMethodDecl
172//===----------------------------------------------------------------------===//
173
174ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
175 SourceLocation beginLoc,
176 SourceLocation endLoc,
177 Selector SelInfo, QualType T,
178 DeclContext *contextDecl,
179 bool isInstance,
180 bool isVariadic,
181 bool isSynthesized,
182 ImplementationControl impControl) {
183 return new (C) ObjCMethodDecl(beginLoc, endLoc,
184 SelInfo, T, contextDecl,
185 isInstance,
186 isVariadic, isSynthesized, impControl);
Chris Lattner10318b82008-03-16 00:19:01 +0000187}
188
Chris Lattner13f63602009-02-20 20:59:54 +0000189void ObjCMethodDecl::Destroy(ASTContext& C) {
190 if (Body) Body->Destroy(C);
191 if (SelfDecl) SelfDecl->Destroy(C);
192
193 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
194 if (*I) (*I)->Destroy(C);
195
196 ParamInfo.clear();
197
198 Decl::Destroy(C);
Chris Lattner10318b82008-03-16 00:19:01 +0000199}
200
Chris Lattner13f63602009-02-20 20:59:54 +0000201void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
202 const ObjCInterfaceDecl *OID) {
203 QualType selfTy;
204 if (isInstanceMethod()) {
205 // There may be no interface context due to error in declaration
206 // of the interface (which has been reported). Recover gracefully.
207 if (OID) {
208 selfTy =Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(OID));
209 selfTy = Context.getPointerType(selfTy);
210 } else {
211 selfTy = Context.getObjCIdType();
212 }
213 } else // we have a factory method.
214 selfTy = Context.getObjCClassType();
215
216 SelfDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
217 &Context.Idents.get("self"), selfTy);
218
219 CmdDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
220 &Context.Idents.get("_cmd"),
221 Context.getObjCSelType());
222}
223
224
225
226/// getSynthesizedMethodSize - Compute size of synthesized method name
227/// as done be the rewrite.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000228///
Chris Lattner13f63602009-02-20 20:59:54 +0000229unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
230 // syntesized method name is a concatenation of -/+[class-name selector]
231 // Get length of this name.
232 unsigned length = 3; // _I_ or _C_
233 length += getClassInterface()->getNameAsString().size()+1; // extra for _
234 if (const ObjCCategoryImplDecl *CID =
235 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
236 length += CID->getNameAsString().size()+1;
237 length += getSelector().getAsString().size(); // selector name
238 return length;
239}
240
241ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
242 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
243 return ID;
244 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
245 return CD->getClassInterface();
246 if (ObjCImplementationDecl *IMD =
247 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
248 return IMD->getClassInterface();
249 if (ObjCCategoryImplDecl *CID =
250 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
251 return CID->getClassInterface();
252 assert(false && "unknown method context");
Fariborz Jahanian68342282008-12-05 22:32:48 +0000253 return 0;
254}
255
Chris Lattner13f63602009-02-20 20:59:54 +0000256//===----------------------------------------------------------------------===//
257// ObjCInterfaceDecl
258//===----------------------------------------------------------------------===//
259
260ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
261 DeclContext *DC,
262 SourceLocation atLoc,
263 IdentifierInfo *Id,
264 SourceLocation ClassLoc,
265 bool ForwardDecl, bool isInternal){
266 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
267 isInternal);
268}
269
270ObjCInterfaceDecl::
271ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
272 SourceLocation CLoc, bool FD, bool isInternal)
273 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
274 TypeForDecl(0), SuperClass(0),
275 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
276 ClassLoc(CLoc) {
277}
278
279void ObjCInterfaceDecl::Destroy(ASTContext &C) {
280 for (ivar_iterator I=ivar_begin(), E=ivar_end(); I!=E; ++I)
281 if (*I) (*I)->Destroy(C);
282
283 IVars.clear();
284 // FIXME: CategoryList?
285
286 // FIXME: Because there is no clear ownership
287 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
288 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
289 Decl::Destroy(C);
290}
291
292
293/// FindCategoryDeclaration - Finds category declaration in the list of
294/// categories for this class and returns it. Name of the category is passed
295/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000296///
Chris Lattner13f63602009-02-20 20:59:54 +0000297ObjCCategoryDecl *
298ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
299 for (ObjCCategoryDecl *Category = getCategoryList();
300 Category; Category = Category->getNextClassCategory())
301 if (Category->getIdentifier() == CategoryId)
302 return Category;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000303 return 0;
304}
305
Chris Lattner13f63602009-02-20 20:59:54 +0000306/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
307/// storage which matches this 'ivar'.
308///
309FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
310 const ObjCIvarDecl *IVar) {
311 const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
312 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
313 DeclarationName Member = IVar->getDeclName();
314 DeclContext::lookup_result Lookup =
315 (const_cast< RecordDecl *>(RecordForDecl))->lookup(Member);
316 assert((Lookup.first != Lookup.second) && "field decl not found");
317 FieldDecl *MemberDecl = dyn_cast<FieldDecl>(*Lookup.first);
318 assert(MemberDecl && "field decl not found");
319 return MemberDecl;
320}
321
322//===----------------------------------------------------------------------===//
323// ObjCIvarDecl
324//===----------------------------------------------------------------------===//
325
326ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
327 SourceLocation L, IdentifierInfo *Id,
328 QualType T, AccessControl ac, Expr *BW) {
329 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
330}
331
332
333
334//===----------------------------------------------------------------------===//
335// ObjCAtDefsFieldDecl
336//===----------------------------------------------------------------------===//
337
338ObjCAtDefsFieldDecl
339*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
340 IdentifierInfo *Id, QualType T, Expr *BW) {
341 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
342}
343
344void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
345 this->~ObjCAtDefsFieldDecl();
346 C.Deallocate((void *)this);
347}
348
349//===----------------------------------------------------------------------===//
350// ObjCProtocolDecl
351//===----------------------------------------------------------------------===//
352
353ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
354 SourceLocation L,
355 IdentifierInfo *Id) {
356 return new (C) ObjCProtocolDecl(DC, L, Id);
357}
358
359void ObjCProtocolDecl::Destroy(ASTContext &C) {
360 ReferencedProtocols.clear();
361 ObjCContainerDecl::Destroy(C);
362}
363
364// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
365// it inherited.
366ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
367 ObjCMethodDecl *MethodDecl = NULL;
368
369 if ((MethodDecl = getInstanceMethod(Sel)))
370 return MethodDecl;
371
372 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
373 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
374 return MethodDecl;
375 return NULL;
376}
377
378// lookupInstanceMethod - Lookup a class method in the protocol and protocols
379// it inherited.
380ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
381 ObjCMethodDecl *MethodDecl = NULL;
382
383 if ((MethodDecl = getClassMethod(Sel)))
384 return MethodDecl;
385
386 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
387 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
388 return MethodDecl;
389 return NULL;
390}
391
392//===----------------------------------------------------------------------===//
393// ObjCClassDecl
394//===----------------------------------------------------------------------===//
395
396ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
397 SourceLocation L,
398 ObjCInterfaceDecl *const *Elts,
399 unsigned nElts) {
400 return new (C) ObjCClassDecl(DC, L, Elts, nElts);
401}
402
403void ObjCClassDecl::Destroy(ASTContext &C) {
404
405 // FIXME: There is no clear ownership policy now for referenced
406 // ObjCInterfaceDecls. Some of them can be forward declarations that
407 // are never later defined (in which case the ObjCClassDecl owns them)
408 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
409 // we should have separate objects for forward declarations and definitions,
410 // obviating this problem. Because of this situation, referenced
411 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
412
413 ForwardDecls.clear();
414 Decl::Destroy(C);
415}
416
417//===----------------------------------------------------------------------===//
418// ObjCForwardProtocolDecl
419//===----------------------------------------------------------------------===//
420
421ObjCForwardProtocolDecl *
422ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
423 SourceLocation L,
424 ObjCProtocolDecl *const *Elts,
425 unsigned NumElts) {
426 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts);
427}
428
429ObjCForwardProtocolDecl::
430ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
431 ObjCProtocolDecl *const *Elts, unsigned nElts)
432 : Decl(ObjCForwardProtocol, DC, L) {
433 ReferencedProtocols.set(Elts, nElts);
434}
435
436void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
437 ReferencedProtocols.clear();
438 Decl::Destroy(C);
439}
440
441//===----------------------------------------------------------------------===//
442// ObjCCategoryDecl
443//===----------------------------------------------------------------------===//
444
445ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
446 SourceLocation L,
447 IdentifierInfo *Id) {
448 return new (C) ObjCCategoryDecl(DC, L, Id);
449}
450
451//===----------------------------------------------------------------------===//
452// ObjCCategoryImplDecl
453//===----------------------------------------------------------------------===//
454
455ObjCCategoryImplDecl *
456ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
457 SourceLocation L,IdentifierInfo *Id,
458 ObjCInterfaceDecl *ClassInterface) {
459 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
460}
461
462
Fariborz Jahanian68342282008-12-05 22:32:48 +0000463/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattner715e8482009-02-16 19:24:31 +0000464/// properties implemented in this category @implementation block and returns
465/// the implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000466///
Chris Lattner715e8482009-02-16 19:24:31 +0000467ObjCPropertyImplDecl *ObjCCategoryImplDecl::
468FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
469 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000470 ObjCPropertyImplDecl *PID = *i;
471 if (PID->getPropertyIvarDecl() &&
472 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
473 return PID;
474 }
475 return 0;
476}
477
478/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
479/// added to the list of those properties @synthesized/@dynamic in this
480/// category @implementation block.
481///
Chris Lattner715e8482009-02-16 19:24:31 +0000482ObjCPropertyImplDecl *ObjCCategoryImplDecl::
483FindPropertyImplDecl(IdentifierInfo *Id) const {
484 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000485 ObjCPropertyImplDecl *PID = *i;
486 if (PID->getPropertyDecl()->getIdentifier() == Id)
487 return PID;
488 }
489 return 0;
490}
491
Chris Lattner10318b82008-03-16 00:19:01 +0000492// lookupInstanceMethod - This method returns an instance method by looking in
493// the class implementation. Unlike interfaces, we don't look outside the
494// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000495ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000496 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
497 if ((*I)->getSelector() == Sel)
498 return *I;
499 return NULL;
500}
501
502// lookupClassMethod - This method returns an instance method by looking in
503// the class implementation. Unlike interfaces, we don't look outside the
504// implementation.
Daniel Dunbardd24b9a2008-08-26 06:53:45 +0000505ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000506 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
507 I != E; ++I)
508 if ((*I)->getSelector() == Sel)
509 return *I;
510 return NULL;
511}
512
Chris Lattner13f63602009-02-20 20:59:54 +0000513//===----------------------------------------------------------------------===//
514// ObjCImplementationDecl
515//===----------------------------------------------------------------------===//
516
517ObjCImplementationDecl *
518ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
519 SourceLocation L,
520 ObjCInterfaceDecl *ClassInterface,
521 ObjCInterfaceDecl *SuperDecl) {
522 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
523}
524
525/// Destroy - Call destructors and release memory.
526void ObjCImplementationDecl::Destroy(ASTContext& C) {
527 IVars.clear();
528 Decl::Destroy(C);
529}
530
531/// getInstanceMethod - This method returns an instance method by
532/// looking in the class implementation. Unlike interfaces, we don't
533/// look outside the implementation.
534ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) const {
535 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
536 if ((*I)->getSelector() == Sel)
537 return *I;
Chris Lattner10318b82008-03-16 00:19:01 +0000538 return NULL;
539}
540
Chris Lattner13f63602009-02-20 20:59:54 +0000541/// getClassMethod - This method returns a class method by looking in
542/// the class implementation. Unlike interfaces, we don't look outside
543/// the implementation.
544ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) const {
545 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
546 I != E; ++I)
547 if ((*I)->getSelector() == Sel)
548 return *I;
Chris Lattner10318b82008-03-16 00:19:01 +0000549 return NULL;
550}
551
Chris Lattner13f63602009-02-20 20:59:54 +0000552/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
553/// added to the list of those properties @synthesized/@dynamic in this
554/// @implementation block.
Chris Lattner10318b82008-03-16 00:19:01 +0000555///
Chris Lattner13f63602009-02-20 20:59:54 +0000556ObjCPropertyImplDecl *ObjCImplementationDecl::
557FindPropertyImplDecl(IdentifierInfo *Id) const {
558 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
559 ObjCPropertyImplDecl *PID = *i;
560 if (PID->getPropertyDecl()->getIdentifier() == Id)
561 return PID;
562 }
Chris Lattner10318b82008-03-16 00:19:01 +0000563 return 0;
564}
Chris Lattner44859612008-03-17 01:19:02 +0000565
Chris Lattner13f63602009-02-20 20:59:54 +0000566/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
567/// properties implemented in this @implementation block and returns the
568/// implemented property that uses it.
569///
570ObjCPropertyImplDecl *ObjCImplementationDecl::
571FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
572 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
573 ObjCPropertyImplDecl *PID = *i;
574 if (PID->getPropertyIvarDecl() &&
575 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
576 return PID;
577 }
578 return 0;
579}
580
581//===----------------------------------------------------------------------===//
582// ObjCCompatibleAliasDecl
583//===----------------------------------------------------------------------===//
584
585ObjCCompatibleAliasDecl *
586ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
587 SourceLocation L,
588 IdentifierInfo *Id,
589 ObjCInterfaceDecl* AliasedClass) {
590 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
591}
592
593//===----------------------------------------------------------------------===//
594// ObjCPropertyDecl
595//===----------------------------------------------------------------------===//
596
597ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
598 SourceLocation L,
599 IdentifierInfo *Id,
600 QualType T,
601 PropertyControl propControl) {
602 return new (C) ObjCPropertyDecl(DC, L, Id, T);
603}
604
605
606//===----------------------------------------------------------------------===//
607// ObjCPropertyImplDecl
608//===----------------------------------------------------------------------===//
609
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000610ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000611 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000612 SourceLocation atLoc,
613 SourceLocation L,
614 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000615 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000616 ObjCIvarDecl *ivar) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000617 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000618}
Chris Lattner44859612008-03-17 01:19:02 +0000619
Chris Lattnereee57c02008-04-04 06:12:32 +0000620