blob: a92c624b4ca2dd5ea8f29b83b9e088dea955bf3c [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"
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +000017#include "clang/AST/ASTMutationListener.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000018#include "llvm/ADT/STLExtras.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000019using namespace clang;
20
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000021//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000022// ObjCListBase
23//===----------------------------------------------------------------------===//
24
Chris Lattner38af2de2009-02-20 21:35:13 +000025void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorff331c12010-07-25 18:17:45 +000026 List = 0;
Chris Lattner11e1e1a2009-02-20 21:16:26 +000027 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump1eb44332009-09-09 15:08:12 +000028
29
Chris Lattner4ee413b2009-02-20 21:44:01 +000030 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000031 NumElts = Elts;
32 memcpy(List, InList, sizeof(void*)*Elts);
33}
34
Douglas Gregor18df52b2010-01-16 15:02:53 +000035void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
36 const SourceLocation *Locs, ASTContext &Ctx) {
37 if (Elts == 0)
38 return;
39
40 Locations = new (Ctx) SourceLocation[Elts];
41 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
42 set(InList, Elts, Ctx);
43}
44
Chris Lattner11e1e1a2009-02-20 21:16:26 +000045//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000046// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000047//===----------------------------------------------------------------------===//
48
David Blaikie99ba9e32011-12-20 02:48:34 +000049void ObjCContainerDecl::anchor() { }
50
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000051/// getIvarDecl - This method looks up an ivar in this ContextDecl.
52///
53ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000054ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000055 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000056 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000057 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
58 return ivar;
59 }
60 return 0;
61}
62
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000063// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000064ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000065ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000066 // Since instance & class methods can have the same name, the loop below
67 // ensures we get the correct method.
68 //
69 // @interface Whatever
70 // - (int) class_method;
71 // + (float) class_method;
72 // @end
73 //
74 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000075 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000076 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000077 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000078 return MD;
79 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000080 return 0;
81}
82
Ted Kremenek9f550ff2010-03-15 20:11:46 +000083ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000084ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000085 IdentifierInfo *propertyID) {
86
Ted Kremenekde09d0c2010-03-15 20:11:53 +000087 DeclContext::lookup_const_iterator I, E;
Ted Kremenek9f550ff2010-03-15 20:11:46 +000088 llvm::tie(I, E) = DC->lookup(propertyID);
89 for ( ; I != E; ++I)
90 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
91 return PD;
92
93 return 0;
94}
95
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000096/// FindPropertyDeclaration - Finds declaration of the property given its name
97/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +000098ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000099ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000101 if (ObjCPropertyDecl *PD =
102 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
103 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000105 switch (getKind()) {
106 default:
107 break;
108 case Decl::ObjCProtocol: {
109 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
110 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
111 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000112 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
113 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000114 break;
115 }
116 case Decl::ObjCInterface: {
117 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
118 // Look through categories.
119 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
120 Cat; Cat = Cat->getNextClassCategory())
121 if (!Cat->IsClassExtension())
122 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
123 return P;
124
125 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000126 for (ObjCInterfaceDecl::all_protocol_iterator
127 I = OID->all_referenced_protocol_begin(),
128 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000129 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
130 return P;
131
132 // Finally, check the super class.
133 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
134 return superClass->FindPropertyDeclaration(PropertyId);
135 break;
136 }
137 case Decl::ObjCCategory: {
138 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
139 // Look through protocols.
140 if (!OCD->IsClassExtension())
141 for (ObjCCategoryDecl::protocol_iterator
142 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
143 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
144 return P;
145
146 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000147 }
148 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000149 return 0;
150}
151
David Blaikie99ba9e32011-12-20 02:48:34 +0000152void ObjCInterfaceDecl::anchor() { }
153
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000154/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
155/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000156/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000157///
158ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000159ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000160 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000161 // FIXME: Should make sure no callers ever do this.
162 if (!hasDefinition())
163 return 0;
164
165 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000166 LoadExternalDefinition();
167
Ted Kremenek37cafb02010-03-15 20:30:07 +0000168 if (ObjCPropertyDecl *PD =
169 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
170 return PD;
171
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000172 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000173 for (ObjCInterfaceDecl::all_protocol_iterator
174 I = all_referenced_protocol_begin(),
175 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000176 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
177 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000178
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000179 return 0;
180}
181
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000182void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
183 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
184 ASTContext &C)
185{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000186 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000187 LoadExternalDefinition();
188
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000189 if (data().AllReferencedProtocols.empty() &&
190 data().ReferencedProtocols.empty()) {
191 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000192 return;
193 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000194
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000195 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000196 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000197 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000198 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000199 for (unsigned i = 0; i < ExtNum; i++) {
200 bool protocolExists = false;
201 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000202 for (all_protocol_iterator
203 p = all_referenced_protocol_begin(),
204 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000205 ObjCProtocolDecl *Proto = (*p);
206 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
207 protocolExists = true;
208 break;
209 }
210 }
211 // Do we want to warn on a protocol in extension class which
212 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000213 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000214 ProtocolRefs.push_back(ProtoInExtension);
215 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000216
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000217 if (ProtocolRefs.empty())
218 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000219
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000220 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000221 for (all_protocol_iterator p = all_referenced_protocol_begin(),
222 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000223 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000224 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000225
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000226 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000227}
228
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000229void ObjCInterfaceDecl::allocateDefinitionData() {
230 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor26fec632011-12-15 18:17:27 +0000231 Data = new (getASTContext()) DefinitionData();
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000232 Data->Definition = this;
233
234 // Make the type point at the definition, now that we have one.
235 if (TypeForDecl)
236 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000237}
238
239void ObjCInterfaceDecl::startDefinition() {
240 allocateDefinitionData();
241
Douglas Gregor53df7a12011-12-15 18:03:09 +0000242 // Update all of the declarations with a pointer to the definition.
243 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
244 RD != RDEnd; ++RD) {
245 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000246 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000247 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000248}
249
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000250/// getFirstClassExtension - Find first class extension of the given class.
251ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
252 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000253 CDecl = CDecl->getNextClassCategory())
254 if (CDecl->IsClassExtension())
255 return CDecl;
256 return 0;
257}
258
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000259/// getNextClassCategory - Find next class extension in list of categories.
260const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
261 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
262 CDecl = CDecl->getNextClassCategory())
263 if (CDecl->IsClassExtension())
264 return CDecl;
265 return 0;
266}
267
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000268ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
269 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000270 // FIXME: Should make sure no callers ever do this.
271 if (!hasDefinition())
272 return 0;
273
274 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000275 LoadExternalDefinition();
276
Chris Lattner1e03a562008-03-16 00:19:01 +0000277 ObjCInterfaceDecl* ClassDecl = this;
278 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000279 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000280 clsDeclared = ClassDecl;
281 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000282 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000283 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
284 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000285 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
286 clsDeclared = ClassDecl;
287 return I;
288 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000289 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000290
Chris Lattner1e03a562008-03-16 00:19:01 +0000291 ClassDecl = ClassDecl->getSuperClass();
292 }
293 return NULL;
294}
295
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000296/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
297/// class whose name is passed as argument. If it is not one of the super classes
298/// the it returns NULL.
299ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
300 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000301 // FIXME: Should make sure no callers ever do this.
302 if (!hasDefinition())
303 return 0;
304
305 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000306 LoadExternalDefinition();
307
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000308 ObjCInterfaceDecl* ClassDecl = this;
309 while (ClassDecl != NULL) {
310 if (ClassDecl->getIdentifier() == ICName)
311 return ClassDecl;
312 ClassDecl = ClassDecl->getSuperClass();
313 }
314 return NULL;
315}
316
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000317/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000318/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000319ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000320 bool isInstance,
321 bool noCategoryLookup) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000322 // FIXME: Should make sure no callers ever do this.
323 if (!hasDefinition())
324 return 0;
325
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000326 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000327 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000329 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000330 LoadExternalDefinition();
331
Chris Lattner1e03a562008-03-16 00:19:01 +0000332 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000333 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000334 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Chris Lattner1e03a562008-03-16 00:19:01 +0000336 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000337 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
338 E = ClassDecl->protocol_end();
339 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000340 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000341 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000342 if (!noCategoryLookup) {
343 // Didn't find one yet - now look through categories.
344 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
345 while (CatDecl) {
346 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000347 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000348
349 // Didn't find one yet - look through protocols.
350 const ObjCList<ObjCProtocolDecl> &Protocols =
351 CatDecl->getReferencedProtocols();
352 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
353 E = Protocols.end(); I != E; ++I)
354 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
355 return MethodDecl;
356 CatDecl = CatDecl->getNextClassCategory();
357 }
Chris Lattner1e03a562008-03-16 00:19:01 +0000358 }
359 ClassDecl = ClassDecl->getSuperClass();
360 }
361 return NULL;
362}
363
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000364ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
365 const Selector &Sel,
366 bool Instance) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000367 // FIXME: Should make sure no callers ever do this.
368 if (!hasDefinition())
369 return 0;
370
371 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000372 LoadExternalDefinition();
373
Steve Naroffd789d3d2009-10-01 23:46:04 +0000374 ObjCMethodDecl *Method = 0;
375 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000376 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
377 : ImpDecl->getClassMethod(Sel);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000378
379 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000380 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000381 return Method;
382}
Chris Lattnerab351632009-02-20 20:59:54 +0000383
384//===----------------------------------------------------------------------===//
385// ObjCMethodDecl
386//===----------------------------------------------------------------------===//
387
388ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000389 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000390 SourceLocation endLoc,
391 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000392 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000393 DeclContext *contextDecl,
394 bool isInstance,
395 bool isVariadic,
396 bool isSynthesized,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000397 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000398 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000399 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000400 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000401 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000402 SelInfo, T, ResultTInfo, contextDecl,
403 isInstance,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000404 isVariadic, isSynthesized, isImplicitlyDeclared,
405 isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000406 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000407 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000408}
409
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000410ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
411 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
412 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
413 Selector(), QualType(), 0, 0);
414}
415
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000416void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
417 assert(PrevMethod);
418 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
419 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000420 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000421}
422
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000423void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
424 ArrayRef<ParmVarDecl*> Params,
425 ArrayRef<SourceLocation> SelLocs) {
426 ParamsAndSelLocs = 0;
427 NumParams = Params.size();
428 if (Params.empty() && SelLocs.empty())
429 return;
430
431 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
432 sizeof(SourceLocation) * SelLocs.size();
433 ParamsAndSelLocs = C.Allocate(Size);
434 std::copy(Params.begin(), Params.end(), getParams());
435 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
436}
437
438void ObjCMethodDecl::getSelectorLocs(
439 SmallVectorImpl<SourceLocation> &SelLocs) const {
440 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
441 SelLocs.push_back(getSelectorLoc(i));
442}
443
444void ObjCMethodDecl::setMethodParams(ASTContext &C,
445 ArrayRef<ParmVarDecl*> Params,
446 ArrayRef<SourceLocation> SelLocs) {
447 assert((!SelLocs.empty() || isImplicit()) &&
448 "No selector locs for non-implicit method");
449 if (isImplicit())
450 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
451
452 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc);
453 if (SelLocsKind != SelLoc_NonStandard)
454 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
455
456 setParamsAndSelLocs(C, Params, SelLocs);
457}
458
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000459/// \brief A definition will return its interface declaration.
460/// An interface declaration will return its definition.
461/// Otherwise it will return itself.
462ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
463 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000464 ObjCMethodDecl *Redecl = 0;
465 if (HasRedeclaration)
466 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000467 if (Redecl)
468 return Redecl;
469
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000470 Decl *CtxD = cast<Decl>(getDeclContext());
471
472 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
473 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
474 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
475
476 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
477 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
478 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
479
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000480 } else if (ObjCImplementationDecl *ImplD =
481 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000482 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
483 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000484
485 } else if (ObjCCategoryImplDecl *CImplD =
486 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000487 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000488 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000489 }
490
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000491 if (!Redecl && isRedeclaration()) {
492 // This is the last redeclaration, go back to the first method.
493 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
494 isInstanceMethod());
495 }
496
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000497 return Redecl ? Redecl : this;
498}
499
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000500ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
501 Decl *CtxD = cast<Decl>(getDeclContext());
502
503 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
504 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
505 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
506 isInstanceMethod()))
507 return MD;
508
509 } else if (ObjCCategoryImplDecl *CImplD =
510 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000511 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000512 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
513 isInstanceMethod()))
514 return MD;
515 }
516
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000517 if (isRedeclaration())
518 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
519 isInstanceMethod());
520
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000521 return this;
522}
523
John McCall85f3d762011-03-02 01:50:55 +0000524ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
525 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000526 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000527 return family;
528
John McCalld5313b02011-03-02 11:33:24 +0000529 // Check for an explicit attribute.
530 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
531 // The unfortunate necessity of mapping between enums here is due
532 // to the attributes framework.
533 switch (attr->getFamily()) {
534 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
535 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
536 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
537 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
538 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
539 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
540 }
541 Family = static_cast<unsigned>(family);
542 return family;
543 }
544
John McCall85f3d762011-03-02 01:50:55 +0000545 family = getSelector().getMethodFamily();
546 switch (family) {
547 case OMF_None: break;
548
549 // init only has a conventional meaning for an instance method, and
550 // it has to return an object.
551 case OMF_init:
552 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
553 family = OMF_None;
554 break;
555
556 // alloc/copy/new have a conventional meaning for both class and
557 // instance methods, but they require an object return.
558 case OMF_alloc:
559 case OMF_copy:
560 case OMF_mutableCopy:
561 case OMF_new:
562 if (!getResultType()->isObjCObjectPointerType())
563 family = OMF_None;
564 break;
565
566 // These selectors have a conventional meaning only for instance methods.
567 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000568 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000569 case OMF_retain:
570 case OMF_release:
571 case OMF_autorelease:
572 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000573 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000574 if (!isInstanceMethod())
575 family = OMF_None;
576 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000577
578 case OMF_performSelector:
579 if (!isInstanceMethod() ||
580 !getResultType()->isObjCIdType())
581 family = OMF_None;
582 else {
583 unsigned noParams = param_size();
584 if (noParams < 1 || noParams > 3)
585 family = OMF_None;
586 else {
587 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
588 QualType ArgT = (*it);
589 if (!ArgT->isObjCSelType()) {
590 family = OMF_None;
591 break;
592 }
593 while (--noParams) {
594 it++;
595 ArgT = (*it);
596 if (!ArgT->isObjCIdType()) {
597 family = OMF_None;
598 break;
599 }
600 }
601 }
602 }
603 break;
604
John McCall85f3d762011-03-02 01:50:55 +0000605 }
606
607 // Cache the result.
608 Family = static_cast<unsigned>(family);
609 return family;
610}
611
Mike Stump1eb44332009-09-09 15:08:12 +0000612void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000613 const ObjCInterfaceDecl *OID) {
614 QualType selfTy;
615 if (isInstanceMethod()) {
616 // There may be no interface context due to error in declaration
617 // of the interface (which has been reported). Recover gracefully.
618 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000619 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000620 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000621 } else {
622 selfTy = Context.getObjCIdType();
623 }
624 } else // we have a factory method.
625 selfTy = Context.getObjCClassType();
626
John McCall7acddac2011-06-17 06:42:21 +0000627 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000628 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000629
David Blaikie4e4d0842012-03-11 07:00:24 +0000630 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000631 if (isInstanceMethod()) {
632 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000633
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000634 // 'self' is always __strong. It's actually pseudo-strong except
635 // in init methods (or methods labeled ns_consumes_self), though.
636 Qualifiers qs;
637 qs.setObjCLifetime(Qualifiers::OCL_Strong);
638 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000639
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000640 // In addition, 'self' is const unless this is an init method.
641 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
642 selfTy = selfTy.withConst();
643 selfIsPseudoStrong = true;
644 }
645 }
646 else {
647 assert(isClassMethod());
648 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000649 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000650 selfIsPseudoStrong = true;
651 }
John McCallf85e1932011-06-15 23:02:42 +0000652 }
653
654 ImplicitParamDecl *self
655 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
656 &Context.Idents.get("self"), selfTy);
657 setSelfDecl(self);
658
659 if (selfIsConsumed)
660 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000661
John McCall7acddac2011-06-17 06:42:21 +0000662 if (selfIsPseudoStrong)
663 self->setARCPseudoStrong(true);
664
Mike Stump1eb44332009-09-09 15:08:12 +0000665 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
666 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000667 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000668}
669
Chris Lattnerab351632009-02-20 20:59:54 +0000670ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
671 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
672 return ID;
673 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
674 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000675 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000676 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000677
678 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000679 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000680}
681
Chris Lattnerab351632009-02-20 20:59:54 +0000682//===----------------------------------------------------------------------===//
683// ObjCInterfaceDecl
684//===----------------------------------------------------------------------===//
685
Douglas Gregora6ea10e2012-01-17 18:09:05 +0000686ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +0000687 DeclContext *DC,
688 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000689 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +0000690 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +0000691 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000692 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +0000693 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000694 PrevDecl, isInternal);
Douglas Gregor0af55012011-12-16 03:12:41 +0000695 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +0000696 return Result;
697}
698
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000699ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
700 unsigned ID) {
701 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
702 return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
703 0, false);
Chris Lattnerab351632009-02-20 20:59:54 +0000704}
705
706ObjCInterfaceDecl::
707ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000708 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
709 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000710 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +0000711 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000712{
Douglas Gregorfd002a72011-12-16 22:37:11 +0000713 setPreviousDeclaration(PrevDecl);
714
715 // Copy the 'data' pointer over.
716 if (PrevDecl)
717 Data = PrevDecl->Data;
718
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +0000719 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000720}
721
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000722void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000723 assert(data().ExternallyCompleted && "Class is not externally completed");
724 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000725 getASTContext().getExternalSource()->CompleteType(
726 const_cast<ObjCInterfaceDecl *>(this));
727}
728
729void ObjCInterfaceDecl::setExternallyCompleted() {
730 assert(getASTContext().getExternalSource() &&
731 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000732 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000733 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000734 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000735}
736
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000737ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000738 if (const ObjCInterfaceDecl *Def = getDefinition()) {
739 if (data().ExternallyCompleted)
740 LoadExternalDefinition();
741
742 return getASTContext().getObjCImplementation(
743 const_cast<ObjCInterfaceDecl*>(Def));
744 }
745
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000746 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +0000747 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000748}
749
750void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000751 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000752}
753
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000754/// all_declared_ivar_begin - return first ivar declared in this class,
755/// its extensions and its implementation. Lazily build the list on first
756/// access.
757ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000758 // FIXME: Should make sure no callers ever do this.
759 if (!hasDefinition())
760 return 0;
761
762 if (data().IvarList)
763 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000764
765 ObjCIvarDecl *curIvar = 0;
766 if (!ivar_empty()) {
767 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000768 data().IvarList = (*I); ++I;
769 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000770 curIvar->setNextIvar(*I);
771 }
772
773 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
774 CDecl = CDecl->getNextClassExtension()) {
775 if (!CDecl->ivar_empty()) {
776 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
777 E = CDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000778 if (!data().IvarList) {
779 data().IvarList = (*I); ++I;
780 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000781 }
782 for ( ;I != E; curIvar = *I, ++I)
783 curIvar->setNextIvar(*I);
784 }
785 }
786
787 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
788 if (!ImplDecl->ivar_empty()) {
789 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
790 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000791 if (!data().IvarList) {
792 data().IvarList = (*I); ++I;
793 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000794 }
795 for ( ;I != E; curIvar = *I, ++I)
796 curIvar->setNextIvar(*I);
797 }
798 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000799 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000800}
Chris Lattnerab351632009-02-20 20:59:54 +0000801
802/// FindCategoryDeclaration - Finds category declaration in the list of
803/// categories for this class and returns it. Name of the category is passed
804/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000805///
Chris Lattnerab351632009-02-20 20:59:54 +0000806ObjCCategoryDecl *
807ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000808 // FIXME: Should make sure no callers ever do this.
809 if (!hasDefinition())
810 return 0;
811
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000812 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000813 LoadExternalDefinition();
814
Chris Lattnerab351632009-02-20 20:59:54 +0000815 for (ObjCCategoryDecl *Category = getCategoryList();
816 Category; Category = Category->getNextClassCategory())
817 if (Category->getIdentifier() == CategoryId)
818 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000819 return 0;
820}
821
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000822ObjCMethodDecl *
823ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
824 for (ObjCCategoryDecl *Category = getCategoryList();
825 Category; Category = Category->getNextClassCategory())
826 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
827 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
828 return MD;
829 return 0;
830}
831
832ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
833 for (ObjCCategoryDecl *Category = getCategoryList();
834 Category; Category = Category->getNextClassCategory())
835 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
836 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
837 return MD;
838 return 0;
839}
840
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000841/// ClassImplementsProtocol - Checks that 'lProto' protocol
842/// has been implemented in IDecl class, its super class or categories (if
843/// lookupCategory is true).
844bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
845 bool lookupCategory,
846 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000847 if (!hasDefinition())
848 return false;
849
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000850 ObjCInterfaceDecl *IDecl = this;
851 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000852 for (ObjCInterfaceDecl::protocol_iterator
853 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000854 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
855 return true;
856 // This is dubious and is added to be compatible with gcc. In gcc, it is
857 // also allowed assigning a protocol-qualified 'id' type to a LHS object
858 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
859 // object. This IMO, should be a bug.
860 // FIXME: Treat this as an extension, and flag this as an error when GCC
861 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000862 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000863 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
864 return true;
865 }
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000867 // 2nd, look up the category.
868 if (lookupCategory)
869 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
870 CDecl = CDecl->getNextClassCategory()) {
871 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
872 E = CDecl->protocol_end(); PI != E; ++PI)
873 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
874 return true;
875 }
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000877 // 3rd, look up the super class(s)
878 if (IDecl->getSuperClass())
879 return
880 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
881 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000883 return false;
884}
885
Chris Lattnerab351632009-02-20 20:59:54 +0000886//===----------------------------------------------------------------------===//
887// ObjCIvarDecl
888//===----------------------------------------------------------------------===//
889
David Blaikie99ba9e32011-12-20 02:48:34 +0000890void ObjCIvarDecl::anchor() { }
891
Daniel Dunbara0654922010-04-02 20:10:03 +0000892ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000893 SourceLocation StartLoc,
894 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000895 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000896 AccessControl ac, Expr *BW,
897 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +0000898 if (DC) {
899 // Ivar's can only appear in interfaces, implementations (via synthesized
900 // properties), and class extensions (via direct declaration, or synthesized
901 // properties).
902 //
903 // FIXME: This should really be asserting this:
904 // (isa<ObjCCategoryDecl>(DC) &&
905 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
906 // but unfortunately we sometimes place ivars into non-class extension
907 // categories on error. This breaks an AST invariant, and should not be
908 // fixed.
909 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
910 isa<ObjCCategoryDecl>(DC)) &&
911 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000912 // Once a new ivar is created in any of class/class-extension/implementation
913 // decl contexts, the previously built IvarList must be rebuilt.
914 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
915 if (!ID) {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000916 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000917 ID = IM->getClassInterface();
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000918 if (BW)
919 IM->setHasSynthBitfield(true);
Chad Rosier30601782011-08-17 23:08:45 +0000920 } else {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000921 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
922 ID = CD->getClassInterface();
923 if (BW)
924 CD->setHasSynthBitfield(true);
925 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000926 }
927 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +0000928 }
929
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000930 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
931 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +0000932}
933
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000934ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
935 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
936 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
937 QualType(), 0, ObjCIvarDecl::None, 0, false);
938}
939
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000940const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
941 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +0000942
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000943 switch (DC->getKind()) {
944 default:
945 case ObjCCategoryImpl:
946 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +0000947 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000948
949 // Ivars can only appear in class extension categories.
950 case ObjCCategory: {
951 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
952 assert(CD->IsClassExtension() && "invalid container for ivar!");
953 return CD->getClassInterface();
954 }
955
956 case ObjCImplementation:
957 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
958
959 case ObjCInterface:
960 return cast<ObjCInterfaceDecl>(DC);
961 }
962}
Chris Lattnerab351632009-02-20 20:59:54 +0000963
964//===----------------------------------------------------------------------===//
965// ObjCAtDefsFieldDecl
966//===----------------------------------------------------------------------===//
967
David Blaikie99ba9e32011-12-20 02:48:34 +0000968void ObjCAtDefsFieldDecl::anchor() { }
969
Chris Lattnerab351632009-02-20 20:59:54 +0000970ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000971*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
972 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000973 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000974 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +0000975}
976
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000977ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
978 unsigned ID) {
979 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
980 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
981 0, QualType(), 0);
982}
983
Chris Lattnerab351632009-02-20 20:59:54 +0000984//===----------------------------------------------------------------------===//
985// ObjCProtocolDecl
986//===----------------------------------------------------------------------===//
987
David Blaikie99ba9e32011-12-20 02:48:34 +0000988void ObjCProtocolDecl::anchor() { }
989
Douglas Gregor27c6da22012-01-01 20:30:41 +0000990ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
991 SourceLocation nameLoc,
992 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000993 ObjCProtocolDecl *PrevDecl)
994 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +0000995{
996 setPreviousDeclaration(PrevDecl);
997 if (PrevDecl)
998 Data = PrevDecl->Data;
999}
1000
Chris Lattnerab351632009-02-20 20:59:54 +00001001ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001002 IdentifierInfo *Id,
1003 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001004 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001005 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001006 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001007 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001008
1009 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001010}
1011
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001012ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1013 unsigned ID) {
1014 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1015 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1016 0);
1017}
1018
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001019ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1020 ObjCProtocolDecl *PDecl = this;
1021
1022 if (Name == getIdentifier())
1023 return PDecl;
1024
1025 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1026 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1027 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001029 return NULL;
1030}
1031
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001032// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001033// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001034ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1035 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001036 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001038 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001039 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Chris Lattnerab351632009-02-20 20:59:54 +00001041 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001042 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001043 return MethodDecl;
1044 return NULL;
1045}
1046
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001047void ObjCProtocolDecl::allocateDefinitionData() {
1048 assert(!Data && "Protocol already has a definition!");
Douglas Gregor1d784b22012-01-01 19:51:50 +00001049 Data = new (getASTContext()) DefinitionData;
1050 Data->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001051}
1052
1053void ObjCProtocolDecl::startDefinition() {
1054 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001055
1056 // Update all of the declarations with a pointer to the definition.
1057 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1058 RD != RDEnd; ++RD)
1059 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001060}
1061
Chris Lattnerab351632009-02-20 20:59:54 +00001062//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001063// ObjCCategoryDecl
1064//===----------------------------------------------------------------------===//
1065
David Blaikie99ba9e32011-12-20 02:48:34 +00001066void ObjCCategoryDecl::anchor() { }
1067
Chris Lattnerab351632009-02-20 20:59:54 +00001068ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001069 SourceLocation AtLoc,
1070 SourceLocation ClassNameLoc,
1071 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001072 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001073 ObjCInterfaceDecl *IDecl,
1074 SourceLocation IvarLBraceLoc,
1075 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001076 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1077 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001078 IDecl,
1079 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001080 if (IDecl) {
1081 // Link this category into its class's category list.
1082 CatDecl->NextClassCategory = IDecl->getCategoryList();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001083 if (IDecl->hasDefinition()) {
1084 IDecl->setCategoryList(CatDecl);
1085 if (ASTMutationListener *L = C.getASTMutationListener())
1086 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1087 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001088 }
1089
1090 return CatDecl;
1091}
1092
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001093ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1094 unsigned ID) {
1095 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1096 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1097 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001098}
1099
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001100ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1101 return getASTContext().getObjCImplementation(
1102 const_cast<ObjCCategoryDecl*>(this));
1103}
1104
1105void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1106 getASTContext().setObjCImplementation(this, ImplD);
1107}
1108
1109
Chris Lattnerab351632009-02-20 20:59:54 +00001110//===----------------------------------------------------------------------===//
1111// ObjCCategoryImplDecl
1112//===----------------------------------------------------------------------===//
1113
David Blaikie99ba9e32011-12-20 02:48:34 +00001114void ObjCCategoryImplDecl::anchor() { }
1115
Chris Lattnerab351632009-02-20 20:59:54 +00001116ObjCCategoryImplDecl *
1117ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001118 IdentifierInfo *Id,
1119 ObjCInterfaceDecl *ClassInterface,
1120 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001121 SourceLocation atStartLoc,
1122 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001123 if (ClassInterface && ClassInterface->hasDefinition())
1124 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001125 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001126 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001127}
1128
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001129ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1130 unsigned ID) {
1131 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1132 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1133 SourceLocation(), SourceLocation());
1134}
1135
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001136ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001137 // The class interface might be NULL if we are working with invalid code.
1138 if (const ObjCInterfaceDecl *ID = getClassInterface())
1139 return ID->FindCategoryDeclaration(getIdentifier());
1140 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001141}
1142
Chris Lattnerab351632009-02-20 20:59:54 +00001143
David Blaikie99ba9e32011-12-20 02:48:34 +00001144void ObjCImplDecl::anchor() { }
1145
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001146void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001147 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001148 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001149 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001150}
1151
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001152void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1153 ASTContext &Ctx = getASTContext();
1154
1155 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001156 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001157 if (IFace)
1158 Ctx.setObjCImplementation(IFace, ImplD);
1159
Duncan Sands98f2cca2009-07-21 07:56:29 +00001160 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001161 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1162 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1163 Ctx.setObjCImplementation(CD, ImplD);
1164 }
1165
1166 ClassInterface = IFace;
1167}
1168
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001169/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001170/// properties implemented in this category @implementation block and returns
1171/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001172///
Chris Lattner3aa18612009-02-28 18:42:10 +00001173ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001174FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1175 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001176 ObjCPropertyImplDecl *PID = *i;
1177 if (PID->getPropertyIvarDecl() &&
1178 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1179 return PID;
1180 }
1181 return 0;
1182}
1183
1184/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1185/// added to the list of those properties @synthesized/@dynamic in this
1186/// category @implementation block.
1187///
Chris Lattner3aa18612009-02-28 18:42:10 +00001188ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001189FindPropertyImplDecl(IdentifierInfo *Id) const {
1190 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001191 ObjCPropertyImplDecl *PID = *i;
1192 if (PID->getPropertyDecl()->getIdentifier() == Id)
1193 return PID;
1194 }
1195 return 0;
1196}
1197
Chris Lattner5f9e2722011-07-23 10:55:15 +00001198raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001199 const ObjCCategoryImplDecl &CID) {
1200 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001201 return OS;
1202}
1203
Chris Lattnerab351632009-02-20 20:59:54 +00001204//===----------------------------------------------------------------------===//
1205// ObjCImplementationDecl
1206//===----------------------------------------------------------------------===//
1207
David Blaikie99ba9e32011-12-20 02:48:34 +00001208void ObjCImplementationDecl::anchor() { }
1209
Chris Lattnerab351632009-02-20 20:59:54 +00001210ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001211ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001212 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001213 ObjCInterfaceDecl *SuperDecl,
1214 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001215 SourceLocation atStartLoc,
1216 SourceLocation IvarLBraceLoc,
1217 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001218 if (ClassInterface && ClassInterface->hasDefinition())
1219 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001220 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001221 nameLoc, atStartLoc,
1222 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001223}
1224
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001225ObjCImplementationDecl *
1226ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1227 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1228 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1229 SourceLocation());
1230}
1231
John McCallda6d9762011-07-22 04:15:06 +00001232void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1233 CXXCtorInitializer ** initializers,
1234 unsigned numInitializers) {
1235 if (numInitializers > 0) {
1236 NumIvarInitializers = numInitializers;
1237 CXXCtorInitializer **ivarInitializers =
1238 new (C) CXXCtorInitializer*[NumIvarInitializers];
1239 memcpy(ivarInitializers, initializers,
1240 numInitializers * sizeof(CXXCtorInitializer*));
1241 IvarInitializers = ivarInitializers;
1242 }
1243}
1244
Chris Lattner5f9e2722011-07-23 10:55:15 +00001245raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001246 const ObjCImplementationDecl &ID) {
1247 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001248 return OS;
1249}
1250
Chris Lattnerab351632009-02-20 20:59:54 +00001251//===----------------------------------------------------------------------===//
1252// ObjCCompatibleAliasDecl
1253//===----------------------------------------------------------------------===//
1254
David Blaikie99ba9e32011-12-20 02:48:34 +00001255void ObjCCompatibleAliasDecl::anchor() { }
1256
Chris Lattnerab351632009-02-20 20:59:54 +00001257ObjCCompatibleAliasDecl *
1258ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1259 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001260 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001261 ObjCInterfaceDecl* AliasedClass) {
1262 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1263}
1264
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001265ObjCCompatibleAliasDecl *
1266ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1267 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1268 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1269}
1270
Chris Lattnerab351632009-02-20 20:59:54 +00001271//===----------------------------------------------------------------------===//
1272// ObjCPropertyDecl
1273//===----------------------------------------------------------------------===//
1274
David Blaikie99ba9e32011-12-20 02:48:34 +00001275void ObjCPropertyDecl::anchor() { }
1276
Chris Lattnerab351632009-02-20 20:59:54 +00001277ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1278 SourceLocation L,
1279 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001280 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001281 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001282 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001283 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001284 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001285}
1286
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001287ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1288 unsigned ID) {
1289 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1290 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001291 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001292 0);
1293}
1294
Chris Lattnerab351632009-02-20 20:59:54 +00001295//===----------------------------------------------------------------------===//
1296// ObjCPropertyImplDecl
1297//===----------------------------------------------------------------------===//
1298
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001299ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001300 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001301 SourceLocation atLoc,
1302 SourceLocation L,
1303 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001304 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001305 ObjCIvarDecl *ivar,
1306 SourceLocation ivarLoc) {
1307 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1308 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001309}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001310
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001311ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1312 unsigned ID) {
1313 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1314 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1315 0, Dynamic, 0, SourceLocation());
1316}
1317
Douglas Gregora4ffd852010-11-17 01:03:52 +00001318SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1319 SourceLocation EndLoc = getLocation();
1320 if (IvarLoc.isValid())
1321 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001322
Douglas Gregora4ffd852010-11-17 01:03:52 +00001323 return SourceRange(AtLoc, EndLoc);
1324}