blob: 816c43200a89d9a0263271227b11d361b3b3011b [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());
Fariborz Jahanianc328d9c2013-01-12 00:28:34 +0000950 // If container is class extension, find its primary class.
951 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
952 if (CatDecl->IsClassExtension())
953 Container = CatDecl->getClassInterface();
954
Jordan Rose04bec392012-10-10 16:42:54 +0000955 bool IsGetter = (NumArgs == 0);
956
957 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
958 E = Container->prop_end();
959 I != E; ++I) {
960 Selector NextSel = IsGetter ? (*I)->getGetterName()
961 : (*I)->getSetterName();
962 if (NextSel == Sel)
963 return *I;
964 }
965
966 llvm_unreachable("Marked as a property accessor but no property found!");
967 }
968
969 if (!CheckOverrides)
970 return 0;
971
972 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
973 OverridesTy Overrides;
974 getOverriddenMethods(Overrides);
975 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
976 I != E; ++I) {
977 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
978 return Prop;
979 }
980
981 return 0;
982
983}
984
Chris Lattnerab351632009-02-20 20:59:54 +0000985//===----------------------------------------------------------------------===//
986// ObjCInterfaceDecl
987//===----------------------------------------------------------------------===//
988
Douglas Gregora6ea10e2012-01-17 18:09:05 +0000989ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +0000990 DeclContext *DC,
991 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000992 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +0000993 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +0000994 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +0000995 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +0000996 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +0000997 PrevDecl, isInternal);
Douglas Gregor0af55012011-12-16 03:12:41 +0000998 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +0000999 return Result;
1000}
1001
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001002ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
1003 unsigned ID) {
1004 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
1005 return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
1006 0, false);
Chris Lattnerab351632009-02-20 20:59:54 +00001007}
1008
1009ObjCInterfaceDecl::
1010ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001011 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1012 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001013 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +00001014 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001015{
Douglas Gregorfd002a72011-12-16 22:37:11 +00001016 setPreviousDeclaration(PrevDecl);
1017
1018 // Copy the 'data' pointer over.
1019 if (PrevDecl)
1020 Data = PrevDecl->Data;
1021
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +00001022 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +00001023}
1024
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001025void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001026 assert(data().ExternallyCompleted && "Class is not externally completed");
1027 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001028 getASTContext().getExternalSource()->CompleteType(
1029 const_cast<ObjCInterfaceDecl *>(this));
1030}
1031
1032void ObjCInterfaceDecl::setExternallyCompleted() {
1033 assert(getASTContext().getExternalSource() &&
1034 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001035 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001036 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001037 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001038}
1039
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001040ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001041 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1042 if (data().ExternallyCompleted)
1043 LoadExternalDefinition();
1044
1045 return getASTContext().getObjCImplementation(
1046 const_cast<ObjCInterfaceDecl*>(Def));
1047 }
1048
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001049 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +00001050 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001051}
1052
1053void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001054 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001055}
1056
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001057/// all_declared_ivar_begin - return first ivar declared in this class,
1058/// its extensions and its implementation. Lazily build the list on first
1059/// access.
1060ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001061 // FIXME: Should make sure no callers ever do this.
1062 if (!hasDefinition())
1063 return 0;
1064
1065 if (data().IvarList)
1066 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001067
1068 ObjCIvarDecl *curIvar = 0;
1069 if (!ivar_empty()) {
1070 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
David Blaikie581deb32012-06-06 20:45:41 +00001071 data().IvarList = *I; ++I;
1072 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1073 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001074 }
1075
1076 for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
1077 CDecl = CDecl->getNextClassExtension()) {
1078 if (!CDecl->ivar_empty()) {
1079 ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
1080 E = CDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001081 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001082 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001083 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001084 }
David Blaikie581deb32012-06-06 20:45:41 +00001085 for ( ;I != E; curIvar = *I, ++I)
1086 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001087 }
1088 }
1089
1090 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1091 if (!ImplDecl->ivar_empty()) {
1092 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1093 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001094 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001095 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001096 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001097 }
David Blaikie581deb32012-06-06 20:45:41 +00001098 for ( ;I != E; curIvar = *I, ++I)
1099 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001100 }
1101 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001102 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001103}
Chris Lattnerab351632009-02-20 20:59:54 +00001104
1105/// FindCategoryDeclaration - Finds category declaration in the list of
1106/// categories for this class and returns it. Name of the category is passed
1107/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001108///
Chris Lattnerab351632009-02-20 20:59:54 +00001109ObjCCategoryDecl *
1110ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001111 // FIXME: Should make sure no callers ever do this.
1112 if (!hasDefinition())
1113 return 0;
1114
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001115 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001116 LoadExternalDefinition();
1117
Chris Lattnerab351632009-02-20 20:59:54 +00001118 for (ObjCCategoryDecl *Category = getCategoryList();
1119 Category; Category = Category->getNextClassCategory())
1120 if (Category->getIdentifier() == CategoryId)
1121 return Category;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001122 return 0;
1123}
1124
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001125ObjCMethodDecl *
1126ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1127 for (ObjCCategoryDecl *Category = getCategoryList();
1128 Category; Category = Category->getNextClassCategory())
1129 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1130 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1131 return MD;
1132 return 0;
1133}
1134
1135ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1136 for (ObjCCategoryDecl *Category = getCategoryList();
1137 Category; Category = Category->getNextClassCategory())
1138 if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
1139 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1140 return MD;
1141 return 0;
1142}
1143
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001144/// ClassImplementsProtocol - Checks that 'lProto' protocol
1145/// has been implemented in IDecl class, its super class or categories (if
1146/// lookupCategory is true).
1147bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1148 bool lookupCategory,
1149 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001150 if (!hasDefinition())
1151 return false;
1152
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001153 ObjCInterfaceDecl *IDecl = this;
1154 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00001155 for (ObjCInterfaceDecl::protocol_iterator
1156 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001157 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1158 return true;
1159 // This is dubious and is added to be compatible with gcc. In gcc, it is
1160 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1161 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1162 // object. This IMO, should be a bug.
1163 // FIXME: Treat this as an extension, and flag this as an error when GCC
1164 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001165 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001166 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1167 return true;
1168 }
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001170 // 2nd, look up the category.
1171 if (lookupCategory)
1172 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
1173 CDecl = CDecl->getNextClassCategory()) {
1174 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
1175 E = CDecl->protocol_end(); PI != E; ++PI)
1176 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1177 return true;
1178 }
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001180 // 3rd, look up the super class(s)
1181 if (IDecl->getSuperClass())
1182 return
1183 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1184 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001186 return false;
1187}
1188
Chris Lattnerab351632009-02-20 20:59:54 +00001189//===----------------------------------------------------------------------===//
1190// ObjCIvarDecl
1191//===----------------------------------------------------------------------===//
1192
David Blaikie99ba9e32011-12-20 02:48:34 +00001193void ObjCIvarDecl::anchor() { }
1194
Daniel Dunbara0654922010-04-02 20:10:03 +00001195ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001196 SourceLocation StartLoc,
1197 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001198 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001199 AccessControl ac, Expr *BW,
1200 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001201 if (DC) {
1202 // Ivar's can only appear in interfaces, implementations (via synthesized
1203 // properties), and class extensions (via direct declaration, or synthesized
1204 // properties).
1205 //
1206 // FIXME: This should really be asserting this:
1207 // (isa<ObjCCategoryDecl>(DC) &&
1208 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1209 // but unfortunately we sometimes place ivars into non-class extension
1210 // categories on error. This breaks an AST invariant, and should not be
1211 // fixed.
1212 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1213 isa<ObjCCategoryDecl>(DC)) &&
1214 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001215 // Once a new ivar is created in any of class/class-extension/implementation
1216 // decl contexts, the previously built IvarList must be rebuilt.
1217 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1218 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001219 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001220 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001221 else
1222 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001223 }
1224 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001225 }
1226
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001227 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1228 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001229}
1230
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001231ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1232 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1233 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1234 QualType(), 0, ObjCIvarDecl::None, 0, false);
1235}
1236
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001237const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1238 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001239
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001240 switch (DC->getKind()) {
1241 default:
1242 case ObjCCategoryImpl:
1243 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001244 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001245
1246 // Ivars can only appear in class extension categories.
1247 case ObjCCategory: {
1248 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1249 assert(CD->IsClassExtension() && "invalid container for ivar!");
1250 return CD->getClassInterface();
1251 }
1252
1253 case ObjCImplementation:
1254 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1255
1256 case ObjCInterface:
1257 return cast<ObjCInterfaceDecl>(DC);
1258 }
1259}
Chris Lattnerab351632009-02-20 20:59:54 +00001260
1261//===----------------------------------------------------------------------===//
1262// ObjCAtDefsFieldDecl
1263//===----------------------------------------------------------------------===//
1264
David Blaikie99ba9e32011-12-20 02:48:34 +00001265void ObjCAtDefsFieldDecl::anchor() { }
1266
Chris Lattnerab351632009-02-20 20:59:54 +00001267ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001268*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1269 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001270 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001271 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001272}
1273
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001274ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1275 unsigned ID) {
1276 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1277 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1278 0, QualType(), 0);
1279}
1280
Chris Lattnerab351632009-02-20 20:59:54 +00001281//===----------------------------------------------------------------------===//
1282// ObjCProtocolDecl
1283//===----------------------------------------------------------------------===//
1284
David Blaikie99ba9e32011-12-20 02:48:34 +00001285void ObjCProtocolDecl::anchor() { }
1286
Douglas Gregor27c6da22012-01-01 20:30:41 +00001287ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1288 SourceLocation nameLoc,
1289 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001290 ObjCProtocolDecl *PrevDecl)
1291 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001292{
1293 setPreviousDeclaration(PrevDecl);
1294 if (PrevDecl)
1295 Data = PrevDecl->Data;
1296}
1297
Chris Lattnerab351632009-02-20 20:59:54 +00001298ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001299 IdentifierInfo *Id,
1300 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001301 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001302 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001303 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001304 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001305
1306 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001307}
1308
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001309ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1310 unsigned ID) {
1311 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1312 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1313 0);
1314}
1315
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001316ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1317 ObjCProtocolDecl *PDecl = this;
1318
1319 if (Name == getIdentifier())
1320 return PDecl;
1321
1322 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1323 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1324 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001326 return NULL;
1327}
1328
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001329// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001330// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001331ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1332 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001333 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001335 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001336 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Chris Lattnerab351632009-02-20 20:59:54 +00001338 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001339 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001340 return MethodDecl;
1341 return NULL;
1342}
1343
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001344void ObjCProtocolDecl::allocateDefinitionData() {
1345 assert(!Data && "Protocol already has a definition!");
Douglas Gregor1d784b22012-01-01 19:51:50 +00001346 Data = new (getASTContext()) DefinitionData;
1347 Data->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001348}
1349
1350void ObjCProtocolDecl::startDefinition() {
1351 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001352
1353 // Update all of the declarations with a pointer to the definition.
1354 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1355 RD != RDEnd; ++RD)
1356 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001357}
1358
Anna Zakse63aedd2012-10-31 01:18:22 +00001359void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM) const {
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001360
1361 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1362 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1363 E = PDecl->prop_end(); P != E; ++P) {
1364 ObjCPropertyDecl *Prop = *P;
1365 // Insert into PM if not there already.
1366 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
1367 }
1368 // Scan through protocol's protocols.
1369 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1370 E = PDecl->protocol_end(); PI != E; ++PI)
1371 (*PI)->collectPropertiesToImplement(PM);
Anna Zaksb36ea372012-10-18 19:17:53 +00001372 }
Anna Zaksb36ea372012-10-18 19:17:53 +00001373}
1374
1375
Chris Lattnerab351632009-02-20 20:59:54 +00001376//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001377// ObjCCategoryDecl
1378//===----------------------------------------------------------------------===//
1379
David Blaikie99ba9e32011-12-20 02:48:34 +00001380void ObjCCategoryDecl::anchor() { }
1381
Chris Lattnerab351632009-02-20 20:59:54 +00001382ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001383 SourceLocation AtLoc,
1384 SourceLocation ClassNameLoc,
1385 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001386 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001387 ObjCInterfaceDecl *IDecl,
1388 SourceLocation IvarLBraceLoc,
1389 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001390 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1391 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001392 IDecl,
1393 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001394 if (IDecl) {
1395 // Link this category into its class's category list.
1396 CatDecl->NextClassCategory = IDecl->getCategoryList();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001397 if (IDecl->hasDefinition()) {
1398 IDecl->setCategoryList(CatDecl);
1399 if (ASTMutationListener *L = C.getASTMutationListener())
1400 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1401 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001402 }
1403
1404 return CatDecl;
1405}
1406
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001407ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1408 unsigned ID) {
1409 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1410 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1411 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001412}
1413
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001414ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1415 return getASTContext().getObjCImplementation(
1416 const_cast<ObjCCategoryDecl*>(this));
1417}
1418
1419void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1420 getASTContext().setObjCImplementation(this, ImplD);
1421}
1422
1423
Chris Lattnerab351632009-02-20 20:59:54 +00001424//===----------------------------------------------------------------------===//
1425// ObjCCategoryImplDecl
1426//===----------------------------------------------------------------------===//
1427
David Blaikie99ba9e32011-12-20 02:48:34 +00001428void ObjCCategoryImplDecl::anchor() { }
1429
Chris Lattnerab351632009-02-20 20:59:54 +00001430ObjCCategoryImplDecl *
1431ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001432 IdentifierInfo *Id,
1433 ObjCInterfaceDecl *ClassInterface,
1434 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001435 SourceLocation atStartLoc,
1436 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001437 if (ClassInterface && ClassInterface->hasDefinition())
1438 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001439 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001440 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001441}
1442
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001443ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1444 unsigned ID) {
1445 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1446 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1447 SourceLocation(), SourceLocation());
1448}
1449
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001450ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001451 // The class interface might be NULL if we are working with invalid code.
1452 if (const ObjCInterfaceDecl *ID = getClassInterface())
1453 return ID->FindCategoryDeclaration(getIdentifier());
1454 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001455}
1456
Chris Lattnerab351632009-02-20 20:59:54 +00001457
David Blaikie99ba9e32011-12-20 02:48:34 +00001458void ObjCImplDecl::anchor() { }
1459
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001460void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001461 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001462 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001463 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001464}
1465
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001466void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1467 ASTContext &Ctx = getASTContext();
1468
1469 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001470 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001471 if (IFace)
1472 Ctx.setObjCImplementation(IFace, ImplD);
1473
Duncan Sands98f2cca2009-07-21 07:56:29 +00001474 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001475 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1476 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1477 Ctx.setObjCImplementation(CD, ImplD);
1478 }
1479
1480 ClassInterface = IFace;
1481}
1482
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001483/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
James Dennett1c3a46a2012-06-15 22:30:14 +00001484/// properties implemented in this category \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001485/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001486///
Chris Lattner3aa18612009-02-28 18:42:10 +00001487ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001488FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1489 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001490 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001491 if (PID->getPropertyIvarDecl() &&
1492 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1493 return PID;
1494 }
1495 return 0;
1496}
1497
1498/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001499/// added to the list of those properties \@synthesized/\@dynamic in this
1500/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001501///
Chris Lattner3aa18612009-02-28 18:42:10 +00001502ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001503FindPropertyImplDecl(IdentifierInfo *Id) const {
1504 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001505 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001506 if (PID->getPropertyDecl()->getIdentifier() == Id)
1507 return PID;
1508 }
1509 return 0;
1510}
1511
Chris Lattner5f9e2722011-07-23 10:55:15 +00001512raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001513 const ObjCCategoryImplDecl &CID) {
1514 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001515 return OS;
1516}
1517
Chris Lattnerab351632009-02-20 20:59:54 +00001518//===----------------------------------------------------------------------===//
1519// ObjCImplementationDecl
1520//===----------------------------------------------------------------------===//
1521
David Blaikie99ba9e32011-12-20 02:48:34 +00001522void ObjCImplementationDecl::anchor() { }
1523
Chris Lattnerab351632009-02-20 20:59:54 +00001524ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001525ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001526 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001527 ObjCInterfaceDecl *SuperDecl,
1528 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001529 SourceLocation atStartLoc,
1530 SourceLocation IvarLBraceLoc,
1531 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001532 if (ClassInterface && ClassInterface->hasDefinition())
1533 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001534 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001535 nameLoc, atStartLoc,
1536 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001537}
1538
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001539ObjCImplementationDecl *
1540ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1541 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1542 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1543 SourceLocation());
1544}
1545
John McCallda6d9762011-07-22 04:15:06 +00001546void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1547 CXXCtorInitializer ** initializers,
1548 unsigned numInitializers) {
1549 if (numInitializers > 0) {
1550 NumIvarInitializers = numInitializers;
1551 CXXCtorInitializer **ivarInitializers =
1552 new (C) CXXCtorInitializer*[NumIvarInitializers];
1553 memcpy(ivarInitializers, initializers,
1554 numInitializers * sizeof(CXXCtorInitializer*));
1555 IvarInitializers = ivarInitializers;
1556 }
1557}
1558
Chris Lattner5f9e2722011-07-23 10:55:15 +00001559raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001560 const ObjCImplementationDecl &ID) {
1561 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001562 return OS;
1563}
1564
Chris Lattnerab351632009-02-20 20:59:54 +00001565//===----------------------------------------------------------------------===//
1566// ObjCCompatibleAliasDecl
1567//===----------------------------------------------------------------------===//
1568
David Blaikie99ba9e32011-12-20 02:48:34 +00001569void ObjCCompatibleAliasDecl::anchor() { }
1570
Chris Lattnerab351632009-02-20 20:59:54 +00001571ObjCCompatibleAliasDecl *
1572ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1573 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001574 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001575 ObjCInterfaceDecl* AliasedClass) {
1576 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1577}
1578
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001579ObjCCompatibleAliasDecl *
1580ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1581 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1582 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1583}
1584
Chris Lattnerab351632009-02-20 20:59:54 +00001585//===----------------------------------------------------------------------===//
1586// ObjCPropertyDecl
1587//===----------------------------------------------------------------------===//
1588
David Blaikie99ba9e32011-12-20 02:48:34 +00001589void ObjCPropertyDecl::anchor() { }
1590
Chris Lattnerab351632009-02-20 20:59:54 +00001591ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1592 SourceLocation L,
1593 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001594 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001595 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001596 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001597 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001598 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001599}
1600
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001601ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1602 unsigned ID) {
1603 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1604 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001605 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001606 0);
1607}
1608
Chris Lattnerab351632009-02-20 20:59:54 +00001609//===----------------------------------------------------------------------===//
1610// ObjCPropertyImplDecl
1611//===----------------------------------------------------------------------===//
1612
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001613ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001614 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001615 SourceLocation atLoc,
1616 SourceLocation L,
1617 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001618 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001619 ObjCIvarDecl *ivar,
1620 SourceLocation ivarLoc) {
1621 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1622 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001623}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001624
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001625ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1626 unsigned ID) {
1627 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1628 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1629 0, Dynamic, 0, SourceLocation());
1630}
1631
Douglas Gregora4ffd852010-11-17 01:03:52 +00001632SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1633 SourceLocation EndLoc = getLocation();
1634 if (IvarLoc.isValid())
1635 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001636
Douglas Gregora4ffd852010-11-17 01:03:52 +00001637 return SourceRange(AtLoc, EndLoc);
1638}