blob: 97d0870c4b68410568b79a611946cb02c5720e01 [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 {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +000069 // If this context is a hidden protocol definition, don't find any
70 // methods there.
71 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
72 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
73 if (Def->isHidden())
74 return 0;
75 }
76
Steve Naroff0de21fd2009-02-22 19:35:57 +000077 // Since instance & class methods can have the same name, the loop below
78 // ensures we get the correct method.
79 //
80 // @interface Whatever
81 // - (int) class_method;
82 // + (float) class_method;
83 // @end
84 //
David Blaikie3bc93e32012-12-19 00:45:41 +000085 lookup_const_result R = lookup(Sel);
86 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
87 Meth != MethEnd; ++Meth) {
Steve Naroff0de21fd2009-02-22 19:35:57 +000088 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000089 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000090 return MD;
91 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000092 return 0;
93}
94
Ted Kremenek9f550ff2010-03-15 20:11:46 +000095ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +000096ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +000097 IdentifierInfo *propertyID) {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +000098 // If this context is a hidden protocol definition, don't find any
99 // property.
100 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
101 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
102 if (Def->isHidden())
103 return 0;
104 }
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000105
David Blaikie3bc93e32012-12-19 00:45:41 +0000106 DeclContext::lookup_const_result R = DC->lookup(propertyID);
107 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
108 ++I)
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000109 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
110 return PD;
111
112 return 0;
113}
114
Anna Zaksad0ce532012-09-27 19:45:11 +0000115IdentifierInfo *
116ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
117 SmallString<128> ivarName;
118 {
119 llvm::raw_svector_ostream os(ivarName);
120 os << '_' << getIdentifier()->getName();
121 }
122 return &Ctx.Idents.get(ivarName.str());
123}
124
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000125/// FindPropertyDeclaration - Finds declaration of the property given its name
126/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000127ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000128ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000129 // Don't find properties within hidden protocol definitions.
130 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
131 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
132 if (Def->isHidden())
133 return 0;
134 }
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000136 if (ObjCPropertyDecl *PD =
137 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
138 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000140 switch (getKind()) {
141 default:
142 break;
143 case Decl::ObjCProtocol: {
144 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
145 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
146 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian25760612010-02-15 21:55:26 +0000147 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
148 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000149 break;
150 }
151 case Decl::ObjCInterface: {
152 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregord3297242013-01-16 23:00:23 +0000153 // Look through categories (but not extensions).
154 for (ObjCInterfaceDecl::visible_categories_iterator
155 Cat = OID->visible_categories_begin(),
156 CatEnd = OID->visible_categories_end();
157 Cat != CatEnd; ++Cat) {
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000158 if (!Cat->IsClassExtension())
159 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
160 return P;
Douglas Gregord3297242013-01-16 23:00:23 +0000161 }
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000162
163 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000164 for (ObjCInterfaceDecl::all_protocol_iterator
165 I = OID->all_referenced_protocol_begin(),
166 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000167 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
168 return P;
169
170 // Finally, check the super class.
171 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
172 return superClass->FindPropertyDeclaration(PropertyId);
173 break;
174 }
175 case Decl::ObjCCategory: {
176 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
177 // Look through protocols.
178 if (!OCD->IsClassExtension())
179 for (ObjCCategoryDecl::protocol_iterator
180 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
181 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
182 return P;
183
184 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000185 }
186 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000187 return 0;
188}
189
David Blaikie99ba9e32011-12-20 02:48:34 +0000190void ObjCInterfaceDecl::anchor() { }
191
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000192/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
193/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000194/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000195///
196ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000197ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000198 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000199 // FIXME: Should make sure no callers ever do this.
200 if (!hasDefinition())
201 return 0;
202
203 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000204 LoadExternalDefinition();
205
Ted Kremenek37cafb02010-03-15 20:30:07 +0000206 if (ObjCPropertyDecl *PD =
207 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
208 return PD;
209
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000210 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000211 for (ObjCInterfaceDecl::all_protocol_iterator
212 I = all_referenced_protocol_begin(),
213 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000214 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
215 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000216
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000217 return 0;
218}
219
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000220void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
221 PropertyDeclOrder &PO) const {
Anna Zaksb36ea372012-10-18 19:17:53 +0000222 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
223 E = prop_end(); P != E; ++P) {
224 ObjCPropertyDecl *Prop = *P;
225 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000226 PO.push_back(Prop);
Anna Zaksb36ea372012-10-18 19:17:53 +0000227 }
228 for (ObjCInterfaceDecl::all_protocol_iterator
229 PI = all_referenced_protocol_begin(),
230 E = all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000231 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zakse63aedd2012-10-31 01:18:22 +0000232 // Note, the properties declared only in class extensions are still copied
233 // into the main @interface's property list, and therefore we don't
234 // explicitly, have to search class extension properties.
Anna Zaksb36ea372012-10-18 19:17:53 +0000235}
236
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000237bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
238 const ObjCInterfaceDecl *Class = this;
239 while (Class) {
240 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
241 return true;
242 Class = Class->getSuperClass();
243 }
244 return false;
245}
246
247const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
248 const ObjCInterfaceDecl *Class = this;
249 while (Class) {
250 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
251 return Class;
252 Class = Class->getSuperClass();
253 }
254 return 0;
255}
256
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000257void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
258 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
259 ASTContext &C)
260{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000261 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000262 LoadExternalDefinition();
263
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000264 if (data().AllReferencedProtocols.empty() &&
265 data().ReferencedProtocols.empty()) {
266 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000267 return;
268 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000269
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000270 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000271 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000272 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000273 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000274 for (unsigned i = 0; i < ExtNum; i++) {
275 bool protocolExists = false;
276 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek53b94412010-09-01 01:21:15 +0000277 for (all_protocol_iterator
278 p = all_referenced_protocol_begin(),
279 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000280 ObjCProtocolDecl *Proto = (*p);
281 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
282 protocolExists = true;
283 break;
284 }
285 }
286 // Do we want to warn on a protocol in extension class which
287 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000288 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000289 ProtocolRefs.push_back(ProtoInExtension);
290 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000291
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000292 if (ProtocolRefs.empty())
293 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000294
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000295 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek53b94412010-09-01 01:21:15 +0000296 for (all_protocol_iterator p = all_referenced_protocol_begin(),
297 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000298 ProtocolRefs.push_back(*p);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000299 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000300
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000301 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000302}
303
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000304void ObjCInterfaceDecl::allocateDefinitionData() {
305 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor6bd99292013-02-09 01:35:03 +0000306 Data.setPointer(new (getASTContext()) DefinitionData());
307 Data.getPointer()->Definition = this;
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000308
309 // Make the type point at the definition, now that we have one.
310 if (TypeForDecl)
311 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000312}
313
314void ObjCInterfaceDecl::startDefinition() {
315 allocateDefinitionData();
316
Douglas Gregor53df7a12011-12-15 18:03:09 +0000317 // Update all of the declarations with a pointer to the definition.
318 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
319 RD != RDEnd; ++RD) {
320 if (*RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000321 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000322 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000323}
324
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000325ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
326 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000327 // FIXME: Should make sure no callers ever do this.
328 if (!hasDefinition())
329 return 0;
330
331 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000332 LoadExternalDefinition();
333
Chris Lattner1e03a562008-03-16 00:19:01 +0000334 ObjCInterfaceDecl* ClassDecl = this;
335 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000336 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000337 clsDeclared = ClassDecl;
338 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000339 }
Douglas Gregord3297242013-01-16 23:00:23 +0000340
341 for (ObjCInterfaceDecl::visible_extensions_iterator
342 Ext = ClassDecl->visible_extensions_begin(),
343 ExtEnd = ClassDecl->visible_extensions_end();
344 Ext != ExtEnd; ++Ext) {
345 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000346 clsDeclared = ClassDecl;
347 return I;
348 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000349 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000350
Chris Lattner1e03a562008-03-16 00:19:01 +0000351 ClassDecl = ClassDecl->getSuperClass();
352 }
353 return NULL;
354}
355
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000356/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
357/// class whose name is passed as argument. If it is not one of the super classes
358/// the it returns NULL.
359ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
360 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000361 // FIXME: Should make sure no callers ever do this.
362 if (!hasDefinition())
363 return 0;
364
365 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000366 LoadExternalDefinition();
367
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000368 ObjCInterfaceDecl* ClassDecl = this;
369 while (ClassDecl != NULL) {
370 if (ClassDecl->getIdentifier() == ICName)
371 return ClassDecl;
372 ClassDecl = ClassDecl->getSuperClass();
373 }
374 return NULL;
375}
376
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000377/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000378/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000379ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
380 bool isInstance,
381 bool shallowCategoryLookup) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000382 // FIXME: Should make sure no callers ever do this.
383 if (!hasDefinition())
384 return 0;
385
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000386 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000387 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000389 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000390 LoadExternalDefinition();
391
Chris Lattner1e03a562008-03-16 00:19:01 +0000392 while (ClassDecl != NULL) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000393 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000394 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattner1e03a562008-03-16 00:19:01 +0000396 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +0000397 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
398 E = ClassDecl->protocol_end();
399 I != E; ++I)
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000400 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000401 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000402
403 // Didn't find one yet - now look through categories.
Douglas Gregord3297242013-01-16 23:00:23 +0000404 for (ObjCInterfaceDecl::visible_categories_iterator
405 Cat = ClassDecl->visible_categories_begin(),
406 CatEnd = ClassDecl->visible_categories_end();
407 Cat != CatEnd; ++Cat) {
408 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000409 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000410
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000411 if (!shallowCategoryLookup) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000412 // Didn't find one yet - look through protocols.
413 const ObjCList<ObjCProtocolDecl> &Protocols =
Douglas Gregord3297242013-01-16 23:00:23 +0000414 Cat->getReferencedProtocols();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000415 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
416 E = Protocols.end(); I != E; ++I)
417 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
418 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000419 }
Chris Lattner1e03a562008-03-16 00:19:01 +0000420 }
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000421
Chris Lattner1e03a562008-03-16 00:19:01 +0000422 ClassDecl = ClassDecl->getSuperClass();
423 }
424 return NULL;
425}
426
Anna Zakse61354b2012-07-27 19:07:44 +0000427// Will search "local" class/category implementations for a method decl.
428// If failed, then we search in class's root for an instance method.
429// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000430ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
431 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000432 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000433 // FIXME: Should make sure no callers ever do this.
434 if (!hasDefinition())
435 return 0;
436
437 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000438 LoadExternalDefinition();
439
Steve Naroffd789d3d2009-10-01 23:46:04 +0000440 ObjCMethodDecl *Method = 0;
441 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000442 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
443 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000444
445 // Look through local category implementations associated with the class.
446 if (!Method)
447 Method = Instance ? getCategoryInstanceMethod(Sel)
448 : getCategoryClassMethod(Sel);
449
450 // Before we give up, check if the selector is an instance method.
451 // But only in the root. This matches gcc's behavior and what the
452 // runtime expects.
453 if (!Instance && !Method && !getSuperClass()) {
454 Method = lookupInstanceMethod(Sel);
455 // Look through local category implementations associated
456 // with the root class.
457 if (!Method)
458 Method = lookupPrivateMethod(Sel, true);
459 }
460
Steve Naroffd789d3d2009-10-01 23:46:04 +0000461 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000462 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000463 return Method;
464}
Chris Lattnerab351632009-02-20 20:59:54 +0000465
466//===----------------------------------------------------------------------===//
467// ObjCMethodDecl
468//===----------------------------------------------------------------------===//
469
470ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000471 SourceLocation beginLoc,
Chris Lattnerab351632009-02-20 20:59:54 +0000472 SourceLocation endLoc,
473 Selector SelInfo, QualType T,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000474 TypeSourceInfo *ResultTInfo,
Chris Lattnerab351632009-02-20 20:59:54 +0000475 DeclContext *contextDecl,
476 bool isInstance,
477 bool isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000478 bool isPropertyAccessor,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +0000479 bool isImplicitlyDeclared,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000480 bool isDefined,
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000481 ImplementationControl impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000482 bool HasRelatedResultType) {
Chris Lattnerab351632009-02-20 20:59:54 +0000483 return new (C) ObjCMethodDecl(beginLoc, endLoc,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000484 SelInfo, T, ResultTInfo, contextDecl,
Jordan Rose1e4691b2012-10-10 16:42:25 +0000485 isInstance, isVariadic, isPropertyAccessor,
486 isImplicitlyDeclared, isDefined,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000487 impControl,
Argyrios Kyrtzidisda92a7f2011-10-03 06:36:29 +0000488 HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000489}
490
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000491ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
492 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
493 return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
494 Selector(), QualType(), 0, 0);
495}
496
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000497Stmt *ObjCMethodDecl::getBody() const {
498 return Body.get(getASTContext().getExternalSource());
499}
500
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000501void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
502 assert(PrevMethod);
503 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
504 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000505 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000506}
507
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000508void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
509 ArrayRef<ParmVarDecl*> Params,
510 ArrayRef<SourceLocation> SelLocs) {
511 ParamsAndSelLocs = 0;
512 NumParams = Params.size();
513 if (Params.empty() && SelLocs.empty())
514 return;
515
516 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
517 sizeof(SourceLocation) * SelLocs.size();
518 ParamsAndSelLocs = C.Allocate(Size);
519 std::copy(Params.begin(), Params.end(), getParams());
520 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
521}
522
523void ObjCMethodDecl::getSelectorLocs(
524 SmallVectorImpl<SourceLocation> &SelLocs) const {
525 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
526 SelLocs.push_back(getSelectorLoc(i));
527}
528
529void ObjCMethodDecl::setMethodParams(ASTContext &C,
530 ArrayRef<ParmVarDecl*> Params,
531 ArrayRef<SourceLocation> SelLocs) {
532 assert((!SelLocs.empty() || isImplicit()) &&
533 "No selector locs for non-implicit method");
534 if (isImplicit())
535 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
536
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000537 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
538 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000539 if (SelLocsKind != SelLoc_NonStandard)
540 return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
541
542 setParamsAndSelLocs(C, Params, SelLocs);
543}
544
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000545/// \brief A definition will return its interface declaration.
546/// An interface declaration will return its definition.
547/// Otherwise it will return itself.
548ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
549 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000550 ObjCMethodDecl *Redecl = 0;
551 if (HasRedeclaration)
552 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000553 if (Redecl)
554 return Redecl;
555
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000556 Decl *CtxD = cast<Decl>(getDeclContext());
557
558 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
559 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
560 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
561
562 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
563 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
564 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
565
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000566 } else if (ObjCImplementationDecl *ImplD =
567 dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000568 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
569 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000570
571 } else if (ObjCCategoryImplDecl *CImplD =
572 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000573 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000574 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000575 }
576
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000577 if (!Redecl && isRedeclaration()) {
578 // This is the last redeclaration, go back to the first method.
579 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
580 isInstanceMethod());
581 }
582
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000583 return Redecl ? Redecl : this;
584}
585
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000586ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
587 Decl *CtxD = cast<Decl>(getDeclContext());
588
589 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
590 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
591 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
592 isInstanceMethod()))
593 return MD;
594
595 } else if (ObjCCategoryImplDecl *CImplD =
596 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000597 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000598 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
599 isInstanceMethod()))
600 return MD;
601 }
602
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000603 if (isRedeclaration())
604 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
605 isInstanceMethod());
606
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000607 return this;
608}
609
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000610SourceLocation ObjCMethodDecl::getLocEnd() const {
611 if (Stmt *Body = getBody())
612 return Body->getLocEnd();
613 return DeclEndLoc;
614}
615
John McCall85f3d762011-03-02 01:50:55 +0000616ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
617 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000618 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000619 return family;
620
John McCalld5313b02011-03-02 11:33:24 +0000621 // Check for an explicit attribute.
622 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
623 // The unfortunate necessity of mapping between enums here is due
624 // to the attributes framework.
625 switch (attr->getFamily()) {
626 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
627 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
628 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
629 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
630 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
631 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
632 }
633 Family = static_cast<unsigned>(family);
634 return family;
635 }
636
John McCall85f3d762011-03-02 01:50:55 +0000637 family = getSelector().getMethodFamily();
638 switch (family) {
639 case OMF_None: break;
640
641 // init only has a conventional meaning for an instance method, and
642 // it has to return an object.
643 case OMF_init:
644 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
645 family = OMF_None;
646 break;
647
648 // alloc/copy/new have a conventional meaning for both class and
649 // instance methods, but they require an object return.
650 case OMF_alloc:
651 case OMF_copy:
652 case OMF_mutableCopy:
653 case OMF_new:
654 if (!getResultType()->isObjCObjectPointerType())
655 family = OMF_None;
656 break;
657
658 // These selectors have a conventional meaning only for instance methods.
659 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000660 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000661 case OMF_retain:
662 case OMF_release:
663 case OMF_autorelease:
664 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000665 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000666 if (!isInstanceMethod())
667 family = OMF_None;
668 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000669
670 case OMF_performSelector:
671 if (!isInstanceMethod() ||
672 !getResultType()->isObjCIdType())
673 family = OMF_None;
674 else {
675 unsigned noParams = param_size();
676 if (noParams < 1 || noParams > 3)
677 family = OMF_None;
678 else {
679 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
680 QualType ArgT = (*it);
681 if (!ArgT->isObjCSelType()) {
682 family = OMF_None;
683 break;
684 }
685 while (--noParams) {
686 it++;
687 ArgT = (*it);
688 if (!ArgT->isObjCIdType()) {
689 family = OMF_None;
690 break;
691 }
692 }
693 }
694 }
695 break;
696
John McCall85f3d762011-03-02 01:50:55 +0000697 }
698
699 // Cache the result.
700 Family = static_cast<unsigned>(family);
701 return family;
702}
703
Mike Stump1eb44332009-09-09 15:08:12 +0000704void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000705 const ObjCInterfaceDecl *OID) {
706 QualType selfTy;
707 if (isInstanceMethod()) {
708 // There may be no interface context due to error in declaration
709 // of the interface (which has been reported). Recover gracefully.
710 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000711 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000712 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000713 } else {
714 selfTy = Context.getObjCIdType();
715 }
716 } else // we have a factory method.
717 selfTy = Context.getObjCClassType();
718
John McCall7acddac2011-06-17 06:42:21 +0000719 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000720 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000721
David Blaikie4e4d0842012-03-11 07:00:24 +0000722 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000723 if (isInstanceMethod()) {
724 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000725
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000726 // 'self' is always __strong. It's actually pseudo-strong except
727 // in init methods (or methods labeled ns_consumes_self), though.
728 Qualifiers qs;
729 qs.setObjCLifetime(Qualifiers::OCL_Strong);
730 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000731
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000732 // In addition, 'self' is const unless this is an init method.
733 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
734 selfTy = selfTy.withConst();
735 selfIsPseudoStrong = true;
736 }
737 }
738 else {
739 assert(isClassMethod());
740 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000741 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000742 selfIsPseudoStrong = true;
743 }
John McCallf85e1932011-06-15 23:02:42 +0000744 }
745
746 ImplicitParamDecl *self
747 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
748 &Context.Idents.get("self"), selfTy);
749 setSelfDecl(self);
750
751 if (selfIsConsumed)
752 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000753
John McCall7acddac2011-06-17 06:42:21 +0000754 if (selfIsPseudoStrong)
755 self->setARCPseudoStrong(true);
756
Mike Stump1eb44332009-09-09 15:08:12 +0000757 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
758 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000759 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000760}
761
Chris Lattnerab351632009-02-20 20:59:54 +0000762ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
763 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
764 return ID;
765 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
766 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000767 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000768 return IMD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000769
770 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikieb219cfc2011-09-23 05:06:16 +0000771 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000772}
773
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000774static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
775 const ObjCMethodDecl *Method,
776 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
777 bool MovedToSuper) {
778 if (!Container)
779 return;
780
781 // In categories look for overriden methods from protocols. A method from
782 // category is not "overriden" since it is considered as the "same" method
783 // (same USR) as the one from the interface.
784 if (const ObjCCategoryDecl *
785 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
786 // Check whether we have a matching method at this category but only if we
787 // are at the super class level.
788 if (MovedToSuper)
789 if (ObjCMethodDecl *
790 Overridden = Container->getMethod(Method->getSelector(),
791 Method->isInstanceMethod()))
792 if (Method != Overridden) {
793 // We found an override at this category; there is no need to look
794 // into its protocols.
795 Methods.push_back(Overridden);
796 return;
797 }
798
799 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
800 PEnd = Category->protocol_end();
801 P != PEnd; ++P)
802 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
803 return;
804 }
805
806 // Check whether we have a matching method at this level.
807 if (const ObjCMethodDecl *
808 Overridden = Container->getMethod(Method->getSelector(),
809 Method->isInstanceMethod()))
810 if (Method != Overridden) {
811 // We found an override at this level; there is no need to look
812 // into other protocols or categories.
813 Methods.push_back(Overridden);
814 return;
815 }
816
817 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
818 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
819 PEnd = Protocol->protocol_end();
820 P != PEnd; ++P)
821 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
822 }
823
824 if (const ObjCInterfaceDecl *
825 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
826 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
827 PEnd = Interface->protocol_end();
828 P != PEnd; ++P)
829 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
830
Douglas Gregord3297242013-01-16 23:00:23 +0000831 for (ObjCInterfaceDecl::visible_categories_iterator
832 Cat = Interface->visible_categories_begin(),
833 CatEnd = Interface->visible_categories_end();
834 Cat != CatEnd; ++Cat) {
835 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000836 MovedToSuper);
Douglas Gregord3297242013-01-16 23:00:23 +0000837 }
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000838
839 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
840 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
841 /*MovedToSuper=*/true);
842 }
843}
844
845static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
846 const ObjCMethodDecl *Method,
847 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
848 CollectOverriddenMethodsRecurse(Container, Method, Methods,
849 /*MovedToSuper=*/false);
850}
851
852static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
853 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
854 assert(Method->isOverriding());
855
856 if (const ObjCProtocolDecl *
857 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
858 CollectOverriddenMethods(ProtD, Method, overridden);
859
860 } else if (const ObjCImplDecl *
861 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
862 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
863 if (!ID)
864 return;
865 // Start searching for overridden methods using the method from the
866 // interface as starting point.
867 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
868 Method->isInstanceMethod()))
869 Method = IFaceMeth;
870 CollectOverriddenMethods(ID, Method, overridden);
871
872 } else if (const ObjCCategoryDecl *
873 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
874 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
875 if (!ID)
876 return;
877 // Start searching for overridden methods using the method from the
878 // interface as starting point.
879 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
880 Method->isInstanceMethod()))
881 Method = IFaceMeth;
882 CollectOverriddenMethods(ID, Method, overridden);
883
884 } else {
885 CollectOverriddenMethods(
886 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
887 Method, overridden);
888 }
889}
890
891static void collectOnCategoriesAfterLocation(SourceLocation Loc,
892 const ObjCInterfaceDecl *Class,
893 SourceManager &SM,
894 const ObjCMethodDecl *Method,
895 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
896 if (!Class)
897 return;
898
Douglas Gregord3297242013-01-16 23:00:23 +0000899 for (ObjCInterfaceDecl::visible_categories_iterator
900 Cat = Class->visible_categories_begin(),
901 CatEnd = Class->visible_categories_end();
902 Cat != CatEnd; ++Cat) {
903 if (SM.isBeforeInTranslationUnit(Loc, Cat->getLocation()))
904 CollectOverriddenMethodsRecurse(*Cat, Method, Methods, true);
905 }
906
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000907 collectOnCategoriesAfterLocation(Loc, Class->getSuperClass(), SM,
908 Method, Methods);
909}
910
911/// \brief Faster collection that is enabled when ObjCMethodDecl::isOverriding()
912/// returns false.
913/// You'd think that in that case there are no overrides but categories can
914/// "introduce" new overridden methods that are missed by Sema because the
915/// overrides lookup that it does for methods, inside implementations, will
916/// stop at the interface level (if there is a method there) and not look
917/// further in super classes.
Fariborz Jahanianbf967be2012-10-10 18:34:52 +0000918/// Methods in an implementation can overide methods in super class's category
919/// but not in current class's category. But, such methods
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000920static void collectOverriddenMethodsFast(SourceManager &SM,
921 const ObjCMethodDecl *Method,
922 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
923 assert(!Method->isOverriding());
924
925 const ObjCContainerDecl *
926 ContD = cast<ObjCContainerDecl>(Method->getDeclContext());
927 if (isa<ObjCInterfaceDecl>(ContD) || isa<ObjCProtocolDecl>(ContD))
928 return;
929 const ObjCInterfaceDecl *Class = Method->getClassInterface();
930 if (!Class)
931 return;
932
933 collectOnCategoriesAfterLocation(Class->getLocation(), Class->getSuperClass(),
934 SM, Method, Methods);
935}
936
937void ObjCMethodDecl::getOverriddenMethods(
938 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
939 const ObjCMethodDecl *Method = this;
940
941 if (Method->isRedeclaration()) {
942 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
943 getMethod(Method->getSelector(), Method->isInstanceMethod());
944 }
945
946 if (!Method->isOverriding()) {
947 collectOverriddenMethodsFast(getASTContext().getSourceManager(),
948 Method, Overridden);
949 } else {
950 collectOverriddenMethodsSlow(Method, Overridden);
951 assert(!Overridden.empty() &&
952 "ObjCMethodDecl's overriding bit is not as expected");
953 }
954}
955
Jordan Rose04bec392012-10-10 16:42:54 +0000956const ObjCPropertyDecl *
957ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
958 Selector Sel = getSelector();
959 unsigned NumArgs = Sel.getNumArgs();
960 if (NumArgs > 1)
961 return 0;
962
Jordan Rose50d2b262012-10-11 16:02:02 +0000963 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose04bec392012-10-10 16:42:54 +0000964 return 0;
965
966 if (isPropertyAccessor()) {
967 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanianc328d9c2013-01-12 00:28:34 +0000968 // If container is class extension, find its primary class.
969 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
970 if (CatDecl->IsClassExtension())
971 Container = CatDecl->getClassInterface();
972
Jordan Rose04bec392012-10-10 16:42:54 +0000973 bool IsGetter = (NumArgs == 0);
974
975 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
976 E = Container->prop_end();
977 I != E; ++I) {
978 Selector NextSel = IsGetter ? (*I)->getGetterName()
979 : (*I)->getSetterName();
980 if (NextSel == Sel)
981 return *I;
982 }
983
984 llvm_unreachable("Marked as a property accessor but no property found!");
985 }
986
987 if (!CheckOverrides)
988 return 0;
989
990 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
991 OverridesTy Overrides;
992 getOverriddenMethods(Overrides);
993 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
994 I != E; ++I) {
995 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
996 return Prop;
997 }
998
999 return 0;
1000
1001}
1002
Chris Lattnerab351632009-02-20 20:59:54 +00001003//===----------------------------------------------------------------------===//
1004// ObjCInterfaceDecl
1005//===----------------------------------------------------------------------===//
1006
Douglas Gregora6ea10e2012-01-17 18:09:05 +00001007ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +00001008 DeclContext *DC,
1009 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001010 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +00001011 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +00001012 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +00001013 bool isInternal){
Douglas Gregor0af55012011-12-16 03:12:41 +00001014 ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001015 PrevDecl, isInternal);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001016 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor0af55012011-12-16 03:12:41 +00001017 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +00001018 return Result;
1019}
1020
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001021ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
1022 unsigned ID) {
1023 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00001024 ObjCInterfaceDecl *Result = new (Mem) ObjCInterfaceDecl(0, SourceLocation(),
1025 0, SourceLocation(),
1026 0, false);
1027 Result->Data.setInt(!C.getLangOpts().Modules);
1028 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001029}
1030
1031ObjCInterfaceDecl::
1032ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001033 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1034 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001035 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +00001036 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001037{
Douglas Gregorfd002a72011-12-16 22:37:11 +00001038 setPreviousDeclaration(PrevDecl);
1039
1040 // Copy the 'data' pointer over.
1041 if (PrevDecl)
1042 Data = PrevDecl->Data;
1043
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +00001044 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +00001045}
1046
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001047void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001048 assert(data().ExternallyCompleted && "Class is not externally completed");
1049 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001050 getASTContext().getExternalSource()->CompleteType(
1051 const_cast<ObjCInterfaceDecl *>(this));
1052}
1053
1054void ObjCInterfaceDecl::setExternallyCompleted() {
1055 assert(getASTContext().getExternalSource() &&
1056 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001057 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001058 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001059 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001060}
1061
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001062ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001063 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1064 if (data().ExternallyCompleted)
1065 LoadExternalDefinition();
1066
1067 return getASTContext().getObjCImplementation(
1068 const_cast<ObjCInterfaceDecl*>(Def));
1069 }
1070
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001071 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +00001072 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001073}
1074
1075void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001076 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001077}
1078
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001079namespace {
1080 struct SynthesizeIvarChunk {
1081 uint64_t Size;
1082 ObjCIvarDecl *Ivar;
1083 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1084 : Size(size), Ivar(ivar) {}
1085 };
1086
1087 bool operator<(const SynthesizeIvarChunk & LHS,
1088 const SynthesizeIvarChunk &RHS) {
1089 return LHS.Size < RHS.Size;
1090 }
1091}
1092
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001093/// all_declared_ivar_begin - return first ivar declared in this class,
1094/// its extensions and its implementation. Lazily build the list on first
1095/// access.
Adrian Prantl4919de62013-03-06 22:03:30 +00001096///
1097/// Caveat: The list returned by this method reflects the current
1098/// state of the parser. The cache will be updated for every ivar
1099/// added by an extension or the implementation when they are
1100/// encountered.
1101/// See also ObjCIvarDecl::Create().
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001102ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001103 // FIXME: Should make sure no callers ever do this.
1104 if (!hasDefinition())
1105 return 0;
1106
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001107 ObjCIvarDecl *curIvar = 0;
Adrian Prantl4919de62013-03-06 22:03:30 +00001108 if (!data().IvarList) {
1109 if (!ivar_empty()) {
1110 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1111 data().IvarList = *I; ++I;
1112 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl10b4df72013-02-27 01:31:55 +00001113 curIvar->setNextIvar(*I);
1114 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001115
1116 for (ObjCInterfaceDecl::known_extensions_iterator
1117 Ext = known_extensions_begin(),
1118 ExtEnd = known_extensions_end();
1119 Ext != ExtEnd; ++Ext) {
1120 if (!Ext->ivar_empty()) {
1121 ObjCCategoryDecl::ivar_iterator
1122 I = Ext->ivar_begin(),
1123 E = Ext->ivar_end();
1124 if (!data().IvarList) {
1125 data().IvarList = *I; ++I;
1126 curIvar = data().IvarList;
1127 }
1128 for ( ;I != E; curIvar = *I, ++I)
1129 curIvar->setNextIvar(*I);
1130 }
1131 }
1132 data().IvarListMissingImplementation = true;
Adrian Prantl10b4df72013-02-27 01:31:55 +00001133 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001134
1135 // cached and complete!
1136 if (!data().IvarListMissingImplementation)
1137 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001138
1139 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantl4919de62013-03-06 22:03:30 +00001140 data().IvarListMissingImplementation = false;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001141 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001142 SmallVector<SynthesizeIvarChunk, 16> layout;
1143 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1144 E = ImplDecl->ivar_end(); I != E; ++I) {
1145 ObjCIvarDecl *IV = *I;
1146 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1147 layout.push_back(SynthesizeIvarChunk(
1148 IV->getASTContext().getTypeSize(IV->getType()), IV));
1149 continue;
1150 }
1151 if (!data().IvarList)
1152 data().IvarList = *I;
1153 else
1154 curIvar->setNextIvar(*I);
1155 curIvar = *I;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001156 }
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001157
1158 if (!layout.empty()) {
1159 // Order synthesized ivars by their size.
1160 std::stable_sort(layout.begin(), layout.end());
1161 unsigned Ix = 0, EIx = layout.size();
1162 if (!data().IvarList) {
1163 data().IvarList = layout[0].Ivar; Ix++;
1164 curIvar = data().IvarList;
1165 }
1166 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1167 curIvar->setNextIvar(layout[Ix].Ivar);
1168 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001169 }
1170 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001171 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001172}
Chris Lattnerab351632009-02-20 20:59:54 +00001173
1174/// FindCategoryDeclaration - Finds category declaration in the list of
1175/// categories for this class and returns it. Name of the category is passed
1176/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001177///
Chris Lattnerab351632009-02-20 20:59:54 +00001178ObjCCategoryDecl *
1179ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001180 // FIXME: Should make sure no callers ever do this.
1181 if (!hasDefinition())
1182 return 0;
1183
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001184 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001185 LoadExternalDefinition();
1186
Douglas Gregord3297242013-01-16 23:00:23 +00001187 for (visible_categories_iterator Cat = visible_categories_begin(),
1188 CatEnd = visible_categories_end();
1189 Cat != CatEnd;
1190 ++Cat) {
1191 if (Cat->getIdentifier() == CategoryId)
1192 return *Cat;
1193 }
1194
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001195 return 0;
1196}
1197
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001198ObjCMethodDecl *
1199ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregord3297242013-01-16 23:00:23 +00001200 for (visible_categories_iterator Cat = visible_categories_begin(),
1201 CatEnd = visible_categories_end();
1202 Cat != CatEnd;
1203 ++Cat) {
1204 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001205 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1206 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001207 }
1208
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001209 return 0;
1210}
1211
1212ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregord3297242013-01-16 23:00:23 +00001213 for (visible_categories_iterator Cat = visible_categories_begin(),
1214 CatEnd = visible_categories_end();
1215 Cat != CatEnd;
1216 ++Cat) {
1217 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001218 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1219 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001220 }
1221
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001222 return 0;
1223}
1224
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001225/// ClassImplementsProtocol - Checks that 'lProto' protocol
1226/// has been implemented in IDecl class, its super class or categories (if
1227/// lookupCategory is true).
1228bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1229 bool lookupCategory,
1230 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001231 if (!hasDefinition())
1232 return false;
1233
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001234 ObjCInterfaceDecl *IDecl = this;
1235 // 1st, look up the class.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00001236 for (ObjCInterfaceDecl::protocol_iterator
1237 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001238 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1239 return true;
1240 // This is dubious and is added to be compatible with gcc. In gcc, it is
1241 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1242 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1243 // object. This IMO, should be a bug.
1244 // FIXME: Treat this as an extension, and flag this as an error when GCC
1245 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001246 if (RHSIsQualifiedID &&
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001247 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1248 return true;
1249 }
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001251 // 2nd, look up the category.
1252 if (lookupCategory)
Douglas Gregord3297242013-01-16 23:00:23 +00001253 for (visible_categories_iterator Cat = visible_categories_begin(),
1254 CatEnd = visible_categories_end();
1255 Cat != CatEnd;
1256 ++Cat) {
1257 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1258 E = Cat->protocol_end();
1259 PI != E; ++PI)
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001260 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1261 return true;
1262 }
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001264 // 3rd, look up the super class(s)
1265 if (IDecl->getSuperClass())
1266 return
1267 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1268 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001270 return false;
1271}
1272
Chris Lattnerab351632009-02-20 20:59:54 +00001273//===----------------------------------------------------------------------===//
1274// ObjCIvarDecl
1275//===----------------------------------------------------------------------===//
1276
David Blaikie99ba9e32011-12-20 02:48:34 +00001277void ObjCIvarDecl::anchor() { }
1278
Daniel Dunbara0654922010-04-02 20:10:03 +00001279ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001280 SourceLocation StartLoc,
1281 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001282 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001283 AccessControl ac, Expr *BW,
1284 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001285 if (DC) {
1286 // Ivar's can only appear in interfaces, implementations (via synthesized
1287 // properties), and class extensions (via direct declaration, or synthesized
1288 // properties).
1289 //
1290 // FIXME: This should really be asserting this:
1291 // (isa<ObjCCategoryDecl>(DC) &&
1292 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1293 // but unfortunately we sometimes place ivars into non-class extension
1294 // categories on error. This breaks an AST invariant, and should not be
1295 // fixed.
1296 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1297 isa<ObjCCategoryDecl>(DC)) &&
1298 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001299 // Once a new ivar is created in any of class/class-extension/implementation
1300 // decl contexts, the previously built IvarList must be rebuilt.
1301 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1302 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001303 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001304 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001305 else
1306 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001307 }
1308 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001309 }
1310
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001311 return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1312 ac, BW, synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001313}
1314
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001315ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1316 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1317 return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1318 QualType(), 0, ObjCIvarDecl::None, 0, false);
1319}
1320
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001321const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1322 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001323
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001324 switch (DC->getKind()) {
1325 default:
1326 case ObjCCategoryImpl:
1327 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001328 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001329
1330 // Ivars can only appear in class extension categories.
1331 case ObjCCategory: {
1332 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1333 assert(CD->IsClassExtension() && "invalid container for ivar!");
1334 return CD->getClassInterface();
1335 }
1336
1337 case ObjCImplementation:
1338 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1339
1340 case ObjCInterface:
1341 return cast<ObjCInterfaceDecl>(DC);
1342 }
1343}
Chris Lattnerab351632009-02-20 20:59:54 +00001344
1345//===----------------------------------------------------------------------===//
1346// ObjCAtDefsFieldDecl
1347//===----------------------------------------------------------------------===//
1348
David Blaikie99ba9e32011-12-20 02:48:34 +00001349void ObjCAtDefsFieldDecl::anchor() { }
1350
Chris Lattnerab351632009-02-20 20:59:54 +00001351ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001352*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1353 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001354 IdentifierInfo *Id, QualType T, Expr *BW) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001355 return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001356}
1357
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001358ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1359 unsigned ID) {
1360 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1361 return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1362 0, QualType(), 0);
1363}
1364
Chris Lattnerab351632009-02-20 20:59:54 +00001365//===----------------------------------------------------------------------===//
1366// ObjCProtocolDecl
1367//===----------------------------------------------------------------------===//
1368
David Blaikie99ba9e32011-12-20 02:48:34 +00001369void ObjCProtocolDecl::anchor() { }
1370
Douglas Gregor27c6da22012-01-01 20:30:41 +00001371ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1372 SourceLocation nameLoc,
1373 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001374 ObjCProtocolDecl *PrevDecl)
1375 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001376{
1377 setPreviousDeclaration(PrevDecl);
1378 if (PrevDecl)
1379 Data = PrevDecl->Data;
1380}
1381
Chris Lattnerab351632009-02-20 20:59:54 +00001382ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001383 IdentifierInfo *Id,
1384 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001385 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001386 ObjCProtocolDecl *PrevDecl) {
Douglas Gregor27c6da22012-01-01 20:30:41 +00001387 ObjCProtocolDecl *Result
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001388 = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001389 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001390 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001391}
1392
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001393ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1394 unsigned ID) {
1395 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +00001396 ObjCProtocolDecl *Result = new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(),
1397 SourceLocation(), 0);
1398 Result->Data.setInt(!C.getLangOpts().Modules);
1399 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001400}
1401
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001402ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1403 ObjCProtocolDecl *PDecl = this;
1404
1405 if (Name == getIdentifier())
1406 return PDecl;
1407
1408 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1409 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1410 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001412 return NULL;
1413}
1414
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001415// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001416// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001417ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1418 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001419 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Douglas Gregor0f9b9f32013-01-17 00:38:46 +00001421 // If there is no definition or the definition is hidden, we don't find
1422 // anything.
1423 const ObjCProtocolDecl *Def = getDefinition();
1424 if (!Def || Def->isHidden())
1425 return NULL;
1426
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001427 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001428 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Chris Lattnerab351632009-02-20 20:59:54 +00001430 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001431 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001432 return MethodDecl;
1433 return NULL;
1434}
1435
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001436void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor6bd99292013-02-09 01:35:03 +00001437 assert(!Data.getPointer() && "Protocol already has a definition!");
1438 Data.setPointer(new (getASTContext()) DefinitionData);
1439 Data.getPointer()->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001440}
1441
1442void ObjCProtocolDecl::startDefinition() {
1443 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001444
1445 // Update all of the declarations with a pointer to the definition.
1446 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1447 RD != RDEnd; ++RD)
1448 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001449}
1450
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001451void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1452 PropertyDeclOrder &PO) const {
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001453
1454 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1455 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1456 E = PDecl->prop_end(); P != E; ++P) {
1457 ObjCPropertyDecl *Prop = *P;
1458 // Insert into PM if not there already.
1459 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001460 PO.push_back(Prop);
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001461 }
1462 // Scan through protocol's protocols.
1463 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1464 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001465 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaksb36ea372012-10-18 19:17:53 +00001466 }
Anna Zaksb36ea372012-10-18 19:17:53 +00001467}
1468
1469
Chris Lattnerab351632009-02-20 20:59:54 +00001470//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001471// ObjCCategoryDecl
1472//===----------------------------------------------------------------------===//
1473
David Blaikie99ba9e32011-12-20 02:48:34 +00001474void ObjCCategoryDecl::anchor() { }
1475
Chris Lattnerab351632009-02-20 20:59:54 +00001476ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001477 SourceLocation AtLoc,
1478 SourceLocation ClassNameLoc,
1479 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001480 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001481 ObjCInterfaceDecl *IDecl,
1482 SourceLocation IvarLBraceLoc,
1483 SourceLocation IvarRBraceLoc) {
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001484 ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1485 CategoryNameLoc, Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001486 IDecl,
1487 IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001488 if (IDecl) {
1489 // Link this category into its class's category list.
Douglas Gregord3297242013-01-16 23:00:23 +00001490 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001491 if (IDecl->hasDefinition()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001492 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001493 if (ASTMutationListener *L = C.getASTMutationListener())
1494 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1495 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001496 }
1497
1498 return CatDecl;
1499}
1500
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001501ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1502 unsigned ID) {
1503 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1504 return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1505 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001506}
1507
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001508ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1509 return getASTContext().getObjCImplementation(
1510 const_cast<ObjCCategoryDecl*>(this));
1511}
1512
1513void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1514 getASTContext().setObjCImplementation(this, ImplD);
1515}
1516
1517
Chris Lattnerab351632009-02-20 20:59:54 +00001518//===----------------------------------------------------------------------===//
1519// ObjCCategoryImplDecl
1520//===----------------------------------------------------------------------===//
1521
David Blaikie99ba9e32011-12-20 02:48:34 +00001522void ObjCCategoryImplDecl::anchor() { }
1523
Chris Lattnerab351632009-02-20 20:59:54 +00001524ObjCCategoryImplDecl *
1525ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001526 IdentifierInfo *Id,
1527 ObjCInterfaceDecl *ClassInterface,
1528 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001529 SourceLocation atStartLoc,
1530 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001531 if (ClassInterface && ClassInterface->hasDefinition())
1532 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001533 return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001534 nameLoc, atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001535}
1536
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001537ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1538 unsigned ID) {
1539 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1540 return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1541 SourceLocation(), SourceLocation());
1542}
1543
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001544ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001545 // The class interface might be NULL if we are working with invalid code.
1546 if (const ObjCInterfaceDecl *ID = getClassInterface())
1547 return ID->FindCategoryDeclaration(getIdentifier());
1548 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001549}
1550
Chris Lattnerab351632009-02-20 20:59:54 +00001551
David Blaikie99ba9e32011-12-20 02:48:34 +00001552void ObjCImplDecl::anchor() { }
1553
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001554void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001555 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001556 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001557 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001558}
1559
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001560void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1561 ASTContext &Ctx = getASTContext();
1562
1563 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001564 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001565 if (IFace)
1566 Ctx.setObjCImplementation(IFace, ImplD);
1567
Duncan Sands98f2cca2009-07-21 07:56:29 +00001568 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001569 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1570 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1571 Ctx.setObjCImplementation(CD, ImplD);
1572 }
1573
1574 ClassInterface = IFace;
1575}
1576
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001577/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian6d1cb5c2013-03-12 17:43:00 +00001578/// properties implemented in this \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001579/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001580///
Chris Lattner3aa18612009-02-28 18:42:10 +00001581ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001582FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1583 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001584 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001585 if (PID->getPropertyIvarDecl() &&
1586 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1587 return PID;
1588 }
1589 return 0;
1590}
1591
1592/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001593/// added to the list of those properties \@synthesized/\@dynamic in this
1594/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001595///
Chris Lattner3aa18612009-02-28 18:42:10 +00001596ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001597FindPropertyImplDecl(IdentifierInfo *Id) const {
1598 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie581deb32012-06-06 20:45:41 +00001599 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001600 if (PID->getPropertyDecl()->getIdentifier() == Id)
1601 return PID;
1602 }
1603 return 0;
1604}
1605
Chris Lattner5f9e2722011-07-23 10:55:15 +00001606raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001607 const ObjCCategoryImplDecl &CID) {
1608 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001609 return OS;
1610}
1611
Chris Lattnerab351632009-02-20 20:59:54 +00001612//===----------------------------------------------------------------------===//
1613// ObjCImplementationDecl
1614//===----------------------------------------------------------------------===//
1615
David Blaikie99ba9e32011-12-20 02:48:34 +00001616void ObjCImplementationDecl::anchor() { }
1617
Chris Lattnerab351632009-02-20 20:59:54 +00001618ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001619ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001620 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001621 ObjCInterfaceDecl *SuperDecl,
1622 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001623 SourceLocation atStartLoc,
1624 SourceLocation IvarLBraceLoc,
1625 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001626 if (ClassInterface && ClassInterface->hasDefinition())
1627 ClassInterface = ClassInterface->getDefinition();
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001628 return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001629 nameLoc, atStartLoc,
1630 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001631}
1632
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001633ObjCImplementationDecl *
1634ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1635 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1636 return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1637 SourceLocation());
1638}
1639
John McCallda6d9762011-07-22 04:15:06 +00001640void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1641 CXXCtorInitializer ** initializers,
1642 unsigned numInitializers) {
1643 if (numInitializers > 0) {
1644 NumIvarInitializers = numInitializers;
1645 CXXCtorInitializer **ivarInitializers =
1646 new (C) CXXCtorInitializer*[NumIvarInitializers];
1647 memcpy(ivarInitializers, initializers,
1648 numInitializers * sizeof(CXXCtorInitializer*));
1649 IvarInitializers = ivarInitializers;
1650 }
1651}
1652
Chris Lattner5f9e2722011-07-23 10:55:15 +00001653raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001654 const ObjCImplementationDecl &ID) {
1655 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001656 return OS;
1657}
1658
Chris Lattnerab351632009-02-20 20:59:54 +00001659//===----------------------------------------------------------------------===//
1660// ObjCCompatibleAliasDecl
1661//===----------------------------------------------------------------------===//
1662
David Blaikie99ba9e32011-12-20 02:48:34 +00001663void ObjCCompatibleAliasDecl::anchor() { }
1664
Chris Lattnerab351632009-02-20 20:59:54 +00001665ObjCCompatibleAliasDecl *
1666ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1667 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001668 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001669 ObjCInterfaceDecl* AliasedClass) {
1670 return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1671}
1672
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001673ObjCCompatibleAliasDecl *
1674ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1675 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1676 return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1677}
1678
Chris Lattnerab351632009-02-20 20:59:54 +00001679//===----------------------------------------------------------------------===//
1680// ObjCPropertyDecl
1681//===----------------------------------------------------------------------===//
1682
David Blaikie99ba9e32011-12-20 02:48:34 +00001683void ObjCPropertyDecl::anchor() { }
1684
Chris Lattnerab351632009-02-20 20:59:54 +00001685ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1686 SourceLocation L,
1687 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001688 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001689 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001690 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001691 PropertyControl propControl) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001692 return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001693}
1694
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001695ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1696 unsigned ID) {
1697 void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1698 return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001699 SourceLocation(),
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001700 0);
1701}
1702
Chris Lattnerab351632009-02-20 20:59:54 +00001703//===----------------------------------------------------------------------===//
1704// ObjCPropertyImplDecl
1705//===----------------------------------------------------------------------===//
1706
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001707ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001708 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001709 SourceLocation atLoc,
1710 SourceLocation L,
1711 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001712 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001713 ObjCIvarDecl *ivar,
1714 SourceLocation ivarLoc) {
1715 return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1716 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001717}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001718
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001719ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1720 unsigned ID) {
1721 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1722 return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1723 0, Dynamic, 0, SourceLocation());
1724}
1725
Douglas Gregora4ffd852010-11-17 01:03:52 +00001726SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1727 SourceLocation EndLoc = getLocation();
1728 if (IvarLoc.isValid())
1729 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001730
Douglas Gregora4ffd852010-11-17 01:03:52 +00001731 return SourceRange(AtLoc, EndLoc);
1732}