blob: 0b1a1ec456a5220fb474e4f5aa6248e4d70424f3 [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);
Douglas Gregord3297242013-01-16 23:00:23 +0000132 // Look through categories (but not extensions).
133 for (ObjCInterfaceDecl::visible_categories_iterator
134 Cat = OID->visible_categories_begin(),
135 CatEnd = OID->visible_categories_end();
136 Cat != CatEnd; ++Cat) {
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000137 if (!Cat->IsClassExtension())
138 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
139 return P;
Douglas Gregord3297242013-01-16 23:00:23 +0000140 }
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000141
142 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000143 for (ObjCInterfaceDecl::all_protocol_iterator
144 I = OID->all_referenced_protocol_begin(),
145 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000146 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
147 return P;
148
149 // Finally, check the super class.
150 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
151 return superClass->FindPropertyDeclaration(PropertyId);
152 break;
153 }
154 case Decl::ObjCCategory: {
155 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
156 // Look through protocols.
157 if (!OCD->IsClassExtension())
158 for (ObjCCategoryDecl::protocol_iterator
159 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
160 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
161 return P;
162
163 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000164 }
165 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000166 return 0;
167}
168
David Blaikie99ba9e32011-12-20 02:48:34 +0000169void ObjCInterfaceDecl::anchor() { }
170
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000171/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
172/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000173/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000174///
175ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000176ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000177 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000178 // FIXME: Should make sure no callers ever do this.
179 if (!hasDefinition())
180 return 0;
181
182 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000183 LoadExternalDefinition();
184
Ted Kremenek37cafb02010-03-15 20:30:07 +0000185 if (ObjCPropertyDecl *PD =
186 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
187 return PD;
188
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000189 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000190 for (ObjCInterfaceDecl::all_protocol_iterator
191 I = all_referenced_protocol_begin(),
192 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000193 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
194 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000195
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000196 return 0;
197}
198
Anna Zakse63aedd2012-10-31 01:18:22 +0000199void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM) const {
Anna Zaksb36ea372012-10-18 19:17:53 +0000200 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
201 E = prop_end(); P != E; ++P) {
202 ObjCPropertyDecl *Prop = *P;
203 PM[Prop->getIdentifier()] = Prop;
204 }
205 for (ObjCInterfaceDecl::all_protocol_iterator
206 PI = all_referenced_protocol_begin(),
207 E = all_referenced_protocol_end(); PI != E; ++PI)
208 (*PI)->collectPropertiesToImplement(PM);
Anna Zakse63aedd2012-10-31 01:18:22 +0000209 // Note, the properties declared only in class extensions are still copied
210 // into the main @interface's property list, and therefore we don't
211 // explicitly, have to search class extension properties.
Anna Zaksb36ea372012-10-18 19:17:53 +0000212}
213
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000214bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
215 const ObjCInterfaceDecl *Class = this;
216 while (Class) {
217 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
218 return true;
219 Class = Class->getSuperClass();
220 }
221 return false;
222}
223
224const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
225 const ObjCInterfaceDecl *Class = this;
226 while (Class) {
227 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
228 return Class;
229 Class = Class->getSuperClass();
230 }
231 return 0;
232}
233
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000234void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
235 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
236 ASTContext &C)
237{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000238 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000239 LoadExternalDefinition();
240
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000241 if (data().AllReferencedProtocols.empty() &&
242 data().ReferencedProtocols.empty()) {
243 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000244 return;
245 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000246
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000247 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000248 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000249 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000250 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000251 for (unsigned i = 0; i < ExtNum; i++) {
252 bool protocolExists = false;
253 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000254 for (all_protocol_iterator
255 p = all_referenced_protocol_begin(),
256 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000257 ObjCProtocolDecl *Proto = (*p);
258 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
259 protocolExists = true;
260 break;
261 }
262 }
263 // Do we want to warn on a protocol in extension class which
264 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000265 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000266 ProtocolRefs.push_back(ProtoInExtension);
267 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000268
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000269 if (ProtocolRefs.empty())
270 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000271
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000272 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000273 for (all_protocol_iterator p = all_referenced_protocol_begin(),
274 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000275 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000276 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000277
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000278 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000279}
280
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000281void ObjCInterfaceDecl::allocateDefinitionData() {
282 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor26fec632011-12-15 18:17:27 +0000283 Data = new (getASTContext()) DefinitionData();
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000284 Data->Definition = this;
285
286 // Make the type point at the definition, now that we have one.
287 if (TypeForDecl)
288 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000289}
290
291void ObjCInterfaceDecl::startDefinition() {
292 allocateDefinitionData();
293
Douglas Gregor53df7a12011-12-15 18:03:09 +0000294 // Update all of the declarations with a pointer to the definition.
295 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
296 RD != RDEnd; ++RD) {
297 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000298 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000299 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000300}
301
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000302ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
303 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000304 // FIXME: Should make sure no callers ever do this.
305 if (!hasDefinition())
306 return 0;
307
308 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000309 LoadExternalDefinition();
310
Chris Lattner1e03a562008-03-16 00:19:01 +0000311 ObjCInterfaceDecl* ClassDecl = this;
312 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000313 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000314 clsDeclared = ClassDecl;
315 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000316 }
Douglas Gregord3297242013-01-16 23:00:23 +0000317
318 for (ObjCInterfaceDecl::visible_extensions_iterator
319 Ext = ClassDecl->visible_extensions_begin(),
320 ExtEnd = ClassDecl->visible_extensions_end();
321 Ext != ExtEnd; ++Ext) {
322 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000323 clsDeclared = ClassDecl;
324 return I;
325 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000326 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000327
Chris Lattner1e03a562008-03-16 00:19:01 +0000328 ClassDecl = ClassDecl->getSuperClass();
329 }
330 return NULL;
331}
332
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000333/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
334/// class whose name is passed as argument. If it is not one of the super classes
335/// the it returns NULL.
336ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
337 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000338 // FIXME: Should make sure no callers ever do this.
339 if (!hasDefinition())
340 return 0;
341
342 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000343 LoadExternalDefinition();
344
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000345 ObjCInterfaceDecl* ClassDecl = this;
346 while (ClassDecl != NULL) {
347 if (ClassDecl->getIdentifier() == ICName)
348 return ClassDecl;
349 ClassDecl = ClassDecl->getSuperClass();
350 }
351 return NULL;
352}
353
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000354/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000355/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000356ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
357 bool isInstance,
358 bool shallowCategoryLookup) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000359 // FIXME: Should make sure no callers ever do this.
360 if (!hasDefinition())
361 return 0;
362
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000363 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000364 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000366 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000367 LoadExternalDefinition();
368
Chris Lattner1e03a562008-03-16 00:19:01 +0000369 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000370 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000371 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Chris Lattner1e03a562008-03-16 00:19:01 +0000373 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000374 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
375 E = ClassDecl->protocol_end();
376 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000377 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000378 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000379
380 // Didn't find one yet - now look through categories.
Douglas Gregord3297242013-01-16 23:00:23 +0000381 for (ObjCInterfaceDecl::visible_categories_iterator
382 Cat = ClassDecl->visible_categories_begin(),
383 CatEnd = ClassDecl->visible_categories_end();
384 Cat != CatEnd; ++Cat) {
385 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000386 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000387
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000388 if (!shallowCategoryLookup) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000389 // Didn't find one yet - look through protocols.
390 const ObjCList<ObjCProtocolDecl> &Protocols =
Douglas Gregord3297242013-01-16 23:00:23 +0000391 Cat->getReferencedProtocols();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000392 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
393 E = Protocols.end(); I != E; ++I)
394 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
395 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000396 }
Chris Lattner1e03a562008-03-16 00:19:01 +0000397 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000398
Chris Lattner1e03a562008-03-16 00:19:01 +0000399 ClassDecl = ClassDecl->getSuperClass();
400 }
401 return NULL;
402}
403
Anna Zakse61354b2012-07-27 19:07:44 +0000404// Will search "local" class/category implementations for a method decl.
405// If failed, then we search in class's root for an instance method.
406// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000407ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
408 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000409 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000410 // FIXME: Should make sure no callers ever do this.
411 if (!hasDefinition())
412 return 0;
413
414 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000415 LoadExternalDefinition();
416
Steve Naroffd789d3d2009-10-01 23:46:04 +0000417 ObjCMethodDecl *Method = 0;
418 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000419 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
420 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000421
422 // Look through local category implementations associated with the class.
423 if (!Method)
424 Method = Instance ? getCategoryInstanceMethod(Sel)
425 : getCategoryClassMethod(Sel);
426
427 // Before we give up, check if the selector is an instance method.
428 // But only in the root. This matches gcc's behavior and what the
429 // runtime expects.
430 if (!Instance && !Method && !getSuperClass()) {
431 Method = lookupInstanceMethod(Sel);
432 // Look through local category implementations associated
433 // with the root class.
434 if (!Method)
435 Method = lookupPrivateMethod(Sel, true);
436 }
437
Steve Naroffd789d3d2009-10-01 23:46:04 +0000438 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000439 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000440 return Method;
441}
Chris Lattnerab351632009-02-20 20:59:54 +0000442
443//===----------------------------------------------------------------------===//
444// ObjCMethodDecl
445//===----------------------------------------------------------------------===//
446
447ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000448 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000449 SourceLocation endLoc,
450 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000451 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000452 DeclContext *contextDecl,
453 bool isInstance,
454 bool isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000455 bool isPropertyAccessor,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000456 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000457 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000458 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000459 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000460 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000461 SelInfo, T, ResultTInfo, contextDecl,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000462 isInstance, isVariadic, isPropertyAccessor,
463 isImplicitlyDeclared, isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000464 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000465 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000466}
467
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000468ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
469 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
470 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
471 Selector(), QualType(), 0, 0);
472}
473
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000474Stmt *ObjCMethodDecl::getBody() const {
475 return Body.get(getASTContext().getExternalSource());
476}
477
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000478void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
479 assert(PrevMethod);
480 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
481 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000482 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000483}
484
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000485void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
486 ArrayRef<ParmVarDecl*> Params,
487 ArrayRef<SourceLocation> SelLocs) {
488 ParamsAndSelLocs = 0;
489 NumParams = Params.size();
490 if (Params.empty() && SelLocs.empty())
491 return;
492
493 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
494 sizeof(SourceLocation) * SelLocs.size();
495 ParamsAndSelLocs = C.Allocate(Size);
496 std::copy(Params.begin(), Params.end(), getParams());
497 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
498}
499
500void ObjCMethodDecl::getSelectorLocs(
501 SmallVectorImpl<SourceLocation> &SelLocs) const {
502 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
503 SelLocs.push_back(getSelectorLoc(i));
504}
505
506void ObjCMethodDecl::setMethodParams(ASTContext &C,
507 ArrayRef<ParmVarDecl*> Params,
508 ArrayRef<SourceLocation> SelLocs) {
509 assert((!SelLocs.empty() || isImplicit()) &&
510 "No selector locs for non-implicit method");
511 if (isImplicit())
512 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
513
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000514 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
515 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000516 if (SelLocsKind != SelLoc_NonStandard)
517 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
518
519 setParamsAndSelLocs(C, Params, SelLocs);
520}
521
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000522/// \brief A definition will return its interface declaration.
523/// An interface declaration will return its definition.
524/// Otherwise it will return itself.
525ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
526 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000527 ObjCMethodDecl *Redecl = 0;
528 if (HasRedeclaration)
529 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000530 if (Redecl)
531 return Redecl;
532
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000533 Decl *CtxD = cast<Decl>(getDeclContext());
534
535 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
536 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
537 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
538
539 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
540 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
541 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
542
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000543 } else if (ObjCImplementationDecl *ImplD =
544 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000545 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
546 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000547
548 } else if (ObjCCategoryImplDecl *CImplD =
549 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000550 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000551 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000552 }
553
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000554 if (!Redecl && isRedeclaration()) {
555 // This is the last redeclaration, go back to the first method.
556 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
557 isInstanceMethod());
558 }
559
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000560 return Redecl ? Redecl : this;
561}
562
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000563ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
564 Decl *CtxD = cast<Decl>(getDeclContext());
565
566 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
567 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
568 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
569 isInstanceMethod()))
570 return MD;
571
572 } else if (ObjCCategoryImplDecl *CImplD =
573 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000574 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000575 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
576 isInstanceMethod()))
577 return MD;
578 }
579
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000580 if (isRedeclaration())
581 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
582 isInstanceMethod());
583
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000584 return this;
585}
586
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000587SourceLocation ObjCMethodDecl::getLocEnd() const {
588 if (Stmt *Body = getBody())
589 return Body->getLocEnd();
590 return DeclEndLoc;
591}
592
John McCall85f3d762011-03-02 01:50:55 +0000593ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
594 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000595 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000596 return family;
597
John McCalld5313b02011-03-02 11:33:24 +0000598 // Check for an explicit attribute.
599 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
600 // The unfortunate necessity of mapping between enums here is due
601 // to the attributes framework.
602 switch (attr->getFamily()) {
603 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
604 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
605 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
606 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
607 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
608 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
609 }
610 Family = static_cast<unsigned>(family);
611 return family;
612 }
613
John McCall85f3d762011-03-02 01:50:55 +0000614 family = getSelector().getMethodFamily();
615 switch (family) {
616 case OMF_None: break;
617
618 // init only has a conventional meaning for an instance method, and
619 // it has to return an object.
620 case OMF_init:
621 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
622 family = OMF_None;
623 break;
624
625 // alloc/copy/new have a conventional meaning for both class and
626 // instance methods, but they require an object return.
627 case OMF_alloc:
628 case OMF_copy:
629 case OMF_mutableCopy:
630 case OMF_new:
631 if (!getResultType()->isObjCObjectPointerType())
632 family = OMF_None;
633 break;
634
635 // These selectors have a conventional meaning only for instance methods.
636 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000637 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000638 case OMF_retain:
639 case OMF_release:
640 case OMF_autorelease:
641 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000642 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000643 if (!isInstanceMethod())
644 family = OMF_None;
645 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000646
647 case OMF_performSelector:
648 if (!isInstanceMethod() ||
649 !getResultType()->isObjCIdType())
650 family = OMF_None;
651 else {
652 unsigned noParams = param_size();
653 if (noParams < 1 || noParams > 3)
654 family = OMF_None;
655 else {
656 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
657 QualType ArgT = (*it);
658 if (!ArgT->isObjCSelType()) {
659 family = OMF_None;
660 break;
661 }
662 while (--noParams) {
663 it++;
664 ArgT = (*it);
665 if (!ArgT->isObjCIdType()) {
666 family = OMF_None;
667 break;
668 }
669 }
670 }
671 }
672 break;
673
John McCall85f3d762011-03-02 01:50:55 +0000674 }
675
676 // Cache the result.
677 Family = static_cast<unsigned>(family);
678 return family;
679}
680
Mike Stump1eb44332009-09-09 15:08:12 +0000681void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000682 const ObjCInterfaceDecl *OID) {
683 QualType selfTy;
684 if (isInstanceMethod()) {
685 // There may be no interface context due to error in declaration
686 // of the interface (which has been reported). Recover gracefully.
687 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000688 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000689 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000690 } else {
691 selfTy = Context.getObjCIdType();
692 }
693 } else // we have a factory method.
694 selfTy = Context.getObjCClassType();
695
John McCall7acddac2011-06-17 06:42:21 +0000696 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000697 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000698
David Blaikie4e4d0842012-03-11 07:00:24 +0000699 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000700 if (isInstanceMethod()) {
701 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000702
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000703 // 'self' is always __strong. It's actually pseudo-strong except
704 // in init methods (or methods labeled ns_consumes_self), though.
705 Qualifiers qs;
706 qs.setObjCLifetime(Qualifiers::OCL_Strong);
707 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000708
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000709 // In addition, 'self' is const unless this is an init method.
710 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
711 selfTy = selfTy.withConst();
712 selfIsPseudoStrong = true;
713 }
714 }
715 else {
716 assert(isClassMethod());
717 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000718 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000719 selfIsPseudoStrong = true;
720 }
John McCallf85e1932011-06-15 23:02:42 +0000721 }
722
723 ImplicitParamDecl *self
724 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
725 &Context.Idents.get("self"), selfTy);
726 setSelfDecl(self);
727
728 if (selfIsConsumed)
729 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000730
John McCall7acddac2011-06-17 06:42:21 +0000731 if (selfIsPseudoStrong)
732 self->setARCPseudoStrong(true);
733
Mike Stump1eb44332009-09-09 15:08:12 +0000734 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
735 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000736 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000737}
738
Chris Lattnerab351632009-02-20 20:59:54 +0000739ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
740 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
741 return ID;
742 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
743 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000744 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000745 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000746
747 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000748 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000749}
750
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000751static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
752 const ObjCMethodDecl *Method,
753 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
754 bool MovedToSuper) {
755 if (!Container)
756 return;
757
758 // In categories look for overriden methods from protocols. A method from
759 // category is not "overriden" since it is considered as the "same" method
760 // (same USR) as the one from the interface.
761 if (const ObjCCategoryDecl *
762 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
763 // Check whether we have a matching method at this category but only if we
764 // are at the super class level.
765 if (MovedToSuper)
766 if (ObjCMethodDecl *
767 Overridden = Container->getMethod(Method->getSelector(),
768 Method->isInstanceMethod()))
769 if (Method != Overridden) {
770 // We found an override at this category; there is no need to look
771 // into its protocols.
772 Methods.push_back(Overridden);
773 return;
774 }
775
776 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
777 PEnd = Category->protocol_end();
778 P != PEnd; ++P)
779 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
780 return;
781 }
782
783 // Check whether we have a matching method at this level.
784 if (const ObjCMethodDecl *
785 Overridden = Container->getMethod(Method->getSelector(),
786 Method->isInstanceMethod()))
787 if (Method != Overridden) {
788 // We found an override at this level; there is no need to look
789 // into other protocols or categories.
790 Methods.push_back(Overridden);
791 return;
792 }
793
794 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
795 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
796 PEnd = Protocol->protocol_end();
797 P != PEnd; ++P)
798 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
799 }
800
801 if (const ObjCInterfaceDecl *
802 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
803 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
804 PEnd = Interface->protocol_end();
805 P != PEnd; ++P)
806 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
807
Douglas Gregord3297242013-01-16 23:00:23 +0000808 for (ObjCInterfaceDecl::visible_categories_iterator
809 Cat = Interface->visible_categories_begin(),
810 CatEnd = Interface->visible_categories_end();
811 Cat != CatEnd; ++Cat) {
812 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000813 MovedToSuper);
Douglas Gregord3297242013-01-16 23:00:23 +0000814 }
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000815
816 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
817 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
818 /*MovedToSuper=*/true);
819 }
820}
821
822static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
823 const ObjCMethodDecl *Method,
824 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
825 CollectOverriddenMethodsRecurse(Container, Method, Methods,
826 /*MovedToSuper=*/false);
827}
828
829static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
830 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
831 assert(Method->isOverriding());
832
833 if (const ObjCProtocolDecl *
834 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
835 CollectOverriddenMethods(ProtD, Method, overridden);
836
837 } else if (const ObjCImplDecl *
838 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
839 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
840 if (!ID)
841 return;
842 // Start searching for overridden methods using the method from the
843 // interface as starting point.
844 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
845 Method->isInstanceMethod()))
846 Method = IFaceMeth;
847 CollectOverriddenMethods(ID, Method, overridden);
848
849 } else if (const ObjCCategoryDecl *
850 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
851 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
852 if (!ID)
853 return;
854 // Start searching for overridden methods using the method from the
855 // interface as starting point.
856 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
857 Method->isInstanceMethod()))
858 Method = IFaceMeth;
859 CollectOverriddenMethods(ID, Method, overridden);
860
861 } else {
862 CollectOverriddenMethods(
863 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
864 Method, overridden);
865 }
866}
867
868static void collectOnCategoriesAfterLocation(SourceLocation Loc,
869 const ObjCInterfaceDecl *Class,
870 SourceManager &SM,
871 const ObjCMethodDecl *Method,
872 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
873 if (!Class)
874 return;
875
Douglas Gregord3297242013-01-16 23:00:23 +0000876 for (ObjCInterfaceDecl::visible_categories_iterator
877 Cat = Class->visible_categories_begin(),
878 CatEnd = Class->visible_categories_end();
879 Cat != CatEnd; ++Cat) {
880 if (SM.isBeforeInTranslationUnit(Loc, Cat->getLocation()))
881 CollectOverriddenMethodsRecurse(*Cat, Method, Methods, true);
882 }
883
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000884 collectOnCategoriesAfterLocation(Loc, Class->getSuperClass(), SM,
885 Method, Methods);
886}
887
888/// \brief Faster collection that is enabled when ObjCMethodDecl::isOverriding()
889/// returns false.
890/// You'd think that in that case there are no overrides but categories can
891/// "introduce" new overridden methods that are missed by Sema because the
892/// overrides lookup that it does for methods, inside implementations, will
893/// stop at the interface level (if there is a method there) and not look
894/// further in super classes.
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000895/// Methods in an implementation can overide methods in super class's category
896/// but not in current class's category. But, such methods
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000897static void collectOverriddenMethodsFast(SourceManager &SM,
898 const ObjCMethodDecl *Method,
899 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
900 assert(!Method->isOverriding());
901
902 const ObjCContainerDecl *
903 ContD = cast<ObjCContainerDecl>(Method->getDeclContext());
904 if (isa<ObjCInterfaceDecl>(ContD) || isa<ObjCProtocolDecl>(ContD))
905 return;
906 const ObjCInterfaceDecl *Class = Method->getClassInterface();
907 if (!Class)
908 return;
909
910 collectOnCategoriesAfterLocation(Class->getLocation(), Class->getSuperClass(),
911 SM, Method, Methods);
912}
913
914void ObjCMethodDecl::getOverriddenMethods(
915 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
916 const ObjCMethodDecl *Method = this;
917
918 if (Method->isRedeclaration()) {
919 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
920 getMethod(Method->getSelector(), Method->isInstanceMethod());
921 }
922
923 if (!Method->isOverriding()) {
924 collectOverriddenMethodsFast(getASTContext().getSourceManager(),
925 Method, Overridden);
926 } else {
927 collectOverriddenMethodsSlow(Method, Overridden);
928 assert(!Overridden.empty() &&
929 "ObjCMethodDecl's overriding bit is not as expected");
930 }
931}
932
Jordan Rose04bec392012-10-10 16:42:54 +0000933const ObjCPropertyDecl *
934ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
935 Selector Sel = getSelector();
936 unsigned NumArgs = Sel.getNumArgs();
937 if (NumArgs > 1)
938 return 0;
939
Jordan Rose50d2b262012-10-11 16:02:02 +0000940 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose04bec392012-10-10 16:42:54 +0000941 return 0;
942
943 if (isPropertyAccessor()) {
944 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanianc328d9c2013-01-12 00:28:34 +0000945 // If container is class extension, find its primary class.
946 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
947 if (CatDecl->IsClassExtension())
948 Container = CatDecl->getClassInterface();
949
Jordan Rose04bec392012-10-10 16:42:54 +0000950 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 }
Douglas Gregord3297242013-01-16 23:00:23 +00001070
1071 for (ObjCInterfaceDecl::known_extensions_iterator
1072 Ext = known_extensions_begin(),
1073 ExtEnd = known_extensions_end();
1074 Ext != ExtEnd; ++Ext) {
1075 if (!Ext->ivar_empty()) {
1076 ObjCCategoryDecl::ivar_iterator I = Ext->ivar_begin(),E = Ext->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001077 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001078 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001079 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001080 }
David Blaikie581deb32012-06-06 20:45:41 +00001081 for ( ;I != E; curIvar = *I, ++I)
1082 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001083 }
1084 }
1085
1086 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1087 if (!ImplDecl->ivar_empty()) {
1088 ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1089 E = ImplDecl->ivar_end();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001090 if (!data().IvarList) {
David Blaikie581deb32012-06-06 20:45:41 +00001091 data().IvarList = *I; ++I;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001092 curIvar = data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001093 }
David Blaikie581deb32012-06-06 20:45:41 +00001094 for ( ;I != E; curIvar = *I, ++I)
1095 curIvar->setNextIvar(*I);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001096 }
1097 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001098 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001099}
Chris Lattnerab351632009-02-20 20:59:54 +00001100
1101/// FindCategoryDeclaration - Finds category declaration in the list of
1102/// categories for this class and returns it. Name of the category is passed
1103/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001104///
Chris Lattnerab351632009-02-20 20:59:54 +00001105ObjCCategoryDecl *
1106ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001107 // FIXME: Should make sure no callers ever do this.
1108 if (!hasDefinition())
1109 return 0;
1110
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001111 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001112 LoadExternalDefinition();
1113
Douglas Gregord3297242013-01-16 23:00:23 +00001114 for (visible_categories_iterator Cat = visible_categories_begin(),
1115 CatEnd = visible_categories_end();
1116 Cat != CatEnd;
1117 ++Cat) {
1118 if (Cat->getIdentifier() == CategoryId)
1119 return *Cat;
1120 }
1121
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 {
Douglas Gregord3297242013-01-16 23:00:23 +00001127 for (visible_categories_iterator Cat = visible_categories_begin(),
1128 CatEnd = visible_categories_end();
1129 Cat != CatEnd;
1130 ++Cat) {
1131 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001132 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1133 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001134 }
1135
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001136 return 0;
1137}
1138
1139ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregord3297242013-01-16 23:00:23 +00001140 for (visible_categories_iterator Cat = visible_categories_begin(),
1141 CatEnd = visible_categories_end();
1142 Cat != CatEnd;
1143 ++Cat) {
1144 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001145 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1146 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001147 }
1148
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001149 return 0;
1150}
1151
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001152/// ClassImplementsProtocol - Checks that 'lProto' protocol
1153/// has been implemented in IDecl class, its super class or categories (if
1154/// lookupCategory is true).
1155bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1156 bool lookupCategory,
1157 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001158 if (!hasDefinition())
1159 return false;
1160
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001161 ObjCInterfaceDecl *IDecl = this;
1162 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00001163 for (ObjCInterfaceDecl::protocol_iterator
1164 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001165 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1166 return true;
1167 // This is dubious and is added to be compatible with gcc. In gcc, it is
1168 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1169 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1170 // object. This IMO, should be a bug.
1171 // FIXME: Treat this as an extension, and flag this as an error when GCC
1172 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001173 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001174 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1175 return true;
1176 }
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001178 // 2nd, look up the category.
1179 if (lookupCategory)
Douglas Gregord3297242013-01-16 23:00:23 +00001180 for (visible_categories_iterator Cat = visible_categories_begin(),
1181 CatEnd = visible_categories_end();
1182 Cat != CatEnd;
1183 ++Cat) {
1184 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1185 E = Cat->protocol_end();
1186 PI != E; ++PI)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001187 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1188 return true;
1189 }
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001191 // 3rd, look up the super class(s)
1192 if (IDecl->getSuperClass())
1193 return
1194 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1195 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001197 return false;
1198}
1199
Chris Lattnerab351632009-02-20 20:59:54 +00001200//===----------------------------------------------------------------------===//
1201// ObjCIvarDecl
1202//===----------------------------------------------------------------------===//
1203
David Blaikie99ba9e32011-12-20 02:48:34 +00001204void ObjCIvarDecl::anchor() { }
1205
Daniel Dunbara0654922010-04-02 20:10:03 +00001206ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001207 SourceLocation StartLoc,
1208 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001209 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001210 AccessControl ac, Expr *BW,
1211 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001212 if (DC) {
1213 // Ivar's can only appear in interfaces, implementations (via synthesized
1214 // properties), and class extensions (via direct declaration, or synthesized
1215 // properties).
1216 //
1217 // FIXME: This should really be asserting this:
1218 // (isa<ObjCCategoryDecl>(DC) &&
1219 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1220 // but unfortunately we sometimes place ivars into non-class extension
1221 // categories on error. This breaks an AST invariant, and should not be
1222 // fixed.
1223 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1224 isa<ObjCCategoryDecl>(DC)) &&
1225 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001226 // Once a new ivar is created in any of class/class-extension/implementation
1227 // decl contexts, the previously built IvarList must be rebuilt.
1228 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1229 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001230 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001231 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001232 else
1233 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001234 }
1235 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001236 }
1237
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001238 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1239 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001240}
1241
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001242ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1243 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1244 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1245 QualType(), 0, ObjCIvarDecl::None, 0, false);
1246}
1247
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001248const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1249 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001250
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001251 switch (DC->getKind()) {
1252 default:
1253 case ObjCCategoryImpl:
1254 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001255 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001256
1257 // Ivars can only appear in class extension categories.
1258 case ObjCCategory: {
1259 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1260 assert(CD->IsClassExtension() && "invalid container for ivar!");
1261 return CD->getClassInterface();
1262 }
1263
1264 case ObjCImplementation:
1265 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1266
1267 case ObjCInterface:
1268 return cast<ObjCInterfaceDecl>(DC);
1269 }
1270}
Chris Lattnerab351632009-02-20 20:59:54 +00001271
1272//===----------------------------------------------------------------------===//
1273// ObjCAtDefsFieldDecl
1274//===----------------------------------------------------------------------===//
1275
David Blaikie99ba9e32011-12-20 02:48:34 +00001276void ObjCAtDefsFieldDecl::anchor() { }
1277
Chris Lattnerab351632009-02-20 20:59:54 +00001278ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001279*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1280 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001281 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001282 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001283}
1284
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001285ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1286 unsigned ID) {
1287 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1288 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1289 0, QualType(), 0);
1290}
1291
Chris Lattnerab351632009-02-20 20:59:54 +00001292//===----------------------------------------------------------------------===//
1293// ObjCProtocolDecl
1294//===----------------------------------------------------------------------===//
1295
David Blaikie99ba9e32011-12-20 02:48:34 +00001296void ObjCProtocolDecl::anchor() { }
1297
Douglas Gregor27c6da22012-01-01 20:30:41 +00001298ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1299 SourceLocation nameLoc,
1300 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001301 ObjCProtocolDecl *PrevDecl)
1302 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001303{
1304 setPreviousDeclaration(PrevDecl);
1305 if (PrevDecl)
1306 Data = PrevDecl->Data;
1307}
1308
Chris Lattnerab351632009-02-20 20:59:54 +00001309ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001310 IdentifierInfo *Id,
1311 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001312 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001313 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001314 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001315 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001316
1317 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001318}
1319
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001320ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1321 unsigned ID) {
1322 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1323 return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1324 0);
1325}
1326
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001327ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1328 ObjCProtocolDecl *PDecl = this;
1329
1330 if (Name == getIdentifier())
1331 return PDecl;
1332
1333 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1334 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1335 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001337 return NULL;
1338}
1339
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001340// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001341// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001342ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1343 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001344 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001346 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001347 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Chris Lattnerab351632009-02-20 20:59:54 +00001349 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001350 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001351 return MethodDecl;
1352 return NULL;
1353}
1354
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001355void ObjCProtocolDecl::allocateDefinitionData() {
1356 assert(!Data && "Protocol already has a definition!");
Douglas Gregor1d784b22012-01-01 19:51:50 +00001357 Data = new (getASTContext()) DefinitionData;
1358 Data->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001359}
1360
1361void ObjCProtocolDecl::startDefinition() {
1362 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001363
1364 // Update all of the declarations with a pointer to the definition.
1365 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1366 RD != RDEnd; ++RD)
1367 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001368}
1369
Anna Zakse63aedd2012-10-31 01:18:22 +00001370void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM) const {
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001371
1372 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1373 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1374 E = PDecl->prop_end(); P != E; ++P) {
1375 ObjCPropertyDecl *Prop = *P;
1376 // Insert into PM if not there already.
1377 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
1378 }
1379 // Scan through protocol's protocols.
1380 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1381 E = PDecl->protocol_end(); PI != E; ++PI)
1382 (*PI)->collectPropertiesToImplement(PM);
Anna Zaksb36ea372012-10-18 19:17:53 +00001383 }
Anna Zaksb36ea372012-10-18 19:17:53 +00001384}
1385
1386
Chris Lattnerab351632009-02-20 20:59:54 +00001387//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001388// ObjCCategoryDecl
1389//===----------------------------------------------------------------------===//
1390
David Blaikie99ba9e32011-12-20 02:48:34 +00001391void ObjCCategoryDecl::anchor() { }
1392
Chris Lattnerab351632009-02-20 20:59:54 +00001393ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001394 SourceLocation AtLoc,
1395 SourceLocation ClassNameLoc,
1396 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001397 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001398 ObjCInterfaceDecl *IDecl,
1399 SourceLocation IvarLBraceLoc,
1400 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001401 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1402 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001403 IDecl,
1404 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001405 if (IDecl) {
1406 // Link this category into its class's category list.
Douglas Gregord3297242013-01-16 23:00:23 +00001407 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001408 if (IDecl->hasDefinition()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001409 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001410 if (ASTMutationListener *L = C.getASTMutationListener())
1411 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1412 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001413 }
1414
1415 return CatDecl;
1416}
1417
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001418ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1419 unsigned ID) {
1420 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1421 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1422 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001423}
1424
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001425ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1426 return getASTContext().getObjCImplementation(
1427 const_cast<ObjCCategoryDecl*>(this));
1428}
1429
1430void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1431 getASTContext().setObjCImplementation(this, ImplD);
1432}
1433
1434
Chris Lattnerab351632009-02-20 20:59:54 +00001435//===----------------------------------------------------------------------===//
1436// ObjCCategoryImplDecl
1437//===----------------------------------------------------------------------===//
1438
David Blaikie99ba9e32011-12-20 02:48:34 +00001439void ObjCCategoryImplDecl::anchor() { }
1440
Chris Lattnerab351632009-02-20 20:59:54 +00001441ObjCCategoryImplDecl *
1442ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001443 IdentifierInfo *Id,
1444 ObjCInterfaceDecl *ClassInterface,
1445 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001446 SourceLocation atStartLoc,
1447 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001448 if (ClassInterface && ClassInterface->hasDefinition())
1449 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001450 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001451 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001452}
1453
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001454ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1455 unsigned ID) {
1456 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1457 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1458 SourceLocation(), SourceLocation());
1459}
1460
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001461ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001462 // The class interface might be NULL if we are working with invalid code.
1463 if (const ObjCInterfaceDecl *ID = getClassInterface())
1464 return ID->FindCategoryDeclaration(getIdentifier());
1465 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001466}
1467
Chris Lattnerab351632009-02-20 20:59:54 +00001468
David Blaikie99ba9e32011-12-20 02:48:34 +00001469void ObjCImplDecl::anchor() { }
1470
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001471void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001472 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001473 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001474 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001475}
1476
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001477void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1478 ASTContext &Ctx = getASTContext();
1479
1480 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001481 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001482 if (IFace)
1483 Ctx.setObjCImplementation(IFace, ImplD);
1484
Duncan Sands98f2cca2009-07-21 07:56:29 +00001485 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001486 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1487 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1488 Ctx.setObjCImplementation(CD, ImplD);
1489 }
1490
1491 ClassInterface = IFace;
1492}
1493
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001494/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
James Dennett1c3a46a2012-06-15 22:30:14 +00001495/// properties implemented in this category \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001496/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001497///
Chris Lattner3aa18612009-02-28 18:42:10 +00001498ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001499FindPropertyImplIvarDecl(IdentifierInfo *ivarId) 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->getPropertyIvarDecl() &&
1503 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1504 return PID;
1505 }
1506 return 0;
1507}
1508
1509/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001510/// added to the list of those properties \@synthesized/\@dynamic in this
1511/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001512///
Chris Lattner3aa18612009-02-28 18:42:10 +00001513ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001514FindPropertyImplDecl(IdentifierInfo *Id) const {
1515 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001516 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001517 if (PID->getPropertyDecl()->getIdentifier() == Id)
1518 return PID;
1519 }
1520 return 0;
1521}
1522
Chris Lattner5f9e2722011-07-23 10:55:15 +00001523raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001524 const ObjCCategoryImplDecl &CID) {
1525 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001526 return OS;
1527}
1528
Chris Lattnerab351632009-02-20 20:59:54 +00001529//===----------------------------------------------------------------------===//
1530// ObjCImplementationDecl
1531//===----------------------------------------------------------------------===//
1532
David Blaikie99ba9e32011-12-20 02:48:34 +00001533void ObjCImplementationDecl::anchor() { }
1534
Chris Lattnerab351632009-02-20 20:59:54 +00001535ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001536ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001537 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001538 ObjCInterfaceDecl *SuperDecl,
1539 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001540 SourceLocation atStartLoc,
1541 SourceLocation IvarLBraceLoc,
1542 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001543 if (ClassInterface && ClassInterface->hasDefinition())
1544 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001545 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001546 nameLoc, atStartLoc,
1547 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001548}
1549
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001550ObjCImplementationDecl *
1551ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1552 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1553 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1554 SourceLocation());
1555}
1556
John McCallda6d9762011-07-22 04:15:06 +00001557void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1558 CXXCtorInitializer ** initializers,
1559 unsigned numInitializers) {
1560 if (numInitializers > 0) {
1561 NumIvarInitializers = numInitializers;
1562 CXXCtorInitializer **ivarInitializers =
1563 new (C) CXXCtorInitializer*[NumIvarInitializers];
1564 memcpy(ivarInitializers, initializers,
1565 numInitializers * sizeof(CXXCtorInitializer*));
1566 IvarInitializers = ivarInitializers;
1567 }
1568}
1569
Chris Lattner5f9e2722011-07-23 10:55:15 +00001570raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001571 const ObjCImplementationDecl &ID) {
1572 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001573 return OS;
1574}
1575
Chris Lattnerab351632009-02-20 20:59:54 +00001576//===----------------------------------------------------------------------===//
1577// ObjCCompatibleAliasDecl
1578//===----------------------------------------------------------------------===//
1579
David Blaikie99ba9e32011-12-20 02:48:34 +00001580void ObjCCompatibleAliasDecl::anchor() { }
1581
Chris Lattnerab351632009-02-20 20:59:54 +00001582ObjCCompatibleAliasDecl *
1583ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1584 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001585 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001586 ObjCInterfaceDecl* AliasedClass) {
1587 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1588}
1589
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001590ObjCCompatibleAliasDecl *
1591ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1592 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1593 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1594}
1595
Chris Lattnerab351632009-02-20 20:59:54 +00001596//===----------------------------------------------------------------------===//
1597// ObjCPropertyDecl
1598//===----------------------------------------------------------------------===//
1599
David Blaikie99ba9e32011-12-20 02:48:34 +00001600void ObjCPropertyDecl::anchor() { }
1601
Chris Lattnerab351632009-02-20 20:59:54 +00001602ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1603 SourceLocation L,
1604 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001605 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001606 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001607 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001608 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001609 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001610}
1611
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001612ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1613 unsigned ID) {
1614 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1615 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001616 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001617 0);
1618}
1619
Chris Lattnerab351632009-02-20 20:59:54 +00001620//===----------------------------------------------------------------------===//
1621// ObjCPropertyImplDecl
1622//===----------------------------------------------------------------------===//
1623
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001624ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001625 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001626 SourceLocation atLoc,
1627 SourceLocation L,
1628 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001629 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001630 ObjCIvarDecl *ivar,
1631 SourceLocation ivarLoc) {
1632 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1633 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001634}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001635
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001636ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1637 unsigned ID) {
1638 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1639 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1640 0, Dynamic, 0, SourceLocation());
1641}
1642
Douglas Gregora4ffd852010-11-17 01:03:52 +00001643SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1644 SourceLocation EndLoc = getLocation();
1645 if (IvarLoc.isValid())
1646 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001647
Douglas Gregora4ffd852010-11-17 01:03:52 +00001648 return SourceRange(AtLoc, EndLoc);
1649}