blob: 2b7b13b43fa08b0bf59066bd3b5c600a10c57b27 [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"
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +000016#include "clang/AST/ASTMutationListener.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/Stmt.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000019#include "llvm/ADT/STLExtras.h"
Anna Zaksad0ce532012-09-27 19:45:11 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000021using namespace clang;
22
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000023//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000024// ObjCListBase
25//===----------------------------------------------------------------------===//
26
Chris Lattner38af2de2009-02-20 21:35:13 +000027void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorff331c12010-07-25 18:17:45 +000028 List = 0;
Chris Lattner11e1e1a2009-02-20 21:16:26 +000029 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump1eb44332009-09-09 15:08:12 +000030
31
Chris Lattner4ee413b2009-02-20 21:44:01 +000032 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000033 NumElts = Elts;
34 memcpy(List, InList, sizeof(void*)*Elts);
35}
36
Douglas Gregor18df52b2010-01-16 15:02:53 +000037void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
38 const SourceLocation *Locs, ASTContext &Ctx) {
39 if (Elts == 0)
40 return;
41
42 Locations = new (Ctx) SourceLocation[Elts];
43 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
44 set(InList, Elts, Ctx);
45}
46
Chris Lattner11e1e1a2009-02-20 21:16:26 +000047//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000048// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000049//===----------------------------------------------------------------------===//
50
David Blaikie99ba9e32011-12-20 02:48:34 +000051void ObjCContainerDecl::anchor() { }
52
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000053/// getIvarDecl - This method looks up an ivar in this ContextDecl.
54///
55ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000056ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000057 lookup_const_iterator Ivar, IvarEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000058 for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000059 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
60 return ivar;
61 }
62 return 0;
63}
64
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000065// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000066ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000067ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000068 // Since instance & class methods can have the same name, the loop below
69 // ensures we get the correct method.
70 //
71 // @interface Whatever
72 // - (int) class_method;
73 // + (float) class_method;
74 // @end
75 //
76 lookup_const_iterator Meth, MethEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000077 for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000078 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000079 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000080 return MD;
81 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000082 return 0;
83}
84
Ted Kremenek9f550ff2010-03-15 20:11:46 +000085ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000086ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000087 IdentifierInfo *propertyID) {
88
Ted Kremenekde09d0c2010-03-15 20:11:53 +000089 DeclContext::lookup_const_iterator I, E;
Ted Kremenek9f550ff2010-03-15 20:11:46 +000090 llvm::tie(I, E) = DC->lookup(propertyID);
91 for ( ; I != E; ++I)
92 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
93 return PD;
94
95 return 0;
96}
97
Anna Zaksad0ce532012-09-27 19:45:11 +000098IdentifierInfo *
99ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
100 SmallString<128> ivarName;
101 {
102 llvm::raw_svector_ostream os(ivarName);
103 os << '_' << getIdentifier()->getName();
104 }
105 return &Ctx.Idents.get(ivarName.str());
106}
107
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000108/// FindPropertyDeclaration - Finds declaration of the property given its name
109/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000110ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000111ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000113 if (ObjCPropertyDecl *PD =
114 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
115 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000117 switch (getKind()) {
118 default:
119 break;
120 case Decl::ObjCProtocol: {
121 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
122 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
123 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000124 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
125 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000126 break;
127 }
128 case Decl::ObjCInterface: {
129 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
130 // Look through categories.
131 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
132 Cat; Cat = Cat->getNextClassCategory())
133 if (!Cat->IsClassExtension())
134 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
135 return P;
136
137 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000138 for (ObjCInterfaceDecl::all_protocol_iterator
139 I = OID->all_referenced_protocol_begin(),
140 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000141 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
142 return P;
143
144 // Finally, check the super class.
145 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
146 return superClass->FindPropertyDeclaration(PropertyId);
147 break;
148 }
149 case Decl::ObjCCategory: {
150 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
151 // Look through protocols.
152 if (!OCD->IsClassExtension())
153 for (ObjCCategoryDecl::protocol_iterator
154 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
155 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
156 return P;
157
158 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000159 }
160 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000161 return 0;
162}
163
David Blaikie99ba9e32011-12-20 02:48:34 +0000164void ObjCInterfaceDecl::anchor() { }
165
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000166/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
167/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000168/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000169///
170ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000171ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000172 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000173 // FIXME: Should make sure no callers ever do this.
174 if (!hasDefinition())
175 return 0;
176
177 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000178 LoadExternalDefinition();
179
Ted Kremenek37cafb02010-03-15 20:30:07 +0000180 if (ObjCPropertyDecl *PD =
181 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
182 return PD;
183
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000184 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000185 for (ObjCInterfaceDecl::all_protocol_iterator
186 I = all_referenced_protocol_begin(),
187 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000188 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
189 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000190
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000191 return 0;
192}
193
Anna Zakse63aedd2012-10-31 01:18:22 +0000194void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM) const {
Anna Zaksb36ea372012-10-18 19:17:53 +0000195 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
196 E = prop_end(); P != E; ++P) {
197 ObjCPropertyDecl *Prop = *P;
198 PM[Prop->getIdentifier()] = Prop;
199 }
200 for (ObjCInterfaceDecl::all_protocol_iterator
201 PI = all_referenced_protocol_begin(),
202 E = all_referenced_protocol_end(); PI != E; ++PI)
203 (*PI)->collectPropertiesToImplement(PM);
Anna Zakse63aedd2012-10-31 01:18:22 +0000204 // Note, the properties declared only in class extensions are still copied
205 // into the main @interface's property list, and therefore we don't
206 // explicitly, have to search class extension properties.
Anna Zaksb36ea372012-10-18 19:17:53 +0000207}
208
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000209bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
210 const ObjCInterfaceDecl *Class = this;
211 while (Class) {
212 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
213 return true;
214 Class = Class->getSuperClass();
215 }
216 return false;
217}
218
219const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
220 const ObjCInterfaceDecl *Class = this;
221 while (Class) {
222 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
223 return Class;
224 Class = Class->getSuperClass();
225 }
226 return 0;
227}
228
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000229void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
230 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
231 ASTContext &C)
232{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000233 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000234 LoadExternalDefinition();
235
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000236 if (data().AllReferencedProtocols.empty() &&
237 data().ReferencedProtocols.empty()) {
238 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000239 return;
240 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000241
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000242 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000243 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000244 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000245 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000246 for (unsigned i = 0; i < ExtNum; i++) {
247 bool protocolExists = false;
248 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000249 for (all_protocol_iterator
250 p = all_referenced_protocol_begin(),
251 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000252 ObjCProtocolDecl *Proto = (*p);
253 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
254 protocolExists = true;
255 break;
256 }
257 }
258 // Do we want to warn on a protocol in extension class which
259 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000260 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000261 ProtocolRefs.push_back(ProtoInExtension);
262 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000263
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000264 if (ProtocolRefs.empty())
265 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000266
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000267 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000268 for (all_protocol_iterator p = all_referenced_protocol_begin(),
269 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000270 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000271 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000272
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000273 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000274}
275
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000276void ObjCInterfaceDecl::allocateDefinitionData() {
277 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor26fec632011-12-15 18:17:27 +0000278 Data = new (getASTContext()) DefinitionData();
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000279 Data->Definition = this;
280
281 // Make the type point at the definition, now that we have one.
282 if (TypeForDecl)
283 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000284}
285
286void ObjCInterfaceDecl::startDefinition() {
287 allocateDefinitionData();
288
Douglas Gregor53df7a12011-12-15 18:03:09 +0000289 // Update all of the declarations with a pointer to the definition.
290 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
291 RD != RDEnd; ++RD) {
292 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000293 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000294 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000295}
296
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000297/// getFirstClassExtension - Find first class extension of the given class.
298ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
299 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000300 CDecl = CDecl->getNextClassCategory())
301 if (CDecl->IsClassExtension())
302 return CDecl;
303 return 0;
304}
305
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000306/// getNextClassCategory - Find next class extension in list of categories.
307const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
308 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
309 CDecl = CDecl->getNextClassCategory())
310 if (CDecl->IsClassExtension())
311 return CDecl;
312 return 0;
313}
314
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000315ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
316 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000317 // FIXME: Should make sure no callers ever do this.
318 if (!hasDefinition())
319 return 0;
320
321 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000322 LoadExternalDefinition();
323
Chris Lattner1e03a562008-03-16 00:19:01 +0000324 ObjCInterfaceDecl* ClassDecl = this;
325 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000326 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000327 clsDeclared = ClassDecl;
328 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000329 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000330 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
331 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000332 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
333 clsDeclared = ClassDecl;
334 return I;
335 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000336 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000337
Chris Lattner1e03a562008-03-16 00:19:01 +0000338 ClassDecl = ClassDecl->getSuperClass();
339 }
340 return NULL;
341}
342
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000343/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
344/// class whose name is passed as argument. If it is not one of the super classes
345/// the it returns NULL.
346ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
347 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000348 // FIXME: Should make sure no callers ever do this.
349 if (!hasDefinition())
350 return 0;
351
352 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000353 LoadExternalDefinition();
354
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000355 ObjCInterfaceDecl* ClassDecl = this;
356 while (ClassDecl != NULL) {
357 if (ClassDecl->getIdentifier() == ICName)
358 return ClassDecl;
359 ClassDecl = ClassDecl->getSuperClass();
360 }
361 return NULL;
362}
363
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000364/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000365/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000366ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
367 bool isInstance,
368 bool shallowCategoryLookup) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000369 // FIXME: Should make sure no callers ever do this.
370 if (!hasDefinition())
371 return 0;
372
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000373 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000374 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000376 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000377 LoadExternalDefinition();
378
Chris Lattner1e03a562008-03-16 00:19:01 +0000379 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000380 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000381 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattner1e03a562008-03-16 00:19:01 +0000383 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000384 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
385 E = ClassDecl->protocol_end();
386 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000387 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000388 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000389
390 // Didn't find one yet - now look through categories.
391 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
392 while (CatDecl) {
393 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
394 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000395
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000396 if (!shallowCategoryLookup) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000397 // Didn't find one yet - look through protocols.
398 const ObjCList<ObjCProtocolDecl> &Protocols =
399 CatDecl->getReferencedProtocols();
400 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
401 E = Protocols.end(); I != E; ++I)
402 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
403 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000404 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000405 CatDecl = CatDecl->getNextClassCategory();
Chris Lattner1e03a562008-03-16 00:19:01 +0000406 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000407
Chris Lattner1e03a562008-03-16 00:19:01 +0000408 ClassDecl = ClassDecl->getSuperClass();
409 }
410 return NULL;
411}
412
Anna Zakse61354b2012-07-27 19:07:44 +0000413// Will search "local" class/category implementations for a method decl.
414// If failed, then we search in class's root for an instance method.
415// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000416ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
417 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000418 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000419 // FIXME: Should make sure no callers ever do this.
420 if (!hasDefinition())
421 return 0;
422
423 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000424 LoadExternalDefinition();
425
Steve Naroffd789d3d2009-10-01 23:46:04 +0000426 ObjCMethodDecl *Method = 0;
427 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000428 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
429 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000430
431 // Look through local category implementations associated with the class.
432 if (!Method)
433 Method = Instance ? getCategoryInstanceMethod(Sel)
434 : getCategoryClassMethod(Sel);
435
436 // Before we give up, check if the selector is an instance method.
437 // But only in the root. This matches gcc's behavior and what the
438 // runtime expects.
439 if (!Instance && !Method && !getSuperClass()) {
440 Method = lookupInstanceMethod(Sel);
441 // Look through local category implementations associated
442 // with the root class.
443 if (!Method)
444 Method = lookupPrivateMethod(Sel, true);
445 }
446
Steve Naroffd789d3d2009-10-01 23:46:04 +0000447 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000448 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000449 return Method;
450}
Chris Lattnerab351632009-02-20 20:59:54 +0000451
452//===----------------------------------------------------------------------===//
453// ObjCMethodDecl
454//===----------------------------------------------------------------------===//
455
456ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000457 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000458 SourceLocation endLoc,
459 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000460 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000461 DeclContext *contextDecl,
462 bool isInstance,
463 bool isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000464 bool isPropertyAccessor,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000465 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000466 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000467 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000468 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000469 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000470 SelInfo, T, ResultTInfo, contextDecl,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000471 isInstance, isVariadic, isPropertyAccessor,
472 isImplicitlyDeclared, isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000473 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000474 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000475}
476
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000477ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
478 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
479 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
480 Selector(), QualType(), 0, 0);
481}
482
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000483Stmt *ObjCMethodDecl::getBody() const {
484 return Body.get(getASTContext().getExternalSource());
485}
486
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000487void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
488 assert(PrevMethod);
489 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
490 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000491 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000492}
493
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000494void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
495 ArrayRef<ParmVarDecl*> Params,
496 ArrayRef<SourceLocation> SelLocs) {
497 ParamsAndSelLocs = 0;
498 NumParams = Params.size();
499 if (Params.empty() && SelLocs.empty())
500 return;
501
502 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
503 sizeof(SourceLocation) * SelLocs.size();
504 ParamsAndSelLocs = C.Allocate(Size);
505 std::copy(Params.begin(), Params.end(), getParams());
506 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
507}
508
509void ObjCMethodDecl::getSelectorLocs(
510 SmallVectorImpl<SourceLocation> &SelLocs) const {
511 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
512 SelLocs.push_back(getSelectorLoc(i));
513}
514
515void ObjCMethodDecl::setMethodParams(ASTContext &C,
516 ArrayRef<ParmVarDecl*> Params,
517 ArrayRef<SourceLocation> SelLocs) {
518 assert((!SelLocs.empty() || isImplicit()) &&
519 "No selector locs for non-implicit method");
520 if (isImplicit())
521 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
522
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000523 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
524 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000525 if (SelLocsKind != SelLoc_NonStandard)
526 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
527
528 setParamsAndSelLocs(C, Params, SelLocs);
529}
530
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000531/// \brief A definition will return its interface declaration.
532/// An interface declaration will return its definition.
533/// Otherwise it will return itself.
534ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
535 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000536 ObjCMethodDecl *Redecl = 0;
537 if (HasRedeclaration)
538 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000539 if (Redecl)
540 return Redecl;
541
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000542 Decl *CtxD = cast<Decl>(getDeclContext());
543
544 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
545 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
546 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
547
548 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
549 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
550 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
551
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000552 } else if (ObjCImplementationDecl *ImplD =
553 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000554 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
555 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000556
557 } else if (ObjCCategoryImplDecl *CImplD =
558 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000559 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000560 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000561 }
562
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000563 if (!Redecl && isRedeclaration()) {
564 // This is the last redeclaration, go back to the first method.
565 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
566 isInstanceMethod());
567 }
568
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000569 return Redecl ? Redecl : this;
570}
571
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000572ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
573 Decl *CtxD = cast<Decl>(getDeclContext());
574
575 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
576 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
577 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
578 isInstanceMethod()))
579 return MD;
580
581 } else if (ObjCCategoryImplDecl *CImplD =
582 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000583 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000584 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
585 isInstanceMethod()))
586 return MD;
587 }
588
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000589 if (isRedeclaration())
590 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
591 isInstanceMethod());
592
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000593 return this;
594}
595
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000596SourceLocation ObjCMethodDecl::getLocEnd() const {
597 if (Stmt *Body = getBody())
598 return Body->getLocEnd();
599 return DeclEndLoc;
600}
601
John McCall85f3d762011-03-02 01:50:55 +0000602ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
603 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000604 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000605 return family;
606
John McCalld5313b02011-03-02 11:33:24 +0000607 // Check for an explicit attribute.
608 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
609 // The unfortunate necessity of mapping between enums here is due
610 // to the attributes framework.
611 switch (attr->getFamily()) {
612 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
613 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
614 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
615 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
616 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
617 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
618 }
619 Family = static_cast<unsigned>(family);
620 return family;
621 }
622
John McCall85f3d762011-03-02 01:50:55 +0000623 family = getSelector().getMethodFamily();
624 switch (family) {
625 case OMF_None: break;
626
627 // init only has a conventional meaning for an instance method, and
628 // it has to return an object.
629 case OMF_init:
630 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
631 family = OMF_None;
632 break;
633
634 // alloc/copy/new have a conventional meaning for both class and
635 // instance methods, but they require an object return.
636 case OMF_alloc:
637 case OMF_copy:
638 case OMF_mutableCopy:
639 case OMF_new:
640 if (!getResultType()->isObjCObjectPointerType())
641 family = OMF_None;
642 break;
643
644 // These selectors have a conventional meaning only for instance methods.
645 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000646 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000647 case OMF_retain:
648 case OMF_release:
649 case OMF_autorelease:
650 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000651 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000652 if (!isInstanceMethod())
653 family = OMF_None;
654 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000655
656 case OMF_performSelector:
657 if (!isInstanceMethod() ||
658 !getResultType()->isObjCIdType())
659 family = OMF_None;
660 else {
661 unsigned noParams = param_size();
662 if (noParams < 1 || noParams > 3)
663 family = OMF_None;
664 else {
665 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
666 QualType ArgT = (*it);
667 if (!ArgT->isObjCSelType()) {
668 family = OMF_None;
669 break;
670 }
671 while (--noParams) {
672 it++;
673 ArgT = (*it);
674 if (!ArgT->isObjCIdType()) {
675 family = OMF_None;
676 break;
677 }
678 }
679 }
680 }
681 break;
682
John McCall85f3d762011-03-02 01:50:55 +0000683 }
684
685 // Cache the result.
686 Family = static_cast<unsigned>(family);
687 return family;
688}
689
Mike Stump1eb44332009-09-09 15:08:12 +0000690void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000691 const ObjCInterfaceDecl *OID) {
692 QualType selfTy;
693 if (isInstanceMethod()) {
694 // There may be no interface context due to error in declaration
695 // of the interface (which has been reported). Recover gracefully.
696 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000697 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000698 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000699 } else {
700 selfTy = Context.getObjCIdType();
701 }
702 } else // we have a factory method.
703 selfTy = Context.getObjCClassType();
704
John McCall7acddac2011-06-17 06:42:21 +0000705 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000706 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000707
David Blaikie4e4d0842012-03-11 07:00:24 +0000708 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000709 if (isInstanceMethod()) {
710 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000711
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000712 // 'self' is always __strong. It's actually pseudo-strong except
713 // in init methods (or methods labeled ns_consumes_self), though.
714 Qualifiers qs;
715 qs.setObjCLifetime(Qualifiers::OCL_Strong);
716 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000717
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000718 // In addition, 'self' is const unless this is an init method.
719 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
720 selfTy = selfTy.withConst();
721 selfIsPseudoStrong = true;
722 }
723 }
724 else {
725 assert(isClassMethod());
726 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000727 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000728 selfIsPseudoStrong = true;
729 }
John McCallf85e1932011-06-15 23:02:42 +0000730 }
731
732 ImplicitParamDecl *self
733 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
734 &Context.Idents.get("self"), selfTy);
735 setSelfDecl(self);
736
737 if (selfIsConsumed)
738 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000739
John McCall7acddac2011-06-17 06:42:21 +0000740 if (selfIsPseudoStrong)
741 self->setARCPseudoStrong(true);
742
Mike Stump1eb44332009-09-09 15:08:12 +0000743 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
744 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000745 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000746}
747
Chris Lattnerab351632009-02-20 20:59:54 +0000748ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
749 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
750 return ID;
751 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
752 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000753 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000754 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000755
756 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000757 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000758}
759
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000760static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
761 const ObjCMethodDecl *Method,
762 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
763 bool MovedToSuper) {
764 if (!Container)
765 return;
766
767 // In categories look for overriden methods from protocols. A method from
768 // category is not "overriden" since it is considered as the "same" method
769 // (same USR) as the one from the interface.
770 if (const ObjCCategoryDecl *
771 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
772 // Check whether we have a matching method at this category but only if we
773 // are at the super class level.
774 if (MovedToSuper)
775 if (ObjCMethodDecl *
776 Overridden = Container->getMethod(Method->getSelector(),
777 Method->isInstanceMethod()))
778 if (Method != Overridden) {
779 // We found an override at this category; there is no need to look
780 // into its protocols.
781 Methods.push_back(Overridden);
782 return;
783 }
784
785 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
786 PEnd = Category->protocol_end();
787 P != PEnd; ++P)
788 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
789 return;
790 }
791
792 // Check whether we have a matching method at this level.
793 if (const ObjCMethodDecl *
794 Overridden = Container->getMethod(Method->getSelector(),
795 Method->isInstanceMethod()))
796 if (Method != Overridden) {
797 // We found an override at this level; there is no need to look
798 // into other protocols or categories.
799 Methods.push_back(Overridden);
800 return;
801 }
802
803 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
804 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
805 PEnd = Protocol->protocol_end();
806 P != PEnd; ++P)
807 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
808 }
809
810 if (const ObjCInterfaceDecl *
811 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
812 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
813 PEnd = Interface->protocol_end();
814 P != PEnd; ++P)
815 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
816
817 for (const ObjCCategoryDecl *Category = Interface->getCategoryList();
818 Category; Category = Category->getNextClassCategory())
819 CollectOverriddenMethodsRecurse(Category, Method, Methods,
820 MovedToSuper);
821
822 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
823 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
824 /*MovedToSuper=*/true);
825 }
826}
827
828static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
829 const ObjCMethodDecl *Method,
830 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
831 CollectOverriddenMethodsRecurse(Container, Method, Methods,
832 /*MovedToSuper=*/false);
833}
834
835static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
836 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
837 assert(Method->isOverriding());
838
839 if (const ObjCProtocolDecl *
840 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
841 CollectOverriddenMethods(ProtD, Method, overridden);
842
843 } else if (const ObjCImplDecl *
844 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
845 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
846 if (!ID)
847 return;
848 // Start searching for overridden methods using the method from the
849 // interface as starting point.
850 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
851 Method->isInstanceMethod()))
852 Method = IFaceMeth;
853 CollectOverriddenMethods(ID, Method, overridden);
854
855 } else if (const ObjCCategoryDecl *
856 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
857 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
858 if (!ID)
859 return;
860 // Start searching for overridden methods using the method from the
861 // interface as starting point.
862 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
863 Method->isInstanceMethod()))
864 Method = IFaceMeth;
865 CollectOverriddenMethods(ID, Method, overridden);
866
867 } else {
868 CollectOverriddenMethods(
869 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
870 Method, overridden);
871 }
872}
873
874static void collectOnCategoriesAfterLocation(SourceLocation Loc,
875 const ObjCInterfaceDecl *Class,
876 SourceManager &SM,
877 const ObjCMethodDecl *Method,
878 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
879 if (!Class)
880 return;
881
882 for (const ObjCCategoryDecl *Category = Class->getCategoryList();
883 Category; Category = Category->getNextClassCategory())
884 if (SM.isBeforeInTranslationUnit(Loc, Category->getLocation()))
885 CollectOverriddenMethodsRecurse(Category, Method, Methods, true);
886
887 collectOnCategoriesAfterLocation(Loc, Class->getSuperClass(), SM,
888 Method, Methods);
889}
890
891/// \brief Faster collection that is enabled when ObjCMethodDecl::isOverriding()
892/// returns false.
893/// You'd think that in that case there are no overrides but categories can
894/// "introduce" new overridden methods that are missed by Sema because the
895/// overrides lookup that it does for methods, inside implementations, will
896/// stop at the interface level (if there is a method there) and not look
897/// further in super classes.
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000898/// Methods in an implementation can overide methods in super class's category
899/// but not in current class's category. But, such methods
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000900static void collectOverriddenMethodsFast(SourceManager &SM,
901 const ObjCMethodDecl *Method,
902 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
903 assert(!Method->isOverriding());
904
905 const ObjCContainerDecl *
906 ContD = cast<ObjCContainerDecl>(Method->getDeclContext());
907 if (isa<ObjCInterfaceDecl>(ContD) || isa<ObjCProtocolDecl>(ContD))
908 return;
909 const ObjCInterfaceDecl *Class = Method->getClassInterface();
910 if (!Class)
911 return;
912
913 collectOnCategoriesAfterLocation(Class->getLocation(), Class->getSuperClass(),
914 SM, Method, Methods);
915}
916
917void ObjCMethodDecl::getOverriddenMethods(
918 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
919 const ObjCMethodDecl *Method = this;
920
921 if (Method->isRedeclaration()) {
922 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
923 getMethod(Method->getSelector(), Method->isInstanceMethod());
924 }
925
926 if (!Method->isOverriding()) {
927 collectOverriddenMethodsFast(getASTContext().getSourceManager(),
928 Method, Overridden);
929 } else {
930 collectOverriddenMethodsSlow(Method, Overridden);
931 assert(!Overridden.empty() &&
932 "ObjCMethodDecl's overriding bit is not as expected");
933 }
934}
935
Jordan Rose04bec392012-10-10 16:42:54 +0000936const ObjCPropertyDecl *
937ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
938 Selector Sel = getSelector();
939 unsigned NumArgs = Sel.getNumArgs();
940 if (NumArgs > 1)
941 return 0;
942
Jordan Rose50d2b262012-10-11 16:02:02 +0000943 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose04bec392012-10-10 16:42:54 +0000944 return 0;
945
946 if (isPropertyAccessor()) {
947 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
948 bool IsGetter = (NumArgs == 0);
949
950 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
951 E = Container->prop_end();
952 I != E; ++I) {
953 Selector NextSel = IsGetter ? (*I)->getGetterName()
954 : (*I)->getSetterName();
955 if (NextSel == Sel)
956 return *I;
957 }
958
959 llvm_unreachable("Marked as a property accessor but no property found!");
960 }
961
962 if (!CheckOverrides)
963 return 0;
964
965 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
966 OverridesTy Overrides;
967 getOverriddenMethods(Overrides);
968 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
969 I != E; ++I) {
970 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
971 return Prop;
972 }
973
974 return 0;
975
976}
977
Chris Lattnerab351632009-02-20 20:59:54 +0000978//===----------------------------------------------------------------------===//
979// ObjCInterfaceDecl
980//===----------------------------------------------------------------------===//
981
Douglas Gregora6ea10e2012-01-17 18:09:05 +0000982ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +0000983 DeclContext *DC,
984 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000985 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +0000986 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +0000987 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000988 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +0000989 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000990 PrevDecl, isInternal);
Douglas Gregor0af55012011-12-16 03:12:41 +0000991 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +0000992 return Result;
993}
994
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000995ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
996 unsigned ID) {
997 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
998 return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
999 0, false);
Chris Lattnerab351632009-02-20 20:59:54 +00001000}
1001
1002ObjCInterfaceDecl::
1003ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001004 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1005 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001006 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +00001007 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001008{
Douglas Gregorfd002a72011-12-16 22:37:11 +00001009 setPreviousDeclaration(PrevDecl);
1010
1011 // Copy the 'data' pointer over.
1012 if (PrevDecl)
1013 Data = PrevDecl->Data;
1014
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +00001015 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +00001016}
1017
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001018void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001019 assert(data().ExternallyCompleted && "Class is not externally completed");
1020 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001021 getASTContext().getExternalSource()->CompleteType(
1022 const_cast<ObjCInterfaceDecl *>(this));
1023}
1024
1025void ObjCInterfaceDecl::setExternallyCompleted() {
1026 assert(getASTContext().getExternalSource() &&
1027 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001028 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001029 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001030 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001031}
1032
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001033ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001034 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1035 if (data().ExternallyCompleted)
1036 LoadExternalDefinition();
1037
1038 return getASTContext().getObjCImplementation(
1039 const_cast<ObjCInterfaceDecl*>(Def));
1040 }
1041
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001042 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +00001043 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001044}
1045
1046void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001047 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001048}
1049
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001050/// all_declared_ivar_begin - return first ivar declared in this class,
1051/// its extensions and its implementation. Lazily build the list on first
1052/// access.
1053ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001054 // FIXME: Should make sure no callers ever do this.
1055 if (!hasDefinition())
1056 return 0;
1057
1058 if (data().IvarList)
1059 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001060
1061 ObjCIvarDecl *curIvar = 0;
1062 if (!ivar_empty()) {
1063 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
David Blaikie581deb32012-06-06 20:45:41 +00001064 data().IvarList = *I; ++I;
1065 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1066 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001067 }
1068
1069 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
1070 CDecl = CDecl->getNextClassExtension()) {
1071 if (!CDecl->ivar_empty()) {
1072 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
1073 E = CDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001074 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001075 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001076 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001077 }
David Blaikie581deb32012-06-06 20:45:41 +00001078 for ( ;I != E; curIvar = *I, ++I)
1079 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001080 }
1081 }
1082
1083 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1084 if (!ImplDecl->ivar_empty()) {
1085 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1086 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001087 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001088 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001089 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001090 }
David Blaikie581deb32012-06-06 20:45:41 +00001091 for ( ;I != E; curIvar = *I, ++I)
1092 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001093 }
1094 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001095 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001096}
Chris Lattnerab351632009-02-20 20:59:54 +00001097
1098/// FindCategoryDeclaration - Finds category declaration in the list of
1099/// categories for this class and returns it. Name of the category is passed
1100/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001101///
Chris Lattnerab351632009-02-20 20:59:54 +00001102ObjCCategoryDecl *
1103ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001104 // FIXME: Should make sure no callers ever do this.
1105 if (!hasDefinition())
1106 return 0;
1107
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001108 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001109 LoadExternalDefinition();
1110
Chris Lattnerab351632009-02-20 20:59:54 +00001111 for (ObjCCategoryDecl *Category = getCategoryList();
1112 Category; Category = Category->getNextClassCategory())
1113 if (Category->getIdentifier() == CategoryId)
1114 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001115 return 0;
1116}
1117
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001118ObjCMethodDecl *
1119ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1120 for (ObjCCategoryDecl *Category = getCategoryList();
1121 Category; Category = Category->getNextClassCategory())
1122 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1123 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1124 return MD;
1125 return 0;
1126}
1127
1128ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1129 for (ObjCCategoryDecl *Category = getCategoryList();
1130 Category; Category = Category->getNextClassCategory())
1131 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1132 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1133 return MD;
1134 return 0;
1135}
1136
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001137/// ClassImplementsProtocol - Checks that 'lProto' protocol
1138/// has been implemented in IDecl class, its super class or categories (if
1139/// lookupCategory is true).
1140bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1141 bool lookupCategory,
1142 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001143 if (!hasDefinition())
1144 return false;
1145
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001146 ObjCInterfaceDecl *IDecl = this;
1147 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00001148 for (ObjCInterfaceDecl::protocol_iterator
1149 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001150 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1151 return true;
1152 // This is dubious and is added to be compatible with gcc. In gcc, it is
1153 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1154 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1155 // object. This IMO, should be a bug.
1156 // FIXME: Treat this as an extension, and flag this as an error when GCC
1157 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001158 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001159 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1160 return true;
1161 }
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001163 // 2nd, look up the category.
1164 if (lookupCategory)
1165 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
1166 CDecl = CDecl->getNextClassCategory()) {
1167 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
1168 E = CDecl->protocol_end(); PI != E; ++PI)
1169 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1170 return true;
1171 }
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001173 // 3rd, look up the super class(s)
1174 if (IDecl->getSuperClass())
1175 return
1176 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1177 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001179 return false;
1180}
1181
Chris Lattnerab351632009-02-20 20:59:54 +00001182//===----------------------------------------------------------------------===//
1183// ObjCIvarDecl
1184//===----------------------------------------------------------------------===//
1185
David Blaikie99ba9e32011-12-20 02:48:34 +00001186void ObjCIvarDecl::anchor() { }
1187
Daniel Dunbara0654922010-04-02 20:10:03 +00001188ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001189 SourceLocation StartLoc,
1190 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001191 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001192 AccessControl ac, Expr *BW,
1193 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001194 if (DC) {
1195 // Ivar's can only appear in interfaces, implementations (via synthesized
1196 // properties), and class extensions (via direct declaration, or synthesized
1197 // properties).
1198 //
1199 // FIXME: This should really be asserting this:
1200 // (isa<ObjCCategoryDecl>(DC) &&
1201 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1202 // but unfortunately we sometimes place ivars into non-class extension
1203 // categories on error. This breaks an AST invariant, and should not be
1204 // fixed.
1205 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1206 isa<ObjCCategoryDecl>(DC)) &&
1207 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001208 // Once a new ivar is created in any of class/class-extension/implementation
1209 // decl contexts, the previously built IvarList must be rebuilt.
1210 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1211 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001212 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001213 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001214 else
1215 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001216 }
1217 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001218 }
1219
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001220 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1221 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001222}
1223
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001224ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1225 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1226 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1227 QualType(), 0, ObjCIvarDecl::None, 0, false);
1228}
1229
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001230const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1231 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001232
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001233 switch (DC->getKind()) {
1234 default:
1235 case ObjCCategoryImpl:
1236 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001237 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001238
1239 // Ivars can only appear in class extension categories.
1240 case ObjCCategory: {
1241 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1242 assert(CD->IsClassExtension() && "invalid container for ivar!");
1243 return CD->getClassInterface();
1244 }
1245
1246 case ObjCImplementation:
1247 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1248
1249 case ObjCInterface:
1250 return cast<ObjCInterfaceDecl>(DC);
1251 }
1252}
Chris Lattnerab351632009-02-20 20:59:54 +00001253
1254//===----------------------------------------------------------------------===//
1255// ObjCAtDefsFieldDecl
1256//===----------------------------------------------------------------------===//
1257
David Blaikie99ba9e32011-12-20 02:48:34 +00001258void ObjCAtDefsFieldDecl::anchor() { }
1259
Chris Lattnerab351632009-02-20 20:59:54 +00001260ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001261*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1262 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001263 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001264 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001265}
1266
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001267ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1268 unsigned ID) {
1269 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1270 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1271 0, QualType(), 0);
1272}
1273
Chris Lattnerab351632009-02-20 20:59:54 +00001274//===----------------------------------------------------------------------===//
1275// ObjCProtocolDecl
1276//===----------------------------------------------------------------------===//
1277
David Blaikie99ba9e32011-12-20 02:48:34 +00001278void ObjCProtocolDecl::anchor() { }
1279
Douglas Gregor27c6da22012-01-01 20:30:41 +00001280ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1281 SourceLocation nameLoc,
1282 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001283 ObjCProtocolDecl *PrevDecl)
1284 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001285{
1286 setPreviousDeclaration(PrevDecl);
1287 if (PrevDecl)
1288 Data = PrevDecl->Data;
1289}
1290
Chris Lattnerab351632009-02-20 20:59:54 +00001291ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001292 IdentifierInfo *Id,
1293 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001294 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001295 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001296 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001297 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001298
1299 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001300}
1301
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001302ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1303 unsigned ID) {
1304 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1305 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1306 0);
1307}
1308
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001309ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1310 ObjCProtocolDecl *PDecl = this;
1311
1312 if (Name == getIdentifier())
1313 return PDecl;
1314
1315 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1316 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1317 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001319 return NULL;
1320}
1321
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001322// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001323// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001324ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1325 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001326 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001328 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001329 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Chris Lattnerab351632009-02-20 20:59:54 +00001331 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001332 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001333 return MethodDecl;
1334 return NULL;
1335}
1336
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001337void ObjCProtocolDecl::allocateDefinitionData() {
1338 assert(!Data && "Protocol already has a definition!");
Douglas Gregor1d784b22012-01-01 19:51:50 +00001339 Data = new (getASTContext()) DefinitionData;
1340 Data->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001341}
1342
1343void ObjCProtocolDecl::startDefinition() {
1344 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001345
1346 // Update all of the declarations with a pointer to the definition.
1347 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1348 RD != RDEnd; ++RD)
1349 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001350}
1351
Anna Zakse63aedd2012-10-31 01:18:22 +00001352void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM) const {
Anna Zaksb36ea372012-10-18 19:17:53 +00001353 for (ObjCProtocolDecl::prop_iterator P = prop_begin(),
1354 E = prop_end(); P != E; ++P) {
1355 ObjCPropertyDecl *Prop = *P;
1356 // Insert into PM if not there already.
1357 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
1358 }
1359 // Scan through protocol's protocols.
1360 for (ObjCProtocolDecl::protocol_iterator PI = protocol_begin(),
1361 E = protocol_end(); PI != E; ++PI)
1362 (*PI)->collectPropertiesToImplement(PM);
1363}
1364
1365
Chris Lattnerab351632009-02-20 20:59:54 +00001366//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001367// ObjCCategoryDecl
1368//===----------------------------------------------------------------------===//
1369
David Blaikie99ba9e32011-12-20 02:48:34 +00001370void ObjCCategoryDecl::anchor() { }
1371
Chris Lattnerab351632009-02-20 20:59:54 +00001372ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001373 SourceLocation AtLoc,
1374 SourceLocation ClassNameLoc,
1375 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001376 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001377 ObjCInterfaceDecl *IDecl,
1378 SourceLocation IvarLBraceLoc,
1379 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001380 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1381 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001382 IDecl,
1383 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001384 if (IDecl) {
1385 // Link this category into its class's category list.
1386 CatDecl->NextClassCategory = IDecl->getCategoryList();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001387 if (IDecl->hasDefinition()) {
1388 IDecl->setCategoryList(CatDecl);
1389 if (ASTMutationListener *L = C.getASTMutationListener())
1390 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1391 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001392 }
1393
1394 return CatDecl;
1395}
1396
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001397ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1398 unsigned ID) {
1399 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1400 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1401 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001402}
1403
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001404ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1405 return getASTContext().getObjCImplementation(
1406 const_cast<ObjCCategoryDecl*>(this));
1407}
1408
1409void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1410 getASTContext().setObjCImplementation(this, ImplD);
1411}
1412
1413
Chris Lattnerab351632009-02-20 20:59:54 +00001414//===----------------------------------------------------------------------===//
1415// ObjCCategoryImplDecl
1416//===----------------------------------------------------------------------===//
1417
David Blaikie99ba9e32011-12-20 02:48:34 +00001418void ObjCCategoryImplDecl::anchor() { }
1419
Chris Lattnerab351632009-02-20 20:59:54 +00001420ObjCCategoryImplDecl *
1421ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001422 IdentifierInfo *Id,
1423 ObjCInterfaceDecl *ClassInterface,
1424 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001425 SourceLocation atStartLoc,
1426 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001427 if (ClassInterface && ClassInterface->hasDefinition())
1428 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001429 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001430 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001431}
1432
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001433ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1434 unsigned ID) {
1435 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1436 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1437 SourceLocation(), SourceLocation());
1438}
1439
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001440ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001441 // The class interface might be NULL if we are working with invalid code.
1442 if (const ObjCInterfaceDecl *ID = getClassInterface())
1443 return ID->FindCategoryDeclaration(getIdentifier());
1444 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001445}
1446
Chris Lattnerab351632009-02-20 20:59:54 +00001447
David Blaikie99ba9e32011-12-20 02:48:34 +00001448void ObjCImplDecl::anchor() { }
1449
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001450void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001451 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001452 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001453 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001454}
1455
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001456void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1457 ASTContext &Ctx = getASTContext();
1458
1459 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001460 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001461 if (IFace)
1462 Ctx.setObjCImplementation(IFace, ImplD);
1463
Duncan Sands98f2cca2009-07-21 07:56:29 +00001464 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001465 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1466 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1467 Ctx.setObjCImplementation(CD, ImplD);
1468 }
1469
1470 ClassInterface = IFace;
1471}
1472
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001473/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
James Dennett1c3a46a2012-06-15 22:30:14 +00001474/// properties implemented in this category \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001475/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001476///
Chris Lattner3aa18612009-02-28 18:42:10 +00001477ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001478FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1479 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001480 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001481 if (PID->getPropertyIvarDecl() &&
1482 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1483 return PID;
1484 }
1485 return 0;
1486}
1487
1488/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001489/// added to the list of those properties \@synthesized/\@dynamic in this
1490/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001491///
Chris Lattner3aa18612009-02-28 18:42:10 +00001492ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001493FindPropertyImplDecl(IdentifierInfo *Id) const {
1494 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001495 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001496 if (PID->getPropertyDecl()->getIdentifier() == Id)
1497 return PID;
1498 }
1499 return 0;
1500}
1501
Chris Lattner5f9e2722011-07-23 10:55:15 +00001502raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001503 const ObjCCategoryImplDecl &CID) {
1504 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001505 return OS;
1506}
1507
Chris Lattnerab351632009-02-20 20:59:54 +00001508//===----------------------------------------------------------------------===//
1509// ObjCImplementationDecl
1510//===----------------------------------------------------------------------===//
1511
David Blaikie99ba9e32011-12-20 02:48:34 +00001512void ObjCImplementationDecl::anchor() { }
1513
Chris Lattnerab351632009-02-20 20:59:54 +00001514ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001515ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001516 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001517 ObjCInterfaceDecl *SuperDecl,
1518 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001519 SourceLocation atStartLoc,
1520 SourceLocation IvarLBraceLoc,
1521 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001522 if (ClassInterface && ClassInterface->hasDefinition())
1523 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001524 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001525 nameLoc, atStartLoc,
1526 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001527}
1528
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001529ObjCImplementationDecl *
1530ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1531 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1532 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1533 SourceLocation());
1534}
1535
John McCallda6d9762011-07-22 04:15:06 +00001536void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1537 CXXCtorInitializer ** initializers,
1538 unsigned numInitializers) {
1539 if (numInitializers > 0) {
1540 NumIvarInitializers = numInitializers;
1541 CXXCtorInitializer **ivarInitializers =
1542 new (C) CXXCtorInitializer*[NumIvarInitializers];
1543 memcpy(ivarInitializers, initializers,
1544 numInitializers * sizeof(CXXCtorInitializer*));
1545 IvarInitializers = ivarInitializers;
1546 }
1547}
1548
Chris Lattner5f9e2722011-07-23 10:55:15 +00001549raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001550 const ObjCImplementationDecl &ID) {
1551 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001552 return OS;
1553}
1554
Chris Lattnerab351632009-02-20 20:59:54 +00001555//===----------------------------------------------------------------------===//
1556// ObjCCompatibleAliasDecl
1557//===----------------------------------------------------------------------===//
1558
David Blaikie99ba9e32011-12-20 02:48:34 +00001559void ObjCCompatibleAliasDecl::anchor() { }
1560
Chris Lattnerab351632009-02-20 20:59:54 +00001561ObjCCompatibleAliasDecl *
1562ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1563 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001564 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001565 ObjCInterfaceDecl* AliasedClass) {
1566 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1567}
1568
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001569ObjCCompatibleAliasDecl *
1570ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1571 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1572 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1573}
1574
Chris Lattnerab351632009-02-20 20:59:54 +00001575//===----------------------------------------------------------------------===//
1576// ObjCPropertyDecl
1577//===----------------------------------------------------------------------===//
1578
David Blaikie99ba9e32011-12-20 02:48:34 +00001579void ObjCPropertyDecl::anchor() { }
1580
Chris Lattnerab351632009-02-20 20:59:54 +00001581ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1582 SourceLocation L,
1583 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001584 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001585 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001586 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001587 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001588 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001589}
1590
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001591ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1592 unsigned ID) {
1593 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1594 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001595 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001596 0);
1597}
1598
Chris Lattnerab351632009-02-20 20:59:54 +00001599//===----------------------------------------------------------------------===//
1600// ObjCPropertyImplDecl
1601//===----------------------------------------------------------------------===//
1602
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001603ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001604 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001605 SourceLocation atLoc,
1606 SourceLocation L,
1607 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001608 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001609 ObjCIvarDecl *ivar,
1610 SourceLocation ivarLoc) {
1611 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1612 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001613}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001614
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001615ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1616 unsigned ID) {
1617 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1618 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1619 0, Dynamic, 0, SourceLocation());
1620}
1621
Douglas Gregora4ffd852010-11-17 01:03:52 +00001622SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1623 SourceLocation EndLoc = getLocation();
1624 if (IvarLoc.isValid())
1625 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001626
Douglas Gregora4ffd852010-11-17 01:03:52 +00001627 return SourceRange(AtLoc, EndLoc);
1628}