blob: b0aeabf68f8afc0b01799548a4ddd28cfd0bef80 [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 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000248
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000249 if (ASTMutationListener *L = getASTContext().getASTMutationListener())
250 L->CompletedObjCForwardRef(this);
251}
252
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000253/// getFirstClassExtension - Find first class extension of the given class.
254ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
255 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000256 CDecl = CDecl->getNextClassCategory())
257 if (CDecl->IsClassExtension())
258 return CDecl;
259 return 0;
260}
261
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000262/// getNextClassCategory - Find next class extension in list of categories.
263const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
264 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
265 CDecl = CDecl->getNextClassCategory())
266 if (CDecl->IsClassExtension())
267 return CDecl;
268 return 0;
269}
270
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000271ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
272 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000273 // FIXME: Should make sure no callers ever do this.
274 if (!hasDefinition())
275 return 0;
276
277 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000278 LoadExternalDefinition();
279
Chris Lattner1e03a562008-03-16 00:19:01 +0000280 ObjCInterfaceDecl* ClassDecl = this;
281 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000282 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000283 clsDeclared = ClassDecl;
284 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000285 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000286 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
287 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000288 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
289 clsDeclared = ClassDecl;
290 return I;
291 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000292 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000293
Chris Lattner1e03a562008-03-16 00:19:01 +0000294 ClassDecl = ClassDecl->getSuperClass();
295 }
296 return NULL;
297}
298
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000299/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
300/// class whose name is passed as argument. If it is not one of the super classes
301/// the it returns NULL.
302ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
303 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000304 // FIXME: Should make sure no callers ever do this.
305 if (!hasDefinition())
306 return 0;
307
308 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000309 LoadExternalDefinition();
310
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000311 ObjCInterfaceDecl* ClassDecl = this;
312 while (ClassDecl != NULL) {
313 if (ClassDecl->getIdentifier() == ICName)
314 return ClassDecl;
315 ClassDecl = ClassDecl->getSuperClass();
316 }
317 return NULL;
318}
319
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000320/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000321/// the class, its categories, and its super classes (using a linear search).
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000322ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
323 bool isInstance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000324 // FIXME: Should make sure no callers ever do this.
325 if (!hasDefinition())
326 return 0;
327
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000328 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000329 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000331 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000332 LoadExternalDefinition();
333
Chris Lattner1e03a562008-03-16 00:19:01 +0000334 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000335 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000336 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Chris Lattner1e03a562008-03-16 00:19:01 +0000338 // Didn't find one yet - look through protocols.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000339 const ObjCList<ObjCProtocolDecl> &Protocols =
340 ClassDecl->getReferencedProtocols();
341 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
342 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000343 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000344 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattner1e03a562008-03-16 00:19:01 +0000346 // Didn't find one yet - now look through categories.
347 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
348 while (CatDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000349 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000350 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Steve Naroffb79c01e2008-12-08 20:57:28 +0000352 // Didn't find one yet - look through protocols.
353 const ObjCList<ObjCProtocolDecl> &Protocols =
354 CatDecl->getReferencedProtocols();
355 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
356 E = Protocols.end(); I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000357 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Steve Naroffb5584222009-02-26 11:32:02 +0000358 return MethodDecl;
Chris Lattner1e03a562008-03-16 00:19:01 +0000359 CatDecl = CatDecl->getNextClassCategory();
360 }
361 ClassDecl = ClassDecl->getSuperClass();
362 }
363 return NULL;
364}
365
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000366ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
367 const Selector &Sel,
368 bool Instance) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000369 // FIXME: Should make sure no callers ever do this.
370 if (!hasDefinition())
371 return 0;
372
373 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000374 LoadExternalDefinition();
375
Steve Naroffd789d3d2009-10-01 23:46:04 +0000376 ObjCMethodDecl *Method = 0;
377 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000378 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
379 : ImpDecl->getClassMethod(Sel);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000380
381 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000382 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000383 return Method;
384}
Chris Lattnerab351632009-02-20 20:59:54 +0000385
386//===----------------------------------------------------------------------===//
387// ObjCMethodDecl
388//===----------------------------------------------------------------------===//
389
390ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000391 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000392 SourceLocation endLoc,
393 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000394 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000395 DeclContext *contextDecl,
396 bool isInstance,
397 bool isVariadic,
398 bool isSynthesized,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000399 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000400 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000401 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000402 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000403 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000404 SelInfo, T, ResultTInfo, contextDecl,
405 isInstance,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000406 isVariadic, isSynthesized, isImplicitlyDeclared,
407 isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000408 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000409 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000410}
411
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000412ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
413 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
414 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
415 Selector(), QualType(), 0, 0);
416}
417
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000418void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
419 assert(PrevMethod);
420 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
421 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000422 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000423}
424
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000425void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
426 ArrayRef<ParmVarDecl*> Params,
427 ArrayRef<SourceLocation> SelLocs) {
428 ParamsAndSelLocs = 0;
429 NumParams = Params.size();
430 if (Params.empty() && SelLocs.empty())
431 return;
432
433 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
434 sizeof(SourceLocation) * SelLocs.size();
435 ParamsAndSelLocs = C.Allocate(Size);
436 std::copy(Params.begin(), Params.end(), getParams());
437 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
438}
439
440void ObjCMethodDecl::getSelectorLocs(
441 SmallVectorImpl<SourceLocation> &SelLocs) const {
442 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
443 SelLocs.push_back(getSelectorLoc(i));
444}
445
446void ObjCMethodDecl::setMethodParams(ASTContext &C,
447 ArrayRef<ParmVarDecl*> Params,
448 ArrayRef<SourceLocation> SelLocs) {
449 assert((!SelLocs.empty() || isImplicit()) &&
450 "No selector locs for non-implicit method");
451 if (isImplicit())
452 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
453
454 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc);
455 if (SelLocsKind != SelLoc_NonStandard)
456 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
457
458 setParamsAndSelLocs(C, Params, SelLocs);
459}
460
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000461/// \brief A definition will return its interface declaration.
462/// An interface declaration will return its definition.
463/// Otherwise it will return itself.
464ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
465 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000466 ObjCMethodDecl *Redecl = 0;
467 if (HasRedeclaration)
468 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000469 if (Redecl)
470 return Redecl;
471
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000472 Decl *CtxD = cast<Decl>(getDeclContext());
473
474 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
475 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
476 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
477
478 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
479 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
480 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
481
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000482 } else if (ObjCImplementationDecl *ImplD =
483 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000484 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
485 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000486
487 } else if (ObjCCategoryImplDecl *CImplD =
488 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000489 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000490 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000491 }
492
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000493 if (!Redecl && isRedeclaration()) {
494 // This is the last redeclaration, go back to the first method.
495 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
496 isInstanceMethod());
497 }
498
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000499 return Redecl ? Redecl : this;
500}
501
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000502ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
503 Decl *CtxD = cast<Decl>(getDeclContext());
504
505 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
506 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
507 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
508 isInstanceMethod()))
509 return MD;
510
511 } else if (ObjCCategoryImplDecl *CImplD =
512 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000513 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000514 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
515 isInstanceMethod()))
516 return MD;
517 }
518
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000519 if (isRedeclaration())
520 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
521 isInstanceMethod());
522
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000523 return this;
524}
525
John McCall85f3d762011-03-02 01:50:55 +0000526ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
527 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000528 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000529 return family;
530
John McCalld5313b02011-03-02 11:33:24 +0000531 // Check for an explicit attribute.
532 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
533 // The unfortunate necessity of mapping between enums here is due
534 // to the attributes framework.
535 switch (attr->getFamily()) {
536 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
537 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
538 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
539 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
540 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
541 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
542 }
543 Family = static_cast<unsigned>(family);
544 return family;
545 }
546
John McCall85f3d762011-03-02 01:50:55 +0000547 family = getSelector().getMethodFamily();
548 switch (family) {
549 case OMF_None: break;
550
551 // init only has a conventional meaning for an instance method, and
552 // it has to return an object.
553 case OMF_init:
554 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
555 family = OMF_None;
556 break;
557
558 // alloc/copy/new have a conventional meaning for both class and
559 // instance methods, but they require an object return.
560 case OMF_alloc:
561 case OMF_copy:
562 case OMF_mutableCopy:
563 case OMF_new:
564 if (!getResultType()->isObjCObjectPointerType())
565 family = OMF_None;
566 break;
567
568 // These selectors have a conventional meaning only for instance methods.
569 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000570 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000571 case OMF_retain:
572 case OMF_release:
573 case OMF_autorelease:
574 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000575 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000576 if (!isInstanceMethod())
577 family = OMF_None;
578 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000579
580 case OMF_performSelector:
581 if (!isInstanceMethod() ||
582 !getResultType()->isObjCIdType())
583 family = OMF_None;
584 else {
585 unsigned noParams = param_size();
586 if (noParams < 1 || noParams > 3)
587 family = OMF_None;
588 else {
589 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
590 QualType ArgT = (*it);
591 if (!ArgT->isObjCSelType()) {
592 family = OMF_None;
593 break;
594 }
595 while (--noParams) {
596 it++;
597 ArgT = (*it);
598 if (!ArgT->isObjCIdType()) {
599 family = OMF_None;
600 break;
601 }
602 }
603 }
604 }
605 break;
606
John McCall85f3d762011-03-02 01:50:55 +0000607 }
608
609 // Cache the result.
610 Family = static_cast<unsigned>(family);
611 return family;
612}
613
Mike Stump1eb44332009-09-09 15:08:12 +0000614void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000615 const ObjCInterfaceDecl *OID) {
616 QualType selfTy;
617 if (isInstanceMethod()) {
618 // There may be no interface context due to error in declaration
619 // of the interface (which has been reported). Recover gracefully.
620 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000621 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000622 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000623 } else {
624 selfTy = Context.getObjCIdType();
625 }
626 } else // we have a factory method.
627 selfTy = Context.getObjCClassType();
628
John McCall7acddac2011-06-17 06:42:21 +0000629 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000630 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000631
632 if (Context.getLangOptions().ObjCAutoRefCount) {
633 if (isInstanceMethod()) {
634 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000635
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000636 // 'self' is always __strong. It's actually pseudo-strong except
637 // in init methods (or methods labeled ns_consumes_self), though.
638 Qualifiers qs;
639 qs.setObjCLifetime(Qualifiers::OCL_Strong);
640 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000641
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000642 // In addition, 'self' is const unless this is an init method.
643 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
644 selfTy = selfTy.withConst();
645 selfIsPseudoStrong = true;
646 }
647 }
648 else {
649 assert(isClassMethod());
650 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000651 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000652 selfIsPseudoStrong = true;
653 }
John McCallf85e1932011-06-15 23:02:42 +0000654 }
655
656 ImplicitParamDecl *self
657 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
658 &Context.Idents.get("self"), selfTy);
659 setSelfDecl(self);
660
661 if (selfIsConsumed)
662 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000663
John McCall7acddac2011-06-17 06:42:21 +0000664 if (selfIsPseudoStrong)
665 self->setARCPseudoStrong(true);
666
Mike Stump1eb44332009-09-09 15:08:12 +0000667 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
668 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000669 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000670}
671
Chris Lattnerab351632009-02-20 20:59:54 +0000672ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
673 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
674 return ID;
675 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
676 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000677 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000678 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000679
680 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000681 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000682}
683
Chris Lattnerab351632009-02-20 20:59:54 +0000684//===----------------------------------------------------------------------===//
685// ObjCInterfaceDecl
686//===----------------------------------------------------------------------===//
687
688ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C,
689 DeclContext *DC,
690 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000691 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +0000692 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +0000693 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000694 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +0000695 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000696 PrevDecl, isInternal);
Douglas Gregor0af55012011-12-16 03:12:41 +0000697 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +0000698 return Result;
699}
700
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000701ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
702 unsigned ID) {
703 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
704 return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
705 0, false);
Chris Lattnerab351632009-02-20 20:59:54 +0000706}
707
708ObjCInterfaceDecl::
709ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000710 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
711 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000712 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +0000713 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000714{
Douglas Gregorfd002a72011-12-16 22:37:11 +0000715 setPreviousDeclaration(PrevDecl);
716
717 // Copy the 'data' pointer over.
718 if (PrevDecl)
719 Data = PrevDecl->Data;
720
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +0000721 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +0000722}
723
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000724void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000725 assert(data().ExternallyCompleted && "Class is not externally completed");
726 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000727 getASTContext().getExternalSource()->CompleteType(
728 const_cast<ObjCInterfaceDecl *>(this));
729}
730
731void ObjCInterfaceDecl::setExternallyCompleted() {
732 assert(getASTContext().getExternalSource() &&
733 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000734 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000735 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000736 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000737}
738
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000739ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000740 if (const ObjCInterfaceDecl *Def = getDefinition()) {
741 if (data().ExternallyCompleted)
742 LoadExternalDefinition();
743
744 return getASTContext().getObjCImplementation(
745 const_cast<ObjCInterfaceDecl*>(Def));
746 }
747
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000748 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +0000749 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000750}
751
752void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +0000753 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000754}
755
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000756/// all_declared_ivar_begin - return first ivar declared in this class,
757/// its extensions and its implementation. Lazily build the list on first
758/// access.
759ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000760 // FIXME: Should make sure no callers ever do this.
761 if (!hasDefinition())
762 return 0;
763
764 if (data().IvarList)
765 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000766
767 ObjCIvarDecl *curIvar = 0;
768 if (!ivar_empty()) {
769 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000770 data().IvarList = (*I); ++I;
771 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000772 curIvar->setNextIvar(*I);
773 }
774
775 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
776 CDecl = CDecl->getNextClassExtension()) {
777 if (!CDecl->ivar_empty()) {
778 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
779 E = CDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000780 if (!data().IvarList) {
781 data().IvarList = (*I); ++I;
782 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000783 }
784 for ( ;I != E; curIvar = *I, ++I)
785 curIvar->setNextIvar(*I);
786 }
787 }
788
789 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
790 if (!ImplDecl->ivar_empty()) {
791 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
792 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000793 if (!data().IvarList) {
794 data().IvarList = (*I); ++I;
795 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000796 }
797 for ( ;I != E; curIvar = *I, ++I)
798 curIvar->setNextIvar(*I);
799 }
800 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000801 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000802}
Chris Lattnerab351632009-02-20 20:59:54 +0000803
804/// FindCategoryDeclaration - Finds category declaration in the list of
805/// categories for this class and returns it. Name of the category is passed
806/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000807///
Chris Lattnerab351632009-02-20 20:59:54 +0000808ObjCCategoryDecl *
809ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000810 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000811 LoadExternalDefinition();
812
Chris Lattnerab351632009-02-20 20:59:54 +0000813 for (ObjCCategoryDecl *Category = getCategoryList();
814 Category; Category = Category->getNextClassCategory())
815 if (Category->getIdentifier() == CategoryId)
816 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000817 return 0;
818}
819
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000820ObjCMethodDecl *
821ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
822 for (ObjCCategoryDecl *Category = getCategoryList();
823 Category; Category = Category->getNextClassCategory())
824 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
825 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
826 return MD;
827 return 0;
828}
829
830ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
831 for (ObjCCategoryDecl *Category = getCategoryList();
832 Category; Category = Category->getNextClassCategory())
833 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
834 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
835 return MD;
836 return 0;
837}
838
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000839/// ClassImplementsProtocol - Checks that 'lProto' protocol
840/// has been implemented in IDecl class, its super class or categories (if
841/// lookupCategory is true).
842bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
843 bool lookupCategory,
844 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000845 if (!hasDefinition())
846 return false;
847
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000848 ObjCInterfaceDecl *IDecl = this;
849 // 1st, look up the class.
850 const ObjCList<ObjCProtocolDecl> &Protocols =
851 IDecl->getReferencedProtocols();
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000853 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
854 E = Protocols.end(); PI != E; ++PI) {
855 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
856 return true;
857 // This is dubious and is added to be compatible with gcc. In gcc, it is
858 // also allowed assigning a protocol-qualified 'id' type to a LHS object
859 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
860 // object. This IMO, should be a bug.
861 // FIXME: Treat this as an extension, and flag this as an error when GCC
862 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000863 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000864 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
865 return true;
866 }
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000868 // 2nd, look up the category.
869 if (lookupCategory)
870 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
871 CDecl = CDecl->getNextClassCategory()) {
872 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
873 E = CDecl->protocol_end(); PI != E; ++PI)
874 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
875 return true;
876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000878 // 3rd, look up the super class(s)
879 if (IDecl->getSuperClass())
880 return
881 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
882 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Fariborz Jahanian0fd89042009-08-11 22:02:25 +0000884 return false;
885}
886
Chris Lattnerab351632009-02-20 20:59:54 +0000887//===----------------------------------------------------------------------===//
888// ObjCIvarDecl
889//===----------------------------------------------------------------------===//
890
David Blaikie99ba9e32011-12-20 02:48:34 +0000891void ObjCIvarDecl::anchor() { }
892
Daniel Dunbara0654922010-04-02 20:10:03 +0000893ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000894 SourceLocation StartLoc,
895 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +0000896 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000897 AccessControl ac, Expr *BW,
898 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +0000899 if (DC) {
900 // Ivar's can only appear in interfaces, implementations (via synthesized
901 // properties), and class extensions (via direct declaration, or synthesized
902 // properties).
903 //
904 // FIXME: This should really be asserting this:
905 // (isa<ObjCCategoryDecl>(DC) &&
906 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
907 // but unfortunately we sometimes place ivars into non-class extension
908 // categories on error. This breaks an AST invariant, and should not be
909 // fixed.
910 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
911 isa<ObjCCategoryDecl>(DC)) &&
912 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000913 // Once a new ivar is created in any of class/class-extension/implementation
914 // decl contexts, the previously built IvarList must be rebuilt.
915 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
916 if (!ID) {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000917 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000918 ID = IM->getClassInterface();
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000919 if (BW)
920 IM->setHasSynthBitfield(true);
Chad Rosier30601782011-08-17 23:08:45 +0000921 } else {
Fariborz Jahanian000835d2010-08-23 18:51:39 +0000922 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
923 ID = CD->getClassInterface();
924 if (BW)
925 CD->setHasSynthBitfield(true);
926 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000927 }
928 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +0000929 }
930
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000931 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
932 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +0000933}
934
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000935ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
936 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
937 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
938 QualType(), 0, ObjCIvarDecl::None, 0, false);
939}
940
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000941const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
942 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +0000943
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000944 switch (DC->getKind()) {
945 default:
946 case ObjCCategoryImpl:
947 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +0000948 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +0000949
950 // Ivars can only appear in class extension categories.
951 case ObjCCategory: {
952 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
953 assert(CD->IsClassExtension() && "invalid container for ivar!");
954 return CD->getClassInterface();
955 }
956
957 case ObjCImplementation:
958 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
959
960 case ObjCInterface:
961 return cast<ObjCInterfaceDecl>(DC);
962 }
963}
Chris Lattnerab351632009-02-20 20:59:54 +0000964
965//===----------------------------------------------------------------------===//
966// ObjCAtDefsFieldDecl
967//===----------------------------------------------------------------------===//
968
David Blaikie99ba9e32011-12-20 02:48:34 +0000969void ObjCAtDefsFieldDecl::anchor() { }
970
Chris Lattnerab351632009-02-20 20:59:54 +0000971ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000972*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
973 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000974 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000975 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +0000976}
977
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000978ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
979 unsigned ID) {
980 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
981 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
982 0, QualType(), 0);
983}
984
Chris Lattnerab351632009-02-20 20:59:54 +0000985//===----------------------------------------------------------------------===//
986// ObjCProtocolDecl
987//===----------------------------------------------------------------------===//
988
David Blaikie99ba9e32011-12-20 02:48:34 +0000989void ObjCProtocolDecl::anchor() { }
990
Douglas Gregor27c6da22012-01-01 20:30:41 +0000991ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
992 SourceLocation nameLoc,
993 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000994 ObjCProtocolDecl *PrevDecl)
995 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +0000996{
997 setPreviousDeclaration(PrevDecl);
998 if (PrevDecl)
999 Data = PrevDecl->Data;
1000}
1001
Chris Lattnerab351632009-02-20 20:59:54 +00001002ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001003 IdentifierInfo *Id,
1004 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001005 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001006 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001007 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001008 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001009
1010 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001011}
1012
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001013ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1014 unsigned ID) {
1015 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1016 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1017 0);
1018}
1019
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001020ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1021 ObjCProtocolDecl *PDecl = this;
1022
1023 if (Name == getIdentifier())
1024 return PDecl;
1025
1026 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1027 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1028 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001030 return NULL;
1031}
1032
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001033// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001034// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001035ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1036 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001037 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001039 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001040 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Chris Lattnerab351632009-02-20 20:59:54 +00001042 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001043 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001044 return MethodDecl;
1045 return NULL;
1046}
1047
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001048void ObjCProtocolDecl::allocateDefinitionData() {
1049 assert(!Data && "Protocol already has a definition!");
Douglas Gregor1d784b22012-01-01 19:51:50 +00001050 Data = new (getASTContext()) DefinitionData;
1051 Data->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001052}
1053
1054void ObjCProtocolDecl::startDefinition() {
1055 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001056
1057 // Update all of the declarations with a pointer to the definition.
1058 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1059 RD != RDEnd; ++RD)
1060 RD->Data = this->Data;
Douglas Gregor27c6da22012-01-01 20:30:41 +00001061
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001062 if (ASTMutationListener *L = getASTContext().getASTMutationListener())
1063 L->CompletedObjCForwardRef(this);
1064}
1065
Chris Lattnerab351632009-02-20 20:59:54 +00001066//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001067// ObjCCategoryDecl
1068//===----------------------------------------------------------------------===//
1069
David Blaikie99ba9e32011-12-20 02:48:34 +00001070void ObjCCategoryDecl::anchor() { }
1071
Chris Lattnerab351632009-02-20 20:59:54 +00001072ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001073 SourceLocation AtLoc,
1074 SourceLocation ClassNameLoc,
1075 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001076 IdentifierInfo *Id,
1077 ObjCInterfaceDecl *IDecl) {
1078 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1079 CategoryNameLoc, Id,
1080 IDecl);
1081 if (IDecl) {
1082 // Link this category into its class's category list.
1083 CatDecl->NextClassCategory = IDecl->getCategoryList();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001084 if (IDecl->hasDefinition()) {
1085 IDecl->setCategoryList(CatDecl);
1086 if (ASTMutationListener *L = C.getASTMutationListener())
1087 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1088 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001089 }
1090
1091 return CatDecl;
1092}
1093
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001094ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1095 unsigned ID) {
1096 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1097 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1098 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001099}
1100
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001101ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1102 return getASTContext().getObjCImplementation(
1103 const_cast<ObjCCategoryDecl*>(this));
1104}
1105
1106void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1107 getASTContext().setObjCImplementation(this, ImplD);
1108}
1109
1110
Chris Lattnerab351632009-02-20 20:59:54 +00001111//===----------------------------------------------------------------------===//
1112// ObjCCategoryImplDecl
1113//===----------------------------------------------------------------------===//
1114
David Blaikie99ba9e32011-12-20 02:48:34 +00001115void ObjCCategoryImplDecl::anchor() { }
1116
Chris Lattnerab351632009-02-20 20:59:54 +00001117ObjCCategoryImplDecl *
1118ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001119 IdentifierInfo *Id,
1120 ObjCInterfaceDecl *ClassInterface,
1121 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001122 SourceLocation atStartLoc,
1123 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001124 if (ClassInterface && ClassInterface->hasDefinition())
1125 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001126 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001127 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001128}
1129
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001130ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1131 unsigned ID) {
1132 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1133 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1134 SourceLocation(), SourceLocation());
1135}
1136
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001137ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001138 // The class interface might be NULL if we are working with invalid code.
1139 if (const ObjCInterfaceDecl *ID = getClassInterface())
1140 return ID->FindCategoryDeclaration(getIdentifier());
1141 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001142}
1143
Chris Lattnerab351632009-02-20 20:59:54 +00001144
David Blaikie99ba9e32011-12-20 02:48:34 +00001145void ObjCImplDecl::anchor() { }
1146
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001147void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001148 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001149 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001150 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001151}
1152
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001153void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1154 ASTContext &Ctx = getASTContext();
1155
1156 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001157 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001158 if (IFace)
1159 Ctx.setObjCImplementation(IFace, ImplD);
1160
Duncan Sands98f2cca2009-07-21 07:56:29 +00001161 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001162 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1163 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1164 Ctx.setObjCImplementation(CD, ImplD);
1165 }
1166
1167 ClassInterface = IFace;
1168}
1169
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001170/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001171/// properties implemented in this category @implementation block and returns
1172/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001173///
Chris Lattner3aa18612009-02-28 18:42:10 +00001174ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001175FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1176 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001177 ObjCPropertyImplDecl *PID = *i;
1178 if (PID->getPropertyIvarDecl() &&
1179 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1180 return PID;
1181 }
1182 return 0;
1183}
1184
1185/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1186/// added to the list of those properties @synthesized/@dynamic in this
1187/// category @implementation block.
1188///
Chris Lattner3aa18612009-02-28 18:42:10 +00001189ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001190FindPropertyImplDecl(IdentifierInfo *Id) const {
1191 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001192 ObjCPropertyImplDecl *PID = *i;
1193 if (PID->getPropertyDecl()->getIdentifier() == Id)
1194 return PID;
1195 }
1196 return 0;
1197}
1198
Chris Lattner5f9e2722011-07-23 10:55:15 +00001199raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001200 const ObjCCategoryImplDecl *CID) {
1201 OS << CID->getName();
1202 return OS;
1203}
1204
Chris Lattnerab351632009-02-20 20:59:54 +00001205//===----------------------------------------------------------------------===//
1206// ObjCImplementationDecl
1207//===----------------------------------------------------------------------===//
1208
David Blaikie99ba9e32011-12-20 02:48:34 +00001209void ObjCImplementationDecl::anchor() { }
1210
Chris Lattnerab351632009-02-20 20:59:54 +00001211ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001212ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001213 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001214 ObjCInterfaceDecl *SuperDecl,
1215 SourceLocation nameLoc,
1216 SourceLocation atStartLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001217 if (ClassInterface && ClassInterface->hasDefinition())
1218 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001219 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1220 nameLoc, atStartLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001221}
1222
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001223ObjCImplementationDecl *
1224ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1225 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1226 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1227 SourceLocation());
1228}
1229
John McCallda6d9762011-07-22 04:15:06 +00001230void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1231 CXXCtorInitializer ** initializers,
1232 unsigned numInitializers) {
1233 if (numInitializers > 0) {
1234 NumIvarInitializers = numInitializers;
1235 CXXCtorInitializer **ivarInitializers =
1236 new (C) CXXCtorInitializer*[NumIvarInitializers];
1237 memcpy(ivarInitializers, initializers,
1238 numInitializers * sizeof(CXXCtorInitializer*));
1239 IvarInitializers = ivarInitializers;
1240 }
1241}
1242
Chris Lattner5f9e2722011-07-23 10:55:15 +00001243raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer900fc632010-04-17 09:33:03 +00001244 const ObjCImplementationDecl *ID) {
1245 OS << ID->getName();
1246 return OS;
1247}
1248
Chris Lattnerab351632009-02-20 20:59:54 +00001249//===----------------------------------------------------------------------===//
1250// ObjCCompatibleAliasDecl
1251//===----------------------------------------------------------------------===//
1252
David Blaikie99ba9e32011-12-20 02:48:34 +00001253void ObjCCompatibleAliasDecl::anchor() { }
1254
Chris Lattnerab351632009-02-20 20:59:54 +00001255ObjCCompatibleAliasDecl *
1256ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1257 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001258 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001259 ObjCInterfaceDecl* AliasedClass) {
1260 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1261}
1262
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001263ObjCCompatibleAliasDecl *
1264ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1265 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1266 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1267}
1268
Chris Lattnerab351632009-02-20 20:59:54 +00001269//===----------------------------------------------------------------------===//
1270// ObjCPropertyDecl
1271//===----------------------------------------------------------------------===//
1272
David Blaikie99ba9e32011-12-20 02:48:34 +00001273void ObjCPropertyDecl::anchor() { }
1274
Chris Lattnerab351632009-02-20 20:59:54 +00001275ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1276 SourceLocation L,
1277 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001278 SourceLocation AtLoc,
John McCall83a230c2010-06-04 20:50:08 +00001279 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001280 PropertyControl propControl) {
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001281 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001282}
1283
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001284ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1285 unsigned ID) {
1286 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1287 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1288 0);
1289}
1290
Chris Lattnerab351632009-02-20 20:59:54 +00001291//===----------------------------------------------------------------------===//
1292// ObjCPropertyImplDecl
1293//===----------------------------------------------------------------------===//
1294
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001295ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001296 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001297 SourceLocation atLoc,
1298 SourceLocation L,
1299 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001300 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001301 ObjCIvarDecl *ivar,
1302 SourceLocation ivarLoc) {
1303 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1304 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001305}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001306
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001307ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1308 unsigned ID) {
1309 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1310 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1311 0, Dynamic, 0, SourceLocation());
1312}
1313
Douglas Gregora4ffd852010-11-17 01:03:52 +00001314SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1315 SourceLocation EndLoc = getLocation();
1316 if (IvarLoc.isValid())
1317 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001318
Douglas Gregora4ffd852010-11-17 01:03:52 +00001319 return SourceRange(AtLoc, EndLoc);
1320}