blob: 503bc7052dc64bf98b501a52a5d9e73dad93d9af [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
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000045/// getIvarDecl - This method looks up an ivar in this ContextDecl.
46///
47ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000048ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000049 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000050 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000051 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
52 return ivar;
53 }
54 return 0;
55}
56
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000057// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000058ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000059ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000060 // Since instance & class methods can have the same name, the loop below
61 // ensures we get the correct method.
62 //
63 // @interface Whatever
64 // - (int) class_method;
65 // + (float) class_method;
66 // @end
67 //
68 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000069 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000070 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000071 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000072 return MD;
73 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000074 return 0;
75}
76
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000077/// FindPropertyDeclaration - Finds declaration of the property given its name
78/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroff93983f82009-01-11 12:47:58 +000079/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000080///
81ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000082ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
83 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
Chris Lattnerab351632009-02-20 20:59:54 +000084 if ((*I)->getIdentifier() == PropertyId)
85 return *I;
86
Fariborz Jahaniana66793e2009-01-09 21:04:52 +000087 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
88 if (PID) {
Chris Lattnerab351632009-02-20 20:59:54 +000089 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
90 E = PID->protocol_end(); I != E; ++I)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000091 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattnerab351632009-02-20 20:59:54 +000092 return P;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +000093 }
94
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +000095 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff09c47192009-01-09 15:36:25 +000096 // Look through categories.
97 for (ObjCCategoryDecl *Category = OID->getCategoryList();
98 Category; Category = Category->getNextClassCategory()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000099 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
Chris Lattnerab351632009-02-20 20:59:54 +0000100 return P;
Steve Naroff09c47192009-01-09 15:36:25 +0000101 }
102 // Look through protocols.
103 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
104 E = OID->protocol_end(); I != E; ++I) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000105 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattnerab351632009-02-20 20:59:54 +0000106 return P;
Steve Naroff09c47192009-01-09 15:36:25 +0000107 }
108 if (OID->getSuperClass())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000109 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattnerab351632009-02-20 20:59:54 +0000110 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000111 // Look through protocols.
112 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
113 E = OCD->protocol_end(); I != E; ++I) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000114 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattnerab351632009-02-20 20:59:54 +0000115 return P;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000116 }
117 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000118 return 0;
119}
120
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000121ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
122 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner1e03a562008-03-16 00:19:01 +0000123 ObjCInterfaceDecl* ClassDecl = this;
124 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000125 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000126 clsDeclared = ClassDecl;
127 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000128 }
129 ClassDecl = ClassDecl->getSuperClass();
130 }
131 return NULL;
132}
133
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000134/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
135/// class whose name is passed as argument. If it is not one of the super classes
136/// the it returns NULL.
137ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
138 const IdentifierInfo*ICName) {
139 ObjCInterfaceDecl* ClassDecl = this;
140 while (ClassDecl != NULL) {
141 if (ClassDecl->getIdentifier() == ICName)
142 return ClassDecl;
143 ClassDecl = ClassDecl->getSuperClass();
144 }
145 return NULL;
146}
147
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000148/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000149/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000150ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
151 bool isInstance) const {
152 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000153 ObjCMethodDecl *MethodDecl = 0;
154
155 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000156 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000157 return MethodDecl;
158
159 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000160 const ObjCList<ObjCProtocolDecl> &Protocols =
161 ClassDecl->getReferencedProtocols();
162 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
163 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000164 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000165 return MethodDecl;
Chris Lattner3db6cae2008-07-21 18:19:38 +0000166
Chris Lattner1e03a562008-03-16 00:19:01 +0000167 // Didn't find one yet - now look through categories.
168 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
169 while (CatDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000170 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000171 return MethodDecl;
Steve Naroffb79c01e2008-12-08 20:57:28 +0000172
173 // Didn't find one yet - look through protocols.
174 const ObjCList<ObjCProtocolDecl> &Protocols =
175 CatDecl->getReferencedProtocols();
176 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
177 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000178 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000179 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000180 CatDecl = CatDecl->getNextClassCategory();
181 }
182 ClassDecl = ClassDecl->getSuperClass();
183 }
184 return NULL;
185}
186
Chris Lattnerab351632009-02-20 20:59:54 +0000187
188
189//===----------------------------------------------------------------------===//
190// ObjCMethodDecl
191//===----------------------------------------------------------------------===//
192
193ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
194 SourceLocation beginLoc,
195 SourceLocation endLoc,
196 Selector SelInfo, QualType T,
197 DeclContext *contextDecl,
198 bool isInstance,
199 bool isVariadic,
200 bool isSynthesized,
201 ImplementationControl impControl) {
202 return new (C) ObjCMethodDecl(beginLoc, endLoc,
203 SelInfo, T, contextDecl,
204 isInstance,
205 isVariadic, isSynthesized, impControl);
Chris Lattner1e03a562008-03-16 00:19:01 +0000206}
207
Chris Lattner38af2de2009-02-20 21:35:13 +0000208void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattnerab351632009-02-20 20:59:54 +0000209 if (Body) Body->Destroy(C);
210 if (SelfDecl) SelfDecl->Destroy(C);
211
212 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
213 if (*I) (*I)->Destroy(C);
214
Chris Lattner38af2de2009-02-20 21:35:13 +0000215 ParamInfo.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000216
217 Decl::Destroy(C);
Chris Lattner1e03a562008-03-16 00:19:01 +0000218}
219
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000220/// \brief A definition will return its interface declaration.
221/// An interface declaration will return its definition.
222/// Otherwise it will return itself.
223ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
224 ASTContext &Ctx = getASTContext();
225 ObjCMethodDecl *Redecl = 0;
226 Decl *CtxD = cast<Decl>(getDeclContext());
227
228 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
229 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
230 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
231
232 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
233 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
234 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
235
236 } else if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(CtxD)) {
237 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
238 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
239 }
240
241 return Redecl ? Redecl : this;
242}
243
Chris Lattnerab351632009-02-20 20:59:54 +0000244void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
245 const ObjCInterfaceDecl *OID) {
246 QualType selfTy;
247 if (isInstanceMethod()) {
248 // There may be no interface context due to error in declaration
249 // of the interface (which has been reported). Recover gracefully.
250 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000251 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000252 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000253 } else {
254 selfTy = Context.getObjCIdType();
255 }
256 } else // we have a factory method.
257 selfTy = Context.getObjCClassType();
258
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000259 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
260 &Context.Idents.get("self"), selfTy));
Chris Lattnerab351632009-02-20 20:59:54 +0000261
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000262 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
263 &Context.Idents.get("_cmd"),
264 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000265}
266
267
268
269/// getSynthesizedMethodSize - Compute size of synthesized method name
270/// as done be the rewrite.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000271///
Chris Lattnerab351632009-02-20 20:59:54 +0000272unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
273 // syntesized method name is a concatenation of -/+[class-name selector]
274 // Get length of this name.
275 unsigned length = 3; // _I_ or _C_
276 length += getClassInterface()->getNameAsString().size()+1; // extra for _
277 if (const ObjCCategoryImplDecl *CID =
278 dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
279 length += CID->getNameAsString().size()+1;
280 length += getSelector().getAsString().size(); // selector name
281 return length;
282}
283
284ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
285 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
286 return ID;
287 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
288 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000289 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000290 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000291
292 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattnerab351632009-02-20 20:59:54 +0000293 assert(false && "unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000294 return 0;
295}
296
Chris Lattnerab351632009-02-20 20:59:54 +0000297//===----------------------------------------------------------------------===//
298// ObjCInterfaceDecl
299//===----------------------------------------------------------------------===//
300
301ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
302 DeclContext *DC,
303 SourceLocation atLoc,
304 IdentifierInfo *Id,
305 SourceLocation ClassLoc,
306 bool ForwardDecl, bool isInternal){
307 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
308 isInternal);
309}
310
311ObjCInterfaceDecl::
312ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
313 SourceLocation CLoc, bool FD, bool isInternal)
314 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
315 TypeForDecl(0), SuperClass(0),
316 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
317 ClassLoc(CLoc) {
318}
319
320void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnerd13d3022009-03-31 08:36:08 +0000321 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattnerab351632009-02-20 20:59:54 +0000322 if (*I) (*I)->Destroy(C);
323
Chris Lattner38af2de2009-02-20 21:35:13 +0000324 IVars.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000325 // FIXME: CategoryList?
326
327 // FIXME: Because there is no clear ownership
328 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
329 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
330 Decl::Destroy(C);
331}
332
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000333ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
334 return getASTContext().getObjCImplementation(
335 const_cast<ObjCInterfaceDecl*>(this));
336}
337
338void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
339 getASTContext().setObjCImplementation(this, ImplD);
340}
341
Chris Lattnerab351632009-02-20 20:59:54 +0000342
343/// FindCategoryDeclaration - Finds category declaration in the list of
344/// categories for this class and returns it. Name of the category is passed
345/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000346///
Chris Lattnerab351632009-02-20 20:59:54 +0000347ObjCCategoryDecl *
348ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
349 for (ObjCCategoryDecl *Category = getCategoryList();
350 Category; Category = Category->getNextClassCategory())
351 if (Category->getIdentifier() == CategoryId)
352 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000353 return 0;
354}
355
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000356ObjCMethodDecl *
357ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
358 for (ObjCCategoryDecl *Category = getCategoryList();
359 Category; Category = Category->getNextClassCategory())
360 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
361 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
362 return MD;
363 return 0;
364}
365
366ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
367 for (ObjCCategoryDecl *Category = getCategoryList();
368 Category; Category = Category->getNextClassCategory())
369 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
370 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
371 return MD;
372 return 0;
373}
374
Chris Lattnerab351632009-02-20 20:59:54 +0000375//===----------------------------------------------------------------------===//
376// ObjCIvarDecl
377//===----------------------------------------------------------------------===//
378
379ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
380 SourceLocation L, IdentifierInfo *Id,
381 QualType T, AccessControl ac, Expr *BW) {
382 return new (C) ObjCIvarDecl(DC, L, Id, T, ac, BW);
383}
384
385
386
387//===----------------------------------------------------------------------===//
388// ObjCAtDefsFieldDecl
389//===----------------------------------------------------------------------===//
390
391ObjCAtDefsFieldDecl
392*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
393 IdentifierInfo *Id, QualType T, Expr *BW) {
394 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
395}
396
397void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
398 this->~ObjCAtDefsFieldDecl();
399 C.Deallocate((void *)this);
400}
401
402//===----------------------------------------------------------------------===//
403// ObjCProtocolDecl
404//===----------------------------------------------------------------------===//
405
406ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
407 SourceLocation L,
408 IdentifierInfo *Id) {
409 return new (C) ObjCProtocolDecl(DC, L, Id);
410}
411
412void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000413 ReferencedProtocols.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000414 ObjCContainerDecl::Destroy(C);
415}
416
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000417ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
418 ObjCProtocolDecl *PDecl = this;
419
420 if (Name == getIdentifier())
421 return PDecl;
422
423 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
424 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
425 return PDecl;
426
427 return NULL;
428}
429
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000430// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +0000431// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000432ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
433 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +0000434 ObjCMethodDecl *MethodDecl = NULL;
435
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000436 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000437 return MethodDecl;
438
439 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000440 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000441 return MethodDecl;
442 return NULL;
443}
444
445//===----------------------------------------------------------------------===//
446// ObjCClassDecl
447//===----------------------------------------------------------------------===//
448
Chris Lattner38af2de2009-02-20 21:35:13 +0000449ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
450 ObjCInterfaceDecl *const *Elts, unsigned nElts,
451 ASTContext &C)
452 : Decl(ObjCClass, DC, L) {
453 ForwardDecls.set(Elts, nElts, C);
454}
455
456
Chris Lattnerab351632009-02-20 20:59:54 +0000457ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
458 SourceLocation L,
459 ObjCInterfaceDecl *const *Elts,
460 unsigned nElts) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000461 return new (C) ObjCClassDecl(DC, L, Elts, nElts, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000462}
463
464void ObjCClassDecl::Destroy(ASTContext &C) {
465
466 // FIXME: There is no clear ownership policy now for referenced
467 // ObjCInterfaceDecls. Some of them can be forward declarations that
468 // are never later defined (in which case the ObjCClassDecl owns them)
469 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
470 // we should have separate objects for forward declarations and definitions,
471 // obviating this problem. Because of this situation, referenced
472 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
473
Chris Lattner38af2de2009-02-20 21:35:13 +0000474 ForwardDecls.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000475 Decl::Destroy(C);
476}
477
478//===----------------------------------------------------------------------===//
479// ObjCForwardProtocolDecl
480//===----------------------------------------------------------------------===//
481
Chris Lattner38af2de2009-02-20 21:35:13 +0000482ObjCForwardProtocolDecl::
483ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
484 ObjCProtocolDecl *const *Elts, unsigned nElts,
485 ASTContext &C)
486: Decl(ObjCForwardProtocol, DC, L) {
487 ReferencedProtocols.set(Elts, nElts, C);
488}
489
490
Chris Lattnerab351632009-02-20 20:59:54 +0000491ObjCForwardProtocolDecl *
492ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
493 SourceLocation L,
494 ObjCProtocolDecl *const *Elts,
495 unsigned NumElts) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000496 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000497}
498
499void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000500 ReferencedProtocols.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000501 Decl::Destroy(C);
502}
503
504//===----------------------------------------------------------------------===//
505// ObjCCategoryDecl
506//===----------------------------------------------------------------------===//
507
508ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
509 SourceLocation L,
510 IdentifierInfo *Id) {
511 return new (C) ObjCCategoryDecl(DC, L, Id);
512}
513
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000514ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
515 return getASTContext().getObjCImplementation(
516 const_cast<ObjCCategoryDecl*>(this));
517}
518
519void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
520 getASTContext().setObjCImplementation(this, ImplD);
521}
522
523
Chris Lattnerab351632009-02-20 20:59:54 +0000524//===----------------------------------------------------------------------===//
525// ObjCCategoryImplDecl
526//===----------------------------------------------------------------------===//
527
528ObjCCategoryImplDecl *
529ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
530 SourceLocation L,IdentifierInfo *Id,
531 ObjCInterfaceDecl *ClassInterface) {
532 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
533}
534
535
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000536void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000537 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000538 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000539 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +0000540}
541
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000542void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
543 ASTContext &Ctx = getASTContext();
544
545 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +0000546 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000547 if (IFace)
548 Ctx.setObjCImplementation(IFace, ImplD);
549
Duncan Sands98f2cca2009-07-21 07:56:29 +0000550 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000551 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
552 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
553 Ctx.setObjCImplementation(CD, ImplD);
554 }
555
556 ClassInterface = IFace;
557}
558
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000559/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +0000560/// properties implemented in this category @implementation block and returns
561/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000562///
Chris Lattner3aa18612009-02-28 18:42:10 +0000563ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000564FindPropertyImplIvarDecl(IdentifierInfo *ivarId) 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->getPropertyIvarDecl() &&
568 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
569 return PID;
570 }
571 return 0;
572}
573
574/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
575/// added to the list of those properties @synthesized/@dynamic in this
576/// category @implementation block.
577///
Chris Lattner3aa18612009-02-28 18:42:10 +0000578ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000579FindPropertyImplDecl(IdentifierInfo *Id) const {
580 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000581 ObjCPropertyImplDecl *PID = *i;
582 if (PID->getPropertyDecl()->getIdentifier() == Id)
583 return PID;
584 }
585 return 0;
586}
587
Chris Lattnerab351632009-02-20 20:59:54 +0000588//===----------------------------------------------------------------------===//
589// ObjCImplementationDecl
590//===----------------------------------------------------------------------===//
591
592ObjCImplementationDecl *
593ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
594 SourceLocation L,
595 ObjCInterfaceDecl *ClassInterface,
596 ObjCInterfaceDecl *SuperDecl) {
597 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
598}
599
Chris Lattnerab351632009-02-20 20:59:54 +0000600//===----------------------------------------------------------------------===//
601// ObjCCompatibleAliasDecl
602//===----------------------------------------------------------------------===//
603
604ObjCCompatibleAliasDecl *
605ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
606 SourceLocation L,
607 IdentifierInfo *Id,
608 ObjCInterfaceDecl* AliasedClass) {
609 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
610}
611
612//===----------------------------------------------------------------------===//
613// ObjCPropertyDecl
614//===----------------------------------------------------------------------===//
615
616ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
617 SourceLocation L,
618 IdentifierInfo *Id,
619 QualType T,
620 PropertyControl propControl) {
621 return new (C) ObjCPropertyDecl(DC, L, Id, T);
622}
623
624
625//===----------------------------------------------------------------------===//
626// ObjCPropertyImplDecl
627//===----------------------------------------------------------------------===//
628
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000629ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +0000630 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000631 SourceLocation atLoc,
632 SourceLocation L,
633 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000634 Kind PK,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000635 ObjCIvarDecl *ivar) {
Steve Naroff3e970492009-01-27 21:25:57 +0000636 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000637}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000638
Chris Lattner0ed844b2008-04-04 06:12:32 +0000639