blob: cb58ad482e101018df9385964279182698544df3 [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 Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000383void ObjCInterfaceDecl::getDesignatedInitializers(
384 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
385 assert(hasDefinition());
386 if (data().ExternallyCompleted)
387 LoadExternalDefinition();
388
389 const ObjCInterfaceDecl *IFace = this;
390 while (IFace) {
391 if (IFace->data().HasDesignatedInitializers)
392 break;
393 IFace = IFace->getSuperClass();
394 }
395
396 if (!IFace)
397 return;
398 for (instmeth_iterator I = IFace->instmeth_begin(),
399 E = IFace->instmeth_end(); I != E; ++I) {
400 const ObjCMethodDecl *MD = *I;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000401 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000402 Methods.push_back(MD);
403 }
404}
405
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000406bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
407 const ObjCMethodDecl **InitMethod) const {
408 assert(hasDefinition());
409 if (data().ExternallyCompleted)
410 LoadExternalDefinition();
411
412 const ObjCInterfaceDecl *IFace = this;
413 while (IFace) {
414 if (IFace->data().HasDesignatedInitializers)
415 break;
416 IFace = IFace->getSuperClass();
417 }
418
419 if (!IFace)
420 return false;
421
422 if (const ObjCMethodDecl *MD = IFace->lookupMethod(Sel, /*isInstance=*/true,
423 /*shallowCategoryLookup=*/true,
424 /*followSuper=*/false)) {
425 if (MD->isThisDeclarationADesignatedInitializer()) {
426 if (InitMethod)
427 *InitMethod = MD;
428 return true;
429 }
430 }
431 return false;
432}
433
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000434void ObjCInterfaceDecl::allocateDefinitionData() {
435 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000436 Data.setPointer(new (getASTContext()) DefinitionData());
437 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000438
439 // Make the type point at the definition, now that we have one.
440 if (TypeForDecl)
441 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000442}
443
444void ObjCInterfaceDecl::startDefinition() {
445 allocateDefinitionData();
446
Douglas Gregor66b310c2011-12-15 18:03:09 +0000447 // Update all of the declarations with a pointer to the definition.
448 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
449 RD != RDEnd; ++RD) {
450 if (*RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000451 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000452 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000453}
454
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000455ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
456 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000457 // FIXME: Should make sure no callers ever do this.
458 if (!hasDefinition())
459 return 0;
460
461 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000462 LoadExternalDefinition();
463
Chris Lattner89375192008-03-16 00:19:01 +0000464 ObjCInterfaceDecl* ClassDecl = this;
465 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000466 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000467 clsDeclared = ClassDecl;
468 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000469 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000470
471 for (ObjCInterfaceDecl::visible_extensions_iterator
472 Ext = ClassDecl->visible_extensions_begin(),
473 ExtEnd = ClassDecl->visible_extensions_end();
474 Ext != ExtEnd; ++Ext) {
475 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000476 clsDeclared = ClassDecl;
477 return I;
478 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000479 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000480
Chris Lattner89375192008-03-16 00:19:01 +0000481 ClassDecl = ClassDecl->getSuperClass();
482 }
483 return NULL;
484}
485
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000486/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
487/// class whose name is passed as argument. If it is not one of the super classes
488/// the it returns NULL.
489ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
490 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000491 // FIXME: Should make sure no callers ever do this.
492 if (!hasDefinition())
493 return 0;
494
495 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000496 LoadExternalDefinition();
497
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000498 ObjCInterfaceDecl* ClassDecl = this;
499 while (ClassDecl != NULL) {
500 if (ClassDecl->getIdentifier() == ICName)
501 return ClassDecl;
502 ClassDecl = ClassDecl->getSuperClass();
503 }
504 return NULL;
505}
506
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000507ObjCProtocolDecl *
508ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
509 for (ObjCInterfaceDecl::all_protocol_iterator P =
510 all_referenced_protocol_begin(), PE = all_referenced_protocol_end();
511 P != PE; ++P)
512 if ((*P)->lookupProtocolNamed(Name))
513 return (*P);
514 ObjCInterfaceDecl *SuperClass = getSuperClass();
515 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
516}
517
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000518/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000519/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000520/// When argument category "C" is specified, any implicit method found
521/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000522ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000523 bool isInstance,
524 bool shallowCategoryLookup,
525 bool followSuper,
Ted Kremenek28eace62013-11-23 01:01:34 +0000526 const ObjCCategoryDecl *C,
527 const ObjCProtocolDecl *P) const
Ted Kremenek00781502013-11-23 01:01:29 +0000528{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000529 // FIXME: Should make sure no callers ever do this.
530 if (!hasDefinition())
531 return 0;
532
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000533 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000534 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000535
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000536 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000537 LoadExternalDefinition();
538
Ted Kremenek00781502013-11-23 01:01:29 +0000539 while (ClassDecl) {
Ted Kremenek28eace62013-11-23 01:01:34 +0000540 // If we are looking for a method that is part of protocol conformance,
541 // check if the superclass has been marked to suppress conformance
542 // of that protocol.
Ted Kremenek33f50432013-11-23 22:29:06 +0000543 if (P && ClassDecl->shouldSuppressProtocol(P))
544 return 0;
Ted Kremenek28eace62013-11-23 01:01:34 +0000545
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000546 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000547 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000548
Chris Lattner89375192008-03-16 00:19:01 +0000549 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +0000550 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
551 E = ClassDecl->protocol_end();
552 I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000553 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000554 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000555
556 // Didn't find one yet - now look through categories.
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000557 for (ObjCInterfaceDecl::visible_categories_iterator
558 Cat = ClassDecl->visible_categories_begin(),
559 CatEnd = ClassDecl->visible_categories_end();
560 Cat != CatEnd; ++Cat) {
561 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
562 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000563 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000564
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000565 if (!shallowCategoryLookup) {
566 // Didn't find one yet - look through protocols.
567 const ObjCList<ObjCProtocolDecl> &Protocols =
568 Cat->getReferencedProtocols();
569 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
570 E = Protocols.end(); I != E; ++I)
571 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
572 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000573 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000574 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000575 }
Ted Kremenek00781502013-11-23 01:01:29 +0000576
577 if (!followSuper)
578 return NULL;
579
580 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000581 ClassDecl = ClassDecl->getSuperClass();
582 }
583 return NULL;
584}
585
Anna Zaksc77a3b12012-07-27 19:07:44 +0000586// Will search "local" class/category implementations for a method decl.
587// If failed, then we search in class's root for an instance method.
588// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000589ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
590 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000591 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000592 // FIXME: Should make sure no callers ever do this.
593 if (!hasDefinition())
594 return 0;
595
596 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000597 LoadExternalDefinition();
598
Steve Naroffbb69c942009-10-01 23:46:04 +0000599 ObjCMethodDecl *Method = 0;
600 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000601 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
602 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000603
604 // Look through local category implementations associated with the class.
605 if (!Method)
606 Method = Instance ? getCategoryInstanceMethod(Sel)
607 : getCategoryClassMethod(Sel);
608
609 // Before we give up, check if the selector is an instance method.
610 // But only in the root. This matches gcc's behavior and what the
611 // runtime expects.
612 if (!Instance && !Method && !getSuperClass()) {
613 Method = lookupInstanceMethod(Sel);
614 // Look through local category implementations associated
615 // with the root class.
616 if (!Method)
617 Method = lookupPrivateMethod(Sel, true);
618 }
619
Steve Naroffbb69c942009-10-01 23:46:04 +0000620 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000621 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000622 return Method;
623}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000624
625//===----------------------------------------------------------------------===//
626// ObjCMethodDecl
627//===----------------------------------------------------------------------===//
628
629ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000630 SourceLocation beginLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000631 SourceLocation endLoc,
632 Selector SelInfo, QualType T,
Douglas Gregor12852d92010-03-08 14:59:44 +0000633 TypeSourceInfo *ResultTInfo,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000634 DeclContext *contextDecl,
635 bool isInstance,
636 bool isVariadic,
Jordan Rosed01e83a2012-10-10 16:42:25 +0000637 bool isPropertyAccessor,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +0000638 bool isImplicitlyDeclared,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000639 bool isDefined,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000640 ImplementationControl impControl,
Argyrios Kyrtzidis38493942011-10-03 06:36:29 +0000641 bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000642 return new (C, contextDecl) ObjCMethodDecl(
643 beginLoc, endLoc, SelInfo, T, ResultTInfo, contextDecl, isInstance,
644 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
645 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000646}
647
Douglas Gregor72172e92012-01-05 21:55:30 +0000648ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000649 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
650 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000651}
652
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000653bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
654 return getMethodFamily() == OMF_init &&
655 hasAttr<ObjCDesignatedInitializerAttr>();
656}
657
658bool ObjCMethodDecl::isDesignatedInitializerForTheInterface() const {
659 const DeclContext *DC = getDeclContext();
660 if (isa<ObjCProtocolDecl>(DC))
661 return false;
662 if (const ObjCInterfaceDecl *ID = getClassInterface())
663 return ID->isDesignatedInitializer(getSelector());
664 return false;
665}
666
Douglas Gregora6017bb2012-10-09 17:21:28 +0000667Stmt *ObjCMethodDecl::getBody() const {
668 return Body.get(getASTContext().getExternalSource());
669}
670
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000671void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
672 assert(PrevMethod);
673 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
674 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000675 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000676}
677
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000678void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
679 ArrayRef<ParmVarDecl*> Params,
680 ArrayRef<SourceLocation> SelLocs) {
681 ParamsAndSelLocs = 0;
682 NumParams = Params.size();
683 if (Params.empty() && SelLocs.empty())
684 return;
685
686 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
687 sizeof(SourceLocation) * SelLocs.size();
688 ParamsAndSelLocs = C.Allocate(Size);
689 std::copy(Params.begin(), Params.end(), getParams());
690 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
691}
692
693void ObjCMethodDecl::getSelectorLocs(
694 SmallVectorImpl<SourceLocation> &SelLocs) const {
695 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
696 SelLocs.push_back(getSelectorLoc(i));
697}
698
699void ObjCMethodDecl::setMethodParams(ASTContext &C,
700 ArrayRef<ParmVarDecl*> Params,
701 ArrayRef<SourceLocation> SelLocs) {
702 assert((!SelLocs.empty() || isImplicit()) &&
703 "No selector locs for non-implicit method");
704 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000705 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000706
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000707 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
708 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000709 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000710 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000711
712 setParamsAndSelLocs(C, Params, SelLocs);
713}
714
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000715/// \brief A definition will return its interface declaration.
716/// An interface declaration will return its definition.
717/// Otherwise it will return itself.
718ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
719 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000720 ObjCMethodDecl *Redecl = 0;
721 if (HasRedeclaration)
722 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000723 if (Redecl)
724 return Redecl;
725
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000726 Decl *CtxD = cast<Decl>(getDeclContext());
727
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000728 if (!CtxD->isInvalidDecl()) {
729 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
730 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
731 if (!ImplD->isInvalidDecl())
732 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000733
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000734 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
735 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
736 if (!ImplD->isInvalidDecl())
737 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000738
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000739 } else if (ObjCImplementationDecl *ImplD =
740 dyn_cast<ObjCImplementationDecl>(CtxD)) {
741 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
742 if (!IFD->isInvalidDecl())
743 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000744
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000745 } else if (ObjCCategoryImplDecl *CImplD =
746 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
747 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
748 if (!CatD->isInvalidDecl())
749 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
750 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000751 }
752
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000753 if (!Redecl && isRedeclaration()) {
754 // This is the last redeclaration, go back to the first method.
755 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
756 isInstanceMethod());
757 }
758
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000759 return Redecl ? Redecl : this;
760}
761
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000762ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
763 Decl *CtxD = cast<Decl>(getDeclContext());
764
765 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
766 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
767 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
768 isInstanceMethod()))
769 return MD;
770
771 } else if (ObjCCategoryImplDecl *CImplD =
772 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000773 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000774 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
775 isInstanceMethod()))
776 return MD;
777 }
778
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000779 if (isRedeclaration())
780 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
781 isInstanceMethod());
782
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000783 return this;
784}
785
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000786SourceLocation ObjCMethodDecl::getLocEnd() const {
787 if (Stmt *Body = getBody())
788 return Body->getLocEnd();
789 return DeclEndLoc;
790}
791
John McCallb4526252011-03-02 01:50:55 +0000792ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
793 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000794 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000795 return family;
796
John McCall86bc21f2011-03-02 11:33:24 +0000797 // Check for an explicit attribute.
798 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
799 // The unfortunate necessity of mapping between enums here is due
800 // to the attributes framework.
801 switch (attr->getFamily()) {
802 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
803 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
804 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
805 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
806 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
807 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
808 }
809 Family = static_cast<unsigned>(family);
810 return family;
811 }
812
John McCallb4526252011-03-02 01:50:55 +0000813 family = getSelector().getMethodFamily();
814 switch (family) {
815 case OMF_None: break;
816
817 // init only has a conventional meaning for an instance method, and
818 // it has to return an object.
819 case OMF_init:
820 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
821 family = OMF_None;
822 break;
823
824 // alloc/copy/new have a conventional meaning for both class and
825 // instance methods, but they require an object return.
826 case OMF_alloc:
827 case OMF_copy:
828 case OMF_mutableCopy:
829 case OMF_new:
830 if (!getResultType()->isObjCObjectPointerType())
831 family = OMF_None;
832 break;
833
834 // These selectors have a conventional meaning only for instance methods.
835 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000836 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000837 case OMF_retain:
838 case OMF_release:
839 case OMF_autorelease:
840 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000841 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000842 if (!isInstanceMethod())
843 family = OMF_None;
844 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000845
846 case OMF_performSelector:
847 if (!isInstanceMethod() ||
848 !getResultType()->isObjCIdType())
849 family = OMF_None;
850 else {
851 unsigned noParams = param_size();
852 if (noParams < 1 || noParams > 3)
853 family = OMF_None;
854 else {
855 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
856 QualType ArgT = (*it);
857 if (!ArgT->isObjCSelType()) {
858 family = OMF_None;
859 break;
860 }
861 while (--noParams) {
862 it++;
863 ArgT = (*it);
864 if (!ArgT->isObjCIdType()) {
865 family = OMF_None;
866 break;
867 }
868 }
869 }
870 }
871 break;
872
John McCallb4526252011-03-02 01:50:55 +0000873 }
874
875 // Cache the result.
876 Family = static_cast<unsigned>(family);
877 return family;
878}
879
Mike Stump11289f42009-09-09 15:08:12 +0000880void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000881 const ObjCInterfaceDecl *OID) {
882 QualType selfTy;
883 if (isInstanceMethod()) {
884 // There may be no interface context due to error in declaration
885 // of the interface (which has been reported). Recover gracefully.
886 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000887 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000888 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000889 } else {
890 selfTy = Context.getObjCIdType();
891 }
892 } else // we have a factory method.
893 selfTy = Context.getObjCClassType();
894
John McCalld4631322011-06-17 06:42:21 +0000895 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000896 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000897
David Blaikiebbafb8a2012-03-11 07:00:24 +0000898 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000899 if (isInstanceMethod()) {
900 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000901
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000902 // 'self' is always __strong. It's actually pseudo-strong except
903 // in init methods (or methods labeled ns_consumes_self), though.
904 Qualifiers qs;
905 qs.setObjCLifetime(Qualifiers::OCL_Strong);
906 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000907
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000908 // In addition, 'self' is const unless this is an init method.
909 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
910 selfTy = selfTy.withConst();
911 selfIsPseudoStrong = true;
912 }
913 }
914 else {
915 assert(isClassMethod());
916 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000917 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000918 selfIsPseudoStrong = true;
919 }
John McCall31168b02011-06-15 23:02:42 +0000920 }
921
922 ImplicitParamDecl *self
923 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
924 &Context.Idents.get("self"), selfTy);
925 setSelfDecl(self);
926
927 if (selfIsConsumed)
928 self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000929
John McCalld4631322011-06-17 06:42:21 +0000930 if (selfIsPseudoStrong)
931 self->setARCPseudoStrong(true);
932
Mike Stump11289f42009-09-09 15:08:12 +0000933 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
934 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000935 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000936}
937
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000938ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
939 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
940 return ID;
941 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
942 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000943 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000944 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000945
946 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikie83d382b2011-09-23 05:06:16 +0000947 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000948}
949
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000950static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
951 const ObjCMethodDecl *Method,
952 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
953 bool MovedToSuper) {
954 if (!Container)
955 return;
956
957 // In categories look for overriden methods from protocols. A method from
958 // category is not "overriden" since it is considered as the "same" method
959 // (same USR) as the one from the interface.
960 if (const ObjCCategoryDecl *
961 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
962 // Check whether we have a matching method at this category but only if we
963 // are at the super class level.
964 if (MovedToSuper)
965 if (ObjCMethodDecl *
966 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000967 Method->isInstanceMethod(),
968 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000969 if (Method != Overridden) {
970 // We found an override at this category; there is no need to look
971 // into its protocols.
972 Methods.push_back(Overridden);
973 return;
974 }
975
976 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
977 PEnd = Category->protocol_end();
978 P != PEnd; ++P)
979 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
980 return;
981 }
982
983 // Check whether we have a matching method at this level.
984 if (const ObjCMethodDecl *
985 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000986 Method->isInstanceMethod(),
987 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000988 if (Method != Overridden) {
989 // We found an override at this level; there is no need to look
990 // into other protocols or categories.
991 Methods.push_back(Overridden);
992 return;
993 }
994
995 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
996 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
997 PEnd = Protocol->protocol_end();
998 P != PEnd; ++P)
999 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1000 }
1001
1002 if (const ObjCInterfaceDecl *
1003 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
1004 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
1005 PEnd = Interface->protocol_end();
1006 P != PEnd; ++P)
1007 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1008
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001009 for (ObjCInterfaceDecl::known_categories_iterator
1010 Cat = Interface->known_categories_begin(),
1011 CatEnd = Interface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001012 Cat != CatEnd; ++Cat) {
1013 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001014 MovedToSuper);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001015 }
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001016
1017 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1018 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1019 /*MovedToSuper=*/true);
1020 }
1021}
1022
1023static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1024 const ObjCMethodDecl *Method,
1025 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1026 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1027 /*MovedToSuper=*/false);
1028}
1029
1030static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1031 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1032 assert(Method->isOverriding());
1033
1034 if (const ObjCProtocolDecl *
1035 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1036 CollectOverriddenMethods(ProtD, Method, overridden);
1037
1038 } else if (const ObjCImplDecl *
1039 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1040 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1041 if (!ID)
1042 return;
1043 // Start searching for overridden methods using the method from the
1044 // interface as starting point.
1045 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001046 Method->isInstanceMethod(),
1047 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001048 Method = IFaceMeth;
1049 CollectOverriddenMethods(ID, Method, overridden);
1050
1051 } else if (const ObjCCategoryDecl *
1052 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1053 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1054 if (!ID)
1055 return;
1056 // Start searching for overridden methods using the method from the
1057 // interface as starting point.
1058 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001059 Method->isInstanceMethod(),
1060 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001061 Method = IFaceMeth;
1062 CollectOverriddenMethods(ID, Method, overridden);
1063
1064 } else {
1065 CollectOverriddenMethods(
1066 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1067 Method, overridden);
1068 }
1069}
1070
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001071void ObjCMethodDecl::getOverriddenMethods(
1072 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1073 const ObjCMethodDecl *Method = this;
1074
1075 if (Method->isRedeclaration()) {
1076 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1077 getMethod(Method->getSelector(), Method->isInstanceMethod());
1078 }
1079
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001080 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001081 collectOverriddenMethodsSlow(Method, Overridden);
1082 assert(!Overridden.empty() &&
1083 "ObjCMethodDecl's overriding bit is not as expected");
1084 }
1085}
1086
Jordan Rose2bd991a2012-10-10 16:42:54 +00001087const ObjCPropertyDecl *
1088ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1089 Selector Sel = getSelector();
1090 unsigned NumArgs = Sel.getNumArgs();
1091 if (NumArgs > 1)
1092 return 0;
1093
Jordan Rose59e34ec2012-10-11 16:02:02 +00001094 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001095 return 0;
1096
1097 if (isPropertyAccessor()) {
1098 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001099 // If container is class extension, find its primary class.
1100 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1101 if (CatDecl->IsClassExtension())
1102 Container = CatDecl->getClassInterface();
1103
Jordan Rose2bd991a2012-10-10 16:42:54 +00001104 bool IsGetter = (NumArgs == 0);
1105
1106 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1107 E = Container->prop_end();
1108 I != E; ++I) {
1109 Selector NextSel = IsGetter ? (*I)->getGetterName()
1110 : (*I)->getSetterName();
1111 if (NextSel == Sel)
1112 return *I;
1113 }
1114
1115 llvm_unreachable("Marked as a property accessor but no property found!");
1116 }
1117
1118 if (!CheckOverrides)
1119 return 0;
1120
1121 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1122 OverridesTy Overrides;
1123 getOverriddenMethods(Overrides);
1124 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1125 I != E; ++I) {
1126 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1127 return Prop;
1128 }
1129
1130 return 0;
1131
1132}
1133
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001134//===----------------------------------------------------------------------===//
1135// ObjCInterfaceDecl
1136//===----------------------------------------------------------------------===//
1137
Douglas Gregord53ae832012-01-17 18:09:05 +00001138ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001139 DeclContext *DC,
1140 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001141 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001142 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001143 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001144 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001145 ObjCInterfaceDecl *Result = new (C, DC)
1146 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001147 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001148 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001149 return Result;
1150}
1151
Richard Smithf7981722013-11-22 09:01:48 +00001152ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001153 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001154 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1155 0, SourceLocation(),
1156 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001157 Result->Data.setInt(!C.getLangOpts().Modules);
1158 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001159}
1160
1161ObjCInterfaceDecl::
1162ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001163 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1164 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001165 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001166 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001167{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001168 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001169
1170 // Copy the 'data' pointer over.
1171 if (PrevDecl)
1172 Data = PrevDecl->Data;
1173
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001174 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001175}
1176
Douglas Gregor73693022010-12-01 23:49:52 +00001177void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001178 assert(data().ExternallyCompleted && "Class is not externally completed");
1179 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001180 getASTContext().getExternalSource()->CompleteType(
1181 const_cast<ObjCInterfaceDecl *>(this));
1182}
1183
1184void ObjCInterfaceDecl::setExternallyCompleted() {
1185 assert(getASTContext().getExternalSource() &&
1186 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001187 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001188 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001189 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001190}
1191
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001192void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1193 assert(hasDefinition() && "Forward declarations can't contain methods");
1194 data().HasDesignatedInitializers = true;
1195}
1196
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001197ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001198 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1199 if (data().ExternallyCompleted)
1200 LoadExternalDefinition();
1201
1202 return getASTContext().getObjCImplementation(
1203 const_cast<ObjCInterfaceDecl*>(Def));
1204 }
1205
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001206 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001207 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001208}
1209
1210void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001211 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001212}
1213
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001214namespace {
1215 struct SynthesizeIvarChunk {
1216 uint64_t Size;
1217 ObjCIvarDecl *Ivar;
1218 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1219 : Size(size), Ivar(ivar) {}
1220 };
1221
1222 bool operator<(const SynthesizeIvarChunk & LHS,
1223 const SynthesizeIvarChunk &RHS) {
1224 return LHS.Size < RHS.Size;
1225 }
1226}
1227
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001228/// all_declared_ivar_begin - return first ivar declared in this class,
1229/// its extensions and its implementation. Lazily build the list on first
1230/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001231///
1232/// Caveat: The list returned by this method reflects the current
1233/// state of the parser. The cache will be updated for every ivar
1234/// added by an extension or the implementation when they are
1235/// encountered.
1236/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001237ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001238 // FIXME: Should make sure no callers ever do this.
1239 if (!hasDefinition())
1240 return 0;
1241
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001242 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001243 if (!data().IvarList) {
1244 if (!ivar_empty()) {
1245 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1246 data().IvarList = *I; ++I;
1247 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001248 curIvar->setNextIvar(*I);
1249 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001250
1251 for (ObjCInterfaceDecl::known_extensions_iterator
1252 Ext = known_extensions_begin(),
1253 ExtEnd = known_extensions_end();
1254 Ext != ExtEnd; ++Ext) {
1255 if (!Ext->ivar_empty()) {
1256 ObjCCategoryDecl::ivar_iterator
1257 I = Ext->ivar_begin(),
1258 E = Ext->ivar_end();
1259 if (!data().IvarList) {
1260 data().IvarList = *I; ++I;
1261 curIvar = data().IvarList;
1262 }
1263 for ( ;I != E; curIvar = *I, ++I)
1264 curIvar->setNextIvar(*I);
1265 }
1266 }
1267 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001268 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001269
1270 // cached and complete!
1271 if (!data().IvarListMissingImplementation)
1272 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001273
1274 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001275 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001276 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001277 SmallVector<SynthesizeIvarChunk, 16> layout;
1278 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1279 E = ImplDecl->ivar_end(); I != E; ++I) {
1280 ObjCIvarDecl *IV = *I;
1281 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1282 layout.push_back(SynthesizeIvarChunk(
1283 IV->getASTContext().getTypeSize(IV->getType()), IV));
1284 continue;
1285 }
1286 if (!data().IvarList)
1287 data().IvarList = *I;
1288 else
1289 curIvar->setNextIvar(*I);
1290 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001291 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001292
1293 if (!layout.empty()) {
1294 // Order synthesized ivars by their size.
1295 std::stable_sort(layout.begin(), layout.end());
1296 unsigned Ix = 0, EIx = layout.size();
1297 if (!data().IvarList) {
1298 data().IvarList = layout[0].Ivar; Ix++;
1299 curIvar = data().IvarList;
1300 }
1301 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1302 curIvar->setNextIvar(layout[Ix].Ivar);
1303 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001304 }
1305 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001306 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001307}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001308
1309/// FindCategoryDeclaration - Finds category declaration in the list of
1310/// categories for this class and returns it. Name of the category is passed
1311/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001312///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001313ObjCCategoryDecl *
1314ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001315 // FIXME: Should make sure no callers ever do this.
1316 if (!hasDefinition())
1317 return 0;
1318
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001319 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001320 LoadExternalDefinition();
1321
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001322 for (visible_categories_iterator Cat = visible_categories_begin(),
1323 CatEnd = visible_categories_end();
1324 Cat != CatEnd;
1325 ++Cat) {
1326 if (Cat->getIdentifier() == CategoryId)
1327 return *Cat;
1328 }
1329
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001330 return 0;
1331}
1332
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001333ObjCMethodDecl *
1334ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001335 for (visible_categories_iterator Cat = visible_categories_begin(),
1336 CatEnd = visible_categories_end();
1337 Cat != CatEnd;
1338 ++Cat) {
1339 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001340 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1341 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001342 }
1343
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001344 return 0;
1345}
1346
1347ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001348 for (visible_categories_iterator Cat = visible_categories_begin(),
1349 CatEnd = visible_categories_end();
1350 Cat != CatEnd;
1351 ++Cat) {
1352 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001353 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1354 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001355 }
1356
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001357 return 0;
1358}
1359
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001360/// ClassImplementsProtocol - Checks that 'lProto' protocol
1361/// has been implemented in IDecl class, its super class or categories (if
1362/// lookupCategory is true).
1363bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1364 bool lookupCategory,
1365 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001366 if (!hasDefinition())
1367 return false;
1368
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001369 ObjCInterfaceDecl *IDecl = this;
1370 // 1st, look up the class.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +00001371 for (ObjCInterfaceDecl::protocol_iterator
1372 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001373 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1374 return true;
1375 // This is dubious and is added to be compatible with gcc. In gcc, it is
1376 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1377 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1378 // object. This IMO, should be a bug.
1379 // FIXME: Treat this as an extension, and flag this as an error when GCC
1380 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001381 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001382 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1383 return true;
1384 }
Mike Stump11289f42009-09-09 15:08:12 +00001385
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001386 // 2nd, look up the category.
1387 if (lookupCategory)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001388 for (visible_categories_iterator Cat = visible_categories_begin(),
1389 CatEnd = visible_categories_end();
1390 Cat != CatEnd;
1391 ++Cat) {
1392 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1393 E = Cat->protocol_end();
1394 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001395 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1396 return true;
1397 }
Mike Stump11289f42009-09-09 15:08:12 +00001398
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001399 // 3rd, look up the super class(s)
1400 if (IDecl->getSuperClass())
1401 return
1402 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1403 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001404
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001405 return false;
1406}
1407
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001408//===----------------------------------------------------------------------===//
1409// ObjCIvarDecl
1410//===----------------------------------------------------------------------===//
1411
David Blaikie68e081d2011-12-20 02:48:34 +00001412void ObjCIvarDecl::anchor() { }
1413
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001414ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001415 SourceLocation StartLoc,
1416 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001417 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001418 AccessControl ac, Expr *BW,
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00001419 bool synthesized,
1420 bool backingIvarReferencedInAccessor) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001421 if (DC) {
1422 // Ivar's can only appear in interfaces, implementations (via synthesized
1423 // properties), and class extensions (via direct declaration, or synthesized
1424 // properties).
1425 //
1426 // FIXME: This should really be asserting this:
1427 // (isa<ObjCCategoryDecl>(DC) &&
1428 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1429 // but unfortunately we sometimes place ivars into non-class extension
1430 // categories on error. This breaks an AST invariant, and should not be
1431 // fixed.
1432 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1433 isa<ObjCCategoryDecl>(DC)) &&
1434 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001435 // Once a new ivar is created in any of class/class-extension/implementation
1436 // decl contexts, the previously built IvarList must be rebuilt.
1437 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1438 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001439 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001440 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001441 else
1442 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001443 }
1444 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001445 }
1446
Richard Smithf7981722013-11-22 09:01:48 +00001447 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1448 synthesized, backingIvarReferencedInAccessor);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001449}
1450
Douglas Gregor72172e92012-01-05 21:55:30 +00001451ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001452 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1453 QualType(), 0, ObjCIvarDecl::None, 0, false,
1454 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001455}
1456
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001457const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1458 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001459
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001460 switch (DC->getKind()) {
1461 default:
1462 case ObjCCategoryImpl:
1463 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001464 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001465
1466 // Ivars can only appear in class extension categories.
1467 case ObjCCategory: {
1468 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1469 assert(CD->IsClassExtension() && "invalid container for ivar!");
1470 return CD->getClassInterface();
1471 }
1472
1473 case ObjCImplementation:
1474 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1475
1476 case ObjCInterface:
1477 return cast<ObjCInterfaceDecl>(DC);
1478 }
1479}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001480
1481//===----------------------------------------------------------------------===//
1482// ObjCAtDefsFieldDecl
1483//===----------------------------------------------------------------------===//
1484
David Blaikie68e081d2011-12-20 02:48:34 +00001485void ObjCAtDefsFieldDecl::anchor() { }
1486
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001487ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001488*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1489 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001490 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001491 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001492}
1493
Richard Smithf7981722013-11-22 09:01:48 +00001494ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001495 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001496 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1497 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001498}
1499
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001500//===----------------------------------------------------------------------===//
1501// ObjCProtocolDecl
1502//===----------------------------------------------------------------------===//
1503
David Blaikie68e081d2011-12-20 02:48:34 +00001504void ObjCProtocolDecl::anchor() { }
1505
Douglas Gregor32c17572012-01-01 20:30:41 +00001506ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1507 SourceLocation nameLoc,
1508 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001509 ObjCProtocolDecl *PrevDecl)
1510 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001511{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001512 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001513 if (PrevDecl)
1514 Data = PrevDecl->Data;
1515}
1516
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001517ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001518 IdentifierInfo *Id,
1519 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001520 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001521 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001522 ObjCProtocolDecl *Result =
1523 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001524 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001525 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001526}
1527
Richard Smithf7981722013-11-22 09:01:48 +00001528ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001529 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001530 ObjCProtocolDecl *Result =
1531 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001532 Result->Data.setInt(!C.getLangOpts().Modules);
1533 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001534}
1535
Steve Naroff114aecb2009-03-01 16:12:44 +00001536ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1537 ObjCProtocolDecl *PDecl = this;
1538
1539 if (Name == getIdentifier())
1540 return PDecl;
1541
1542 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1543 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1544 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001545
Steve Naroff114aecb2009-03-01 16:12:44 +00001546 return NULL;
1547}
1548
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001549// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001550// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001551ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1552 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001553 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001554
Douglas Gregoreed49792013-01-17 00:38:46 +00001555 // If there is no definition or the definition is hidden, we don't find
1556 // anything.
1557 const ObjCProtocolDecl *Def = getDefinition();
1558 if (!Def || Def->isHidden())
1559 return NULL;
1560
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001561 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001562 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001563
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001564 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001565 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001566 return MethodDecl;
1567 return NULL;
1568}
1569
Douglas Gregore6e48b12012-01-01 19:29:29 +00001570void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001571 assert(!Data.getPointer() && "Protocol already has a definition!");
1572 Data.setPointer(new (getASTContext()) DefinitionData);
1573 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001574}
1575
1576void ObjCProtocolDecl::startDefinition() {
1577 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001578
1579 // Update all of the declarations with a pointer to the definition.
1580 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1581 RD != RDEnd; ++RD)
1582 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001583}
1584
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001585void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1586 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001587
1588 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1589 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1590 E = PDecl->prop_end(); P != E; ++P) {
1591 ObjCPropertyDecl *Prop = *P;
1592 // Insert into PM if not there already.
1593 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001594 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001595 }
1596 // Scan through protocol's protocols.
1597 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1598 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001599 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001600 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001601}
1602
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001603
1604void ObjCProtocolDecl::collectInheritedProtocolProperties(
1605 const ObjCPropertyDecl *Property,
1606 ProtocolPropertyMap &PM) const {
1607 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1608 bool MatchFound = false;
1609 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1610 E = PDecl->prop_end(); P != E; ++P) {
1611 ObjCPropertyDecl *Prop = *P;
1612 if (Prop == Property)
1613 continue;
1614 if (Prop->getIdentifier() == Property->getIdentifier()) {
1615 PM[PDecl] = Prop;
1616 MatchFound = true;
1617 break;
1618 }
1619 }
1620 // Scan through protocol's protocols which did not have a matching property.
1621 if (!MatchFound)
1622 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1623 E = PDecl->protocol_end(); PI != E; ++PI)
1624 (*PI)->collectInheritedProtocolProperties(Property, PM);
1625 }
1626}
Anna Zaks673d76b2012-10-18 19:17:53 +00001627
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001628//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001629// ObjCCategoryDecl
1630//===----------------------------------------------------------------------===//
1631
David Blaikie68e081d2011-12-20 02:48:34 +00001632void ObjCCategoryDecl::anchor() { }
1633
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001634ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001635 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001636 SourceLocation ClassNameLoc,
1637 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001638 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001639 ObjCInterfaceDecl *IDecl,
1640 SourceLocation IvarLBraceLoc,
1641 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001642 ObjCCategoryDecl *CatDecl =
1643 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1644 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001645 if (IDecl) {
1646 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001647 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001648 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001649 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001650 if (ASTMutationListener *L = C.getASTMutationListener())
1651 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1652 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001653 }
1654
1655 return CatDecl;
1656}
1657
Richard Smithf7981722013-11-22 09:01:48 +00001658ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001659 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001660 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1661 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001662}
1663
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001664ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1665 return getASTContext().getObjCImplementation(
1666 const_cast<ObjCCategoryDecl*>(this));
1667}
1668
1669void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1670 getASTContext().setObjCImplementation(this, ImplD);
1671}
1672
1673
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001674//===----------------------------------------------------------------------===//
1675// ObjCCategoryImplDecl
1676//===----------------------------------------------------------------------===//
1677
David Blaikie68e081d2011-12-20 02:48:34 +00001678void ObjCCategoryImplDecl::anchor() { }
1679
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001680ObjCCategoryImplDecl *
1681ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001682 IdentifierInfo *Id,
1683 ObjCInterfaceDecl *ClassInterface,
1684 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001685 SourceLocation atStartLoc,
1686 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001687 if (ClassInterface && ClassInterface->hasDefinition())
1688 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001689 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1690 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001691}
1692
Douglas Gregor72172e92012-01-05 21:55:30 +00001693ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1694 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001695 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1696 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001697}
1698
Steve Narofff406f4d2009-10-29 21:11:04 +00001699ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001700 // The class interface might be NULL if we are working with invalid code.
1701 if (const ObjCInterfaceDecl *ID = getClassInterface())
1702 return ID->FindCategoryDeclaration(getIdentifier());
1703 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001704}
1705
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001706
David Blaikie68e081d2011-12-20 02:48:34 +00001707void ObjCImplDecl::anchor() { }
1708
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001709void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001710 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001711 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001712 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001713}
1714
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001715void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1716 ASTContext &Ctx = getASTContext();
1717
1718 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001719 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001720 if (IFace)
1721 Ctx.setObjCImplementation(IFace, ImplD);
1722
Duncan Sands49c29ee2009-07-21 07:56:29 +00001723 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001724 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1725 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1726 Ctx.setObjCImplementation(CD, ImplD);
1727 }
1728
1729 ClassInterface = IFace;
1730}
1731
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001732/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001733/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001734/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001735///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001736ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001737FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1738 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001739 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001740 if (PID->getPropertyIvarDecl() &&
1741 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1742 return PID;
1743 }
1744 return 0;
1745}
1746
1747/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001748/// added to the list of those properties \@synthesized/\@dynamic in this
1749/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001750///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001751ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001752FindPropertyImplDecl(IdentifierInfo *Id) const {
1753 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001754 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001755 if (PID->getPropertyDecl()->getIdentifier() == Id)
1756 return PID;
1757 }
1758 return 0;
1759}
1760
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001761raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001762 const ObjCCategoryImplDecl &CID) {
1763 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001764 return OS;
1765}
1766
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001767//===----------------------------------------------------------------------===//
1768// ObjCImplementationDecl
1769//===----------------------------------------------------------------------===//
1770
David Blaikie68e081d2011-12-20 02:48:34 +00001771void ObjCImplementationDecl::anchor() { }
1772
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001773ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001774ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001775 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001776 ObjCInterfaceDecl *SuperDecl,
1777 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001778 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001779 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001780 SourceLocation IvarLBraceLoc,
1781 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001782 if (ClassInterface && ClassInterface->hasDefinition())
1783 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001784 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1785 nameLoc, atStartLoc, superLoc,
1786 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001787}
1788
Douglas Gregor72172e92012-01-05 21:55:30 +00001789ObjCImplementationDecl *
1790ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001791 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1792 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001793}
1794
John McCall0410e572011-07-22 04:15:06 +00001795void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1796 CXXCtorInitializer ** initializers,
1797 unsigned numInitializers) {
1798 if (numInitializers > 0) {
1799 NumIvarInitializers = numInitializers;
1800 CXXCtorInitializer **ivarInitializers =
1801 new (C) CXXCtorInitializer*[NumIvarInitializers];
1802 memcpy(ivarInitializers, initializers,
1803 numInitializers * sizeof(CXXCtorInitializer*));
1804 IvarInitializers = ivarInitializers;
1805 }
1806}
1807
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001808raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001809 const ObjCImplementationDecl &ID) {
1810 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001811 return OS;
1812}
1813
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001814//===----------------------------------------------------------------------===//
1815// ObjCCompatibleAliasDecl
1816//===----------------------------------------------------------------------===//
1817
David Blaikie68e081d2011-12-20 02:48:34 +00001818void ObjCCompatibleAliasDecl::anchor() { }
1819
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001820ObjCCompatibleAliasDecl *
1821ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1822 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001823 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001824 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001825 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001826}
1827
Douglas Gregor72172e92012-01-05 21:55:30 +00001828ObjCCompatibleAliasDecl *
1829ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001830 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001831}
1832
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001833//===----------------------------------------------------------------------===//
1834// ObjCPropertyDecl
1835//===----------------------------------------------------------------------===//
1836
David Blaikie68e081d2011-12-20 02:48:34 +00001837void ObjCPropertyDecl::anchor() { }
1838
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001839ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1840 SourceLocation L,
1841 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001842 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001843 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001844 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001845 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001846 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001847}
1848
Richard Smithf7981722013-11-22 09:01:48 +00001849ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001850 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001851 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1852 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001853}
1854
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001855//===----------------------------------------------------------------------===//
1856// ObjCPropertyImplDecl
1857//===----------------------------------------------------------------------===//
1858
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001859ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001860 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001861 SourceLocation atLoc,
1862 SourceLocation L,
1863 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001864 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001865 ObjCIvarDecl *ivar,
1866 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001867 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1868 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001869}
Chris Lattnered0e1642008-03-17 01:19:02 +00001870
Richard Smithf7981722013-11-22 09:01:48 +00001871ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001872 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001873 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1874 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001875}
1876
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001877SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1878 SourceLocation EndLoc = getLocation();
1879 if (IvarLoc.isValid())
1880 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001881
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001882 return SourceRange(AtLoc, EndLoc);
1883}