blob: 62b4f5ea82c7ccf0fc49fba8c563679466e3495c [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 {
David Blaikie3bc93e32012-12-19 00:45:41 +000057 lookup_const_result R = lookup(Id);
58 for (lookup_const_iterator Ivar = R.begin(), IvarEnd = R.end();
59 Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
63 return 0;
64}
65
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000066// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000067ObjCMethodDecl *
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
Steve Naroff0de21fd2009-02-22 19:35:57 +000069 // Since instance & class methods can have the same name, the loop below
70 // ensures we get the correct method.
71 //
72 // @interface Whatever
73 // - (int) class_method;
74 // + (float) class_method;
75 // @end
76 //
David Blaikie3bc93e32012-12-19 00:45:41 +000077 lookup_const_result R = lookup(Sel);
78 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
79 Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000080 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000081 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000082 return MD;
83 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000084 return 0;
85}
86
Ted Kremenek9f550ff2010-03-15 20:11:46 +000087ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000088ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000089 IdentifierInfo *propertyID) {
90
David Blaikie3bc93e32012-12-19 00:45:41 +000091 DeclContext::lookup_const_result R = DC->lookup(propertyID);
92 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
93 ++I)
Ted Kremenek9f550ff2010-03-15 20:11:46 +000094 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
95 return PD;
96
97 return 0;
98}
99
Anna Zaksad0ce532012-09-27 19:45:11 +0000100IdentifierInfo *
101ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
102 SmallString<128> ivarName;
103 {
104 llvm::raw_svector_ostream os(ivarName);
105 os << '_' << getIdentifier()->getName();
106 }
107 return &Ctx.Idents.get(ivarName.str());
108}
109
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000110/// FindPropertyDeclaration - Finds declaration of the property given its name
111/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000112ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000113ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000115 if (ObjCPropertyDecl *PD =
116 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
117 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000119 switch (getKind()) {
120 default:
121 break;
122 case Decl::ObjCProtocol: {
123 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
124 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
125 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000126 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
127 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000128 break;
129 }
130 case Decl::ObjCInterface: {
131 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
132 // Look through categories.
133 for (ObjCCategoryDecl *Cat = OID->getCategoryList();
134 Cat; Cat = Cat->getNextClassCategory())
135 if (!Cat->IsClassExtension())
136 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
137 return P;
138
139 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000140 for (ObjCInterfaceDecl::all_protocol_iterator
141 I = OID->all_referenced_protocol_begin(),
142 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000143 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
144 return P;
145
146 // Finally, check the super class.
147 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
148 return superClass->FindPropertyDeclaration(PropertyId);
149 break;
150 }
151 case Decl::ObjCCategory: {
152 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
153 // Look through protocols.
154 if (!OCD->IsClassExtension())
155 for (ObjCCategoryDecl::protocol_iterator
156 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
157 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
158 return P;
159
160 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000161 }
162 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000163 return 0;
164}
165
David Blaikie99ba9e32011-12-20 02:48:34 +0000166void ObjCInterfaceDecl::anchor() { }
167
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000168/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
169/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000170/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000171///
172ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000173ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000174 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000175 // FIXME: Should make sure no callers ever do this.
176 if (!hasDefinition())
177 return 0;
178
179 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000180 LoadExternalDefinition();
181
Ted Kremenek37cafb02010-03-15 20:30:07 +0000182 if (ObjCPropertyDecl *PD =
183 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
184 return PD;
185
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000186 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000187 for (ObjCInterfaceDecl::all_protocol_iterator
188 I = all_referenced_protocol_begin(),
189 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000190 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
191 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000192
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000193 return 0;
194}
195
Anna Zakse63aedd2012-10-31 01:18:22 +0000196void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM) const {
Anna Zaksb36ea372012-10-18 19:17:53 +0000197 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
198 E = prop_end(); P != E; ++P) {
199 ObjCPropertyDecl *Prop = *P;
200 PM[Prop->getIdentifier()] = Prop;
201 }
202 for (ObjCInterfaceDecl::all_protocol_iterator
203 PI = all_referenced_protocol_begin(),
204 E = all_referenced_protocol_end(); PI != E; ++PI)
205 (*PI)->collectPropertiesToImplement(PM);
Anna Zakse63aedd2012-10-31 01:18:22 +0000206 // Note, the properties declared only in class extensions are still copied
207 // into the main @interface's property list, and therefore we don't
208 // explicitly, have to search class extension properties.
Anna Zaksb36ea372012-10-18 19:17:53 +0000209}
210
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000211bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
212 const ObjCInterfaceDecl *Class = this;
213 while (Class) {
214 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
215 return true;
216 Class = Class->getSuperClass();
217 }
218 return false;
219}
220
221const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
222 const ObjCInterfaceDecl *Class = this;
223 while (Class) {
224 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
225 return Class;
226 Class = Class->getSuperClass();
227 }
228 return 0;
229}
230
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000231void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
232 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
233 ASTContext &C)
234{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000235 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000236 LoadExternalDefinition();
237
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000238 if (data().AllReferencedProtocols.empty() &&
239 data().ReferencedProtocols.empty()) {
240 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000241 return;
242 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000243
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000244 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000245 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000246 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000247 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000248 for (unsigned i = 0; i < ExtNum; i++) {
249 bool protocolExists = false;
250 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000251 for (all_protocol_iterator
252 p = all_referenced_protocol_begin(),
253 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000254 ObjCProtocolDecl *Proto = (*p);
255 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
256 protocolExists = true;
257 break;
258 }
259 }
260 // Do we want to warn on a protocol in extension class which
261 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000262 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000263 ProtocolRefs.push_back(ProtoInExtension);
264 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000265
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000266 if (ProtocolRefs.empty())
267 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000268
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000269 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000270 for (all_protocol_iterator p = all_referenced_protocol_begin(),
271 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000272 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000273 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000274
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000275 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000276}
277
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000278void ObjCInterfaceDecl::allocateDefinitionData() {
279 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor26fec632011-12-15 18:17:27 +0000280 Data = new (getASTContext()) DefinitionData();
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000281 Data->Definition = this;
282
283 // Make the type point at the definition, now that we have one.
284 if (TypeForDecl)
285 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000286}
287
288void ObjCInterfaceDecl::startDefinition() {
289 allocateDefinitionData();
290
Douglas Gregor53df7a12011-12-15 18:03:09 +0000291 // Update all of the declarations with a pointer to the definition.
292 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
293 RD != RDEnd; ++RD) {
294 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000295 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000296 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000297}
298
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000299/// getFirstClassExtension - Find first class extension of the given class.
300ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
301 for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000302 CDecl = CDecl->getNextClassCategory())
303 if (CDecl->IsClassExtension())
304 return CDecl;
305 return 0;
306}
307
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000308/// getNextClassCategory - Find next class extension in list of categories.
309const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
310 for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
311 CDecl = CDecl->getNextClassCategory())
312 if (CDecl->IsClassExtension())
313 return CDecl;
314 return 0;
315}
316
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000317ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
318 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000319 // FIXME: Should make sure no callers ever do this.
320 if (!hasDefinition())
321 return 0;
322
323 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000324 LoadExternalDefinition();
325
Chris Lattner1e03a562008-03-16 00:19:01 +0000326 ObjCInterfaceDecl* ClassDecl = this;
327 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000328 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000329 clsDeclared = ClassDecl;
330 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000331 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000332 for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
333 CDecl; CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000334 if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
335 clsDeclared = ClassDecl;
336 return I;
337 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000338 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000339
Chris Lattner1e03a562008-03-16 00:19:01 +0000340 ClassDecl = ClassDecl->getSuperClass();
341 }
342 return NULL;
343}
344
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000345/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
346/// class whose name is passed as argument. If it is not one of the super classes
347/// the it returns NULL.
348ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
349 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000350 // FIXME: Should make sure no callers ever do this.
351 if (!hasDefinition())
352 return 0;
353
354 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000355 LoadExternalDefinition();
356
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000357 ObjCInterfaceDecl* ClassDecl = this;
358 while (ClassDecl != NULL) {
359 if (ClassDecl->getIdentifier() == ICName)
360 return ClassDecl;
361 ClassDecl = ClassDecl->getSuperClass();
362 }
363 return NULL;
364}
365
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000366/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000367/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000368ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
369 bool isInstance,
370 bool shallowCategoryLookup) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000371 // FIXME: Should make sure no callers ever do this.
372 if (!hasDefinition())
373 return 0;
374
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000375 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000376 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000378 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000379 LoadExternalDefinition();
380
Chris Lattner1e03a562008-03-16 00:19:01 +0000381 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000382 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000383 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Chris Lattner1e03a562008-03-16 00:19:01 +0000385 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000386 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
387 E = ClassDecl->protocol_end();
388 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000389 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000390 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000391
392 // Didn't find one yet - now look through categories.
393 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
394 while (CatDecl) {
395 if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
396 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000397
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000398 if (!shallowCategoryLookup) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000399 // Didn't find one yet - look through protocols.
400 const ObjCList<ObjCProtocolDecl> &Protocols =
401 CatDecl->getReferencedProtocols();
402 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
403 E = Protocols.end(); I != E; ++I)
404 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
405 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000406 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000407 CatDecl = CatDecl->getNextClassCategory();
Chris Lattner1e03a562008-03-16 00:19:01 +0000408 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000409
Chris Lattner1e03a562008-03-16 00:19:01 +0000410 ClassDecl = ClassDecl->getSuperClass();
411 }
412 return NULL;
413}
414
Anna Zakse61354b2012-07-27 19:07:44 +0000415// Will search "local" class/category implementations for a method decl.
416// If failed, then we search in class's root for an instance method.
417// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000418ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
419 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000420 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000421 // FIXME: Should make sure no callers ever do this.
422 if (!hasDefinition())
423 return 0;
424
425 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000426 LoadExternalDefinition();
427
Steve Naroffd789d3d2009-10-01 23:46:04 +0000428 ObjCMethodDecl *Method = 0;
429 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000430 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
431 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000432
433 // Look through local category implementations associated with the class.
434 if (!Method)
435 Method = Instance ? getCategoryInstanceMethod(Sel)
436 : getCategoryClassMethod(Sel);
437
438 // Before we give up, check if the selector is an instance method.
439 // But only in the root. This matches gcc's behavior and what the
440 // runtime expects.
441 if (!Instance && !Method && !getSuperClass()) {
442 Method = lookupInstanceMethod(Sel);
443 // Look through local category implementations associated
444 // with the root class.
445 if (!Method)
446 Method = lookupPrivateMethod(Sel, true);
447 }
448
Steve Naroffd789d3d2009-10-01 23:46:04 +0000449 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000450 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000451 return Method;
452}
Chris Lattnerab351632009-02-20 20:59:54 +0000453
454//===----------------------------------------------------------------------===//
455// ObjCMethodDecl
456//===----------------------------------------------------------------------===//
457
458ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000459 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000460 SourceLocation endLoc,
461 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000462 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000463 DeclContext *contextDecl,
464 bool isInstance,
465 bool isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000466 bool isPropertyAccessor,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000467 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000468 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000469 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000470 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000471 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000472 SelInfo, T, ResultTInfo, contextDecl,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000473 isInstance, isVariadic, isPropertyAccessor,
474 isImplicitlyDeclared, isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000475 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000476 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000477}
478
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000479ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
480 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
481 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
482 Selector(), QualType(), 0, 0);
483}
484
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000485Stmt *ObjCMethodDecl::getBody() const {
486 return Body.get(getASTContext().getExternalSource());
487}
488
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000489void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
490 assert(PrevMethod);
491 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
492 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000493 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000494}
495
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000496void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
497 ArrayRef<ParmVarDecl*> Params,
498 ArrayRef<SourceLocation> SelLocs) {
499 ParamsAndSelLocs = 0;
500 NumParams = Params.size();
501 if (Params.empty() && SelLocs.empty())
502 return;
503
504 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
505 sizeof(SourceLocation) * SelLocs.size();
506 ParamsAndSelLocs = C.Allocate(Size);
507 std::copy(Params.begin(), Params.end(), getParams());
508 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
509}
510
511void ObjCMethodDecl::getSelectorLocs(
512 SmallVectorImpl<SourceLocation> &SelLocs) const {
513 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
514 SelLocs.push_back(getSelectorLoc(i));
515}
516
517void ObjCMethodDecl::setMethodParams(ASTContext &C,
518 ArrayRef<ParmVarDecl*> Params,
519 ArrayRef<SourceLocation> SelLocs) {
520 assert((!SelLocs.empty() || isImplicit()) &&
521 "No selector locs for non-implicit method");
522 if (isImplicit())
523 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
524
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000525 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
526 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000527 if (SelLocsKind != SelLoc_NonStandard)
528 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
529
530 setParamsAndSelLocs(C, Params, SelLocs);
531}
532
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000533/// \brief A definition will return its interface declaration.
534/// An interface declaration will return its definition.
535/// Otherwise it will return itself.
536ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
537 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000538 ObjCMethodDecl *Redecl = 0;
539 if (HasRedeclaration)
540 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000541 if (Redecl)
542 return Redecl;
543
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000544 Decl *CtxD = cast<Decl>(getDeclContext());
545
546 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
547 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
548 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
549
550 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
551 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
552 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
553
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000554 } else if (ObjCImplementationDecl *ImplD =
555 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000556 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
557 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000558
559 } else if (ObjCCategoryImplDecl *CImplD =
560 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000561 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000562 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000563 }
564
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000565 if (!Redecl && isRedeclaration()) {
566 // This is the last redeclaration, go back to the first method.
567 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
568 isInstanceMethod());
569 }
570
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000571 return Redecl ? Redecl : this;
572}
573
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000574ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
575 Decl *CtxD = cast<Decl>(getDeclContext());
576
577 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
578 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
579 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
580 isInstanceMethod()))
581 return MD;
582
583 } else if (ObjCCategoryImplDecl *CImplD =
584 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000585 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000586 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
587 isInstanceMethod()))
588 return MD;
589 }
590
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000591 if (isRedeclaration())
592 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
593 isInstanceMethod());
594
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000595 return this;
596}
597
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000598SourceLocation ObjCMethodDecl::getLocEnd() const {
599 if (Stmt *Body = getBody())
600 return Body->getLocEnd();
601 return DeclEndLoc;
602}
603
John McCall85f3d762011-03-02 01:50:55 +0000604ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
605 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000606 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000607 return family;
608
John McCalld5313b02011-03-02 11:33:24 +0000609 // Check for an explicit attribute.
610 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
611 // The unfortunate necessity of mapping between enums here is due
612 // to the attributes framework.
613 switch (attr->getFamily()) {
614 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
615 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
616 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
617 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
618 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
619 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
620 }
621 Family = static_cast<unsigned>(family);
622 return family;
623 }
624
John McCall85f3d762011-03-02 01:50:55 +0000625 family = getSelector().getMethodFamily();
626 switch (family) {
627 case OMF_None: break;
628
629 // init only has a conventional meaning for an instance method, and
630 // it has to return an object.
631 case OMF_init:
632 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
633 family = OMF_None;
634 break;
635
636 // alloc/copy/new have a conventional meaning for both class and
637 // instance methods, but they require an object return.
638 case OMF_alloc:
639 case OMF_copy:
640 case OMF_mutableCopy:
641 case OMF_new:
642 if (!getResultType()->isObjCObjectPointerType())
643 family = OMF_None;
644 break;
645
646 // These selectors have a conventional meaning only for instance methods.
647 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000648 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000649 case OMF_retain:
650 case OMF_release:
651 case OMF_autorelease:
652 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000653 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000654 if (!isInstanceMethod())
655 family = OMF_None;
656 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000657
658 case OMF_performSelector:
659 if (!isInstanceMethod() ||
660 !getResultType()->isObjCIdType())
661 family = OMF_None;
662 else {
663 unsigned noParams = param_size();
664 if (noParams < 1 || noParams > 3)
665 family = OMF_None;
666 else {
667 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
668 QualType ArgT = (*it);
669 if (!ArgT->isObjCSelType()) {
670 family = OMF_None;
671 break;
672 }
673 while (--noParams) {
674 it++;
675 ArgT = (*it);
676 if (!ArgT->isObjCIdType()) {
677 family = OMF_None;
678 break;
679 }
680 }
681 }
682 }
683 break;
684
John McCall85f3d762011-03-02 01:50:55 +0000685 }
686
687 // Cache the result.
688 Family = static_cast<unsigned>(family);
689 return family;
690}
691
Mike Stump1eb44332009-09-09 15:08:12 +0000692void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000693 const ObjCInterfaceDecl *OID) {
694 QualType selfTy;
695 if (isInstanceMethod()) {
696 // There may be no interface context due to error in declaration
697 // of the interface (which has been reported). Recover gracefully.
698 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000699 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000700 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000701 } else {
702 selfTy = Context.getObjCIdType();
703 }
704 } else // we have a factory method.
705 selfTy = Context.getObjCClassType();
706
John McCall7acddac2011-06-17 06:42:21 +0000707 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000708 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000709
David Blaikie4e4d0842012-03-11 07:00:24 +0000710 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000711 if (isInstanceMethod()) {
712 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000713
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000714 // 'self' is always __strong. It's actually pseudo-strong except
715 // in init methods (or methods labeled ns_consumes_self), though.
716 Qualifiers qs;
717 qs.setObjCLifetime(Qualifiers::OCL_Strong);
718 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000719
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000720 // In addition, 'self' is const unless this is an init method.
721 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
722 selfTy = selfTy.withConst();
723 selfIsPseudoStrong = true;
724 }
725 }
726 else {
727 assert(isClassMethod());
728 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000729 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000730 selfIsPseudoStrong = true;
731 }
John McCallf85e1932011-06-15 23:02:42 +0000732 }
733
734 ImplicitParamDecl *self
735 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
736 &Context.Idents.get("self"), selfTy);
737 setSelfDecl(self);
738
739 if (selfIsConsumed)
740 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000741
John McCall7acddac2011-06-17 06:42:21 +0000742 if (selfIsPseudoStrong)
743 self->setARCPseudoStrong(true);
744
Mike Stump1eb44332009-09-09 15:08:12 +0000745 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
746 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000747 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000748}
749
Chris Lattnerab351632009-02-20 20:59:54 +0000750ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
751 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
752 return ID;
753 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
754 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000755 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000756 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000757
758 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000759 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000760}
761
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000762static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
763 const ObjCMethodDecl *Method,
764 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
765 bool MovedToSuper) {
766 if (!Container)
767 return;
768
769 // In categories look for overriden methods from protocols. A method from
770 // category is not "overriden" since it is considered as the "same" method
771 // (same USR) as the one from the interface.
772 if (const ObjCCategoryDecl *
773 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
774 // Check whether we have a matching method at this category but only if we
775 // are at the super class level.
776 if (MovedToSuper)
777 if (ObjCMethodDecl *
778 Overridden = Container->getMethod(Method->getSelector(),
779 Method->isInstanceMethod()))
780 if (Method != Overridden) {
781 // We found an override at this category; there is no need to look
782 // into its protocols.
783 Methods.push_back(Overridden);
784 return;
785 }
786
787 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
788 PEnd = Category->protocol_end();
789 P != PEnd; ++P)
790 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
791 return;
792 }
793
794 // Check whether we have a matching method at this level.
795 if (const ObjCMethodDecl *
796 Overridden = Container->getMethod(Method->getSelector(),
797 Method->isInstanceMethod()))
798 if (Method != Overridden) {
799 // We found an override at this level; there is no need to look
800 // into other protocols or categories.
801 Methods.push_back(Overridden);
802 return;
803 }
804
805 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
806 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
807 PEnd = Protocol->protocol_end();
808 P != PEnd; ++P)
809 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
810 }
811
812 if (const ObjCInterfaceDecl *
813 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
814 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
815 PEnd = Interface->protocol_end();
816 P != PEnd; ++P)
817 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
818
819 for (const ObjCCategoryDecl *Category = Interface->getCategoryList();
820 Category; Category = Category->getNextClassCategory())
821 CollectOverriddenMethodsRecurse(Category, Method, Methods,
822 MovedToSuper);
823
824 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
825 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
826 /*MovedToSuper=*/true);
827 }
828}
829
830static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
831 const ObjCMethodDecl *Method,
832 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
833 CollectOverriddenMethodsRecurse(Container, Method, Methods,
834 /*MovedToSuper=*/false);
835}
836
837static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
838 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
839 assert(Method->isOverriding());
840
841 if (const ObjCProtocolDecl *
842 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
843 CollectOverriddenMethods(ProtD, Method, overridden);
844
845 } else if (const ObjCImplDecl *
846 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
847 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
848 if (!ID)
849 return;
850 // Start searching for overridden methods using the method from the
851 // interface as starting point.
852 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
853 Method->isInstanceMethod()))
854 Method = IFaceMeth;
855 CollectOverriddenMethods(ID, Method, overridden);
856
857 } else if (const ObjCCategoryDecl *
858 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
859 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
860 if (!ID)
861 return;
862 // Start searching for overridden methods using the method from the
863 // interface as starting point.
864 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
865 Method->isInstanceMethod()))
866 Method = IFaceMeth;
867 CollectOverriddenMethods(ID, Method, overridden);
868
869 } else {
870 CollectOverriddenMethods(
871 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
872 Method, overridden);
873 }
874}
875
876static void collectOnCategoriesAfterLocation(SourceLocation Loc,
877 const ObjCInterfaceDecl *Class,
878 SourceManager &SM,
879 const ObjCMethodDecl *Method,
880 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
881 if (!Class)
882 return;
883
884 for (const ObjCCategoryDecl *Category = Class->getCategoryList();
885 Category; Category = Category->getNextClassCategory())
886 if (SM.isBeforeInTranslationUnit(Loc, Category->getLocation()))
887 CollectOverriddenMethodsRecurse(Category, Method, Methods, true);
888
889 collectOnCategoriesAfterLocation(Loc, Class->getSuperClass(), SM,
890 Method, Methods);
891}
892
893/// \brief Faster collection that is enabled when ObjCMethodDecl::isOverriding()
894/// returns false.
895/// You'd think that in that case there are no overrides but categories can
896/// "introduce" new overridden methods that are missed by Sema because the
897/// overrides lookup that it does for methods, inside implementations, will
898/// stop at the interface level (if there is a method there) and not look
899/// further in super classes.
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000900/// Methods in an implementation can overide methods in super class's category
901/// but not in current class's category. But, such methods
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000902static void collectOverriddenMethodsFast(SourceManager &SM,
903 const ObjCMethodDecl *Method,
904 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
905 assert(!Method->isOverriding());
906
907 const ObjCContainerDecl *
908 ContD = cast<ObjCContainerDecl>(Method->getDeclContext());
909 if (isa<ObjCInterfaceDecl>(ContD) || isa<ObjCProtocolDecl>(ContD))
910 return;
911 const ObjCInterfaceDecl *Class = Method->getClassInterface();
912 if (!Class)
913 return;
914
915 collectOnCategoriesAfterLocation(Class->getLocation(), Class->getSuperClass(),
916 SM, Method, Methods);
917}
918
919void ObjCMethodDecl::getOverriddenMethods(
920 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
921 const ObjCMethodDecl *Method = this;
922
923 if (Method->isRedeclaration()) {
924 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
925 getMethod(Method->getSelector(), Method->isInstanceMethod());
926 }
927
928 if (!Method->isOverriding()) {
929 collectOverriddenMethodsFast(getASTContext().getSourceManager(),
930 Method, Overridden);
931 } else {
932 collectOverriddenMethodsSlow(Method, Overridden);
933 assert(!Overridden.empty() &&
934 "ObjCMethodDecl's overriding bit is not as expected");
935 }
936}
937
Jordan Rose04bec392012-10-10 16:42:54 +0000938const ObjCPropertyDecl *
939ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
940 Selector Sel = getSelector();
941 unsigned NumArgs = Sel.getNumArgs();
942 if (NumArgs > 1)
943 return 0;
944
Jordan Rose50d2b262012-10-11 16:02:02 +0000945 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose04bec392012-10-10 16:42:54 +0000946 return 0;
947
948 if (isPropertyAccessor()) {
949 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
950 bool IsGetter = (NumArgs == 0);
951
952 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
953 E = Container->prop_end();
954 I != E; ++I) {
955 Selector NextSel = IsGetter ? (*I)->getGetterName()
956 : (*I)->getSetterName();
957 if (NextSel == Sel)
958 return *I;
959 }
960
961 llvm_unreachable("Marked as a property accessor but no property found!");
962 }
963
964 if (!CheckOverrides)
965 return 0;
966
967 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
968 OverridesTy Overrides;
969 getOverriddenMethods(Overrides);
970 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
971 I != E; ++I) {
972 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
973 return Prop;
974 }
975
976 return 0;
977
978}
979
Chris Lattnerab351632009-02-20 20:59:54 +0000980//===----------------------------------------------------------------------===//
981// ObjCInterfaceDecl
982//===----------------------------------------------------------------------===//
983
Douglas Gregora6ea10e2012-01-17 18:09:05 +0000984ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +0000985 DeclContext *DC,
986 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000987 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +0000988 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +0000989 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000990 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +0000991 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000992 PrevDecl, isInternal);
Douglas Gregor0af55012011-12-16 03:12:41 +0000993 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +0000994 return Result;
995}
996
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000997ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
998 unsigned ID) {
999 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
1000 return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
1001 0, false);
Chris Lattnerab351632009-02-20 20:59:54 +00001002}
1003
1004ObjCInterfaceDecl::
1005ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001006 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1007 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001008 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +00001009 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001010{
Douglas Gregorfd002a72011-12-16 22:37:11 +00001011 setPreviousDeclaration(PrevDecl);
1012
1013 // Copy the 'data' pointer over.
1014 if (PrevDecl)
1015 Data = PrevDecl->Data;
1016
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +00001017 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +00001018}
1019
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001020void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001021 assert(data().ExternallyCompleted && "Class is not externally completed");
1022 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001023 getASTContext().getExternalSource()->CompleteType(
1024 const_cast<ObjCInterfaceDecl *>(this));
1025}
1026
1027void ObjCInterfaceDecl::setExternallyCompleted() {
1028 assert(getASTContext().getExternalSource() &&
1029 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001030 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001031 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001032 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001033}
1034
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001035ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001036 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1037 if (data().ExternallyCompleted)
1038 LoadExternalDefinition();
1039
1040 return getASTContext().getObjCImplementation(
1041 const_cast<ObjCInterfaceDecl*>(Def));
1042 }
1043
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001044 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +00001045 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001046}
1047
1048void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001049 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001050}
1051
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001052/// all_declared_ivar_begin - return first ivar declared in this class,
1053/// its extensions and its implementation. Lazily build the list on first
1054/// access.
1055ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001056 // FIXME: Should make sure no callers ever do this.
1057 if (!hasDefinition())
1058 return 0;
1059
1060 if (data().IvarList)
1061 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001062
1063 ObjCIvarDecl *curIvar = 0;
1064 if (!ivar_empty()) {
1065 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
David Blaikie581deb32012-06-06 20:45:41 +00001066 data().IvarList = *I; ++I;
1067 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1068 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001069 }
1070
1071 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
1072 CDecl = CDecl->getNextClassExtension()) {
1073 if (!CDecl->ivar_empty()) {
1074 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
1075 E = CDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001076 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001077 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001078 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001079 }
David Blaikie581deb32012-06-06 20:45:41 +00001080 for ( ;I != E; curIvar = *I, ++I)
1081 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001082 }
1083 }
1084
1085 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1086 if (!ImplDecl->ivar_empty()) {
1087 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1088 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001089 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001090 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001091 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001092 }
David Blaikie581deb32012-06-06 20:45:41 +00001093 for ( ;I != E; curIvar = *I, ++I)
1094 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001095 }
1096 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001097 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001098}
Chris Lattnerab351632009-02-20 20:59:54 +00001099
1100/// FindCategoryDeclaration - Finds category declaration in the list of
1101/// categories for this class and returns it. Name of the category is passed
1102/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001103///
Chris Lattnerab351632009-02-20 20:59:54 +00001104ObjCCategoryDecl *
1105ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001106 // FIXME: Should make sure no callers ever do this.
1107 if (!hasDefinition())
1108 return 0;
1109
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001110 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001111 LoadExternalDefinition();
1112
Chris Lattnerab351632009-02-20 20:59:54 +00001113 for (ObjCCategoryDecl *Category = getCategoryList();
1114 Category; Category = Category->getNextClassCategory())
1115 if (Category->getIdentifier() == CategoryId)
1116 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001117 return 0;
1118}
1119
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001120ObjCMethodDecl *
1121ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1122 for (ObjCCategoryDecl *Category = getCategoryList();
1123 Category; Category = Category->getNextClassCategory())
1124 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1125 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1126 return MD;
1127 return 0;
1128}
1129
1130ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1131 for (ObjCCategoryDecl *Category = getCategoryList();
1132 Category; Category = Category->getNextClassCategory())
1133 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1134 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1135 return MD;
1136 return 0;
1137}
1138
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001139/// ClassImplementsProtocol - Checks that 'lProto' protocol
1140/// has been implemented in IDecl class, its super class or categories (if
1141/// lookupCategory is true).
1142bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1143 bool lookupCategory,
1144 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001145 if (!hasDefinition())
1146 return false;
1147
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001148 ObjCInterfaceDecl *IDecl = this;
1149 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00001150 for (ObjCInterfaceDecl::protocol_iterator
1151 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001152 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1153 return true;
1154 // This is dubious and is added to be compatible with gcc. In gcc, it is
1155 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1156 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1157 // object. This IMO, should be a bug.
1158 // FIXME: Treat this as an extension, and flag this as an error when GCC
1159 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001160 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001161 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1162 return true;
1163 }
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001165 // 2nd, look up the category.
1166 if (lookupCategory)
1167 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
1168 CDecl = CDecl->getNextClassCategory()) {
1169 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
1170 E = CDecl->protocol_end(); PI != E; ++PI)
1171 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1172 return true;
1173 }
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001175 // 3rd, look up the super class(s)
1176 if (IDecl->getSuperClass())
1177 return
1178 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1179 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001181 return false;
1182}
1183
Chris Lattnerab351632009-02-20 20:59:54 +00001184//===----------------------------------------------------------------------===//
1185// ObjCIvarDecl
1186//===----------------------------------------------------------------------===//
1187
David Blaikie99ba9e32011-12-20 02:48:34 +00001188void ObjCIvarDecl::anchor() { }
1189
Daniel Dunbara0654922010-04-02 20:10:03 +00001190ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001191 SourceLocation StartLoc,
1192 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001193 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001194 AccessControl ac, Expr *BW,
1195 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001196 if (DC) {
1197 // Ivar's can only appear in interfaces, implementations (via synthesized
1198 // properties), and class extensions (via direct declaration, or synthesized
1199 // properties).
1200 //
1201 // FIXME: This should really be asserting this:
1202 // (isa<ObjCCategoryDecl>(DC) &&
1203 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1204 // but unfortunately we sometimes place ivars into non-class extension
1205 // categories on error. This breaks an AST invariant, and should not be
1206 // fixed.
1207 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1208 isa<ObjCCategoryDecl>(DC)) &&
1209 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001210 // Once a new ivar is created in any of class/class-extension/implementation
1211 // decl contexts, the previously built IvarList must be rebuilt.
1212 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1213 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001214 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001215 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001216 else
1217 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001218 }
1219 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001220 }
1221
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001222 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1223 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001224}
1225
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001226ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1227 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1228 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1229 QualType(), 0, ObjCIvarDecl::None, 0, false);
1230}
1231
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001232const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1233 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001234
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001235 switch (DC->getKind()) {
1236 default:
1237 case ObjCCategoryImpl:
1238 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001239 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001240
1241 // Ivars can only appear in class extension categories.
1242 case ObjCCategory: {
1243 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1244 assert(CD->IsClassExtension() && "invalid container for ivar!");
1245 return CD->getClassInterface();
1246 }
1247
1248 case ObjCImplementation:
1249 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1250
1251 case ObjCInterface:
1252 return cast<ObjCInterfaceDecl>(DC);
1253 }
1254}
Chris Lattnerab351632009-02-20 20:59:54 +00001255
1256//===----------------------------------------------------------------------===//
1257// ObjCAtDefsFieldDecl
1258//===----------------------------------------------------------------------===//
1259
David Blaikie99ba9e32011-12-20 02:48:34 +00001260void ObjCAtDefsFieldDecl::anchor() { }
1261
Chris Lattnerab351632009-02-20 20:59:54 +00001262ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001263*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1264 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001265 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001266 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001267}
1268
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001269ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1270 unsigned ID) {
1271 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1272 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1273 0, QualType(), 0);
1274}
1275
Chris Lattnerab351632009-02-20 20:59:54 +00001276//===----------------------------------------------------------------------===//
1277// ObjCProtocolDecl
1278//===----------------------------------------------------------------------===//
1279
David Blaikie99ba9e32011-12-20 02:48:34 +00001280void ObjCProtocolDecl::anchor() { }
1281
Douglas Gregor27c6da22012-01-01 20:30:41 +00001282ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1283 SourceLocation nameLoc,
1284 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001285 ObjCProtocolDecl *PrevDecl)
1286 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001287{
1288 setPreviousDeclaration(PrevDecl);
1289 if (PrevDecl)
1290 Data = PrevDecl->Data;
1291}
1292
Chris Lattnerab351632009-02-20 20:59:54 +00001293ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001294 IdentifierInfo *Id,
1295 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001296 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001297 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001298 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001299 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001300
1301 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001302}
1303
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001304ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1305 unsigned ID) {
1306 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1307 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1308 0);
1309}
1310
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001311ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1312 ObjCProtocolDecl *PDecl = this;
1313
1314 if (Name == getIdentifier())
1315 return PDecl;
1316
1317 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1318 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1319 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001321 return NULL;
1322}
1323
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001324// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001325// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001326ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1327 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001328 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001330 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001331 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Chris Lattnerab351632009-02-20 20:59:54 +00001333 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001334 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001335 return MethodDecl;
1336 return NULL;
1337}
1338
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001339void ObjCProtocolDecl::allocateDefinitionData() {
1340 assert(!Data && "Protocol already has a definition!");
Douglas Gregor1d784b22012-01-01 19:51:50 +00001341 Data = new (getASTContext()) DefinitionData;
1342 Data->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001343}
1344
1345void ObjCProtocolDecl::startDefinition() {
1346 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001347
1348 // Update all of the declarations with a pointer to the definition.
1349 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1350 RD != RDEnd; ++RD)
1351 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001352}
1353
Anna Zakse63aedd2012-10-31 01:18:22 +00001354void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM) const {
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001355 const ObjCProtocolDecl *PDecl = this;
1356 if (!isThisDeclarationADefinition() && getDefinition())
1357 PDecl = getDefinition();
1358
1359 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1360 E = PDecl->prop_end(); P != E; ++P) {
Anna Zaksb36ea372012-10-18 19:17:53 +00001361 ObjCPropertyDecl *Prop = *P;
1362 // Insert into PM if not there already.
1363 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
1364 }
1365 // Scan through protocol's protocols.
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001366 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1367 E = PDecl->protocol_end(); PI != E; ++PI)
Anna Zaksb36ea372012-10-18 19:17:53 +00001368 (*PI)->collectPropertiesToImplement(PM);
1369}
1370
1371
Chris Lattnerab351632009-02-20 20:59:54 +00001372//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001373// ObjCCategoryDecl
1374//===----------------------------------------------------------------------===//
1375
David Blaikie99ba9e32011-12-20 02:48:34 +00001376void ObjCCategoryDecl::anchor() { }
1377
Chris Lattnerab351632009-02-20 20:59:54 +00001378ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001379 SourceLocation AtLoc,
1380 SourceLocation ClassNameLoc,
1381 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001382 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001383 ObjCInterfaceDecl *IDecl,
1384 SourceLocation IvarLBraceLoc,
1385 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001386 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1387 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001388 IDecl,
1389 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001390 if (IDecl) {
1391 // Link this category into its class's category list.
1392 CatDecl->NextClassCategory = IDecl->getCategoryList();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001393 if (IDecl->hasDefinition()) {
1394 IDecl->setCategoryList(CatDecl);
1395 if (ASTMutationListener *L = C.getASTMutationListener())
1396 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1397 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001398 }
1399
1400 return CatDecl;
1401}
1402
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001403ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1404 unsigned ID) {
1405 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1406 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1407 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001408}
1409
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001410ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1411 return getASTContext().getObjCImplementation(
1412 const_cast<ObjCCategoryDecl*>(this));
1413}
1414
1415void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1416 getASTContext().setObjCImplementation(this, ImplD);
1417}
1418
1419
Chris Lattnerab351632009-02-20 20:59:54 +00001420//===----------------------------------------------------------------------===//
1421// ObjCCategoryImplDecl
1422//===----------------------------------------------------------------------===//
1423
David Blaikie99ba9e32011-12-20 02:48:34 +00001424void ObjCCategoryImplDecl::anchor() { }
1425
Chris Lattnerab351632009-02-20 20:59:54 +00001426ObjCCategoryImplDecl *
1427ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001428 IdentifierInfo *Id,
1429 ObjCInterfaceDecl *ClassInterface,
1430 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001431 SourceLocation atStartLoc,
1432 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001433 if (ClassInterface && ClassInterface->hasDefinition())
1434 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001435 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001436 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001437}
1438
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001439ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1440 unsigned ID) {
1441 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1442 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1443 SourceLocation(), SourceLocation());
1444}
1445
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001446ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001447 // The class interface might be NULL if we are working with invalid code.
1448 if (const ObjCInterfaceDecl *ID = getClassInterface())
1449 return ID->FindCategoryDeclaration(getIdentifier());
1450 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001451}
1452
Chris Lattnerab351632009-02-20 20:59:54 +00001453
David Blaikie99ba9e32011-12-20 02:48:34 +00001454void ObjCImplDecl::anchor() { }
1455
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001456void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001457 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001458 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001459 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001460}
1461
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001462void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1463 ASTContext &Ctx = getASTContext();
1464
1465 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001466 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001467 if (IFace)
1468 Ctx.setObjCImplementation(IFace, ImplD);
1469
Duncan Sands98f2cca2009-07-21 07:56:29 +00001470 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001471 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1472 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1473 Ctx.setObjCImplementation(CD, ImplD);
1474 }
1475
1476 ClassInterface = IFace;
1477}
1478
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001479/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
James Dennett1c3a46a2012-06-15 22:30:14 +00001480/// properties implemented in this category \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001481/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001482///
Chris Lattner3aa18612009-02-28 18:42:10 +00001483ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001484FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1485 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001486 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001487 if (PID->getPropertyIvarDecl() &&
1488 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1489 return PID;
1490 }
1491 return 0;
1492}
1493
1494/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001495/// added to the list of those properties \@synthesized/\@dynamic in this
1496/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001497///
Chris Lattner3aa18612009-02-28 18:42:10 +00001498ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001499FindPropertyImplDecl(IdentifierInfo *Id) const {
1500 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001501 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001502 if (PID->getPropertyDecl()->getIdentifier() == Id)
1503 return PID;
1504 }
1505 return 0;
1506}
1507
Chris Lattner5f9e2722011-07-23 10:55:15 +00001508raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001509 const ObjCCategoryImplDecl &CID) {
1510 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001511 return OS;
1512}
1513
Chris Lattnerab351632009-02-20 20:59:54 +00001514//===----------------------------------------------------------------------===//
1515// ObjCImplementationDecl
1516//===----------------------------------------------------------------------===//
1517
David Blaikie99ba9e32011-12-20 02:48:34 +00001518void ObjCImplementationDecl::anchor() { }
1519
Chris Lattnerab351632009-02-20 20:59:54 +00001520ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001521ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001522 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001523 ObjCInterfaceDecl *SuperDecl,
1524 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001525 SourceLocation atStartLoc,
1526 SourceLocation IvarLBraceLoc,
1527 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001528 if (ClassInterface && ClassInterface->hasDefinition())
1529 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001530 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001531 nameLoc, atStartLoc,
1532 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001533}
1534
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001535ObjCImplementationDecl *
1536ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1537 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1538 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1539 SourceLocation());
1540}
1541
John McCallda6d9762011-07-22 04:15:06 +00001542void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1543 CXXCtorInitializer ** initializers,
1544 unsigned numInitializers) {
1545 if (numInitializers > 0) {
1546 NumIvarInitializers = numInitializers;
1547 CXXCtorInitializer **ivarInitializers =
1548 new (C) CXXCtorInitializer*[NumIvarInitializers];
1549 memcpy(ivarInitializers, initializers,
1550 numInitializers * sizeof(CXXCtorInitializer*));
1551 IvarInitializers = ivarInitializers;
1552 }
1553}
1554
Chris Lattner5f9e2722011-07-23 10:55:15 +00001555raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001556 const ObjCImplementationDecl &ID) {
1557 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001558 return OS;
1559}
1560
Chris Lattnerab351632009-02-20 20:59:54 +00001561//===----------------------------------------------------------------------===//
1562// ObjCCompatibleAliasDecl
1563//===----------------------------------------------------------------------===//
1564
David Blaikie99ba9e32011-12-20 02:48:34 +00001565void ObjCCompatibleAliasDecl::anchor() { }
1566
Chris Lattnerab351632009-02-20 20:59:54 +00001567ObjCCompatibleAliasDecl *
1568ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1569 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001570 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001571 ObjCInterfaceDecl* AliasedClass) {
1572 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1573}
1574
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001575ObjCCompatibleAliasDecl *
1576ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1577 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1578 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1579}
1580
Chris Lattnerab351632009-02-20 20:59:54 +00001581//===----------------------------------------------------------------------===//
1582// ObjCPropertyDecl
1583//===----------------------------------------------------------------------===//
1584
David Blaikie99ba9e32011-12-20 02:48:34 +00001585void ObjCPropertyDecl::anchor() { }
1586
Chris Lattnerab351632009-02-20 20:59:54 +00001587ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1588 SourceLocation L,
1589 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001590 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001591 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001592 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001593 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001594 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001595}
1596
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001597ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1598 unsigned ID) {
1599 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1600 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001601 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001602 0);
1603}
1604
Chris Lattnerab351632009-02-20 20:59:54 +00001605//===----------------------------------------------------------------------===//
1606// ObjCPropertyImplDecl
1607//===----------------------------------------------------------------------===//
1608
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001609ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001610 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001611 SourceLocation atLoc,
1612 SourceLocation L,
1613 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001614 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001615 ObjCIvarDecl *ivar,
1616 SourceLocation ivarLoc) {
1617 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1618 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001619}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001620
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001621ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1622 unsigned ID) {
1623 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1624 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1625 0, Dynamic, 0, SourceLocation());
1626}
1627
Douglas Gregora4ffd852010-11-17 01:03:52 +00001628SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1629 SourceLocation EndLoc = getLocation();
1630 if (IvarLoc.isValid())
1631 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001632
Douglas Gregora4ffd852010-11-17 01:03:52 +00001633 return SourceRange(AtLoc, EndLoc);
1634}