blob: fb4b9dbd1f43a1afd3e4b5f30c13df7557c8c90c [file] [log] [blame]
Chris Lattner89375192008-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 Kyrtzidis7d847c92011-09-01 00:58:55 +000016#include "clang/AST/ASTMutationListener.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/Stmt.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000019#include "llvm/ADT/STLExtras.h"
Anna Zaks454477c2012-09-27 19:45:11 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner89375192008-03-16 00:19:01 +000021using namespace clang;
22
Chris Lattner8d8829e2008-03-16 00:49:28 +000023//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000024// ObjCListBase
25//===----------------------------------------------------------------------===//
26
Chris Lattner22298722009-02-20 21:35:13 +000027void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorb412e172010-07-25 18:17:45 +000028 List = 0;
Chris Lattner4d1eb762009-02-20 21:16:26 +000029 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000030
31
Chris Lattner7c981a72009-02-20 21:44:01 +000032 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000033 NumElts = Elts;
34 memcpy(List, InList, sizeof(void*)*Elts);
35}
36
Douglas Gregor002b6712010-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 Lattner4d1eb762009-02-20 21:16:26 +000047//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000048// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000049//===----------------------------------------------------------------------===//
50
David Blaikie68e081d2011-12-20 02:48:34 +000051void ObjCContainerDecl::anchor() { }
52
Fariborz Jahanian68453832009-06-05 18:16:35 +000053/// getIvarDecl - This method looks up an ivar in this ContextDecl.
54///
55ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000056ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
David Blaikieff7d47a2012-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 Jahanian68453832009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
63 return 0;
64}
65
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000066// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000067ObjCMethodDecl *
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69 bool AllowHidden) const {
Douglas Gregoreed49792013-01-17 00:38:46 +000070 // If this context is a hidden protocol definition, don't find any
71 // methods there.
72 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
73 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000074 if (Def->isHidden() && !AllowHidden)
Douglas Gregoreed49792013-01-17 00:38:46 +000075 return 0;
76 }
77
Steve Naroffc4173fa2009-02-22 19:35:57 +000078 // Since instance & class methods can have the same name, the loop below
79 // ensures we get the correct method.
80 //
81 // @interface Whatever
82 // - (int) class_method;
83 // + (float) class_method;
84 // @end
85 //
David Blaikieff7d47a2012-12-19 00:45:41 +000086 lookup_const_result R = lookup(Sel);
87 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
88 Meth != MethEnd; ++Meth) {
Steve Naroffc4173fa2009-02-22 19:35:57 +000089 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000090 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +000091 return MD;
92 }
Steve Naroff35c62ae2009-01-08 17:28:14 +000093 return 0;
94}
95
Fariborz Jahanian1446b342013-03-21 20:50:53 +000096/// HasUserDeclaredSetterMethod - This routine returns 'true' if a user declared setter
97/// method was found in the class, its protocols, its super classes or categories.
98/// It also returns 'true' if one of its categories has declared a 'readwrite' property.
99/// This is because, user must provide a setter method for the category's 'readwrite'
100/// property.
101bool
102ObjCContainerDecl::HasUserDeclaredSetterMethod(const ObjCPropertyDecl *Property) const {
103 Selector Sel = Property->getSetterName();
104 lookup_const_result R = lookup(Sel);
105 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
106 Meth != MethEnd; ++Meth) {
107 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
108 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
109 return true;
110 }
111
112 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
113 // Also look into categories, including class extensions, looking
114 // for a user declared instance method.
115 for (ObjCInterfaceDecl::visible_categories_iterator
116 Cat = ID->visible_categories_begin(),
117 CatEnd = ID->visible_categories_end();
118 Cat != CatEnd;
119 ++Cat) {
120 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
121 if (!MD->isImplicit())
122 return true;
123 if (Cat->IsClassExtension())
124 continue;
125 // Also search through the categories looking for a 'readwrite' declaration
126 // of this property. If one found, presumably a setter will be provided
127 // (properties declared in categories will not get auto-synthesized).
128 for (ObjCContainerDecl::prop_iterator P = Cat->prop_begin(),
129 E = Cat->prop_end(); P != E; ++P)
130 if (P->getIdentifier() == Property->getIdentifier()) {
131 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
132 return true;
133 break;
134 }
135 }
136
137 // Also look into protocols, for a user declared instance method.
138 for (ObjCInterfaceDecl::all_protocol_iterator P =
139 ID->all_referenced_protocol_begin(),
140 PE = ID->all_referenced_protocol_end(); P != PE; ++P) {
141 ObjCProtocolDecl *Proto = (*P);
142 if (Proto->HasUserDeclaredSetterMethod(Property))
143 return true;
144 }
145 // And in its super class.
146 ObjCInterfaceDecl *OSC = ID->getSuperClass();
147 while (OSC) {
148 if (OSC->HasUserDeclaredSetterMethod(Property))
149 return true;
150 OSC = OSC->getSuperClass();
151 }
152 }
153 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
154 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
155 E = PD->protocol_end(); PI != E; ++PI) {
156 if ((*PI)->HasUserDeclaredSetterMethod(Property))
157 return true;
158 }
159 return false;
160}
161
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000162ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000163ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000164 IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000165 // If this context is a hidden protocol definition, don't find any
166 // property.
167 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
168 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
169 if (Def->isHidden())
170 return 0;
171 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000172
David Blaikieff7d47a2012-12-19 00:45:41 +0000173 DeclContext::lookup_const_result R = DC->lookup(propertyID);
174 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
175 ++I)
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000176 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
177 return PD;
178
179 return 0;
180}
181
Anna Zaks454477c2012-09-27 19:45:11 +0000182IdentifierInfo *
183ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
184 SmallString<128> ivarName;
185 {
186 llvm::raw_svector_ostream os(ivarName);
187 os << '_' << getIdentifier()->getName();
188 }
189 return &Ctx.Idents.get(ivarName.str());
190}
191
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000192/// FindPropertyDeclaration - Finds declaration of the property given its name
193/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000194ObjCPropertyDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000195ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000196 // Don't find properties within hidden protocol definitions.
197 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
198 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
199 if (Def->isHidden())
200 return 0;
201 }
Mike Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenekddcd1092010-03-15 20:11:53 +0000203 if (ObjCPropertyDecl *PD =
204 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
205 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000206
Ted Kremenekddcd1092010-03-15 20:11:53 +0000207 switch (getKind()) {
208 default:
209 break;
210 case Decl::ObjCProtocol: {
211 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
212 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
213 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000214 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
215 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000216 break;
217 }
218 case Decl::ObjCInterface: {
219 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000220 // Look through categories (but not extensions).
221 for (ObjCInterfaceDecl::visible_categories_iterator
222 Cat = OID->visible_categories_begin(),
223 CatEnd = OID->visible_categories_end();
224 Cat != CatEnd; ++Cat) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000225 if (!Cat->IsClassExtension())
226 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
227 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000228 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000229
230 // Look through protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000231 for (ObjCInterfaceDecl::all_protocol_iterator
232 I = OID->all_referenced_protocol_begin(),
233 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekddcd1092010-03-15 20:11:53 +0000234 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
235 return P;
236
237 // Finally, check the super class.
238 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
239 return superClass->FindPropertyDeclaration(PropertyId);
240 break;
241 }
242 case Decl::ObjCCategory: {
243 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
244 // Look through protocols.
245 if (!OCD->IsClassExtension())
246 for (ObjCCategoryDecl::protocol_iterator
247 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
248 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
249 return P;
250
251 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000252 }
253 }
Steve Narofff9c65242008-06-05 13:55:23 +0000254 return 0;
255}
256
David Blaikie68e081d2011-12-20 02:48:34 +0000257void ObjCInterfaceDecl::anchor() { }
258
Ted Kremenek33f50432013-11-23 22:29:06 +0000259bool ObjCInterfaceDecl::shouldSuppressProtocol(const ObjCProtocolDecl *P) const{
260 if (!hasAttrs())
261 return false;
Ted Kremenek33f50432013-11-23 22:29:06 +0000262 const IdentifierInfo *PI = P->getIdentifier();
Ted Kremenek5ec76a02013-11-23 22:51:36 +0000263 for (specific_attr_iterator<ObjCSuppressProtocolAttr>
264 I = specific_attr_begin<ObjCSuppressProtocolAttr>(),
265 E = specific_attr_end<ObjCSuppressProtocolAttr>(); I != E; ++I)
266 if ((*I)->getProtocol() == PI)
Ted Kremenek33f50432013-11-23 22:29:06 +0000267 return true;
Ted Kremenek33f50432013-11-23 22:29:06 +0000268 return false;
269}
270
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000271/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
272/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000273/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000274///
275ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000276ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000277 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000278 // FIXME: Should make sure no callers ever do this.
279 if (!hasDefinition())
280 return 0;
281
282 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000283 LoadExternalDefinition();
284
Ted Kremenekd133a862010-03-15 20:30:07 +0000285 if (ObjCPropertyDecl *PD =
286 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
287 return PD;
288
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000289 // Look through protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000290 for (ObjCInterfaceDecl::all_protocol_iterator
291 I = all_referenced_protocol_begin(),
292 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000293 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
294 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000295
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000296 return 0;
297}
298
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000299void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
300 PropertyDeclOrder &PO) const {
Anna Zaks673d76b2012-10-18 19:17:53 +0000301 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
302 E = prop_end(); P != E; ++P) {
303 ObjCPropertyDecl *Prop = *P;
304 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000305 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000306 }
307 for (ObjCInterfaceDecl::all_protocol_iterator
308 PI = all_referenced_protocol_begin(),
309 E = all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000310 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000311 // Note, the properties declared only in class extensions are still copied
312 // into the main @interface's property list, and therefore we don't
313 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000314}
315
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000316bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
317 const ObjCInterfaceDecl *Class = this;
318 while (Class) {
319 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
320 return true;
321 Class = Class->getSuperClass();
322 }
323 return false;
324}
325
326const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
327 const ObjCInterfaceDecl *Class = this;
328 while (Class) {
329 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
330 return Class;
331 Class = Class->getSuperClass();
332 }
333 return 0;
334}
335
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000336void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
337 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
338 ASTContext &C)
339{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000340 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000341 LoadExternalDefinition();
342
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000343 if (data().AllReferencedProtocols.empty() &&
344 data().ReferencedProtocols.empty()) {
345 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000346 return;
347 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000348
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000349 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000350 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000351 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000352 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000353 for (unsigned i = 0; i < ExtNum; i++) {
354 bool protocolExists = false;
355 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000356 for (all_protocol_iterator
357 p = all_referenced_protocol_begin(),
358 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000359 ObjCProtocolDecl *Proto = (*p);
360 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
361 protocolExists = true;
362 break;
363 }
364 }
365 // Do we want to warn on a protocol in extension class which
366 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000367 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000368 ProtocolRefs.push_back(ProtoInExtension);
369 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000370
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000371 if (ProtocolRefs.empty())
372 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000373
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000374 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000375 for (all_protocol_iterator p = all_referenced_protocol_begin(),
376 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000377 ProtocolRefs.push_back(*p);
Douglas Gregor002b6712010-01-16 15:02:53 +0000378 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000379
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000380 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000381}
382
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000383const ObjCInterfaceDecl *
384ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
385 const ObjCInterfaceDecl *IFace = this;
386 while (IFace) {
387 if (IFace->hasDesignatedInitializers())
388 return IFace;
389 if (!IFace->inheritsDesignatedInitializers())
390 break;
391 IFace = IFace->getSuperClass();
392 }
393 return 0;
394}
395
396bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
397 switch (data().InheritedDesignatedInitializers) {
398 case DefinitionData::IDI_Inherited:
399 return true;
400 case DefinitionData::IDI_NotInherited:
401 return false;
402 case DefinitionData::IDI_Unknown: {
403 bool isIntroducingInitializers = false;
404 for (instmeth_iterator I = instmeth_begin(),
405 E = instmeth_end(); I != E; ++I) {
406 const ObjCMethodDecl *MD = *I;
407 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding()) {
408 isIntroducingInitializers = true;
409 break;
410 }
411 }
412 // If the class introduced initializers we conservatively assume that we
413 // don't know if any of them is a designated initializer to avoid possible
414 // misleading warnings.
415 if (isIntroducingInitializers) {
416 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
417 return false;
418 } else {
419 data().InheritedDesignatedInitializers = DefinitionData::IDI_Inherited;
420 return true;
421 }
422 }
423 }
424
425 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
426}
427
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000428void ObjCInterfaceDecl::getDesignatedInitializers(
429 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
430 assert(hasDefinition());
431 if (data().ExternallyCompleted)
432 LoadExternalDefinition();
433
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000434 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000435 if (!IFace)
436 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000437
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000438 for (instmeth_iterator I = IFace->instmeth_begin(),
439 E = IFace->instmeth_end(); I != E; ++I) {
440 const ObjCMethodDecl *MD = *I;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000441 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000442 Methods.push_back(MD);
443 }
444}
445
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000446bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
447 const ObjCMethodDecl **InitMethod) const {
448 assert(hasDefinition());
449 if (data().ExternallyCompleted)
450 LoadExternalDefinition();
451
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000452 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000453 if (!IFace)
454 return false;
455
Argyrios Kyrtzidise919fc22013-12-10 18:36:43 +0000456 if (const ObjCMethodDecl *MD = IFace->getMethod(Sel, /*isInstance=*/true)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000457 if (MD->isThisDeclarationADesignatedInitializer()) {
458 if (InitMethod)
459 *InitMethod = MD;
460 return true;
461 }
462 }
463 return false;
464}
465
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000466void ObjCInterfaceDecl::allocateDefinitionData() {
467 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000468 Data.setPointer(new (getASTContext()) DefinitionData());
469 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000470
471 // Make the type point at the definition, now that we have one.
472 if (TypeForDecl)
473 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000474}
475
476void ObjCInterfaceDecl::startDefinition() {
477 allocateDefinitionData();
478
Douglas Gregor66b310c2011-12-15 18:03:09 +0000479 // Update all of the declarations with a pointer to the definition.
480 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
481 RD != RDEnd; ++RD) {
482 if (*RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000483 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000484 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000485}
486
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000487ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
488 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000489 // FIXME: Should make sure no callers ever do this.
490 if (!hasDefinition())
491 return 0;
492
493 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000494 LoadExternalDefinition();
495
Chris Lattner89375192008-03-16 00:19:01 +0000496 ObjCInterfaceDecl* ClassDecl = this;
497 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000498 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000499 clsDeclared = ClassDecl;
500 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000501 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000502
503 for (ObjCInterfaceDecl::visible_extensions_iterator
504 Ext = ClassDecl->visible_extensions_begin(),
505 ExtEnd = ClassDecl->visible_extensions_end();
506 Ext != ExtEnd; ++Ext) {
507 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000508 clsDeclared = ClassDecl;
509 return I;
510 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000511 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000512
Chris Lattner89375192008-03-16 00:19:01 +0000513 ClassDecl = ClassDecl->getSuperClass();
514 }
515 return NULL;
516}
517
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000518/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
519/// class whose name is passed as argument. If it is not one of the super classes
520/// the it returns NULL.
521ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
522 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000523 // FIXME: Should make sure no callers ever do this.
524 if (!hasDefinition())
525 return 0;
526
527 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000528 LoadExternalDefinition();
529
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000530 ObjCInterfaceDecl* ClassDecl = this;
531 while (ClassDecl != NULL) {
532 if (ClassDecl->getIdentifier() == ICName)
533 return ClassDecl;
534 ClassDecl = ClassDecl->getSuperClass();
535 }
536 return NULL;
537}
538
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000539ObjCProtocolDecl *
540ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
541 for (ObjCInterfaceDecl::all_protocol_iterator P =
542 all_referenced_protocol_begin(), PE = all_referenced_protocol_end();
543 P != PE; ++P)
544 if ((*P)->lookupProtocolNamed(Name))
545 return (*P);
546 ObjCInterfaceDecl *SuperClass = getSuperClass();
547 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
548}
549
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000550/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000551/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000552/// When argument category "C" is specified, any implicit method found
553/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000554ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000555 bool isInstance,
556 bool shallowCategoryLookup,
557 bool followSuper,
Ted Kremenek28eace62013-11-23 01:01:34 +0000558 const ObjCCategoryDecl *C,
559 const ObjCProtocolDecl *P) const
Ted Kremenek00781502013-11-23 01:01:29 +0000560{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000561 // FIXME: Should make sure no callers ever do this.
562 if (!hasDefinition())
563 return 0;
564
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000565 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000566 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000567
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000568 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000569 LoadExternalDefinition();
570
Ted Kremenek00781502013-11-23 01:01:29 +0000571 while (ClassDecl) {
Ted Kremenek28eace62013-11-23 01:01:34 +0000572 // If we are looking for a method that is part of protocol conformance,
573 // check if the superclass has been marked to suppress conformance
574 // of that protocol.
Ted Kremenek33f50432013-11-23 22:29:06 +0000575 if (P && ClassDecl->shouldSuppressProtocol(P))
576 return 0;
Ted Kremenek28eace62013-11-23 01:01:34 +0000577
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000578 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000579 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattner89375192008-03-16 00:19:01 +0000581 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +0000582 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
583 E = ClassDecl->protocol_end();
584 I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000585 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000586 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000587
588 // Didn't find one yet - now look through categories.
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000589 for (ObjCInterfaceDecl::visible_categories_iterator
590 Cat = ClassDecl->visible_categories_begin(),
591 CatEnd = ClassDecl->visible_categories_end();
592 Cat != CatEnd; ++Cat) {
593 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
594 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000595 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000596
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000597 if (!shallowCategoryLookup) {
598 // Didn't find one yet - look through protocols.
599 const ObjCList<ObjCProtocolDecl> &Protocols =
600 Cat->getReferencedProtocols();
601 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
602 E = Protocols.end(); I != E; ++I)
603 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
604 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000605 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000606 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000607 }
Ted Kremenek00781502013-11-23 01:01:29 +0000608
609 if (!followSuper)
610 return NULL;
611
612 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000613 ClassDecl = ClassDecl->getSuperClass();
614 }
615 return NULL;
616}
617
Anna Zaksc77a3b12012-07-27 19:07:44 +0000618// Will search "local" class/category implementations for a method decl.
619// If failed, then we search in class's root for an instance method.
620// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000621ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
622 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000623 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000624 // FIXME: Should make sure no callers ever do this.
625 if (!hasDefinition())
626 return 0;
627
628 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000629 LoadExternalDefinition();
630
Steve Naroffbb69c942009-10-01 23:46:04 +0000631 ObjCMethodDecl *Method = 0;
632 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000633 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
634 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000635
636 // Look through local category implementations associated with the class.
637 if (!Method)
638 Method = Instance ? getCategoryInstanceMethod(Sel)
639 : getCategoryClassMethod(Sel);
640
641 // Before we give up, check if the selector is an instance method.
642 // But only in the root. This matches gcc's behavior and what the
643 // runtime expects.
644 if (!Instance && !Method && !getSuperClass()) {
645 Method = lookupInstanceMethod(Sel);
646 // Look through local category implementations associated
647 // with the root class.
648 if (!Method)
649 Method = lookupPrivateMethod(Sel, true);
650 }
651
Steve Naroffbb69c942009-10-01 23:46:04 +0000652 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000653 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000654 return Method;
655}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000656
657//===----------------------------------------------------------------------===//
658// ObjCMethodDecl
659//===----------------------------------------------------------------------===//
660
661ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000662 SourceLocation beginLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000663 SourceLocation endLoc,
664 Selector SelInfo, QualType T,
Douglas Gregor12852d92010-03-08 14:59:44 +0000665 TypeSourceInfo *ResultTInfo,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000666 DeclContext *contextDecl,
667 bool isInstance,
668 bool isVariadic,
Jordan Rosed01e83a2012-10-10 16:42:25 +0000669 bool isPropertyAccessor,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +0000670 bool isImplicitlyDeclared,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000671 bool isDefined,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000672 ImplementationControl impControl,
Argyrios Kyrtzidis38493942011-10-03 06:36:29 +0000673 bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000674 return new (C, contextDecl) ObjCMethodDecl(
675 beginLoc, endLoc, SelInfo, T, ResultTInfo, contextDecl, isInstance,
676 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
677 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000678}
679
Douglas Gregor72172e92012-01-05 21:55:30 +0000680ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000681 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
682 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000683}
684
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000685bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
686 return getMethodFamily() == OMF_init &&
687 hasAttr<ObjCDesignatedInitializerAttr>();
688}
689
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000690bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
691 const ObjCMethodDecl **InitMethod) const {
692 if (getMethodFamily() != OMF_init)
693 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000694 const DeclContext *DC = getDeclContext();
695 if (isa<ObjCProtocolDecl>(DC))
696 return false;
697 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000698 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000699 return false;
700}
701
Douglas Gregora6017bb2012-10-09 17:21:28 +0000702Stmt *ObjCMethodDecl::getBody() const {
703 return Body.get(getASTContext().getExternalSource());
704}
705
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000706void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
707 assert(PrevMethod);
708 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
709 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000710 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000711}
712
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000713void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
714 ArrayRef<ParmVarDecl*> Params,
715 ArrayRef<SourceLocation> SelLocs) {
716 ParamsAndSelLocs = 0;
717 NumParams = Params.size();
718 if (Params.empty() && SelLocs.empty())
719 return;
720
721 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
722 sizeof(SourceLocation) * SelLocs.size();
723 ParamsAndSelLocs = C.Allocate(Size);
724 std::copy(Params.begin(), Params.end(), getParams());
725 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
726}
727
728void ObjCMethodDecl::getSelectorLocs(
729 SmallVectorImpl<SourceLocation> &SelLocs) const {
730 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
731 SelLocs.push_back(getSelectorLoc(i));
732}
733
734void ObjCMethodDecl::setMethodParams(ASTContext &C,
735 ArrayRef<ParmVarDecl*> Params,
736 ArrayRef<SourceLocation> SelLocs) {
737 assert((!SelLocs.empty() || isImplicit()) &&
738 "No selector locs for non-implicit method");
739 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000740 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000741
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000742 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
743 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000744 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000745 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000746
747 setParamsAndSelLocs(C, Params, SelLocs);
748}
749
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000750/// \brief A definition will return its interface declaration.
751/// An interface declaration will return its definition.
752/// Otherwise it will return itself.
753ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
754 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000755 ObjCMethodDecl *Redecl = 0;
756 if (HasRedeclaration)
757 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000758 if (Redecl)
759 return Redecl;
760
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000761 Decl *CtxD = cast<Decl>(getDeclContext());
762
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000763 if (!CtxD->isInvalidDecl()) {
764 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
765 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
766 if (!ImplD->isInvalidDecl())
767 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000768
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000769 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
770 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
771 if (!ImplD->isInvalidDecl())
772 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000773
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000774 } else if (ObjCImplementationDecl *ImplD =
775 dyn_cast<ObjCImplementationDecl>(CtxD)) {
776 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
777 if (!IFD->isInvalidDecl())
778 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000779
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000780 } else if (ObjCCategoryImplDecl *CImplD =
781 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
782 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
783 if (!CatD->isInvalidDecl())
784 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
785 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000786 }
787
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000788 if (!Redecl && isRedeclaration()) {
789 // This is the last redeclaration, go back to the first method.
790 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
791 isInstanceMethod());
792 }
793
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000794 return Redecl ? Redecl : this;
795}
796
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000797ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
798 Decl *CtxD = cast<Decl>(getDeclContext());
799
800 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
801 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
802 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
803 isInstanceMethod()))
804 return MD;
805
806 } else if (ObjCCategoryImplDecl *CImplD =
807 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000808 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000809 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
810 isInstanceMethod()))
811 return MD;
812 }
813
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000814 if (isRedeclaration())
815 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
816 isInstanceMethod());
817
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000818 return this;
819}
820
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000821SourceLocation ObjCMethodDecl::getLocEnd() const {
822 if (Stmt *Body = getBody())
823 return Body->getLocEnd();
824 return DeclEndLoc;
825}
826
John McCallb4526252011-03-02 01:50:55 +0000827ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
828 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000829 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000830 return family;
831
John McCall86bc21f2011-03-02 11:33:24 +0000832 // Check for an explicit attribute.
833 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
834 // The unfortunate necessity of mapping between enums here is due
835 // to the attributes framework.
836 switch (attr->getFamily()) {
837 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
838 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
839 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
840 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
841 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
842 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
843 }
844 Family = static_cast<unsigned>(family);
845 return family;
846 }
847
John McCallb4526252011-03-02 01:50:55 +0000848 family = getSelector().getMethodFamily();
849 switch (family) {
850 case OMF_None: break;
851
852 // init only has a conventional meaning for an instance method, and
853 // it has to return an object.
854 case OMF_init:
855 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
856 family = OMF_None;
857 break;
858
859 // alloc/copy/new have a conventional meaning for both class and
860 // instance methods, but they require an object return.
861 case OMF_alloc:
862 case OMF_copy:
863 case OMF_mutableCopy:
864 case OMF_new:
865 if (!getResultType()->isObjCObjectPointerType())
866 family = OMF_None;
867 break;
868
869 // These selectors have a conventional meaning only for instance methods.
870 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000871 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000872 case OMF_retain:
873 case OMF_release:
874 case OMF_autorelease:
875 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000876 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000877 if (!isInstanceMethod())
878 family = OMF_None;
879 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000880
881 case OMF_performSelector:
882 if (!isInstanceMethod() ||
883 !getResultType()->isObjCIdType())
884 family = OMF_None;
885 else {
886 unsigned noParams = param_size();
887 if (noParams < 1 || noParams > 3)
888 family = OMF_None;
889 else {
890 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
891 QualType ArgT = (*it);
892 if (!ArgT->isObjCSelType()) {
893 family = OMF_None;
894 break;
895 }
896 while (--noParams) {
897 it++;
898 ArgT = (*it);
899 if (!ArgT->isObjCIdType()) {
900 family = OMF_None;
901 break;
902 }
903 }
904 }
905 }
906 break;
907
John McCallb4526252011-03-02 01:50:55 +0000908 }
909
910 // Cache the result.
911 Family = static_cast<unsigned>(family);
912 return family;
913}
914
Mike Stump11289f42009-09-09 15:08:12 +0000915void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000916 const ObjCInterfaceDecl *OID) {
917 QualType selfTy;
918 if (isInstanceMethod()) {
919 // There may be no interface context due to error in declaration
920 // of the interface (which has been reported). Recover gracefully.
921 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000922 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000923 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000924 } else {
925 selfTy = Context.getObjCIdType();
926 }
927 } else // we have a factory method.
928 selfTy = Context.getObjCClassType();
929
John McCalld4631322011-06-17 06:42:21 +0000930 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000931 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000932
David Blaikiebbafb8a2012-03-11 07:00:24 +0000933 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000934 if (isInstanceMethod()) {
935 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000936
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000937 // 'self' is always __strong. It's actually pseudo-strong except
938 // in init methods (or methods labeled ns_consumes_self), though.
939 Qualifiers qs;
940 qs.setObjCLifetime(Qualifiers::OCL_Strong);
941 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000942
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000943 // In addition, 'self' is const unless this is an init method.
944 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
945 selfTy = selfTy.withConst();
946 selfIsPseudoStrong = true;
947 }
948 }
949 else {
950 assert(isClassMethod());
951 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000952 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000953 selfIsPseudoStrong = true;
954 }
John McCall31168b02011-06-15 23:02:42 +0000955 }
956
957 ImplicitParamDecl *self
958 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
959 &Context.Idents.get("self"), selfTy);
960 setSelfDecl(self);
961
962 if (selfIsConsumed)
963 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000964
John McCalld4631322011-06-17 06:42:21 +0000965 if (selfIsPseudoStrong)
966 self->setARCPseudoStrong(true);
967
Mike Stump11289f42009-09-09 15:08:12 +0000968 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
969 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000970 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000971}
972
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000973ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
974 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
975 return ID;
976 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
977 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000978 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000979 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000980
981 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikie83d382b2011-09-23 05:06:16 +0000982 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000983}
984
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000985static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
986 const ObjCMethodDecl *Method,
987 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
988 bool MovedToSuper) {
989 if (!Container)
990 return;
991
992 // In categories look for overriden methods from protocols. A method from
993 // category is not "overriden" since it is considered as the "same" method
994 // (same USR) as the one from the interface.
995 if (const ObjCCategoryDecl *
996 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
997 // Check whether we have a matching method at this category but only if we
998 // are at the super class level.
999 if (MovedToSuper)
1000 if (ObjCMethodDecl *
1001 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001002 Method->isInstanceMethod(),
1003 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001004 if (Method != Overridden) {
1005 // We found an override at this category; there is no need to look
1006 // into its protocols.
1007 Methods.push_back(Overridden);
1008 return;
1009 }
1010
1011 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
1012 PEnd = Category->protocol_end();
1013 P != PEnd; ++P)
1014 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1015 return;
1016 }
1017
1018 // Check whether we have a matching method at this level.
1019 if (const ObjCMethodDecl *
1020 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001021 Method->isInstanceMethod(),
1022 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001023 if (Method != Overridden) {
1024 // We found an override at this level; there is no need to look
1025 // into other protocols or categories.
1026 Methods.push_back(Overridden);
1027 return;
1028 }
1029
1030 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
1031 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
1032 PEnd = Protocol->protocol_end();
1033 P != PEnd; ++P)
1034 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1035 }
1036
1037 if (const ObjCInterfaceDecl *
1038 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
1039 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
1040 PEnd = Interface->protocol_end();
1041 P != PEnd; ++P)
1042 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1043
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001044 for (ObjCInterfaceDecl::known_categories_iterator
1045 Cat = Interface->known_categories_begin(),
1046 CatEnd = Interface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001047 Cat != CatEnd; ++Cat) {
1048 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001049 MovedToSuper);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001050 }
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001051
1052 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1053 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1054 /*MovedToSuper=*/true);
1055 }
1056}
1057
1058static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1059 const ObjCMethodDecl *Method,
1060 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1061 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1062 /*MovedToSuper=*/false);
1063}
1064
1065static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1066 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1067 assert(Method->isOverriding());
1068
1069 if (const ObjCProtocolDecl *
1070 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1071 CollectOverriddenMethods(ProtD, Method, overridden);
1072
1073 } else if (const ObjCImplDecl *
1074 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1075 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1076 if (!ID)
1077 return;
1078 // Start searching for overridden methods using the method from the
1079 // interface as starting point.
1080 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001081 Method->isInstanceMethod(),
1082 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001083 Method = IFaceMeth;
1084 CollectOverriddenMethods(ID, Method, overridden);
1085
1086 } else if (const ObjCCategoryDecl *
1087 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1088 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1089 if (!ID)
1090 return;
1091 // Start searching for overridden methods using the method from the
1092 // interface as starting point.
1093 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001094 Method->isInstanceMethod(),
1095 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001096 Method = IFaceMeth;
1097 CollectOverriddenMethods(ID, Method, overridden);
1098
1099 } else {
1100 CollectOverriddenMethods(
1101 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1102 Method, overridden);
1103 }
1104}
1105
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001106void ObjCMethodDecl::getOverriddenMethods(
1107 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1108 const ObjCMethodDecl *Method = this;
1109
1110 if (Method->isRedeclaration()) {
1111 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1112 getMethod(Method->getSelector(), Method->isInstanceMethod());
1113 }
1114
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001115 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001116 collectOverriddenMethodsSlow(Method, Overridden);
1117 assert(!Overridden.empty() &&
1118 "ObjCMethodDecl's overriding bit is not as expected");
1119 }
1120}
1121
Jordan Rose2bd991a2012-10-10 16:42:54 +00001122const ObjCPropertyDecl *
1123ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1124 Selector Sel = getSelector();
1125 unsigned NumArgs = Sel.getNumArgs();
1126 if (NumArgs > 1)
1127 return 0;
1128
Jordan Rose59e34ec2012-10-11 16:02:02 +00001129 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001130 return 0;
1131
1132 if (isPropertyAccessor()) {
1133 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001134 // If container is class extension, find its primary class.
1135 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1136 if (CatDecl->IsClassExtension())
1137 Container = CatDecl->getClassInterface();
1138
Jordan Rose2bd991a2012-10-10 16:42:54 +00001139 bool IsGetter = (NumArgs == 0);
1140
1141 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1142 E = Container->prop_end();
1143 I != E; ++I) {
1144 Selector NextSel = IsGetter ? (*I)->getGetterName()
1145 : (*I)->getSetterName();
1146 if (NextSel == Sel)
1147 return *I;
1148 }
1149
1150 llvm_unreachable("Marked as a property accessor but no property found!");
1151 }
1152
1153 if (!CheckOverrides)
1154 return 0;
1155
1156 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1157 OverridesTy Overrides;
1158 getOverriddenMethods(Overrides);
1159 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1160 I != E; ++I) {
1161 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1162 return Prop;
1163 }
1164
1165 return 0;
1166
1167}
1168
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001169//===----------------------------------------------------------------------===//
1170// ObjCInterfaceDecl
1171//===----------------------------------------------------------------------===//
1172
Douglas Gregord53ae832012-01-17 18:09:05 +00001173ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001174 DeclContext *DC,
1175 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001176 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001177 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001178 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001179 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001180 ObjCInterfaceDecl *Result = new (C, DC)
1181 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001182 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001183 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001184 return Result;
1185}
1186
Richard Smithf7981722013-11-22 09:01:48 +00001187ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001188 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001189 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1190 0, SourceLocation(),
1191 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001192 Result->Data.setInt(!C.getLangOpts().Modules);
1193 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001194}
1195
1196ObjCInterfaceDecl::
1197ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001198 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1199 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001200 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001201 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001202{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001203 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001204
1205 // Copy the 'data' pointer over.
1206 if (PrevDecl)
1207 Data = PrevDecl->Data;
1208
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001209 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001210}
1211
Douglas Gregor73693022010-12-01 23:49:52 +00001212void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001213 assert(data().ExternallyCompleted && "Class is not externally completed");
1214 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001215 getASTContext().getExternalSource()->CompleteType(
1216 const_cast<ObjCInterfaceDecl *>(this));
1217}
1218
1219void ObjCInterfaceDecl::setExternallyCompleted() {
1220 assert(getASTContext().getExternalSource() &&
1221 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001222 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001223 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001224 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001225}
1226
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001227void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1228 assert(hasDefinition() && "Forward declarations can't contain methods");
1229 data().HasDesignatedInitializers = true;
1230}
1231
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001232bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1233 assert(hasDefinition() && "Forward declarations can't contain methods");
1234 if (data().ExternallyCompleted)
1235 LoadExternalDefinition();
1236
1237 return data().HasDesignatedInitializers;
1238}
1239
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001240ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001241 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1242 if (data().ExternallyCompleted)
1243 LoadExternalDefinition();
1244
1245 return getASTContext().getObjCImplementation(
1246 const_cast<ObjCInterfaceDecl*>(Def));
1247 }
1248
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001249 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001250 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001251}
1252
1253void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001254 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001255}
1256
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001257namespace {
1258 struct SynthesizeIvarChunk {
1259 uint64_t Size;
1260 ObjCIvarDecl *Ivar;
1261 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1262 : Size(size), Ivar(ivar) {}
1263 };
1264
1265 bool operator<(const SynthesizeIvarChunk & LHS,
1266 const SynthesizeIvarChunk &RHS) {
1267 return LHS.Size < RHS.Size;
1268 }
1269}
1270
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001271/// all_declared_ivar_begin - return first ivar declared in this class,
1272/// its extensions and its implementation. Lazily build the list on first
1273/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001274///
1275/// Caveat: The list returned by this method reflects the current
1276/// state of the parser. The cache will be updated for every ivar
1277/// added by an extension or the implementation when they are
1278/// encountered.
1279/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001280ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001281 // FIXME: Should make sure no callers ever do this.
1282 if (!hasDefinition())
1283 return 0;
1284
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001285 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001286 if (!data().IvarList) {
1287 if (!ivar_empty()) {
1288 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1289 data().IvarList = *I; ++I;
1290 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001291 curIvar->setNextIvar(*I);
1292 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001293
1294 for (ObjCInterfaceDecl::known_extensions_iterator
1295 Ext = known_extensions_begin(),
1296 ExtEnd = known_extensions_end();
1297 Ext != ExtEnd; ++Ext) {
1298 if (!Ext->ivar_empty()) {
1299 ObjCCategoryDecl::ivar_iterator
1300 I = Ext->ivar_begin(),
1301 E = Ext->ivar_end();
1302 if (!data().IvarList) {
1303 data().IvarList = *I; ++I;
1304 curIvar = data().IvarList;
1305 }
1306 for ( ;I != E; curIvar = *I, ++I)
1307 curIvar->setNextIvar(*I);
1308 }
1309 }
1310 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001311 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001312
1313 // cached and complete!
1314 if (!data().IvarListMissingImplementation)
1315 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001316
1317 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001318 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001319 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001320 SmallVector<SynthesizeIvarChunk, 16> layout;
1321 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1322 E = ImplDecl->ivar_end(); I != E; ++I) {
1323 ObjCIvarDecl *IV = *I;
1324 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1325 layout.push_back(SynthesizeIvarChunk(
1326 IV->getASTContext().getTypeSize(IV->getType()), IV));
1327 continue;
1328 }
1329 if (!data().IvarList)
1330 data().IvarList = *I;
1331 else
1332 curIvar->setNextIvar(*I);
1333 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001334 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001335
1336 if (!layout.empty()) {
1337 // Order synthesized ivars by their size.
1338 std::stable_sort(layout.begin(), layout.end());
1339 unsigned Ix = 0, EIx = layout.size();
1340 if (!data().IvarList) {
1341 data().IvarList = layout[0].Ivar; Ix++;
1342 curIvar = data().IvarList;
1343 }
1344 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1345 curIvar->setNextIvar(layout[Ix].Ivar);
1346 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001347 }
1348 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001349 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001350}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001351
1352/// FindCategoryDeclaration - Finds category declaration in the list of
1353/// categories for this class and returns it. Name of the category is passed
1354/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001355///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001356ObjCCategoryDecl *
1357ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001358 // FIXME: Should make sure no callers ever do this.
1359 if (!hasDefinition())
1360 return 0;
1361
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001362 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001363 LoadExternalDefinition();
1364
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001365 for (visible_categories_iterator Cat = visible_categories_begin(),
1366 CatEnd = visible_categories_end();
1367 Cat != CatEnd;
1368 ++Cat) {
1369 if (Cat->getIdentifier() == CategoryId)
1370 return *Cat;
1371 }
1372
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001373 return 0;
1374}
1375
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001376ObjCMethodDecl *
1377ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001378 for (visible_categories_iterator Cat = visible_categories_begin(),
1379 CatEnd = visible_categories_end();
1380 Cat != CatEnd;
1381 ++Cat) {
1382 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001383 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1384 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001385 }
1386
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001387 return 0;
1388}
1389
1390ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001391 for (visible_categories_iterator Cat = visible_categories_begin(),
1392 CatEnd = visible_categories_end();
1393 Cat != CatEnd;
1394 ++Cat) {
1395 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001396 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1397 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001398 }
1399
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001400 return 0;
1401}
1402
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001403/// ClassImplementsProtocol - Checks that 'lProto' protocol
1404/// has been implemented in IDecl class, its super class or categories (if
1405/// lookupCategory is true).
1406bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1407 bool lookupCategory,
1408 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001409 if (!hasDefinition())
1410 return false;
1411
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001412 ObjCInterfaceDecl *IDecl = this;
1413 // 1st, look up the class.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +00001414 for (ObjCInterfaceDecl::protocol_iterator
1415 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001416 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1417 return true;
1418 // This is dubious and is added to be compatible with gcc. In gcc, it is
1419 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1420 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1421 // object. This IMO, should be a bug.
1422 // FIXME: Treat this as an extension, and flag this as an error when GCC
1423 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001424 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001425 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1426 return true;
1427 }
Mike Stump11289f42009-09-09 15:08:12 +00001428
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001429 // 2nd, look up the category.
1430 if (lookupCategory)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001431 for (visible_categories_iterator Cat = visible_categories_begin(),
1432 CatEnd = visible_categories_end();
1433 Cat != CatEnd;
1434 ++Cat) {
1435 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1436 E = Cat->protocol_end();
1437 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001438 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1439 return true;
1440 }
Mike Stump11289f42009-09-09 15:08:12 +00001441
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001442 // 3rd, look up the super class(s)
1443 if (IDecl->getSuperClass())
1444 return
1445 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1446 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001447
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001448 return false;
1449}
1450
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001451//===----------------------------------------------------------------------===//
1452// ObjCIvarDecl
1453//===----------------------------------------------------------------------===//
1454
David Blaikie68e081d2011-12-20 02:48:34 +00001455void ObjCIvarDecl::anchor() { }
1456
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001457ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001458 SourceLocation StartLoc,
1459 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001460 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001461 AccessControl ac, Expr *BW,
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00001462 bool synthesized,
1463 bool backingIvarReferencedInAccessor) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001464 if (DC) {
1465 // Ivar's can only appear in interfaces, implementations (via synthesized
1466 // properties), and class extensions (via direct declaration, or synthesized
1467 // properties).
1468 //
1469 // FIXME: This should really be asserting this:
1470 // (isa<ObjCCategoryDecl>(DC) &&
1471 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1472 // but unfortunately we sometimes place ivars into non-class extension
1473 // categories on error. This breaks an AST invariant, and should not be
1474 // fixed.
1475 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1476 isa<ObjCCategoryDecl>(DC)) &&
1477 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001478 // Once a new ivar is created in any of class/class-extension/implementation
1479 // decl contexts, the previously built IvarList must be rebuilt.
1480 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1481 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001482 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001483 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001484 else
1485 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001486 }
1487 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001488 }
1489
Richard Smithf7981722013-11-22 09:01:48 +00001490 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1491 synthesized, backingIvarReferencedInAccessor);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001492}
1493
Douglas Gregor72172e92012-01-05 21:55:30 +00001494ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001495 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1496 QualType(), 0, ObjCIvarDecl::None, 0, false,
1497 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001498}
1499
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001500const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1501 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001502
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001503 switch (DC->getKind()) {
1504 default:
1505 case ObjCCategoryImpl:
1506 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001507 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001508
1509 // Ivars can only appear in class extension categories.
1510 case ObjCCategory: {
1511 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1512 assert(CD->IsClassExtension() && "invalid container for ivar!");
1513 return CD->getClassInterface();
1514 }
1515
1516 case ObjCImplementation:
1517 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1518
1519 case ObjCInterface:
1520 return cast<ObjCInterfaceDecl>(DC);
1521 }
1522}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001523
1524//===----------------------------------------------------------------------===//
1525// ObjCAtDefsFieldDecl
1526//===----------------------------------------------------------------------===//
1527
David Blaikie68e081d2011-12-20 02:48:34 +00001528void ObjCAtDefsFieldDecl::anchor() { }
1529
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001530ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001531*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1532 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001533 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001534 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001535}
1536
Richard Smithf7981722013-11-22 09:01:48 +00001537ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001538 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001539 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1540 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001541}
1542
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001543//===----------------------------------------------------------------------===//
1544// ObjCProtocolDecl
1545//===----------------------------------------------------------------------===//
1546
David Blaikie68e081d2011-12-20 02:48:34 +00001547void ObjCProtocolDecl::anchor() { }
1548
Douglas Gregor32c17572012-01-01 20:30:41 +00001549ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1550 SourceLocation nameLoc,
1551 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001552 ObjCProtocolDecl *PrevDecl)
1553 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001554{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001555 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001556 if (PrevDecl)
1557 Data = PrevDecl->Data;
1558}
1559
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001560ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001561 IdentifierInfo *Id,
1562 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001563 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001564 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001565 ObjCProtocolDecl *Result =
1566 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001567 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001568 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001569}
1570
Richard Smithf7981722013-11-22 09:01:48 +00001571ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001572 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001573 ObjCProtocolDecl *Result =
1574 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001575 Result->Data.setInt(!C.getLangOpts().Modules);
1576 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001577}
1578
Steve Naroff114aecb2009-03-01 16:12:44 +00001579ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1580 ObjCProtocolDecl *PDecl = this;
1581
1582 if (Name == getIdentifier())
1583 return PDecl;
1584
1585 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1586 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1587 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001588
Steve Naroff114aecb2009-03-01 16:12:44 +00001589 return NULL;
1590}
1591
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001592// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001593// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001594ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1595 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001596 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001597
Douglas Gregoreed49792013-01-17 00:38:46 +00001598 // If there is no definition or the definition is hidden, we don't find
1599 // anything.
1600 const ObjCProtocolDecl *Def = getDefinition();
1601 if (!Def || Def->isHidden())
1602 return NULL;
1603
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001604 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001605 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001606
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001607 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001608 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001609 return MethodDecl;
1610 return NULL;
1611}
1612
Douglas Gregore6e48b12012-01-01 19:29:29 +00001613void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001614 assert(!Data.getPointer() && "Protocol already has a definition!");
1615 Data.setPointer(new (getASTContext()) DefinitionData);
1616 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001617}
1618
1619void ObjCProtocolDecl::startDefinition() {
1620 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001621
1622 // Update all of the declarations with a pointer to the definition.
1623 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1624 RD != RDEnd; ++RD)
1625 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001626}
1627
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001628void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1629 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001630
1631 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1632 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1633 E = PDecl->prop_end(); P != E; ++P) {
1634 ObjCPropertyDecl *Prop = *P;
1635 // Insert into PM if not there already.
1636 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001637 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001638 }
1639 // Scan through protocol's protocols.
1640 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1641 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001642 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001643 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001644}
1645
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001646
1647void ObjCProtocolDecl::collectInheritedProtocolProperties(
1648 const ObjCPropertyDecl *Property,
1649 ProtocolPropertyMap &PM) const {
1650 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1651 bool MatchFound = false;
1652 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1653 E = PDecl->prop_end(); P != E; ++P) {
1654 ObjCPropertyDecl *Prop = *P;
1655 if (Prop == Property)
1656 continue;
1657 if (Prop->getIdentifier() == Property->getIdentifier()) {
1658 PM[PDecl] = Prop;
1659 MatchFound = true;
1660 break;
1661 }
1662 }
1663 // Scan through protocol's protocols which did not have a matching property.
1664 if (!MatchFound)
1665 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1666 E = PDecl->protocol_end(); PI != E; ++PI)
1667 (*PI)->collectInheritedProtocolProperties(Property, PM);
1668 }
1669}
Anna Zaks673d76b2012-10-18 19:17:53 +00001670
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001671//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001672// ObjCCategoryDecl
1673//===----------------------------------------------------------------------===//
1674
David Blaikie68e081d2011-12-20 02:48:34 +00001675void ObjCCategoryDecl::anchor() { }
1676
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001677ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001678 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001679 SourceLocation ClassNameLoc,
1680 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001681 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001682 ObjCInterfaceDecl *IDecl,
1683 SourceLocation IvarLBraceLoc,
1684 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001685 ObjCCategoryDecl *CatDecl =
1686 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1687 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001688 if (IDecl) {
1689 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001690 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001691 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001692 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001693 if (ASTMutationListener *L = C.getASTMutationListener())
1694 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1695 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001696 }
1697
1698 return CatDecl;
1699}
1700
Richard Smithf7981722013-11-22 09:01:48 +00001701ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001702 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001703 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1704 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001705}
1706
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001707ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1708 return getASTContext().getObjCImplementation(
1709 const_cast<ObjCCategoryDecl*>(this));
1710}
1711
1712void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1713 getASTContext().setObjCImplementation(this, ImplD);
1714}
1715
1716
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001717//===----------------------------------------------------------------------===//
1718// ObjCCategoryImplDecl
1719//===----------------------------------------------------------------------===//
1720
David Blaikie68e081d2011-12-20 02:48:34 +00001721void ObjCCategoryImplDecl::anchor() { }
1722
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001723ObjCCategoryImplDecl *
1724ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001725 IdentifierInfo *Id,
1726 ObjCInterfaceDecl *ClassInterface,
1727 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001728 SourceLocation atStartLoc,
1729 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001730 if (ClassInterface && ClassInterface->hasDefinition())
1731 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001732 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1733 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001734}
1735
Douglas Gregor72172e92012-01-05 21:55:30 +00001736ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1737 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001738 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1739 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001740}
1741
Steve Narofff406f4d2009-10-29 21:11:04 +00001742ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001743 // The class interface might be NULL if we are working with invalid code.
1744 if (const ObjCInterfaceDecl *ID = getClassInterface())
1745 return ID->FindCategoryDeclaration(getIdentifier());
1746 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001747}
1748
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001749
David Blaikie68e081d2011-12-20 02:48:34 +00001750void ObjCImplDecl::anchor() { }
1751
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001752void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001753 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001754 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001755 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001756}
1757
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001758void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1759 ASTContext &Ctx = getASTContext();
1760
1761 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001762 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001763 if (IFace)
1764 Ctx.setObjCImplementation(IFace, ImplD);
1765
Duncan Sands49c29ee2009-07-21 07:56:29 +00001766 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001767 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1768 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1769 Ctx.setObjCImplementation(CD, ImplD);
1770 }
1771
1772 ClassInterface = IFace;
1773}
1774
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001775/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001776/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001777/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001778///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001779ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001780FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1781 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001782 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001783 if (PID->getPropertyIvarDecl() &&
1784 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1785 return PID;
1786 }
1787 return 0;
1788}
1789
1790/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001791/// added to the list of those properties \@synthesized/\@dynamic in this
1792/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001793///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001794ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001795FindPropertyImplDecl(IdentifierInfo *Id) const {
1796 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001797 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001798 if (PID->getPropertyDecl()->getIdentifier() == Id)
1799 return PID;
1800 }
1801 return 0;
1802}
1803
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001804raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001805 const ObjCCategoryImplDecl &CID) {
1806 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001807 return OS;
1808}
1809
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001810//===----------------------------------------------------------------------===//
1811// ObjCImplementationDecl
1812//===----------------------------------------------------------------------===//
1813
David Blaikie68e081d2011-12-20 02:48:34 +00001814void ObjCImplementationDecl::anchor() { }
1815
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001816ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001817ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001818 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001819 ObjCInterfaceDecl *SuperDecl,
1820 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001821 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001822 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001823 SourceLocation IvarLBraceLoc,
1824 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001825 if (ClassInterface && ClassInterface->hasDefinition())
1826 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001827 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1828 nameLoc, atStartLoc, superLoc,
1829 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001830}
1831
Douglas Gregor72172e92012-01-05 21:55:30 +00001832ObjCImplementationDecl *
1833ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001834 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1835 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001836}
1837
John McCall0410e572011-07-22 04:15:06 +00001838void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1839 CXXCtorInitializer ** initializers,
1840 unsigned numInitializers) {
1841 if (numInitializers > 0) {
1842 NumIvarInitializers = numInitializers;
1843 CXXCtorInitializer **ivarInitializers =
1844 new (C) CXXCtorInitializer*[NumIvarInitializers];
1845 memcpy(ivarInitializers, initializers,
1846 numInitializers * sizeof(CXXCtorInitializer*));
1847 IvarInitializers = ivarInitializers;
1848 }
1849}
1850
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001851raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001852 const ObjCImplementationDecl &ID) {
1853 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001854 return OS;
1855}
1856
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001857//===----------------------------------------------------------------------===//
1858// ObjCCompatibleAliasDecl
1859//===----------------------------------------------------------------------===//
1860
David Blaikie68e081d2011-12-20 02:48:34 +00001861void ObjCCompatibleAliasDecl::anchor() { }
1862
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001863ObjCCompatibleAliasDecl *
1864ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1865 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001866 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001867 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001868 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001869}
1870
Douglas Gregor72172e92012-01-05 21:55:30 +00001871ObjCCompatibleAliasDecl *
1872ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001873 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001874}
1875
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001876//===----------------------------------------------------------------------===//
1877// ObjCPropertyDecl
1878//===----------------------------------------------------------------------===//
1879
David Blaikie68e081d2011-12-20 02:48:34 +00001880void ObjCPropertyDecl::anchor() { }
1881
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001882ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1883 SourceLocation L,
1884 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001885 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001886 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001887 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001888 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001889 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001890}
1891
Richard Smithf7981722013-11-22 09:01:48 +00001892ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001893 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001894 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1895 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001896}
1897
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001898//===----------------------------------------------------------------------===//
1899// ObjCPropertyImplDecl
1900//===----------------------------------------------------------------------===//
1901
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001902ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001903 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001904 SourceLocation atLoc,
1905 SourceLocation L,
1906 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001907 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001908 ObjCIvarDecl *ivar,
1909 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001910 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1911 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001912}
Chris Lattnered0e1642008-03-17 01:19:02 +00001913
Richard Smithf7981722013-11-22 09:01:48 +00001914ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001915 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001916 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1917 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001918}
1919
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001920SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1921 SourceLocation EndLoc = getLocation();
1922 if (IvarLoc.isValid())
1923 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001924
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001925 return SourceRange(AtLoc, EndLoc);
1926}