blob: 597b0d1bf0fb1f69ce640c9bca65117d6ecd9c68 [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
Douglas Gregor18df52b2010-01-16 15:02:53 +000040void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
41 const SourceLocation *Locs, ASTContext &Ctx) {
42 if (Elts == 0)
43 return;
44
45 Locations = new (Ctx) SourceLocation[Elts];
46 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
47 set(InList, Elts, Ctx);
48}
49
50void ObjCProtocolList::Destroy(ASTContext &Ctx) {
51 Ctx.Deallocate(Locations);
52 Locations = 0;
53 ObjCList<ObjCProtocolDecl>::Destroy(Ctx);
54}
Chris Lattner11e1e1a2009-02-20 21:16:26 +000055
56//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000057// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000058//===----------------------------------------------------------------------===//
59
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000060/// getIvarDecl - This method looks up an ivar in this ContextDecl.
61///
62ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000063ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000064 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000065 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000066 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
67 return ivar;
68 }
69 return 0;
70}
71
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000072// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000073ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000074ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000075 // Since instance & class methods can have the same name, the loop below
76 // ensures we get the correct method.
77 //
78 // @interface Whatever
79 // - (int) class_method;
80 // + (float) class_method;
81 // @end
82 //
83 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000084 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000085 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000086 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000087 return MD;
88 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000089 return 0;
90}
91
Ted Kremenek9f550ff2010-03-15 20:11:46 +000092ObjCPropertyDecl *
93ObjCPropertyDecl::findPropertyDecl(DeclContext *DC,
94 IdentifierInfo *propertyID) {
95
96 DeclContext::lookup_iterator I, E;
97 llvm::tie(I, E) = DC->lookup(propertyID);
98 for ( ; I != E; ++I)
99 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
100 return PD;
101
102 return 0;
103}
104
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000105/// FindPropertyDeclaration - Finds declaration of the property given its name
106/// in 'PropertyId' and returns it. It returns 0, if not found.
Steve Naroff93983f82009-01-11 12:47:58 +0000107/// FIXME: Convert to DeclContext lookup...
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000108///
109ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000110ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
111 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
Chris Lattnerab351632009-02-20 20:59:54 +0000112 if ((*I)->getIdentifier() == PropertyId)
113 return *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000115 const ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(this);
116 if (PID) {
Mike Stump1eb44332009-09-09 15:08:12 +0000117 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Chris Lattnerab351632009-02-20 20:59:54 +0000118 E = PID->protocol_end(); I != E; ++I)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000119 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattnerab351632009-02-20 20:59:54 +0000120 return P;
Fariborz Jahaniana66793e2009-01-09 21:04:52 +0000121 }
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000123 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this)) {
Steve Naroff09c47192009-01-09 15:36:25 +0000124 // Look through categories.
125 for (ObjCCategoryDecl *Category = OID->getCategoryList();
126 Category; Category = Category->getNextClassCategory()) {
Fariborz Jahanian25760612010-02-15 21:55:26 +0000127 if (!Category->IsClassExtension())
128 if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
129 return P;
Steve Naroff09c47192009-01-09 15:36:25 +0000130 }
131 // Look through protocols.
132 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
133 E = OID->protocol_end(); I != E; ++I) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000134 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
Chris Lattnerab351632009-02-20 20:59:54 +0000135 return P;
Steve Naroff09c47192009-01-09 15:36:25 +0000136 }
137 if (OID->getSuperClass())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000138 return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
Chris Lattnerab351632009-02-20 20:59:54 +0000139 } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000140 // Look through protocols.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000141 if (!OCD->IsClassExtension())
142 for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
143 E = OCD->protocol_end(); I != E; ++I) {
144 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
145 return P;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000146 }
147 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000148 return 0;
149}
150
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000151/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
152/// with name 'PropertyId' in the primary class; including those in protocols
153/// (direct or indirect) used by the promary class.
154/// FIXME: Convert to DeclContext lookup...
155///
156ObjCPropertyDecl *
157ObjCContainerDecl::FindPropertyVisibleInPrimaryClass(
158 IdentifierInfo *PropertyId) const {
159 assert(isa<ObjCInterfaceDecl>(this) && "FindPropertyVisibleInPrimaryClass");
160 for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
161 if ((*I)->getIdentifier() == PropertyId)
162 return *I;
163 const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this);
164 // Look through protocols.
165 for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
166 E = OID->protocol_end(); I != E; ++I)
167 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
168 return P;
169 return 0;
170}
171
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000172void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
173 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000174 const SourceLocation *Locs,
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000175 ASTContext &C)
176{
177 if (ReferencedProtocols.empty()) {
Douglas Gregor18df52b2010-01-16 15:02:53 +0000178 ReferencedProtocols.set(ExtList, ExtNum, Locs, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000179 return;
180 }
181 // Check for duplicate protocol in class's protocol list.
182 // This is (O)2. But it is extremely rare and number of protocols in
183 // class or its extension are very few.
184 llvm::SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000185 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000186 for (unsigned i = 0; i < ExtNum; i++) {
187 bool protocolExists = false;
188 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
189 for (protocol_iterator p = protocol_begin(), e = protocol_end();
190 p != e; p++) {
191 ObjCProtocolDecl *Proto = (*p);
192 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
193 protocolExists = true;
194 break;
195 }
196 }
197 // Do we want to warn on a protocol in extension class which
198 // already exist in the class? Probably not.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000199 if (!protocolExists) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000200 ProtocolRefs.push_back(ProtoInExtension);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000201 ProtocolLocs.push_back(Locs[i]);
202 }
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000203 }
204 if (ProtocolRefs.empty())
205 return;
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000206 // Merge ProtocolRefs into class's protocol list;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000207 protocol_loc_iterator pl = protocol_loc_begin();
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000208 for (protocol_iterator p = protocol_begin(), e = protocol_end();
Douglas Gregor18df52b2010-01-16 15:02:53 +0000209 p != e; ++p, ++pl) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000210 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000211 ProtocolLocs.push_back(*pl);
212 }
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000213 ReferencedProtocols.Destroy(C);
214 unsigned NumProtoRefs = ProtocolRefs.size();
Douglas Gregor18df52b2010-01-16 15:02:53 +0000215 setProtocolList(ProtocolRefs.data(), NumProtoRefs, ProtocolLocs.data(), C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000216}
217
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000218/// getClassExtension - Find class extension of the given class.
219// FIXME. can speed it up, if need be.
220ObjCCategoryDecl* ObjCInterfaceDecl::getClassExtension() const {
221 const ObjCInterfaceDecl* ClassDecl = this;
222 for (ObjCCategoryDecl *CDecl = ClassDecl->getCategoryList(); CDecl;
223 CDecl = CDecl->getNextClassCategory())
224 if (CDecl->IsClassExtension())
225 return CDecl;
226 return 0;
227}
228
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000229ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
230 ObjCInterfaceDecl *&clsDeclared) {
Chris Lattner1e03a562008-03-16 00:19:01 +0000231 ObjCInterfaceDecl* ClassDecl = this;
232 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000233 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000234 clsDeclared = ClassDecl;
235 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000236 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000237 if (const ObjCCategoryDecl *CDecl = ClassDecl->getClassExtension())
238 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
239 clsDeclared = ClassDecl;
240 return I;
241 }
242
Chris Lattner1e03a562008-03-16 00:19:01 +0000243 ClassDecl = ClassDecl->getSuperClass();
244 }
245 return NULL;
246}
247
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000248/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
249/// class whose name is passed as argument. If it is not one of the super classes
250/// the it returns NULL.
251ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
252 const IdentifierInfo*ICName) {
253 ObjCInterfaceDecl* ClassDecl = this;
254 while (ClassDecl != NULL) {
255 if (ClassDecl->getIdentifier() == ICName)
256 return ClassDecl;
257 ClassDecl = ClassDecl->getSuperClass();
258 }
259 return NULL;
260}
261
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000262/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000263/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000264ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
265 bool isInstance) const {
266 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000267 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Chris Lattner1e03a562008-03-16 00:19:01 +0000269 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000270 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000271 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Chris Lattner1e03a562008-03-16 00:19:01 +0000273 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000274 const ObjCList<ObjCProtocolDecl> &Protocols =
275 ClassDecl->getReferencedProtocols();
276 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
277 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000278 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000279 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattner1e03a562008-03-16 00:19:01 +0000281 // Didn't find one yet - now look through categories.
282 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
283 while (CatDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000284 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000285 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Steve Naroffb79c01e2008-12-08 20:57:28 +0000287 // Didn't find one yet - look through protocols.
288 const ObjCList<ObjCProtocolDecl> &Protocols =
289 CatDecl->getReferencedProtocols();
290 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
291 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000292 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000293 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000294 CatDecl = CatDecl->getNextClassCategory();
295 }
296 ClassDecl = ClassDecl->getSuperClass();
297 }
298 return NULL;
299}
300
Steve Naroffd789d3d2009-10-01 23:46:04 +0000301ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateInstanceMethod(
302 const Selector &Sel) {
303 ObjCMethodDecl *Method = 0;
304 if (ObjCImplementationDecl *ImpDecl = getImplementation())
305 Method = ImpDecl->getInstanceMethod(Sel);
306
307 if (!Method && getSuperClass())
308 return getSuperClass()->lookupPrivateInstanceMethod(Sel);
309 return Method;
310}
Chris Lattnerab351632009-02-20 20:59:54 +0000311
312//===----------------------------------------------------------------------===//
313// ObjCMethodDecl
314//===----------------------------------------------------------------------===//
315
316ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000317 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000318 SourceLocation endLoc,
319 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000320 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000321 DeclContext *contextDecl,
322 bool isInstance,
323 bool isVariadic,
324 bool isSynthesized,
325 ImplementationControl impControl) {
326 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000327 SelInfo, T, ResultTInfo, contextDecl,
328 isInstance,
329 isVariadic, isSynthesized, impControl);
Chris Lattner1e03a562008-03-16 00:19:01 +0000330}
331
Chris Lattner38af2de2009-02-20 21:35:13 +0000332void ObjCMethodDecl::Destroy(ASTContext &C) {
Chris Lattnerab351632009-02-20 20:59:54 +0000333 if (Body) Body->Destroy(C);
334 if (SelfDecl) SelfDecl->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Chris Lattnerab351632009-02-20 20:59:54 +0000336 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
337 if (*I) (*I)->Destroy(C);
338
Chris Lattner38af2de2009-02-20 21:35:13 +0000339 ParamInfo.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000340
341 Decl::Destroy(C);
Chris Lattner1e03a562008-03-16 00:19:01 +0000342}
343
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000344/// \brief A definition will return its interface declaration.
345/// An interface declaration will return its definition.
346/// Otherwise it will return itself.
347ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
348 ASTContext &Ctx = getASTContext();
349 ObjCMethodDecl *Redecl = 0;
350 Decl *CtxD = cast<Decl>(getDeclContext());
351
352 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
353 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
354 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
355
356 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
357 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
358 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
359
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000360 } else if (ObjCImplementationDecl *ImplD =
361 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000362 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
363 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000364
365 } else if (ObjCCategoryImplDecl *CImplD =
366 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000367 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000368 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000369 }
370
371 return Redecl ? Redecl : this;
372}
373
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000374ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
375 Decl *CtxD = cast<Decl>(getDeclContext());
376
377 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
378 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
379 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
380 isInstanceMethod()))
381 return MD;
382
383 } else if (ObjCCategoryImplDecl *CImplD =
384 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000385 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000386 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
387 isInstanceMethod()))
388 return MD;
389 }
390
391 return this;
392}
393
Mike Stump1eb44332009-09-09 15:08:12 +0000394void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000395 const ObjCInterfaceDecl *OID) {
396 QualType selfTy;
397 if (isInstanceMethod()) {
398 // There may be no interface context due to error in declaration
399 // of the interface (which has been reported). Recover gracefully.
400 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000401 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000402 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000403 } else {
404 selfTy = Context.getObjCIdType();
405 }
406 } else // we have a factory method.
407 selfTy = Context.getObjCClassType();
408
Mike Stump1eb44332009-09-09 15:08:12 +0000409 setSelfDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000410 &Context.Idents.get("self"), selfTy));
Chris Lattnerab351632009-02-20 20:59:54 +0000411
Mike Stump1eb44332009-09-09 15:08:12 +0000412 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
413 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000414 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000415}
416
Chris Lattnerab351632009-02-20 20:59:54 +0000417ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
418 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
419 return ID;
420 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
421 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000422 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000423 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000424
425 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
Chris Lattnerab351632009-02-20 20:59:54 +0000426 assert(false && "unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000427 return 0;
428}
429
Chris Lattnerab351632009-02-20 20:59:54 +0000430//===----------------------------------------------------------------------===//
431// ObjCInterfaceDecl
432//===----------------------------------------------------------------------===//
433
434ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
435 DeclContext *DC,
436 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000437 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +0000438 SourceLocation ClassLoc,
439 bool ForwardDecl, bool isInternal){
440 return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl,
441 isInternal);
442}
443
444ObjCInterfaceDecl::
445ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
446 SourceLocation CLoc, bool FD, bool isInternal)
447 : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id),
448 TypeForDecl(0), SuperClass(0),
449 CategoryList(0), ForwardDecl(FD), InternalInterface(isInternal),
450 ClassLoc(CLoc) {
451}
452
Mike Stump1eb44332009-09-09 15:08:12 +0000453void ObjCInterfaceDecl::Destroy(ASTContext &C) {
Chris Lattnerd13d3022009-03-31 08:36:08 +0000454 for (ivar_iterator I = ivar_begin(), E = ivar_end(); I != E; ++I)
Chris Lattnerab351632009-02-20 20:59:54 +0000455 if (*I) (*I)->Destroy(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattnerab351632009-02-20 20:59:54 +0000457 // FIXME: CategoryList?
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Chris Lattnerab351632009-02-20 20:59:54 +0000459 // FIXME: Because there is no clear ownership
460 // role between ObjCInterfaceDecls and the ObjCPropertyDecls that they
461 // reference, we destroy ObjCPropertyDecls in ~TranslationUnit.
462 Decl::Destroy(C);
463}
464
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000465ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
466 return getASTContext().getObjCImplementation(
467 const_cast<ObjCInterfaceDecl*>(this));
468}
469
470void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
471 getASTContext().setObjCImplementation(this, ImplD);
472}
473
Chris Lattnerab351632009-02-20 20:59:54 +0000474
475/// FindCategoryDeclaration - Finds category declaration in the list of
476/// categories for this class and returns it. Name of the category is passed
477/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000478///
Chris Lattnerab351632009-02-20 20:59:54 +0000479ObjCCategoryDecl *
480ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
481 for (ObjCCategoryDecl *Category = getCategoryList();
482 Category; Category = Category->getNextClassCategory())
483 if (Category->getIdentifier() == CategoryId)
484 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000485 return 0;
486}
487
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000488ObjCMethodDecl *
489ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
490 for (ObjCCategoryDecl *Category = getCategoryList();
491 Category; Category = Category->getNextClassCategory())
492 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
493 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
494 return MD;
495 return 0;
496}
497
498ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
499 for (ObjCCategoryDecl *Category = getCategoryList();
500 Category; Category = Category->getNextClassCategory())
501 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
502 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
503 return MD;
504 return 0;
505}
506
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000507/// ClassImplementsProtocol - Checks that 'lProto' protocol
508/// has been implemented in IDecl class, its super class or categories (if
509/// lookupCategory is true).
510bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
511 bool lookupCategory,
512 bool RHSIsQualifiedID) {
513 ObjCInterfaceDecl *IDecl = this;
514 // 1st, look up the class.
515 const ObjCList<ObjCProtocolDecl> &Protocols =
516 IDecl->getReferencedProtocols();
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000518 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
519 E = Protocols.end(); PI != E; ++PI) {
520 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
521 return true;
522 // This is dubious and is added to be compatible with gcc. In gcc, it is
523 // also allowed assigning a protocol-qualified 'id' type to a LHS object
524 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
525 // object. This IMO, should be a bug.
526 // FIXME: Treat this as an extension, and flag this as an error when GCC
527 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000528 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000529 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
530 return true;
531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000533 // 2nd, look up the category.
534 if (lookupCategory)
535 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
536 CDecl = CDecl->getNextClassCategory()) {
537 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
538 E = CDecl->protocol_end(); PI != E; ++PI)
539 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
540 return true;
541 }
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000543 // 3rd, look up the super class(s)
544 if (IDecl->getSuperClass())
545 return
546 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
547 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000549 return false;
550}
551
Chris Lattnerab351632009-02-20 20:59:54 +0000552//===----------------------------------------------------------------------===//
553// ObjCIvarDecl
554//===----------------------------------------------------------------------===//
555
556ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, DeclContext *DC,
557 SourceLocation L, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000558 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000559 AccessControl ac, Expr *BW) {
John McCalla93c9342009-12-07 02:54:59 +0000560 return new (C) ObjCIvarDecl(DC, L, Id, T, TInfo, ac, BW);
Chris Lattnerab351632009-02-20 20:59:54 +0000561}
562
563
564
565//===----------------------------------------------------------------------===//
566// ObjCAtDefsFieldDecl
567//===----------------------------------------------------------------------===//
568
569ObjCAtDefsFieldDecl
570*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
571 IdentifierInfo *Id, QualType T, Expr *BW) {
572 return new (C) ObjCAtDefsFieldDecl(DC, L, Id, T, BW);
573}
574
575void ObjCAtDefsFieldDecl::Destroy(ASTContext& C) {
576 this->~ObjCAtDefsFieldDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000577 C.Deallocate((void *)this);
Chris Lattnerab351632009-02-20 20:59:54 +0000578}
579
580//===----------------------------------------------------------------------===//
581// ObjCProtocolDecl
582//===----------------------------------------------------------------------===//
583
584ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000585 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000586 IdentifierInfo *Id) {
587 return new (C) ObjCProtocolDecl(DC, L, Id);
588}
589
590void ObjCProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000591 ReferencedProtocols.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000592 ObjCContainerDecl::Destroy(C);
593}
594
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000595ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
596 ObjCProtocolDecl *PDecl = this;
597
598 if (Name == getIdentifier())
599 return PDecl;
600
601 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
602 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
603 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Steve Naroff91b0b0c2009-03-01 16:12:44 +0000605 return NULL;
606}
607
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000608// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +0000609// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000610ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
611 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +0000612 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000614 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000615 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Chris Lattnerab351632009-02-20 20:59:54 +0000617 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +0000618 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +0000619 return MethodDecl;
620 return NULL;
621}
622
623//===----------------------------------------------------------------------===//
624// ObjCClassDecl
625//===----------------------------------------------------------------------===//
626
Mike Stump1eb44332009-09-09 15:08:12 +0000627ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L,
Ted Kremenek321c22f2009-11-18 00:28:11 +0000628 ObjCInterfaceDecl *const *Elts,
629 const SourceLocation *Locs,
630 unsigned nElts,
Chris Lattner38af2de2009-02-20 21:35:13 +0000631 ASTContext &C)
632 : Decl(ObjCClass, DC, L) {
Ted Kremenek321c22f2009-11-18 00:28:11 +0000633 setClassList(C, Elts, Locs, nElts);
Chris Lattner38af2de2009-02-20 21:35:13 +0000634}
635
Ted Kremenek321c22f2009-11-18 00:28:11 +0000636void ObjCClassDecl::setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
637 const SourceLocation *Locs, unsigned Num) {
638 ForwardDecls = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef)*Num,
639 llvm::alignof<ObjCClassRef>());
640 for (unsigned i = 0; i < Num; ++i)
641 new (&ForwardDecls[i]) ObjCClassRef(List[i], Locs[i]);
642
643 NumDecls = Num;
644}
Chris Lattner38af2de2009-02-20 21:35:13 +0000645
Chris Lattnerab351632009-02-20 20:59:54 +0000646ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC,
647 SourceLocation L,
648 ObjCInterfaceDecl *const *Elts,
Ted Kremenek321c22f2009-11-18 00:28:11 +0000649 const SourceLocation *Locs,
Chris Lattnerab351632009-02-20 20:59:54 +0000650 unsigned nElts) {
Ted Kremenek321c22f2009-11-18 00:28:11 +0000651 return new (C) ObjCClassDecl(DC, L, Elts, Locs, nElts, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000652}
653
654void ObjCClassDecl::Destroy(ASTContext &C) {
Ted Kremenek321c22f2009-11-18 00:28:11 +0000655 // ObjCInterfaceDecls registered with a DeclContext will get destroyed
656 // when the DeclContext is destroyed. For those created only by a forward
657 // declaration, the first @class that created the ObjCInterfaceDecl gets
658 // to destroy it.
659 // FIXME: Note that this ownership role is very brittle; a better
660 // polict is surely need in the future.
661 for (iterator I = begin(), E = end(); I !=E ; ++I) {
662 ObjCInterfaceDecl *ID = I->getInterface();
663 if (ID->isForwardDecl() && ID->getLocStart() == getLocStart())
664 ID->Destroy(C);
665 }
666
667 C.Deallocate(ForwardDecls);
Chris Lattnerab351632009-02-20 20:59:54 +0000668 Decl::Destroy(C);
669}
670
Ted Kremenek2dbdd622009-11-18 01:26:56 +0000671SourceRange ObjCClassDecl::getSourceRange() const {
672 // FIXME: We should include the semicolon
673 assert(NumDecls);
674 return SourceRange(getLocation(), ForwardDecls[NumDecls-1].getLocation());
675}
676
Chris Lattnerab351632009-02-20 20:59:54 +0000677//===----------------------------------------------------------------------===//
678// ObjCForwardProtocolDecl
679//===----------------------------------------------------------------------===//
680
Chris Lattner38af2de2009-02-20 21:35:13 +0000681ObjCForwardProtocolDecl::
682ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
683 ObjCProtocolDecl *const *Elts, unsigned nElts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000684 const SourceLocation *Locs, ASTContext &C)
Mike Stump1eb44332009-09-09 15:08:12 +0000685: Decl(ObjCForwardProtocol, DC, L) {
Douglas Gregor18df52b2010-01-16 15:02:53 +0000686 ReferencedProtocols.set(Elts, nElts, Locs, C);
Chris Lattner38af2de2009-02-20 21:35:13 +0000687}
688
689
Chris Lattnerab351632009-02-20 20:59:54 +0000690ObjCForwardProtocolDecl *
691ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Mike Stump1eb44332009-09-09 15:08:12 +0000692 SourceLocation L,
Chris Lattnerab351632009-02-20 20:59:54 +0000693 ObjCProtocolDecl *const *Elts,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000694 unsigned NumElts,
695 const SourceLocation *Locs) {
696 return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C);
Chris Lattnerab351632009-02-20 20:59:54 +0000697}
698
699void ObjCForwardProtocolDecl::Destroy(ASTContext &C) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000700 ReferencedProtocols.Destroy(C);
Chris Lattnerab351632009-02-20 20:59:54 +0000701 Decl::Destroy(C);
702}
703
704//===----------------------------------------------------------------------===//
705// ObjCCategoryDecl
706//===----------------------------------------------------------------------===//
707
708ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +0000709 SourceLocation AtLoc,
710 SourceLocation ClassNameLoc,
711 SourceLocation CategoryNameLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000712 IdentifierInfo *Id) {
Douglas Gregor3db211b2010-01-16 16:38:58 +0000713 return new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id);
Chris Lattnerab351632009-02-20 20:59:54 +0000714}
715
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000716ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
717 return getASTContext().getObjCImplementation(
718 const_cast<ObjCCategoryDecl*>(this));
719}
720
721void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
722 getASTContext().setObjCImplementation(this, ImplD);
723}
724
725
Chris Lattnerab351632009-02-20 20:59:54 +0000726//===----------------------------------------------------------------------===//
727// ObjCCategoryImplDecl
728//===----------------------------------------------------------------------===//
729
730ObjCCategoryImplDecl *
731ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
732 SourceLocation L,IdentifierInfo *Id,
733 ObjCInterfaceDecl *ClassInterface) {
734 return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface);
735}
736
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000737ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000738 return getClassInterface()->FindCategoryDeclaration(getIdentifier());
739}
740
Chris Lattnerab351632009-02-20 20:59:54 +0000741
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000742void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000743 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +0000744 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000745 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +0000746}
747
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000748void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
749 ASTContext &Ctx = getASTContext();
750
751 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +0000752 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000753 if (IFace)
754 Ctx.setObjCImplementation(IFace, ImplD);
755
Duncan Sands98f2cca2009-07-21 07:56:29 +0000756 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000757 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
758 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
759 Ctx.setObjCImplementation(CD, ImplD);
760 }
761
762 ClassInterface = IFace;
763}
764
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000765/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +0000766/// properties implemented in this category @implementation block and returns
767/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000768///
Chris Lattner3aa18612009-02-28 18:42:10 +0000769ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000770FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
771 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000772 ObjCPropertyImplDecl *PID = *i;
773 if (PID->getPropertyIvarDecl() &&
774 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
775 return PID;
776 }
777 return 0;
778}
779
780/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
781/// added to the list of those properties @synthesized/@dynamic in this
782/// category @implementation block.
783///
Chris Lattner3aa18612009-02-28 18:42:10 +0000784ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000785FindPropertyImplDecl(IdentifierInfo *Id) const {
786 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000787 ObjCPropertyImplDecl *PID = *i;
788 if (PID->getPropertyDecl()->getIdentifier() == Id)
789 return PID;
790 }
791 return 0;
792}
793
Chris Lattnerab351632009-02-20 20:59:54 +0000794//===----------------------------------------------------------------------===//
795// ObjCImplementationDecl
796//===----------------------------------------------------------------------===//
797
798ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000799ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +0000800 SourceLocation L,
801 ObjCInterfaceDecl *ClassInterface,
802 ObjCInterfaceDecl *SuperDecl) {
803 return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl);
804}
805
Chris Lattnerab351632009-02-20 20:59:54 +0000806//===----------------------------------------------------------------------===//
807// ObjCCompatibleAliasDecl
808//===----------------------------------------------------------------------===//
809
810ObjCCompatibleAliasDecl *
811ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
812 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +0000813 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +0000814 ObjCInterfaceDecl* AliasedClass) {
815 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
816}
817
818//===----------------------------------------------------------------------===//
819// ObjCPropertyDecl
820//===----------------------------------------------------------------------===//
821
822ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
823 SourceLocation L,
824 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000825 SourceLocation AtLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000826 QualType T,
827 PropertyControl propControl) {
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000828 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +0000829}
830
831
832//===----------------------------------------------------------------------===//
833// ObjCPropertyImplDecl
834//===----------------------------------------------------------------------===//
835
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000836ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +0000837 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000838 SourceLocation atLoc,
839 SourceLocation L,
840 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000841 Kind PK,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000842 ObjCIvarDecl *ivar) {
Steve Naroff3e970492009-01-27 21:25:57 +0000843 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000844}
Chris Lattnerf4af5152008-03-17 01:19:02 +0000845
Chris Lattner0ed844b2008-04-04 06:12:32 +0000846