blob: cd1b979dcb0c2e3319d80c55df2b0b8292e8b63b [file] [log] [blame]
Chris Lattner1e03a562008-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 Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000018using namespace clang;
19
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000020//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000021// ObjCListBase
22//===----------------------------------------------------------------------===//
23
Chris Lattner38af2de2009-02-20 21:35:13 +000024void ObjCListBase::Destroy(ASTContext &Ctx) {
Chris Lattner4ee413b2009-02-20 21:44:01 +000025 Ctx.Deallocate(List);
Chris Lattner11e1e1a2009-02-20 21:16:26 +000026 NumElts = 0;
27 List = 0;
28}
29
Chris Lattner38af2de2009-02-20 21:35:13 +000030void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Chris Lattner11e1e1a2009-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 Lattner4ee413b2009-02-20 21:44:01 +000034
35 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000036 NumElts = Elts;
37 memcpy(List, InList, sizeof(void*)*Elts);
38}
39
40
41//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000042// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000043//===----------------------------------------------------------------------===//
44
Steve Naroff0701bbb2009-01-08 17:28:14 +000045// Get the local instance method declared in this interface.
Steve Naroff0701bbb2009-01-08 17:28:14 +000046ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
Steve Naroff0de21fd2009-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 Naroff0701bbb2009-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 Naroff0de21fd2009-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 Naroff0701bbb2009-01-08 17:28:14 +000082 return 0;
83}
84
Fariborz Jahanian559c0c42008-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 Naroff93983f82009-01-11 12:47:58 +000087/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000088///
89ObjCPropertyDecl *
Steve Naroff09c47192009-01-09 15:36:25 +000090ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Chris Lattnerab351632009-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 Jahaniana66793e2009-01-09 21:04:52 +000095 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
96 if (PID) {
Chris Lattnerab351632009-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 Jahaniana66793e2009-01-09 21:04:52 +0000101 }
102
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000103 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff09c47192009-01-09 15:36:25 +0000104 // Look through categories.
105 for (ObjCCategoryDecl *Category = OID->getCategoryList();
106 Category; Category = Category->getNextClassCategory()) {
Chris Lattnerab351632009-02-20 20:59:54 +0000107 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
108 return P;
Steve Naroff09c47192009-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 Lattnerab351632009-02-20 20:59:54 +0000113 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
114 return P;
Steve Naroff09c47192009-01-09 15:36:25 +0000115 }
116 if (OID->getSuperClass())
117 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattnerab351632009-02-20 20:59:54 +0000118 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanianf034e9c2009-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 Lattnerab351632009-02-20 20:59:54 +0000122 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
123 return P;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000124 }
125 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000126 return 0;
127}
128
Chris Lattner1e03a562008-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 Jahanianaf3e7222009-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 }
Chris Lattner1e03a562008-03-16 00:19:01 +0000150 ClassDecl = ClassDecl->getSuperClass();
151 }
152 return NULL;
153}
154
155/// lookupInstanceMethod - This method returns an instance method by looking in
156/// the class, its categories, and its super classes (using a linear search).
157ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
158 ObjCInterfaceDecl* ClassDecl = this;
159 ObjCMethodDecl *MethodDecl = 0;
160
161 while (ClassDecl != NULL) {
162 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
163 return MethodDecl;
164
165 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000166 const ObjCList<ObjCProtocolDecl> &Protocols =
167 ClassDecl->getReferencedProtocols();
168 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
169 E = Protocols.end(); I != E; ++I)
Steve Naroffb5584222009-02-26 11:32:02 +0000170 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000171 return MethodDecl;
Chris Lattner3db6cae2008-07-21 18:19:38 +0000172
Chris Lattner1e03a562008-03-16 00:19:01 +0000173 // Didn't find one yet - now look through categories.
174 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
175 while (CatDecl) {
176 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
177 return MethodDecl;
Steve Naroffb79c01e2008-12-08 20:57:28 +0000178
179 // Didn't find one yet - look through protocols.
180 const ObjCList<ObjCProtocolDecl> &Protocols =
181 CatDecl->getReferencedProtocols();
182 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
183 E = Protocols.end(); I != E; ++I)
Steve Naroffb5584222009-02-26 11:32:02 +0000184 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
Steve Naroffb79c01e2008-12-08 20:57:28 +0000185 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000186 CatDecl = CatDecl->getNextClassCategory();
187 }
188 ClassDecl = ClassDecl->getSuperClass();
189 }
190 return NULL;
191}
192
193// lookupClassMethod - This method returns a class method by looking in the
194// class, its categories, and its super classes (using a linear search).
195ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
196 ObjCInterfaceDecl* ClassDecl = this;
197 ObjCMethodDecl *MethodDecl = 0;
198
199 while (ClassDecl != NULL) {
200 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
201 return MethodDecl;
202
203 // Didn't find one yet - look through protocols.
Chris Lattner780f3292008-07-21 21:32:27 +0000204 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
205 E = ClassDecl->protocol_end(); I != E; ++I)
Steve Naroffb5584222009-02-26 11:32:02 +0000206 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000207 return MethodDecl;
Chris Lattner780f3292008-07-21 21:32:27 +0000208
Chris Lattner1e03a562008-03-16 00:19:01 +0000209 // Didn't find one yet - now look through categories.
210 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
211 while (CatDecl) {
212 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
213 return MethodDecl;
Steve Naroffb5584222009-02-26 11:32:02 +0000214
215 // Didn't find one yet - look through protocols.
216 const ObjCList<ObjCProtocolDecl> &Protocols =
217 CatDecl->getReferencedProtocols();
218 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
219 E = Protocols.end(); I != E; ++I)
220 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
221 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000222 CatDecl = CatDecl->getNextClassCategory();
223 }
224 ClassDecl = ClassDecl->getSuperClass();
225 }
226 return NULL;
227}
228
Chris Lattnerab351632009-02-20 20:59:54 +0000229
230
231//===----------------------------------------------------------------------===//
232// ObjCMethodDecl
233//===----------------------------------------------------------------------===//
234
235ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
236 SourceLocation beginLoc,
237 SourceLocation endLoc,
238 Selector SelInfo, QualType T,
239 DeclContext *contextDecl,
240 bool isInstance,
241 bool isVariadic,
242 bool isSynthesized,
243 ImplementationControl impControl) {
244 return new (C) ObjCMethodDecl(beginLoc, endLoc,
245 SelInfo, T, contextDecl,
246 isInstance,
247 isVariadic, isSynthesized, impControl);
Chris Lattner1e03a562008-03-16 00:19:01 +0000248}
249
Chris Lattner38af2de2009-02-20 21:35:13 +0000250void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattnerab351632009-02-20 20:59:54 +0000251 if (Body) Body->Destroy(C);
252 if (SelfDecl) SelfDecl->Destroy(C);
253
254 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
255 if (*I) (*I)->Destroy(C);
256
Chris Lattner38af2de2009-02-20 21:35:13 +0000257 ParamInfo.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000258
259 Decl::Destroy(C);
Chris Lattner1e03a562008-03-16 00:19:01 +0000260}
261
Chris Lattnerab351632009-02-20 20:59:54 +0000262void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
263 const ObjCInterfaceDecl *OID) {
264 QualType selfTy;
265 if (isInstanceMethod()) {
266 // There may be no interface context due to error in declaration
267 // of the interface (which has been reported). Recover gracefully.
268 if (OID) {
269 selfTy =Context.getObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(OID));
270 selfTy = Context.getPointerType(selfTy);
271 } else {
272 selfTy = Context.getObjCIdType();
273 }
274 } else // we have a factory method.
275 selfTy = Context.getObjCClassType();
276
277 SelfDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
278 &Context.Idents.get("self"), selfTy);
279
280 CmdDecl = ImplicitParamDecl::Create(Context, this, SourceLocation(),
281 &Context.Idents.get("_cmd"),
282 Context.getObjCSelType());
283}
284
285
286
287/// getSynthesizedMethodSize - Compute size of synthesized method name
288/// as done be the rewrite.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000289///
Chris Lattnerab351632009-02-20 20:59:54 +0000290unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
291 // syntesized method name is a concatenation of -/+[class-name selector]
292 // Get length of this name.
293 unsigned length = 3; // _I_ or _C_
294 length += getClassInterface()->getNameAsString().size()+1; // extra for _
295 if (const ObjCCategoryImplDecl *CID =
296 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
297 length += CID->getNameAsString().size()+1;
298 length += getSelector().getAsString().size(); // selector name
299 return length;
300}
301
302ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
303 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
304 return ID;
305 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
306 return CD->getClassInterface();
307 if (ObjCImplementationDecl *IMD =
308 dyn_cast<ObjCImplementationDecl>(getDeclContext()))
309 return IMD->getClassInterface();
310 if (ObjCCategoryImplDecl *CID =
311 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
312 return CID->getClassInterface();
313 assert(false && "unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000314 return 0;
315}
316
Chris Lattnerab351632009-02-20 20:59:54 +0000317//===----------------------------------------------------------------------===//
318// ObjCInterfaceDecl
319//===----------------------------------------------------------------------===//
320
321ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
322 DeclContext *DC,
323 SourceLocation atLoc,
324 IdentifierInfo *Id,
325 SourceLocation ClassLoc,
326 bool ForwardDecl, bool isInternal){
327 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
328 isInternal);
329}
330
331ObjCInterfaceDecl::
332ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
333 SourceLocation CLoc, bool FD, bool isInternal)
334 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
335 TypeForDecl(0), SuperClass(0),
336 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
337 ClassLoc(CLoc) {
338}
339
340void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnerd13d3022009-03-31 08:36:08 +0000341 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattnerab351632009-02-20 20:59:54 +0000342 if (*I) (*I)->Destroy(C);
343
Chris Lattner38af2de2009-02-20 21:35:13 +0000344 IVars.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000345 // FIXME: CategoryList?
346
347 // FIXME: Because there is no clear ownership
348 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
349 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
350 Decl::Destroy(C);
351}
352
353
354/// FindCategoryDeclaration - Finds category declaration in the list of
355/// categories for this class and returns it. Name of the category is passed
356/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000357///
Chris Lattnerab351632009-02-20 20:59:54 +0000358ObjCCategoryDecl *
359ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
360 for (ObjCCategoryDecl *Category = getCategoryList();
361 Category; Category = Category->getNextClassCategory())
362 if (Category->getIdentifier() == CategoryId)
363 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000364 return 0;
365}
366
Chris Lattnerab351632009-02-20 20:59:54 +0000367/// lookupFieldDeclForIvar - looks up a field decl' in the laid out
368/// storage which matches this 'ivar'.
369///
370FieldDecl *ObjCInterfaceDecl::lookupFieldDeclForIvar(ASTContext &Context,
371 const ObjCIvarDecl *IVar) {
372 const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
373 assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
Chris Lattnerd13d3022009-03-31 08:36:08 +0000374 DeclContext::lookup_const_result Lookup =
375 RecordForDecl->lookup(IVar->getDeclName());
Chris Lattnerab351632009-02-20 20:59:54 +0000376 assert((Lookup.first != Lookup.second) && "field decl not found");
Chris Lattnerd13d3022009-03-31 08:36:08 +0000377 return cast<FieldDecl>(*Lookup.first);
Chris Lattnerab351632009-02-20 20:59:54 +0000378}
379
380//===----------------------------------------------------------------------===//
381// ObjCIvarDecl
382//===----------------------------------------------------------------------===//
383
384ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
385 SourceLocation L, IdentifierInfo *Id,
386 QualType T, AccessControl ac, Expr *BW) {
387 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
388}
389
390
391
392//===----------------------------------------------------------------------===//
393// ObjCAtDefsFieldDecl
394//===----------------------------------------------------------------------===//
395
396ObjCAtDefsFieldDecl
397*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
398 IdentifierInfo *Id, QualType T, Expr *BW) {
399 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
400}
401
402void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
403 this->~ObjCAtDefsFieldDecl();
404 C.Deallocate((void *)this);
405}
406
407//===----------------------------------------------------------------------===//
408// ObjCProtocolDecl
409//===----------------------------------------------------------------------===//
410
411ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
412 SourceLocation L,
413 IdentifierInfo *Id) {
414 return new (C) ObjCProtocolDecl(DC, L, Id);
415}
416
417void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000418 ReferencedProtocols.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000419 ObjCContainerDecl::Destroy(C);
420}
421
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000422ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
423 ObjCProtocolDecl *PDecl = this;
424
425 if (Name == getIdentifier())
426 return PDecl;
427
428 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
429 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
430 return PDecl;
431
432 return NULL;
433}
434
Chris Lattnerab351632009-02-20 20:59:54 +0000435// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
436// it inherited.
437ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
438 ObjCMethodDecl *MethodDecl = NULL;
439
440 if ((MethodDecl = getInstanceMethod(Sel)))
441 return MethodDecl;
442
443 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
444 if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
445 return MethodDecl;
446 return NULL;
447}
448
449// lookupInstanceMethod - Lookup a class method in the protocol and protocols
450// it inherited.
451ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
452 ObjCMethodDecl *MethodDecl = NULL;
453
454 if ((MethodDecl = getClassMethod(Sel)))
455 return MethodDecl;
456
457 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
458 if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
459 return MethodDecl;
460 return NULL;
461}
462
463//===----------------------------------------------------------------------===//
464// ObjCClassDecl
465//===----------------------------------------------------------------------===//
466
Chris Lattner38af2de2009-02-20 21:35:13 +0000467ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
468 ObjCInterfaceDecl *const *Elts, unsigned nElts,
469 ASTContext &C)
470 : Decl(ObjCClass, DC, L) {
471 ForwardDecls.set(Elts, nElts, C);
472}
473
474
Chris Lattnerab351632009-02-20 20:59:54 +0000475ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
476 SourceLocation L,
477 ObjCInterfaceDecl *const *Elts,
478 unsigned nElts) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000479 return new (C) ObjCClassDecl(DC, L, Elts, nElts, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000480}
481
482void ObjCClassDecl::Destroy(ASTContext &C) {
483
484 // FIXME: There is no clear ownership policy now for referenced
485 // ObjCInterfaceDecls. Some of them can be forward declarations that
486 // are never later defined (in which case the ObjCClassDecl owns them)
487 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
488 // we should have separate objects for forward declarations and definitions,
489 // obviating this problem. Because of this situation, referenced
490 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
491
Chris Lattner38af2de2009-02-20 21:35:13 +0000492 ForwardDecls.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000493 Decl::Destroy(C);
494}
495
496//===----------------------------------------------------------------------===//
497// ObjCForwardProtocolDecl
498//===----------------------------------------------------------------------===//
499
Chris Lattner38af2de2009-02-20 21:35:13 +0000500ObjCForwardProtocolDecl::
501ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
502 ObjCProtocolDecl *const *Elts, unsigned nElts,
503 ASTContext &C)
504: Decl(ObjCForwardProtocol, DC, L) {
505 ReferencedProtocols.set(Elts, nElts, C);
506}
507
508
Chris Lattnerab351632009-02-20 20:59:54 +0000509ObjCForwardProtocolDecl *
510ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
511 SourceLocation L,
512 ObjCProtocolDecl *const *Elts,
513 unsigned NumElts) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000514 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000515}
516
517void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000518 ReferencedProtocols.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000519 Decl::Destroy(C);
520}
521
522//===----------------------------------------------------------------------===//
523// ObjCCategoryDecl
524//===----------------------------------------------------------------------===//
525
526ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
527 SourceLocation L,
528 IdentifierInfo *Id) {
529 return new (C) ObjCCategoryDecl(DC, L, Id);
530}
531
532//===----------------------------------------------------------------------===//
533// ObjCCategoryImplDecl
534//===----------------------------------------------------------------------===//
535
536ObjCCategoryImplDecl *
537ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
538 SourceLocation L,IdentifierInfo *Id,
539 ObjCInterfaceDecl *ClassInterface) {
540 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
541}
542
543
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000544/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +0000545/// properties implemented in this category @implementation block and returns
546/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000547///
Chris Lattner3aa18612009-02-28 18:42:10 +0000548ObjCPropertyImplDecl *ObjCImplDecl::
Chris Lattnerd6eed1c2009-02-16 19:24:31 +0000549FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
550 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000551 ObjCPropertyImplDecl *PID = *i;
552 if (PID->getPropertyIvarDecl() &&
553 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
554 return PID;
555 }
556 return 0;
557}
558
559/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
560/// added to the list of those properties @synthesized/@dynamic in this
561/// category @implementation block.
562///
Chris Lattner3aa18612009-02-28 18:42:10 +0000563ObjCPropertyImplDecl *ObjCImplDecl::
Chris Lattnerd6eed1c2009-02-16 19:24:31 +0000564FindPropertyImplDecl(IdentifierInfo *Id) const {
565 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000566 ObjCPropertyImplDecl *PID = *i;
567 if (PID->getPropertyDecl()->getIdentifier() == Id)
568 return PID;
569 }
570 return 0;
571}
572
Chris Lattner3aa18612009-02-28 18:42:10 +0000573// getInstanceMethod - This method returns an instance method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000574// the class implementation. Unlike interfaces, we don't look outside the
575// implementation.
Chris Lattner3aa18612009-02-28 18:42:10 +0000576ObjCMethodDecl *ObjCImplDecl::getInstanceMethod(Selector Sel) const {
Chris Lattner1e03a562008-03-16 00:19:01 +0000577 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
578 if ((*I)->getSelector() == Sel)
579 return *I;
580 return NULL;
581}
582
Chris Lattner3aa18612009-02-28 18:42:10 +0000583// getClassMethod - This method returns an instance method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000584// the class implementation. Unlike interfaces, we don't look outside the
585// implementation.
Chris Lattner3aa18612009-02-28 18:42:10 +0000586ObjCMethodDecl *ObjCImplDecl::getClassMethod(Selector Sel) const {
Chris Lattner1e03a562008-03-16 00:19:01 +0000587 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
588 I != E; ++I)
589 if ((*I)->getSelector() == Sel)
590 return *I;
591 return NULL;
592}
593
Chris Lattnerab351632009-02-20 20:59:54 +0000594//===----------------------------------------------------------------------===//
595// ObjCImplementationDecl
596//===----------------------------------------------------------------------===//
597
598ObjCImplementationDecl *
599ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
600 SourceLocation L,
601 ObjCInterfaceDecl *ClassInterface,
602 ObjCInterfaceDecl *SuperDecl) {
603 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
604}
605
606/// Destroy - Call destructors and release memory.
Chris Lattner38af2de2009-02-20 21:35:13 +0000607void ObjCImplementationDecl::Destroy(ASTContext &C) {
608 IVars.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000609 Decl::Destroy(C);
610}
611
Chris Lattnerab351632009-02-20 20:59:54 +0000612//===----------------------------------------------------------------------===//
613// ObjCCompatibleAliasDecl
614//===----------------------------------------------------------------------===//
615
616ObjCCompatibleAliasDecl *
617ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
618 SourceLocation L,
619 IdentifierInfo *Id,
620 ObjCInterfaceDecl* AliasedClass) {
621 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
622}
623
624//===----------------------------------------------------------------------===//
625// ObjCPropertyDecl
626//===----------------------------------------------------------------------===//
627
628ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
629 SourceLocation L,
630 IdentifierInfo *Id,
631 QualType T,
632 PropertyControl propControl) {
633 return new (C) ObjCPropertyDecl(DC, L, Id, T);
634}
635
636
637//===----------------------------------------------------------------------===//
638// ObjCPropertyImplDecl
639//===----------------------------------------------------------------------===//
640
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000641ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +0000642 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000643 SourceLocation atLoc,
644 SourceLocation L,
645 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000646 Kind PK,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000647 ObjCIvarDecl *ivar) {
Steve Naroff3e970492009-01-27 21:25:57 +0000648 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000649}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000650
Chris Lattner0ed844b2008-04-04 06:12:32 +0000651