blob: 57bd8eab1efae313a55e66ff66da4344756d1b25 [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"
Steve Naroffd85ba922009-02-22 19:35:57 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner10318b82008-03-16 00:19:01 +000018using namespace clang;
19
Chris Lattner114add62008-03-16 00:49:28 +000020//===----------------------------------------------------------------------===//
Chris Lattner217eb8c2009-02-20 21:16:26 +000021// ObjCListBase
22//===----------------------------------------------------------------------===//
23
Chris Lattnerdf6133b2009-02-20 21:35:13 +000024void ObjCListBase::Destroy(ASTContext &Ctx) {
Chris Lattnercf6c8282009-02-20 21:44:01 +000025 Ctx.Deallocate(List);
Chris Lattner217eb8c2009-02-20 21:16:26 +000026 NumElts = 0;
27 List = 0;
28}
29
Chris Lattnerdf6133b2009-02-20 21:35:13 +000030void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Chris Lattner217eb8c2009-02-20 21:16:26 +000031 assert(List == 0 && "Elements already set!");
32 if (Elts == 0) return; // Setting to an empty list is a noop.
33
Chris Lattnercf6c8282009-02-20 21:44:01 +000034
35 List = new (Ctx) void*[Elts];
Chris Lattner217eb8c2009-02-20 21:16:26 +000036 NumElts = Elts;
37 memcpy(List, InList, sizeof(void*)*Elts);
38}
39
40
41//===----------------------------------------------------------------------===//
Chris Lattner13f63602009-02-20 20:59:54 +000042// ObjCInterfaceDecl
Chris Lattner114add62008-03-16 00:49:28 +000043//===----------------------------------------------------------------------===//
44
Steve Naroffab63fd62009-01-08 17:28:14 +000045// Get the local instance method declared in this interface.
Steve Naroffab63fd62009-01-08 17:28:14 +000046ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
Steve Naroffd85ba922009-02-22 19:35:57 +000047 // Since instance & class methods can have the same name, the loop below
48 // ensures we get the correct method.
49 //
50 // @interface Whatever
51 // - (int) class_method;
52 // + (float) class_method;
53 // @end
54 //
55 lookup_const_iterator Meth, MethEnd;
56 for (llvm::tie(Meth, MethEnd) = lookup(Sel);
57 Meth != MethEnd; ++Meth) {
58 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
59 if (MD && MD->isInstanceMethod())
60 return MD;
61 }
Steve Naroffab63fd62009-01-08 17:28:14 +000062 return 0;
63}
64
65// Get the local class method declared in this interface.
66ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
Steve Naroffd85ba922009-02-22 19:35:57 +000067 // Since instance & class methods can have the same name, the loop below
68 // ensures we get the correct method.
69 //
70 // @interface Whatever
71 // - (int) class_method;
72 // + (float) class_method;
73 // @end
74 //
75 lookup_const_iterator Meth, MethEnd;
76 for (llvm::tie(Meth, MethEnd) = lookup(Sel);
77 Meth != MethEnd; ++Meth) {
78 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
79 if (MD && MD->isClassMethod())
80 return MD;
81 }
Steve Naroffab63fd62009-01-08 17:28:14 +000082 return 0;
83}
84
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000085/// FindPropertyDeclaration - Finds declaration of the property given its name
86/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroffdcf1e842009-01-11 12:47:58 +000087/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +000088///
89ObjCPropertyDecl *
Steve Naroff451f83c2009-01-09 15:36:25 +000090ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Chris Lattner13f63602009-02-20 20:59:54 +000091 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
92 if ((*I)->getIdentifier() == PropertyId)
93 return *I;
94
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +000095 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
96 if (PID) {
Chris Lattner13f63602009-02-20 20:59:54 +000097 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
98 E = PID->protocol_end(); I != E; ++I)
99 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
100 return P;
Fariborz Jahanianfaca5e22009-01-09 21:04:52 +0000101 }
102
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000103 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff451f83c2009-01-09 15:36:25 +0000104 // Look through categories.
105 for (ObjCCategoryDecl *Category = OID->getCategoryList();
106 Category; Category = Category->getNextClassCategory()) {
Chris Lattner13f63602009-02-20 20:59:54 +0000107 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
108 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +0000109 }
110 // Look through protocols.
111 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
112 E = OID->protocol_end(); I != E; ++I) {
Chris Lattner13f63602009-02-20 20:59:54 +0000113 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
114 return P;
Steve Naroff451f83c2009-01-09 15:36:25 +0000115 }
116 if (OID->getSuperClass())
117 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattner13f63602009-02-20 20:59:54 +0000118 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000119 // Look through protocols.
120 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
121 E = OCD->protocol_end(); I != E; ++I) {
Chris Lattner13f63602009-02-20 20:59:54 +0000122 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
123 return P;
Fariborz Jahanian6b0ed6f2009-01-19 18:16:19 +0000124 }
125 }
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000126 return 0;
127}
128
Chris Lattner10318b82008-03-16 00:19:01 +0000129ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
130 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
131 ObjCInterfaceDecl* ClassDecl = this;
132 while (ClassDecl != NULL) {
133 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
134 I != E; ++I) {
135 if ((*I)->getIdentifier() == ID) {
136 clsDeclared = ClassDecl;
137 return *I;
138 }
139 }
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000140 // look into properties.
141 for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(),
142 E = ClassDecl->prop_end(); I != E; ++I) {
143 ObjCPropertyDecl *PDecl = (*I);
144 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
145 if (IV->getIdentifier() == ID) {
146 clsDeclared = ClassDecl;
147 return IV;
148 }
149 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000150 // look into continuation class.
151 for (ObjCCategoryDecl *Categories = ClassDecl->getCategoryList();
152 Categories; Categories = Categories->getNextClassCategory())
153 if (!Categories->getIdentifier()) {
154 for (ObjCInterfaceDecl::prop_iterator I = Categories->prop_begin(),
155 E = Categories->prop_end(); I != E; ++I) {
156 ObjCPropertyDecl *PDecl = (*I);
157 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
158 if (IV->getIdentifier() == ID) {
159 clsDeclared = ClassDecl;
160 return IV;
161 }
162 }
163 break;
164 }
165
Chris Lattner10318b82008-03-16 00:19:01 +0000166 ClassDecl = ClassDecl->getSuperClass();
167 }
168 return NULL;
169}
170
171/// lookupInstanceMethod - This method returns an instance method by looking in
172/// the class, its categories, and its super classes (using a linear search).
173ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
174 ObjCInterfaceDecl* ClassDecl = this;
175 ObjCMethodDecl *MethodDecl = 0;
176
177 while (ClassDecl != NULL) {
178 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
179 return MethodDecl;
180
181 // Didn't find one yet - look through protocols.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000182 const ObjCList<ObjCProtocolDecl> &Protocols =
183 ClassDecl->getReferencedProtocols();
184 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
185 E = Protocols.end(); I != E; ++I)
Steve Naroff0ba24842009-02-26 11:32:02 +0000186 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000187 return MethodDecl;
Chris Lattner8bcb5252008-07-21 18:19:38 +0000188
Chris Lattner10318b82008-03-16 00:19:01 +0000189 // Didn't find one yet - now look through categories.
190 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
191 while (CatDecl) {
192 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
193 return MethodDecl;
Steve Naroffaf151802008-12-08 20:57:28 +0000194
195 // Didn't find one yet - look through protocols.
196 const ObjCList<ObjCProtocolDecl> &Protocols =
197 CatDecl->getReferencedProtocols();
198 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
199 E = Protocols.end(); I != E; ++I)
Steve Naroff0ba24842009-02-26 11:32:02 +0000200 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Steve Naroffaf151802008-12-08 20:57:28 +0000201 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000202 CatDecl = CatDecl->getNextClassCategory();
203 }
204 ClassDecl = ClassDecl->getSuperClass();
205 }
206 return NULL;
207}
208
209// lookupClassMethod - This method returns a class method by looking in the
210// class, its categories, and its super classes (using a linear search).
211ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
212 ObjCInterfaceDecl* ClassDecl = this;
213 ObjCMethodDecl *MethodDecl = 0;
214
215 while (ClassDecl != NULL) {
216 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
217 return MethodDecl;
218
219 // Didn't find one yet - look through protocols.
Chris Lattner0be08822008-07-21 21:32:27 +0000220 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
221 E = ClassDecl->protocol_end(); I != E; ++I)
Steve Naroff0ba24842009-02-26 11:32:02 +0000222 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner10318b82008-03-16 00:19:01 +0000223 return MethodDecl;
Chris Lattner0be08822008-07-21 21:32:27 +0000224
Chris Lattner10318b82008-03-16 00:19:01 +0000225 // Didn't find one yet - now look through categories.
226 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
227 while (CatDecl) {
228 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
229 return MethodDecl;
Steve Naroff0ba24842009-02-26 11:32:02 +0000230
231 // Didn't find one yet - look through protocols.
232 const ObjCList<ObjCProtocolDecl> &Protocols =
233 CatDecl->getReferencedProtocols();
234 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
235 E = Protocols.end(); I != E; ++I)
236 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
237 return MethodDecl;
Chris Lattner10318b82008-03-16 00:19:01 +0000238 CatDecl = CatDecl->getNextClassCategory();
239 }
240 ClassDecl = ClassDecl->getSuperClass();
241 }
242 return NULL;
243}
244
Chris Lattner13f63602009-02-20 20:59:54 +0000245
246
247//===----------------------------------------------------------------------===//
248// ObjCMethodDecl
249//===----------------------------------------------------------------------===//
250
251ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
252 SourceLocation beginLoc,
253 SourceLocation endLoc,
254 Selector SelInfo, QualType T,
255 DeclContext *contextDecl,
256 bool isInstance,
257 bool isVariadic,
258 bool isSynthesized,
259 ImplementationControl impControl) {
260 return new (C) ObjCMethodDecl(beginLoc, endLoc,
261 SelInfo, T, contextDecl,
262 isInstance,
263 isVariadic, isSynthesized, impControl);
Chris Lattner10318b82008-03-16 00:19:01 +0000264}
265
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000266void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattner13f63602009-02-20 20:59:54 +0000267 if (Body) Body->Destroy(C);
268 if (SelfDecl) SelfDecl->Destroy(C);
269
270 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
271 if (*I) (*I)->Destroy(C);
272
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000273 ParamInfo.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000274
275 Decl::Destroy(C);
Chris Lattner10318b82008-03-16 00:19:01 +0000276}
277
Chris Lattner13f63602009-02-20 20:59:54 +0000278void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
279 const ObjCInterfaceDecl *OID) {
280 QualType selfTy;
281 if (isInstanceMethod()) {
282 // There may be no interface context due to error in declaration
283 // of the interface (which has been reported). Recover gracefully.
284 if (OID) {
285 selfTy =Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(OID));
286 selfTy = Context.getPointerType(selfTy);
287 } else {
288 selfTy = Context.getObjCIdType();
289 }
290 } else // we have a factory method.
291 selfTy = Context.getObjCClassType();
292
293 SelfDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
294 &Context.Idents.get("self"), selfTy);
295
296 CmdDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
297 &Context.Idents.get("_cmd"),
298 Context.getObjCSelType());
299}
300
301
302
303/// getSynthesizedMethodSize - Compute size of synthesized method name
304/// as done be the rewrite.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000305///
Chris Lattner13f63602009-02-20 20:59:54 +0000306unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
307 // syntesized method name is a concatenation of -/+[class-name selector]
308 // Get length of this name.
309 unsigned length = 3; // _I_ or _C_
310 length += getClassInterface()->getNameAsString().size()+1; // extra for _
311 if (const ObjCCategoryImplDecl *CID =
312 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
313 length += CID->getNameAsString().size()+1;
314 length += getSelector().getAsString().size(); // selector name
315 return length;
316}
317
318ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
319 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
320 return ID;
321 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
322 return CD->getClassInterface();
323 if (ObjCImplementationDecl *IMD =
324 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
325 return IMD->getClassInterface();
326 if (ObjCCategoryImplDecl *CID =
327 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
328 return CID->getClassInterface();
329 assert(false && "unknown method context");
Fariborz Jahanian68342282008-12-05 22:32:48 +0000330 return 0;
331}
332
Chris Lattner13f63602009-02-20 20:59:54 +0000333//===----------------------------------------------------------------------===//
334// ObjCInterfaceDecl
335//===----------------------------------------------------------------------===//
336
337ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
338 DeclContext *DC,
339 SourceLocation atLoc,
340 IdentifierInfo *Id,
341 SourceLocation ClassLoc,
342 bool ForwardDecl, bool isInternal){
343 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
344 isInternal);
345}
346
347ObjCInterfaceDecl::
348ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
349 SourceLocation CLoc, bool FD, bool isInternal)
350 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
351 TypeForDecl(0), SuperClass(0),
352 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
353 ClassLoc(CLoc) {
354}
355
356void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnereff36ed2009-03-31 08:36:08 +0000357 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattner13f63602009-02-20 20:59:54 +0000358 if (*I) (*I)->Destroy(C);
359
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000360 IVars.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000361 // FIXME: CategoryList?
362
363 // FIXME: Because there is no clear ownership
364 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
365 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
366 Decl::Destroy(C);
367}
368
369
370/// FindCategoryDeclaration - Finds category declaration in the list of
371/// categories for this class and returns it. Name of the category is passed
372/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000373///
Chris Lattner13f63602009-02-20 20:59:54 +0000374ObjCCategoryDecl *
375ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
376 for (ObjCCategoryDecl *Category = getCategoryList();
377 Category; Category = Category->getNextClassCategory())
378 if (Category->getIdentifier() == CategoryId)
379 return Category;
Fariborz Jahanian68342282008-12-05 22:32:48 +0000380 return 0;
381}
382
Chris Lattner13f63602009-02-20 20:59:54 +0000383/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
384/// storage which matches this 'ivar'.
385///
386FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
387 const ObjCIvarDecl *IVar) {
388 const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
389 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
Chris Lattnereff36ed2009-03-31 08:36:08 +0000390 DeclContext::lookup_const_result Lookup =
391 RecordForDecl->lookup(IVar->getDeclName());
Chris Lattner13f63602009-02-20 20:59:54 +0000392 assert((Lookup.first != Lookup.second) && "field decl not found");
Chris Lattnereff36ed2009-03-31 08:36:08 +0000393 return cast<FieldDecl>(*Lookup.first);
Chris Lattner13f63602009-02-20 20:59:54 +0000394}
395
396//===----------------------------------------------------------------------===//
397// ObjCIvarDecl
398//===----------------------------------------------------------------------===//
399
400ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
401 SourceLocation L, IdentifierInfo *Id,
402 QualType T, AccessControl ac, Expr *BW) {
403 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
404}
405
406
407
408//===----------------------------------------------------------------------===//
409// ObjCAtDefsFieldDecl
410//===----------------------------------------------------------------------===//
411
412ObjCAtDefsFieldDecl
413*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
414 IdentifierInfo *Id, QualType T, Expr *BW) {
415 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
416}
417
418void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
419 this->~ObjCAtDefsFieldDecl();
420 C.Deallocate((void *)this);
421}
422
423//===----------------------------------------------------------------------===//
424// ObjCProtocolDecl
425//===----------------------------------------------------------------------===//
426
427ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
428 SourceLocation L,
429 IdentifierInfo *Id) {
430 return new (C) ObjCProtocolDecl(DC, L, Id);
431}
432
433void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000434 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000435 ObjCContainerDecl::Destroy(C);
436}
437
Steve Naroff98e71b82009-03-01 16:12:44 +0000438ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
439 ObjCProtocolDecl *PDecl = this;
440
441 if (Name == getIdentifier())
442 return PDecl;
443
444 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
445 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
446 return PDecl;
447
448 return NULL;
449}
450
Chris Lattner13f63602009-02-20 20:59:54 +0000451// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
452// it inherited.
453ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
454 ObjCMethodDecl *MethodDecl = NULL;
455
456 if ((MethodDecl = getInstanceMethod(Sel)))
457 return MethodDecl;
458
459 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
460 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
461 return MethodDecl;
462 return NULL;
463}
464
465// lookupInstanceMethod - Lookup a class method in the protocol and protocols
466// it inherited.
467ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
468 ObjCMethodDecl *MethodDecl = NULL;
469
470 if ((MethodDecl = getClassMethod(Sel)))
471 return MethodDecl;
472
473 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
474 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
475 return MethodDecl;
476 return NULL;
477}
478
479//===----------------------------------------------------------------------===//
480// ObjCClassDecl
481//===----------------------------------------------------------------------===//
482
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000483ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
484 ObjCInterfaceDecl *const *Elts, unsigned nElts,
485 ASTContext &C)
486 : Decl(ObjCClass, DC, L) {
487 ForwardDecls.set(Elts, nElts, C);
488}
489
490
Chris Lattner13f63602009-02-20 20:59:54 +0000491ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
492 SourceLocation L,
493 ObjCInterfaceDecl *const *Elts,
494 unsigned nElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000495 return new (C) ObjCClassDecl(DC, L, Elts, nElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000496}
497
498void ObjCClassDecl::Destroy(ASTContext &C) {
499
500 // FIXME: There is no clear ownership policy now for referenced
501 // ObjCInterfaceDecls. Some of them can be forward declarations that
502 // are never later defined (in which case the ObjCClassDecl owns them)
503 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
504 // we should have separate objects for forward declarations and definitions,
505 // obviating this problem. Because of this situation, referenced
506 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
507
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000508 ForwardDecls.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000509 Decl::Destroy(C);
510}
511
512//===----------------------------------------------------------------------===//
513// ObjCForwardProtocolDecl
514//===----------------------------------------------------------------------===//
515
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000516ObjCForwardProtocolDecl::
517ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
518 ObjCProtocolDecl *const *Elts, unsigned nElts,
519 ASTContext &C)
520: Decl(ObjCForwardProtocol, DC, L) {
521 ReferencedProtocols.set(Elts, nElts, C);
522}
523
524
Chris Lattner13f63602009-02-20 20:59:54 +0000525ObjCForwardProtocolDecl *
526ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
527 SourceLocation L,
528 ObjCProtocolDecl *const *Elts,
529 unsigned NumElts) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000530 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, C);
Chris Lattner13f63602009-02-20 20:59:54 +0000531}
532
533void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000534 ReferencedProtocols.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000535 Decl::Destroy(C);
536}
537
538//===----------------------------------------------------------------------===//
539// ObjCCategoryDecl
540//===----------------------------------------------------------------------===//
541
542ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
543 SourceLocation L,
544 IdentifierInfo *Id) {
545 return new (C) ObjCCategoryDecl(DC, L, Id);
546}
547
548//===----------------------------------------------------------------------===//
549// ObjCCategoryImplDecl
550//===----------------------------------------------------------------------===//
551
552ObjCCategoryImplDecl *
553ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
554 SourceLocation L,IdentifierInfo *Id,
555 ObjCInterfaceDecl *ClassInterface) {
556 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
557}
558
559
Fariborz Jahanian68342282008-12-05 22:32:48 +0000560/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattner715e8482009-02-16 19:24:31 +0000561/// properties implemented in this category @implementation block and returns
562/// the implemented property that uses it.
Fariborz Jahanian68342282008-12-05 22:32:48 +0000563///
Chris Lattner649b3da2009-02-28 18:42:10 +0000564ObjCPropertyImplDecl *ObjCImplDecl::
Chris Lattner715e8482009-02-16 19:24:31 +0000565FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
566 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000567 ObjCPropertyImplDecl *PID = *i;
568 if (PID->getPropertyIvarDecl() &&
569 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
570 return PID;
571 }
572 return 0;
573}
574
575/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
576/// added to the list of those properties @synthesized/@dynamic in this
577/// category @implementation block.
578///
Chris Lattner649b3da2009-02-28 18:42:10 +0000579ObjCPropertyImplDecl *ObjCImplDecl::
Chris Lattner715e8482009-02-16 19:24:31 +0000580FindPropertyImplDecl(IdentifierInfo *Id) const {
581 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanian68342282008-12-05 22:32:48 +0000582 ObjCPropertyImplDecl *PID = *i;
583 if (PID->getPropertyDecl()->getIdentifier() == Id)
584 return PID;
585 }
586 return 0;
587}
588
Chris Lattner649b3da2009-02-28 18:42:10 +0000589// getInstanceMethod - This method returns an instance method by looking in
Chris Lattner10318b82008-03-16 00:19:01 +0000590// the class implementation. Unlike interfaces, we don't look outside the
591// implementation.
Chris Lattner649b3da2009-02-28 18:42:10 +0000592ObjCMethodDecl *ObjCImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000593 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
594 if ((*I)->getSelector() == Sel)
595 return *I;
596 return NULL;
597}
598
Chris Lattner649b3da2009-02-28 18:42:10 +0000599// getClassMethod - This method returns an instance method by looking in
Chris Lattner10318b82008-03-16 00:19:01 +0000600// the class implementation. Unlike interfaces, we don't look outside the
601// implementation.
Chris Lattner649b3da2009-02-28 18:42:10 +0000602ObjCMethodDecl *ObjCImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner10318b82008-03-16 00:19:01 +0000603 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
604 I != E; ++I)
605 if ((*I)->getSelector() == Sel)
606 return *I;
607 return NULL;
608}
609
Chris Lattner13f63602009-02-20 20:59:54 +0000610//===----------------------------------------------------------------------===//
611// ObjCImplementationDecl
612//===----------------------------------------------------------------------===//
613
614ObjCImplementationDecl *
615ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
616 SourceLocation L,
617 ObjCInterfaceDecl *ClassInterface,
618 ObjCInterfaceDecl *SuperDecl) {
619 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
620}
621
622/// Destroy - Call destructors and release memory.
Chris Lattnerdf6133b2009-02-20 21:35:13 +0000623void ObjCImplementationDecl::Destroy(ASTContext &C) {
624 IVars.Destroy(C);
Chris Lattner13f63602009-02-20 20:59:54 +0000625 Decl::Destroy(C);
626}
627
Chris Lattner13f63602009-02-20 20:59:54 +0000628//===----------------------------------------------------------------------===//
629// ObjCCompatibleAliasDecl
630//===----------------------------------------------------------------------===//
631
632ObjCCompatibleAliasDecl *
633ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
634 SourceLocation L,
635 IdentifierInfo *Id,
636 ObjCInterfaceDecl* AliasedClass) {
637 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
638}
639
640//===----------------------------------------------------------------------===//
641// ObjCPropertyDecl
642//===----------------------------------------------------------------------===//
643
644ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
645 SourceLocation L,
646 IdentifierInfo *Id,
647 QualType T,
648 PropertyControl propControl) {
649 return new (C) ObjCPropertyDecl(DC, L, Id, T);
650}
651
652
653//===----------------------------------------------------------------------===//
654// ObjCPropertyImplDecl
655//===----------------------------------------------------------------------===//
656
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000657ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +0000658 DeclContext *DC,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000659 SourceLocation atLoc,
660 SourceLocation L,
661 ObjCPropertyDecl *property,
Daniel Dunbar14117fc2008-08-26 04:47:31 +0000662 Kind PK,
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000663 ObjCIvarDecl *ivar) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000664 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000665}
Chris Lattner44859612008-03-17 01:19:02 +0000666
Chris Lattnereee57c02008-04-04 06:12:32 +0000667