blob: 49a566878ff0c8765947004d78391fd012c12ba8 [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.
Mike Stump1eb44332009-09-09 15:08:12 +000033
34
Chris Lattner4ee413b2009-02-20 21:44:01 +000035 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;
Mike Stump1eb44332009-09-09 15:08:12 +000086
Fariborz Jahaniana66793e2009-01-09 21:04:52 +000087 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
88 if (PID) {
Mike Stump1eb44332009-09-09 15:08:12 +000089 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Chris Lattnerab351632009-02-20 20:59:54 +000090 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 }
Mike Stump1eb44332009-09-09 15:08:12 +000094
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
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000121void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
122 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
123 ASTContext &C)
124{
125 if (ReferencedProtocols.empty()) {
126 ReferencedProtocols.set(ExtList, ExtNum, C);
127 return;
128 }
129 // Check for duplicate protocol in class's protocol list.
130 // This is (O)2. But it is extremely rare and number of protocols in
131 // class or its extension are very few.
132 llvm::SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
133 for (unsigned i = 0; i < ExtNum; i++) {
134 bool protocolExists = false;
135 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
136 for (protocol_iterator p = protocol_begin(), e = protocol_end();
137 p != e; p++) {
138 ObjCProtocolDecl *Proto = (*p);
139 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
140 protocolExists = true;
141 break;
142 }
143 }
144 // Do we want to warn on a protocol in extension class which
145 // already exist in the class? Probably not.
146 if (!protocolExists)
147 ProtocolRefs.push_back(ProtoInExtension);
148 }
149 if (ProtocolRefs.empty())
150 return;
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000151 // Merge ProtocolRefs into class's protocol list;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000152 for (protocol_iterator p = protocol_begin(), e = protocol_end();
153 p != e; p++)
154 ProtocolRefs.push_back(*p);
155 ReferencedProtocols.Destroy(C);
156 unsigned NumProtoRefs = ProtocolRefs.size();
157 setProtocolList((ObjCProtocolDecl**)&ProtocolRefs[0], NumProtoRefs, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000158}
159
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000160ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
161 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner1e03a562008-03-16 00:19:01 +0000162 ObjCInterfaceDecl* ClassDecl = this;
163 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000164 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000165 clsDeclared = ClassDecl;
166 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000167 }
168 ClassDecl = ClassDecl->getSuperClass();
169 }
170 return NULL;
171}
172
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000173/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
174/// class whose name is passed as argument. If it is not one of the super classes
175/// the it returns NULL.
176ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
177 const IdentifierInfo*ICName) {
178 ObjCInterfaceDecl* ClassDecl = this;
179 while (ClassDecl != NULL) {
180 if (ClassDecl->getIdentifier() == ICName)
181 return ClassDecl;
182 ClassDecl = ClassDecl->getSuperClass();
183 }
184 return NULL;
185}
186
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000187/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000188/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000189ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
190 bool isInstance) const {
191 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000192 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Chris Lattner1e03a562008-03-16 00:19:01 +0000194 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000195 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000196 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Chris Lattner1e03a562008-03-16 00:19:01 +0000198 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000199 const ObjCList<ObjCProtocolDecl> &Protocols =
200 ClassDecl->getReferencedProtocols();
201 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
202 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000203 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000204 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Chris Lattner1e03a562008-03-16 00:19:01 +0000206 // Didn't find one yet - now look through categories.
207 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
208 while (CatDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000209 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000210 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Steve Naroffb79c01e2008-12-08 20:57:28 +0000212 // Didn't find one yet - look through protocols.
213 const ObjCList<ObjCProtocolDecl> &Protocols =
214 CatDecl->getReferencedProtocols();
215 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
216 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000217 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000218 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000219 CatDecl = CatDecl->getNextClassCategory();
220 }
221 ClassDecl = ClassDecl->getSuperClass();
222 }
223 return NULL;
224}
225
Steve Naroffd789d3d2009-10-01 23:46:04 +0000226ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateInstanceMethod(
227 const Selector &Sel) {
228 ObjCMethodDecl *Method = 0;
229 if (ObjCImplementationDecl *ImpDecl = getImplementation())
230 Method = ImpDecl->getInstanceMethod(Sel);
231
232 if (!Method && getSuperClass())
233 return getSuperClass()->lookupPrivateInstanceMethod(Sel);
234 return Method;
235}
Chris Lattnerab351632009-02-20 20:59:54 +0000236
237//===----------------------------------------------------------------------===//
238// ObjCMethodDecl
239//===----------------------------------------------------------------------===//
240
241ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000242 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000243 SourceLocation endLoc,
244 Selector SelInfo, QualType T,
245 DeclContext *contextDecl,
246 bool isInstance,
247 bool isVariadic,
248 bool isSynthesized,
249 ImplementationControl impControl) {
250 return new (C) ObjCMethodDecl(beginLoc, endLoc,
251 SelInfo, T, contextDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000252 isInstance,
Chris Lattnerab351632009-02-20 20:59:54 +0000253 isVariadic, isSynthesized, impControl);
Chris Lattner1e03a562008-03-16 00:19:01 +0000254}
255
Chris Lattner38af2de2009-02-20 21:35:13 +0000256void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattnerab351632009-02-20 20:59:54 +0000257 if (Body) Body->Destroy(C);
258 if (SelfDecl) SelfDecl->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Chris Lattnerab351632009-02-20 20:59:54 +0000260 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
261 if (*I) (*I)->Destroy(C);
262
Chris Lattner38af2de2009-02-20 21:35:13 +0000263 ParamInfo.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000264
265 Decl::Destroy(C);
Chris Lattner1e03a562008-03-16 00:19:01 +0000266}
267
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000268/// \brief A definition will return its interface declaration.
269/// An interface declaration will return its definition.
270/// Otherwise it will return itself.
271ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
272 ASTContext &Ctx = getASTContext();
273 ObjCMethodDecl *Redecl = 0;
274 Decl *CtxD = cast<Decl>(getDeclContext());
275
276 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
277 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
278 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
279
280 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
281 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
282 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
283
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000284 } else if (ObjCImplementationDecl *ImplD =
285 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000286 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
287 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000288
289 } else if (ObjCCategoryImplDecl *CImplD =
290 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000291 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000292 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000293 }
294
295 return Redecl ? Redecl : this;
296}
297
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000298ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
299 Decl *CtxD = cast<Decl>(getDeclContext());
300
301 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
302 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
303 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
304 isInstanceMethod()))
305 return MD;
306
307 } else if (ObjCCategoryImplDecl *CImplD =
308 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000309 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000310 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
311 isInstanceMethod()))
312 return MD;
313 }
314
315 return this;
316}
317
Mike Stump1eb44332009-09-09 15:08:12 +0000318void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000319 const ObjCInterfaceDecl *OID) {
320 QualType selfTy;
321 if (isInstanceMethod()) {
322 // There may be no interface context due to error in declaration
323 // of the interface (which has been reported). Recover gracefully.
324 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000325 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000326 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000327 } else {
328 selfTy = Context.getObjCIdType();
329 }
330 } else // we have a factory method.
331 selfTy = Context.getObjCClassType();
332
Mike Stump1eb44332009-09-09 15:08:12 +0000333 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000334 &Context.Idents.get("self"), selfTy));
Chris Lattnerab351632009-02-20 20:59:54 +0000335
Mike Stump1eb44332009-09-09 15:08:12 +0000336 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
337 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000338 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000339}
340
Chris Lattnerab351632009-02-20 20:59:54 +0000341ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
342 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
343 return ID;
344 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
345 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000346 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000347 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000348
349 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattnerab351632009-02-20 20:59:54 +0000350 assert(false && "unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000351 return 0;
352}
353
Chris Lattnerab351632009-02-20 20:59:54 +0000354//===----------------------------------------------------------------------===//
355// ObjCInterfaceDecl
356//===----------------------------------------------------------------------===//
357
358ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
359 DeclContext *DC,
360 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000361 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +0000362 SourceLocation ClassLoc,
363 bool ForwardDecl, bool isInternal){
364 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
365 isInternal);
366}
367
368ObjCInterfaceDecl::
369ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
370 SourceLocation CLoc, bool FD, bool isInternal)
371 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
372 TypeForDecl(0), SuperClass(0),
373 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
374 ClassLoc(CLoc) {
375}
376
Mike Stump1eb44332009-09-09 15:08:12 +0000377void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnerd13d3022009-03-31 08:36:08 +0000378 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattnerab351632009-02-20 20:59:54 +0000379 if (*I) (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Chris Lattner38af2de2009-02-20 21:35:13 +0000381 IVars.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000382 // FIXME: CategoryList?
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Chris Lattnerab351632009-02-20 20:59:54 +0000384 // FIXME: Because there is no clear ownership
385 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
386 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
387 Decl::Destroy(C);
388}
389
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000390ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
391 return getASTContext().getObjCImplementation(
392 const_cast<ObjCInterfaceDecl*>(this));
393}
394
395void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
396 getASTContext().setObjCImplementation(this, ImplD);
397}
398
Chris Lattnerab351632009-02-20 20:59:54 +0000399
400/// FindCategoryDeclaration - Finds category declaration in the list of
401/// categories for this class and returns it. Name of the category is passed
402/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000403///
Chris Lattnerab351632009-02-20 20:59:54 +0000404ObjCCategoryDecl *
405ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
406 for (ObjCCategoryDecl *Category = getCategoryList();
407 Category; Category = Category->getNextClassCategory())
408 if (Category->getIdentifier() == CategoryId)
409 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000410 return 0;
411}
412
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000413ObjCMethodDecl *
414ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
415 for (ObjCCategoryDecl *Category = getCategoryList();
416 Category; Category = Category->getNextClassCategory())
417 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
418 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
419 return MD;
420 return 0;
421}
422
423ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
424 for (ObjCCategoryDecl *Category = getCategoryList();
425 Category; Category = Category->getNextClassCategory())
426 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
427 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
428 return MD;
429 return 0;
430}
431
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000432/// ClassImplementsProtocol - Checks that 'lProto' protocol
433/// has been implemented in IDecl class, its super class or categories (if
434/// lookupCategory is true).
435bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
436 bool lookupCategory,
437 bool RHSIsQualifiedID) {
438 ObjCInterfaceDecl *IDecl = this;
439 // 1st, look up the class.
440 const ObjCList<ObjCProtocolDecl> &Protocols =
441 IDecl->getReferencedProtocols();
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000443 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
444 E = Protocols.end(); PI != E; ++PI) {
445 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
446 return true;
447 // This is dubious and is added to be compatible with gcc. In gcc, it is
448 // also allowed assigning a protocol-qualified 'id' type to a LHS object
449 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
450 // object. This IMO, should be a bug.
451 // FIXME: Treat this as an extension, and flag this as an error when GCC
452 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000453 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000454 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
455 return true;
456 }
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000458 // 2nd, look up the category.
459 if (lookupCategory)
460 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
461 CDecl = CDecl->getNextClassCategory()) {
462 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
463 E = CDecl->protocol_end(); PI != E; ++PI)
464 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
465 return true;
466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000468 // 3rd, look up the super class(s)
469 if (IDecl->getSuperClass())
470 return
471 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
472 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000474 return false;
475}
476
Chris Lattnerab351632009-02-20 20:59:54 +0000477//===----------------------------------------------------------------------===//
478// ObjCIvarDecl
479//===----------------------------------------------------------------------===//
480
481ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
482 SourceLocation L, IdentifierInfo *Id,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000483 QualType T, DeclaratorInfo *DInfo,
484 AccessControl ac, Expr *BW) {
485 return new (C) ObjCIvarDecl(DC, L, Id, T, DInfo, ac, BW);
Chris Lattnerab351632009-02-20 20:59:54 +0000486}
487
488
489
490//===----------------------------------------------------------------------===//
491// ObjCAtDefsFieldDecl
492//===----------------------------------------------------------------------===//
493
494ObjCAtDefsFieldDecl
495*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
496 IdentifierInfo *Id, QualType T, Expr *BW) {
497 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
498}
499
500void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
501 this->~ObjCAtDefsFieldDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000502 C.Deallocate((void *)this);
Chris Lattnerab351632009-02-20 20:59:54 +0000503}
504
505//===----------------------------------------------------------------------===//
506// ObjCProtocolDecl
507//===----------------------------------------------------------------------===//
508
509ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000510 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000511 IdentifierInfo *Id) {
512 return new (C) ObjCProtocolDecl(DC, L, Id);
513}
514
515void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000516 ReferencedProtocols.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000517 ObjCContainerDecl::Destroy(C);
518}
519
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000520ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
521 ObjCProtocolDecl *PDecl = this;
522
523 if (Name == getIdentifier())
524 return PDecl;
525
526 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
527 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
528 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000530 return NULL;
531}
532
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000533// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +0000534// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000535ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
536 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +0000537 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000539 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000540 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Chris Lattnerab351632009-02-20 20:59:54 +0000542 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000543 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000544 return MethodDecl;
545 return NULL;
546}
547
548//===----------------------------------------------------------------------===//
549// ObjCClassDecl
550//===----------------------------------------------------------------------===//
551
Mike Stump1eb44332009-09-09 15:08:12 +0000552ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Chris Lattner38af2de2009-02-20 21:35:13 +0000553 ObjCInterfaceDecl *const *Elts, unsigned nElts,
554 ASTContext &C)
555 : Decl(ObjCClass, DC, L) {
556 ForwardDecls.set(Elts, nElts, C);
557}
558
559
Chris Lattnerab351632009-02-20 20:59:54 +0000560ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
561 SourceLocation L,
562 ObjCInterfaceDecl *const *Elts,
563 unsigned nElts) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000564 return new (C) ObjCClassDecl(DC, L, Elts, nElts, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000565}
566
567void ObjCClassDecl::Destroy(ASTContext &C) {
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Chris Lattnerab351632009-02-20 20:59:54 +0000569 // FIXME: There is no clear ownership policy now for referenced
570 // ObjCInterfaceDecls. Some of them can be forward declarations that
571 // are never later defined (in which case the ObjCClassDecl owns them)
572 // or the ObjCInterfaceDecl later becomes a real definition later. Ideally
573 // we should have separate objects for forward declarations and definitions,
574 // obviating this problem. Because of this situation, referenced
575 // ObjCInterfaceDecls are destroyed in ~TranslationUnit.
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattner38af2de2009-02-20 21:35:13 +0000577 ForwardDecls.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000578 Decl::Destroy(C);
579}
580
581//===----------------------------------------------------------------------===//
582// ObjCForwardProtocolDecl
583//===----------------------------------------------------------------------===//
584
Chris Lattner38af2de2009-02-20 21:35:13 +0000585ObjCForwardProtocolDecl::
586ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
587 ObjCProtocolDecl *const *Elts, unsigned nElts,
588 ASTContext &C)
Mike Stump1eb44332009-09-09 15:08:12 +0000589: Decl(ObjCForwardProtocol, DC, L) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000590 ReferencedProtocols.set(Elts, nElts, C);
591}
592
593
Chris Lattnerab351632009-02-20 20:59:54 +0000594ObjCForwardProtocolDecl *
595ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000596 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000597 ObjCProtocolDecl *const *Elts,
598 unsigned NumElts) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000599 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000600}
601
602void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000603 ReferencedProtocols.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000604 Decl::Destroy(C);
605}
606
607//===----------------------------------------------------------------------===//
608// ObjCCategoryDecl
609//===----------------------------------------------------------------------===//
610
611ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
612 SourceLocation L,
613 IdentifierInfo *Id) {
614 return new (C) ObjCCategoryDecl(DC, L, Id);
615}
616
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000617ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
618 return getASTContext().getObjCImplementation(
619 const_cast<ObjCCategoryDecl*>(this));
620}
621
622void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
623 getASTContext().setObjCImplementation(this, ImplD);
624}
625
626
Chris Lattnerab351632009-02-20 20:59:54 +0000627//===----------------------------------------------------------------------===//
628// ObjCCategoryImplDecl
629//===----------------------------------------------------------------------===//
630
631ObjCCategoryImplDecl *
632ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
633 SourceLocation L,IdentifierInfo *Id,
634 ObjCInterfaceDecl *ClassInterface) {
635 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
636}
637
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000638ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000639 return getClassInterface()->FindCategoryDeclaration(getIdentifier());
640}
641
Chris Lattnerab351632009-02-20 20:59:54 +0000642
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000643void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000644 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000645 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000646 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +0000647}
648
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000649void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
650 ASTContext &Ctx = getASTContext();
651
652 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +0000653 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000654 if (IFace)
655 Ctx.setObjCImplementation(IFace, ImplD);
656
Duncan Sands98f2cca2009-07-21 07:56:29 +0000657 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000658 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
659 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
660 Ctx.setObjCImplementation(CD, ImplD);
661 }
662
663 ClassInterface = IFace;
664}
665
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000666/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +0000667/// properties implemented in this category @implementation block and returns
668/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000669///
Chris Lattner3aa18612009-02-28 18:42:10 +0000670ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000671FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
672 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000673 ObjCPropertyImplDecl *PID = *i;
674 if (PID->getPropertyIvarDecl() &&
675 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
676 return PID;
677 }
678 return 0;
679}
680
681/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
682/// added to the list of those properties @synthesized/@dynamic in this
683/// category @implementation block.
684///
Chris Lattner3aa18612009-02-28 18:42:10 +0000685ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000686FindPropertyImplDecl(IdentifierInfo *Id) const {
687 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000688 ObjCPropertyImplDecl *PID = *i;
689 if (PID->getPropertyDecl()->getIdentifier() == Id)
690 return PID;
691 }
692 return 0;
693}
694
Chris Lattnerab351632009-02-20 20:59:54 +0000695//===----------------------------------------------------------------------===//
696// ObjCImplementationDecl
697//===----------------------------------------------------------------------===//
698
699ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000700ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +0000701 SourceLocation L,
702 ObjCInterfaceDecl *ClassInterface,
703 ObjCInterfaceDecl *SuperDecl) {
704 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
705}
706
Chris Lattnerab351632009-02-20 20:59:54 +0000707//===----------------------------------------------------------------------===//
708// ObjCCompatibleAliasDecl
709//===----------------------------------------------------------------------===//
710
711ObjCCompatibleAliasDecl *
712ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
713 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +0000714 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +0000715 ObjCInterfaceDecl* AliasedClass) {
716 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
717}
718
719//===----------------------------------------------------------------------===//
720// ObjCPropertyDecl
721//===----------------------------------------------------------------------===//
722
723ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
724 SourceLocation L,
725 IdentifierInfo *Id,
726 QualType T,
727 PropertyControl propControl) {
728 return new (C) ObjCPropertyDecl(DC, L, Id, T);
729}
730
731
732//===----------------------------------------------------------------------===//
733// ObjCPropertyImplDecl
734//===----------------------------------------------------------------------===//
735
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000736ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +0000737 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000738 SourceLocation atLoc,
739 SourceLocation L,
740 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000741 Kind PK,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000742 ObjCIvarDecl *ivar) {
Steve Naroff3e970492009-01-27 21:25:57 +0000743 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000744}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000745
Chris Lattner0ed844b2008-04-04 06:12:32 +0000746