blob: b76fc5cf38af28a944a75fb34b6f3582937e0fc0 [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
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000259/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
260/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000261/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000262///
263ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000264ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000265 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000266 // FIXME: Should make sure no callers ever do this.
267 if (!hasDefinition())
268 return 0;
269
270 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000271 LoadExternalDefinition();
272
Ted Kremenekd133a862010-03-15 20:30:07 +0000273 if (ObjCPropertyDecl *PD =
274 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
275 return PD;
276
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000277 // Look through protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000278 for (ObjCInterfaceDecl::all_protocol_iterator
279 I = all_referenced_protocol_begin(),
280 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000281 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
282 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000283
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000284 return 0;
285}
286
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000287void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
288 PropertyDeclOrder &PO) const {
Anna Zaks673d76b2012-10-18 19:17:53 +0000289 for (ObjCContainerDecl::prop_iterator P = prop_begin(),
290 E = prop_end(); P != E; ++P) {
291 ObjCPropertyDecl *Prop = *P;
292 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000293 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000294 }
295 for (ObjCInterfaceDecl::all_protocol_iterator
296 PI = all_referenced_protocol_begin(),
297 E = all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000298 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000299 // Note, the properties declared only in class extensions are still copied
300 // into the main @interface's property list, and therefore we don't
301 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000302}
303
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000304bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
305 const ObjCInterfaceDecl *Class = this;
306 while (Class) {
307 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
308 return true;
309 Class = Class->getSuperClass();
310 }
311 return false;
312}
313
314const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
315 const ObjCInterfaceDecl *Class = this;
316 while (Class) {
317 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
318 return Class;
319 Class = Class->getSuperClass();
320 }
321 return 0;
322}
323
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000324void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
325 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
326 ASTContext &C)
327{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000328 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000329 LoadExternalDefinition();
330
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000331 if (data().AllReferencedProtocols.empty() &&
332 data().ReferencedProtocols.empty()) {
333 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000334 return;
335 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000336
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000337 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000338 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000339 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000340 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000341 for (unsigned i = 0; i < ExtNum; i++) {
342 bool protocolExists = false;
343 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000344 for (all_protocol_iterator
345 p = all_referenced_protocol_begin(),
346 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000347 ObjCProtocolDecl *Proto = (*p);
348 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
349 protocolExists = true;
350 break;
351 }
352 }
353 // Do we want to warn on a protocol in extension class which
354 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000355 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000356 ProtocolRefs.push_back(ProtoInExtension);
357 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000358
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000359 if (ProtocolRefs.empty())
360 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000361
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000362 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000363 for (all_protocol_iterator p = all_referenced_protocol_begin(),
364 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000365 ProtocolRefs.push_back(*p);
Douglas Gregor002b6712010-01-16 15:02:53 +0000366 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000367
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000368 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000369}
370
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000371const ObjCInterfaceDecl *
372ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
373 const ObjCInterfaceDecl *IFace = this;
374 while (IFace) {
375 if (IFace->hasDesignatedInitializers())
376 return IFace;
377 if (!IFace->inheritsDesignatedInitializers())
378 break;
379 IFace = IFace->getSuperClass();
380 }
381 return 0;
382}
383
384bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
385 switch (data().InheritedDesignatedInitializers) {
386 case DefinitionData::IDI_Inherited:
387 return true;
388 case DefinitionData::IDI_NotInherited:
389 return false;
390 case DefinitionData::IDI_Unknown: {
391 bool isIntroducingInitializers = false;
392 for (instmeth_iterator I = instmeth_begin(),
393 E = instmeth_end(); I != E; ++I) {
394 const ObjCMethodDecl *MD = *I;
395 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding()) {
396 isIntroducingInitializers = true;
397 break;
398 }
399 }
400 // If the class introduced initializers we conservatively assume that we
401 // don't know if any of them is a designated initializer to avoid possible
402 // misleading warnings.
403 if (isIntroducingInitializers) {
404 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
405 return false;
406 } else {
407 data().InheritedDesignatedInitializers = DefinitionData::IDI_Inherited;
408 return true;
409 }
410 }
411 }
412
413 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
414}
415
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000416void ObjCInterfaceDecl::getDesignatedInitializers(
417 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
418 assert(hasDefinition());
419 if (data().ExternallyCompleted)
420 LoadExternalDefinition();
421
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000422 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000423 if (!IFace)
424 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000425
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000426 for (instmeth_iterator I = IFace->instmeth_begin(),
427 E = IFace->instmeth_end(); I != E; ++I) {
428 const ObjCMethodDecl *MD = *I;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000429 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000430 Methods.push_back(MD);
431 }
432}
433
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000434bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
435 const ObjCMethodDecl **InitMethod) const {
436 assert(hasDefinition());
437 if (data().ExternallyCompleted)
438 LoadExternalDefinition();
439
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000440 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000441 if (!IFace)
442 return false;
443
Argyrios Kyrtzidise919fc22013-12-10 18:36:43 +0000444 if (const ObjCMethodDecl *MD = IFace->getMethod(Sel, /*isInstance=*/true)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000445 if (MD->isThisDeclarationADesignatedInitializer()) {
446 if (InitMethod)
447 *InitMethod = MD;
448 return true;
449 }
450 }
451 return false;
452}
453
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000454void ObjCInterfaceDecl::allocateDefinitionData() {
455 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000456 Data.setPointer(new (getASTContext()) DefinitionData());
457 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000458
459 // Make the type point at the definition, now that we have one.
460 if (TypeForDecl)
461 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000462}
463
464void ObjCInterfaceDecl::startDefinition() {
465 allocateDefinitionData();
466
Douglas Gregor66b310c2011-12-15 18:03:09 +0000467 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000468 for (auto RD : redecls()) {
469 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000470 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000471 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000472}
473
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000474ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
475 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000476 // FIXME: Should make sure no callers ever do this.
477 if (!hasDefinition())
478 return 0;
479
480 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000481 LoadExternalDefinition();
482
Chris Lattner89375192008-03-16 00:19:01 +0000483 ObjCInterfaceDecl* ClassDecl = this;
484 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000485 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000486 clsDeclared = ClassDecl;
487 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000488 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000489
490 for (ObjCInterfaceDecl::visible_extensions_iterator
491 Ext = ClassDecl->visible_extensions_begin(),
492 ExtEnd = ClassDecl->visible_extensions_end();
493 Ext != ExtEnd; ++Ext) {
494 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000495 clsDeclared = ClassDecl;
496 return I;
497 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000498 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000499
Chris Lattner89375192008-03-16 00:19:01 +0000500 ClassDecl = ClassDecl->getSuperClass();
501 }
502 return NULL;
503}
504
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000505/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
506/// class whose name is passed as argument. If it is not one of the super classes
507/// the it returns NULL.
508ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
509 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000510 // FIXME: Should make sure no callers ever do this.
511 if (!hasDefinition())
512 return 0;
513
514 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000515 LoadExternalDefinition();
516
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000517 ObjCInterfaceDecl* ClassDecl = this;
518 while (ClassDecl != NULL) {
519 if (ClassDecl->getIdentifier() == ICName)
520 return ClassDecl;
521 ClassDecl = ClassDecl->getSuperClass();
522 }
523 return NULL;
524}
525
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000526ObjCProtocolDecl *
527ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
528 for (ObjCInterfaceDecl::all_protocol_iterator P =
529 all_referenced_protocol_begin(), PE = all_referenced_protocol_end();
530 P != PE; ++P)
531 if ((*P)->lookupProtocolNamed(Name))
532 return (*P);
533 ObjCInterfaceDecl *SuperClass = getSuperClass();
534 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
535}
536
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000537/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000538/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000539/// When argument category "C" is specified, any implicit method found
540/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000541ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000542 bool isInstance,
543 bool shallowCategoryLookup,
544 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000545 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000546{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000547 // FIXME: Should make sure no callers ever do this.
548 if (!hasDefinition())
549 return 0;
550
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000551 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000552 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000553
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000554 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000555 LoadExternalDefinition();
556
Ted Kremenek00781502013-11-23 01:01:29 +0000557 while (ClassDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000558 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000559 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000560
Chris Lattner89375192008-03-16 00:19:01 +0000561 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +0000562 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
563 E = ClassDecl->protocol_end();
564 I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000565 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000566 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000567
568 // Didn't find one yet - now look through categories.
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000569 for (ObjCInterfaceDecl::visible_categories_iterator
570 Cat = ClassDecl->visible_categories_begin(),
571 CatEnd = ClassDecl->visible_categories_end();
572 Cat != CatEnd; ++Cat) {
573 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
574 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000575 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000576
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000577 if (!shallowCategoryLookup) {
578 // Didn't find one yet - look through protocols.
579 const ObjCList<ObjCProtocolDecl> &Protocols =
580 Cat->getReferencedProtocols();
581 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
582 E = Protocols.end(); I != E; ++I)
583 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
584 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000585 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000586 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000587 }
Ted Kremenek00781502013-11-23 01:01:29 +0000588
589 if (!followSuper)
590 return NULL;
591
592 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000593 ClassDecl = ClassDecl->getSuperClass();
594 }
595 return NULL;
596}
597
Anna Zaksc77a3b12012-07-27 19:07:44 +0000598// Will search "local" class/category implementations for a method decl.
599// If failed, then we search in class's root for an instance method.
600// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000601ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
602 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000603 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000604 // FIXME: Should make sure no callers ever do this.
605 if (!hasDefinition())
606 return 0;
607
608 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000609 LoadExternalDefinition();
610
Steve Naroffbb69c942009-10-01 23:46:04 +0000611 ObjCMethodDecl *Method = 0;
612 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000613 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
614 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000615
616 // Look through local category implementations associated with the class.
617 if (!Method)
618 Method = Instance ? getCategoryInstanceMethod(Sel)
619 : getCategoryClassMethod(Sel);
620
621 // Before we give up, check if the selector is an instance method.
622 // But only in the root. This matches gcc's behavior and what the
623 // runtime expects.
624 if (!Instance && !Method && !getSuperClass()) {
625 Method = lookupInstanceMethod(Sel);
626 // Look through local category implementations associated
627 // with the root class.
628 if (!Method)
629 Method = lookupPrivateMethod(Sel, true);
630 }
631
Steve Naroffbb69c942009-10-01 23:46:04 +0000632 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000633 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000634 return Method;
635}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000636
637//===----------------------------------------------------------------------===//
638// ObjCMethodDecl
639//===----------------------------------------------------------------------===//
640
Alp Toker314cc812014-01-25 16:55:45 +0000641ObjCMethodDecl *ObjCMethodDecl::Create(
642 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
643 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
644 DeclContext *contextDecl, bool isInstance, bool isVariadic,
645 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
646 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000647 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000648 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000649 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
650 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000651}
652
Douglas Gregor72172e92012-01-05 21:55:30 +0000653ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000654 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
655 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000656}
657
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000658bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
659 return getMethodFamily() == OMF_init &&
660 hasAttr<ObjCDesignatedInitializerAttr>();
661}
662
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000663bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
664 const ObjCMethodDecl **InitMethod) const {
665 if (getMethodFamily() != OMF_init)
666 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000667 const DeclContext *DC = getDeclContext();
668 if (isa<ObjCProtocolDecl>(DC))
669 return false;
670 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000671 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000672 return false;
673}
674
Douglas Gregora6017bb2012-10-09 17:21:28 +0000675Stmt *ObjCMethodDecl::getBody() const {
676 return Body.get(getASTContext().getExternalSource());
677}
678
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000679void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
680 assert(PrevMethod);
681 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
682 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000683 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000684}
685
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000686void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
687 ArrayRef<ParmVarDecl*> Params,
688 ArrayRef<SourceLocation> SelLocs) {
689 ParamsAndSelLocs = 0;
690 NumParams = Params.size();
691 if (Params.empty() && SelLocs.empty())
692 return;
693
694 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
695 sizeof(SourceLocation) * SelLocs.size();
696 ParamsAndSelLocs = C.Allocate(Size);
697 std::copy(Params.begin(), Params.end(), getParams());
698 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
699}
700
701void ObjCMethodDecl::getSelectorLocs(
702 SmallVectorImpl<SourceLocation> &SelLocs) const {
703 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
704 SelLocs.push_back(getSelectorLoc(i));
705}
706
707void ObjCMethodDecl::setMethodParams(ASTContext &C,
708 ArrayRef<ParmVarDecl*> Params,
709 ArrayRef<SourceLocation> SelLocs) {
710 assert((!SelLocs.empty() || isImplicit()) &&
711 "No selector locs for non-implicit method");
712 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000713 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000714
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000715 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
716 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000717 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000718 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000719
720 setParamsAndSelLocs(C, Params, SelLocs);
721}
722
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000723/// \brief A definition will return its interface declaration.
724/// An interface declaration will return its definition.
725/// Otherwise it will return itself.
726ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
727 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000728 ObjCMethodDecl *Redecl = 0;
729 if (HasRedeclaration)
730 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000731 if (Redecl)
732 return Redecl;
733
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000734 Decl *CtxD = cast<Decl>(getDeclContext());
735
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000736 if (!CtxD->isInvalidDecl()) {
737 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
738 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
739 if (!ImplD->isInvalidDecl())
740 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000741
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000742 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
743 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
744 if (!ImplD->isInvalidDecl())
745 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000746
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000747 } else if (ObjCImplementationDecl *ImplD =
748 dyn_cast<ObjCImplementationDecl>(CtxD)) {
749 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
750 if (!IFD->isInvalidDecl())
751 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000752
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000753 } else if (ObjCCategoryImplDecl *CImplD =
754 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
755 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
756 if (!CatD->isInvalidDecl())
757 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
758 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000759 }
760
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000761 if (!Redecl && isRedeclaration()) {
762 // This is the last redeclaration, go back to the first method.
763 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
764 isInstanceMethod());
765 }
766
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000767 return Redecl ? Redecl : this;
768}
769
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000770ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
771 Decl *CtxD = cast<Decl>(getDeclContext());
772
773 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
774 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
775 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
776 isInstanceMethod()))
777 return MD;
778
779 } else if (ObjCCategoryImplDecl *CImplD =
780 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000781 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000782 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
783 isInstanceMethod()))
784 return MD;
785 }
786
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000787 if (isRedeclaration())
788 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
789 isInstanceMethod());
790
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000791 return this;
792}
793
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000794SourceLocation ObjCMethodDecl::getLocEnd() const {
795 if (Stmt *Body = getBody())
796 return Body->getLocEnd();
797 return DeclEndLoc;
798}
799
John McCallb4526252011-03-02 01:50:55 +0000800ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
801 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000802 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000803 return family;
804
John McCall86bc21f2011-03-02 11:33:24 +0000805 // Check for an explicit attribute.
806 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
807 // The unfortunate necessity of mapping between enums here is due
808 // to the attributes framework.
809 switch (attr->getFamily()) {
810 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
811 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
812 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
813 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
814 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
815 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
816 }
817 Family = static_cast<unsigned>(family);
818 return family;
819 }
820
John McCallb4526252011-03-02 01:50:55 +0000821 family = getSelector().getMethodFamily();
822 switch (family) {
823 case OMF_None: break;
824
825 // init only has a conventional meaning for an instance method, and
826 // it has to return an object.
827 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000828 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000829 family = OMF_None;
830 break;
831
832 // alloc/copy/new have a conventional meaning for both class and
833 // instance methods, but they require an object return.
834 case OMF_alloc:
835 case OMF_copy:
836 case OMF_mutableCopy:
837 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000838 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000839 family = OMF_None;
840 break;
841
842 // These selectors have a conventional meaning only for instance methods.
843 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000844 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000845 case OMF_retain:
846 case OMF_release:
847 case OMF_autorelease:
848 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000849 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000850 if (!isInstanceMethod())
851 family = OMF_None;
852 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000853
854 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000855 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000856 family = OMF_None;
857 else {
858 unsigned noParams = param_size();
859 if (noParams < 1 || noParams > 3)
860 family = OMF_None;
861 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000862 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000863 QualType ArgT = (*it);
864 if (!ArgT->isObjCSelType()) {
865 family = OMF_None;
866 break;
867 }
868 while (--noParams) {
869 it++;
870 ArgT = (*it);
871 if (!ArgT->isObjCIdType()) {
872 family = OMF_None;
873 break;
874 }
875 }
876 }
877 }
878 break;
879
John McCallb4526252011-03-02 01:50:55 +0000880 }
881
882 // Cache the result.
883 Family = static_cast<unsigned>(family);
884 return family;
885}
886
Mike Stump11289f42009-09-09 15:08:12 +0000887void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000888 const ObjCInterfaceDecl *OID) {
889 QualType selfTy;
890 if (isInstanceMethod()) {
891 // There may be no interface context due to error in declaration
892 // of the interface (which has been reported). Recover gracefully.
893 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000894 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000895 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000896 } else {
897 selfTy = Context.getObjCIdType();
898 }
899 } else // we have a factory method.
900 selfTy = Context.getObjCClassType();
901
John McCalld4631322011-06-17 06:42:21 +0000902 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000903 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000904
David Blaikiebbafb8a2012-03-11 07:00:24 +0000905 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000906 if (isInstanceMethod()) {
907 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000908
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000909 // 'self' is always __strong. It's actually pseudo-strong except
910 // in init methods (or methods labeled ns_consumes_self), though.
911 Qualifiers qs;
912 qs.setObjCLifetime(Qualifiers::OCL_Strong);
913 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000914
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000915 // In addition, 'self' is const unless this is an init method.
916 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
917 selfTy = selfTy.withConst();
918 selfIsPseudoStrong = true;
919 }
920 }
921 else {
922 assert(isClassMethod());
923 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000924 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000925 selfIsPseudoStrong = true;
926 }
John McCall31168b02011-06-15 23:02:42 +0000927 }
928
929 ImplicitParamDecl *self
930 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
931 &Context.Idents.get("self"), selfTy);
932 setSelfDecl(self);
933
934 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000935 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000936
John McCalld4631322011-06-17 06:42:21 +0000937 if (selfIsPseudoStrong)
938 self->setARCPseudoStrong(true);
939
Mike Stump11289f42009-09-09 15:08:12 +0000940 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
941 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000942 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000943}
944
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000945ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
946 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
947 return ID;
948 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
949 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000950 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000951 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000952 if (isa<ObjCProtocolDecl>(getDeclContext()))
953 return 0;
David Blaikie83d382b2011-09-23 05:06:16 +0000954 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000955}
956
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000957static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
958 const ObjCMethodDecl *Method,
959 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
960 bool MovedToSuper) {
961 if (!Container)
962 return;
963
964 // In categories look for overriden methods from protocols. A method from
965 // category is not "overriden" since it is considered as the "same" method
966 // (same USR) as the one from the interface.
967 if (const ObjCCategoryDecl *
968 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
969 // Check whether we have a matching method at this category but only if we
970 // are at the super class level.
971 if (MovedToSuper)
972 if (ObjCMethodDecl *
973 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000974 Method->isInstanceMethod(),
975 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000976 if (Method != Overridden) {
977 // We found an override at this category; there is no need to look
978 // into its protocols.
979 Methods.push_back(Overridden);
980 return;
981 }
982
983 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
984 PEnd = Category->protocol_end();
985 P != PEnd; ++P)
986 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
987 return;
988 }
989
990 // Check whether we have a matching method at this level.
991 if (const ObjCMethodDecl *
992 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000993 Method->isInstanceMethod(),
994 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000995 if (Method != Overridden) {
996 // We found an override at this level; there is no need to look
997 // into other protocols or categories.
998 Methods.push_back(Overridden);
999 return;
1000 }
1001
1002 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
1003 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
1004 PEnd = Protocol->protocol_end();
1005 P != PEnd; ++P)
1006 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1007 }
1008
1009 if (const ObjCInterfaceDecl *
1010 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
1011 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
1012 PEnd = Interface->protocol_end();
1013 P != PEnd; ++P)
1014 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1015
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001016 for (ObjCInterfaceDecl::known_categories_iterator
1017 Cat = Interface->known_categories_begin(),
1018 CatEnd = Interface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001019 Cat != CatEnd; ++Cat) {
1020 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001021 MovedToSuper);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001022 }
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001023
1024 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1025 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1026 /*MovedToSuper=*/true);
1027 }
1028}
1029
1030static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1031 const ObjCMethodDecl *Method,
1032 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1033 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1034 /*MovedToSuper=*/false);
1035}
1036
1037static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1038 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1039 assert(Method->isOverriding());
1040
1041 if (const ObjCProtocolDecl *
1042 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1043 CollectOverriddenMethods(ProtD, Method, overridden);
1044
1045 } else if (const ObjCImplDecl *
1046 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1047 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1048 if (!ID)
1049 return;
1050 // Start searching for overridden methods using the method from the
1051 // interface as starting point.
1052 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001053 Method->isInstanceMethod(),
1054 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001055 Method = IFaceMeth;
1056 CollectOverriddenMethods(ID, Method, overridden);
1057
1058 } else if (const ObjCCategoryDecl *
1059 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1060 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1061 if (!ID)
1062 return;
1063 // Start searching for overridden methods using the method from the
1064 // interface as starting point.
1065 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001066 Method->isInstanceMethod(),
1067 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001068 Method = IFaceMeth;
1069 CollectOverriddenMethods(ID, Method, overridden);
1070
1071 } else {
1072 CollectOverriddenMethods(
1073 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1074 Method, overridden);
1075 }
1076}
1077
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001078void ObjCMethodDecl::getOverriddenMethods(
1079 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1080 const ObjCMethodDecl *Method = this;
1081
1082 if (Method->isRedeclaration()) {
1083 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1084 getMethod(Method->getSelector(), Method->isInstanceMethod());
1085 }
1086
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001087 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001088 collectOverriddenMethodsSlow(Method, Overridden);
1089 assert(!Overridden.empty() &&
1090 "ObjCMethodDecl's overriding bit is not as expected");
1091 }
1092}
1093
Jordan Rose2bd991a2012-10-10 16:42:54 +00001094const ObjCPropertyDecl *
1095ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1096 Selector Sel = getSelector();
1097 unsigned NumArgs = Sel.getNumArgs();
1098 if (NumArgs > 1)
1099 return 0;
1100
Jordan Rose59e34ec2012-10-11 16:02:02 +00001101 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001102 return 0;
1103
1104 if (isPropertyAccessor()) {
1105 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001106 // If container is class extension, find its primary class.
1107 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1108 if (CatDecl->IsClassExtension())
1109 Container = CatDecl->getClassInterface();
1110
Jordan Rose2bd991a2012-10-10 16:42:54 +00001111 bool IsGetter = (NumArgs == 0);
1112
1113 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1114 E = Container->prop_end();
1115 I != E; ++I) {
1116 Selector NextSel = IsGetter ? (*I)->getGetterName()
1117 : (*I)->getSetterName();
1118 if (NextSel == Sel)
1119 return *I;
1120 }
1121
1122 llvm_unreachable("Marked as a property accessor but no property found!");
1123 }
1124
1125 if (!CheckOverrides)
1126 return 0;
1127
1128 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1129 OverridesTy Overrides;
1130 getOverriddenMethods(Overrides);
1131 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1132 I != E; ++I) {
1133 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1134 return Prop;
1135 }
1136
1137 return 0;
1138
1139}
1140
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001141//===----------------------------------------------------------------------===//
1142// ObjCInterfaceDecl
1143//===----------------------------------------------------------------------===//
1144
Douglas Gregord53ae832012-01-17 18:09:05 +00001145ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001146 DeclContext *DC,
1147 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001148 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001149 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001150 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001151 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001152 ObjCInterfaceDecl *Result = new (C, DC)
1153 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001154 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001155 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001156 return Result;
1157}
1158
Richard Smithf7981722013-11-22 09:01:48 +00001159ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001160 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001161 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1162 0, SourceLocation(),
1163 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001164 Result->Data.setInt(!C.getLangOpts().Modules);
1165 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001166}
1167
1168ObjCInterfaceDecl::
1169ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001170 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1171 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001172 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001173 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001174{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001175 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001176
1177 // Copy the 'data' pointer over.
1178 if (PrevDecl)
1179 Data = PrevDecl->Data;
1180
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001181 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001182}
1183
Douglas Gregor73693022010-12-01 23:49:52 +00001184void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001185 assert(data().ExternallyCompleted && "Class is not externally completed");
1186 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001187 getASTContext().getExternalSource()->CompleteType(
1188 const_cast<ObjCInterfaceDecl *>(this));
1189}
1190
1191void ObjCInterfaceDecl::setExternallyCompleted() {
1192 assert(getASTContext().getExternalSource() &&
1193 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001194 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001195 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001196 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001197}
1198
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001199void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1200 assert(hasDefinition() && "Forward declarations can't contain methods");
1201 data().HasDesignatedInitializers = true;
1202}
1203
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001204bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1205 assert(hasDefinition() && "Forward declarations can't contain methods");
1206 if (data().ExternallyCompleted)
1207 LoadExternalDefinition();
1208
1209 return data().HasDesignatedInitializers;
1210}
1211
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001212ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001213 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1214 if (data().ExternallyCompleted)
1215 LoadExternalDefinition();
1216
1217 return getASTContext().getObjCImplementation(
1218 const_cast<ObjCInterfaceDecl*>(Def));
1219 }
1220
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001221 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001222 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001223}
1224
1225void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001226 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001227}
1228
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001229namespace {
1230 struct SynthesizeIvarChunk {
1231 uint64_t Size;
1232 ObjCIvarDecl *Ivar;
1233 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1234 : Size(size), Ivar(ivar) {}
1235 };
1236
1237 bool operator<(const SynthesizeIvarChunk & LHS,
1238 const SynthesizeIvarChunk &RHS) {
1239 return LHS.Size < RHS.Size;
1240 }
1241}
1242
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001243/// all_declared_ivar_begin - return first ivar declared in this class,
1244/// its extensions and its implementation. Lazily build the list on first
1245/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001246///
1247/// Caveat: The list returned by this method reflects the current
1248/// state of the parser. The cache will be updated for every ivar
1249/// added by an extension or the implementation when they are
1250/// encountered.
1251/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001252ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001253 // FIXME: Should make sure no callers ever do this.
1254 if (!hasDefinition())
1255 return 0;
1256
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001257 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001258 if (!data().IvarList) {
1259 if (!ivar_empty()) {
1260 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1261 data().IvarList = *I; ++I;
1262 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001263 curIvar->setNextIvar(*I);
1264 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001265
1266 for (ObjCInterfaceDecl::known_extensions_iterator
1267 Ext = known_extensions_begin(),
1268 ExtEnd = known_extensions_end();
1269 Ext != ExtEnd; ++Ext) {
1270 if (!Ext->ivar_empty()) {
1271 ObjCCategoryDecl::ivar_iterator
1272 I = Ext->ivar_begin(),
1273 E = Ext->ivar_end();
1274 if (!data().IvarList) {
1275 data().IvarList = *I; ++I;
1276 curIvar = data().IvarList;
1277 }
1278 for ( ;I != E; curIvar = *I, ++I)
1279 curIvar->setNextIvar(*I);
1280 }
1281 }
1282 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001283 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001284
1285 // cached and complete!
1286 if (!data().IvarListMissingImplementation)
1287 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001288
1289 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001290 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001291 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001292 SmallVector<SynthesizeIvarChunk, 16> layout;
1293 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1294 E = ImplDecl->ivar_end(); I != E; ++I) {
1295 ObjCIvarDecl *IV = *I;
1296 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1297 layout.push_back(SynthesizeIvarChunk(
1298 IV->getASTContext().getTypeSize(IV->getType()), IV));
1299 continue;
1300 }
1301 if (!data().IvarList)
1302 data().IvarList = *I;
1303 else
1304 curIvar->setNextIvar(*I);
1305 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001306 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001307
1308 if (!layout.empty()) {
1309 // Order synthesized ivars by their size.
1310 std::stable_sort(layout.begin(), layout.end());
1311 unsigned Ix = 0, EIx = layout.size();
1312 if (!data().IvarList) {
1313 data().IvarList = layout[0].Ivar; Ix++;
1314 curIvar = data().IvarList;
1315 }
1316 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1317 curIvar->setNextIvar(layout[Ix].Ivar);
1318 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001319 }
1320 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001321 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001322}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001323
1324/// FindCategoryDeclaration - Finds category declaration in the list of
1325/// categories for this class and returns it. Name of the category is passed
1326/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001327///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001328ObjCCategoryDecl *
1329ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001330 // FIXME: Should make sure no callers ever do this.
1331 if (!hasDefinition())
1332 return 0;
1333
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001334 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001335 LoadExternalDefinition();
1336
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001337 for (visible_categories_iterator Cat = visible_categories_begin(),
1338 CatEnd = visible_categories_end();
1339 Cat != CatEnd;
1340 ++Cat) {
1341 if (Cat->getIdentifier() == CategoryId)
1342 return *Cat;
1343 }
1344
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001345 return 0;
1346}
1347
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001348ObjCMethodDecl *
1349ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001350 for (visible_categories_iterator Cat = visible_categories_begin(),
1351 CatEnd = visible_categories_end();
1352 Cat != CatEnd;
1353 ++Cat) {
1354 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001355 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1356 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001357 }
1358
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001359 return 0;
1360}
1361
1362ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001363 for (visible_categories_iterator Cat = visible_categories_begin(),
1364 CatEnd = visible_categories_end();
1365 Cat != CatEnd;
1366 ++Cat) {
1367 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001368 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1369 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001370 }
1371
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001372 return 0;
1373}
1374
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001375/// ClassImplementsProtocol - Checks that 'lProto' protocol
1376/// has been implemented in IDecl class, its super class or categories (if
1377/// lookupCategory is true).
1378bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1379 bool lookupCategory,
1380 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001381 if (!hasDefinition())
1382 return false;
1383
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001384 ObjCInterfaceDecl *IDecl = this;
1385 // 1st, look up the class.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +00001386 for (ObjCInterfaceDecl::protocol_iterator
1387 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001388 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1389 return true;
1390 // This is dubious and is added to be compatible with gcc. In gcc, it is
1391 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1392 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1393 // object. This IMO, should be a bug.
1394 // FIXME: Treat this as an extension, and flag this as an error when GCC
1395 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001396 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001397 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1398 return true;
1399 }
Mike Stump11289f42009-09-09 15:08:12 +00001400
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001401 // 2nd, look up the category.
1402 if (lookupCategory)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001403 for (visible_categories_iterator Cat = visible_categories_begin(),
1404 CatEnd = visible_categories_end();
1405 Cat != CatEnd;
1406 ++Cat) {
1407 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1408 E = Cat->protocol_end();
1409 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001410 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1411 return true;
1412 }
Mike Stump11289f42009-09-09 15:08:12 +00001413
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001414 // 3rd, look up the super class(s)
1415 if (IDecl->getSuperClass())
1416 return
1417 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1418 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001419
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001420 return false;
1421}
1422
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001423//===----------------------------------------------------------------------===//
1424// ObjCIvarDecl
1425//===----------------------------------------------------------------------===//
1426
David Blaikie68e081d2011-12-20 02:48:34 +00001427void ObjCIvarDecl::anchor() { }
1428
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001429ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001430 SourceLocation StartLoc,
1431 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001432 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001433 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001434 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001435 if (DC) {
1436 // Ivar's can only appear in interfaces, implementations (via synthesized
1437 // properties), and class extensions (via direct declaration, or synthesized
1438 // properties).
1439 //
1440 // FIXME: This should really be asserting this:
1441 // (isa<ObjCCategoryDecl>(DC) &&
1442 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1443 // but unfortunately we sometimes place ivars into non-class extension
1444 // categories on error. This breaks an AST invariant, and should not be
1445 // fixed.
1446 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1447 isa<ObjCCategoryDecl>(DC)) &&
1448 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001449 // Once a new ivar is created in any of class/class-extension/implementation
1450 // decl contexts, the previously built IvarList must be rebuilt.
1451 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1452 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001453 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001454 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001455 else
1456 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001457 }
1458 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001459 }
1460
Richard Smithf7981722013-11-22 09:01:48 +00001461 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001462 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001463}
1464
Douglas Gregor72172e92012-01-05 21:55:30 +00001465ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001466 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001467 QualType(), 0, ObjCIvarDecl::None, 0, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001468}
1469
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001470const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1471 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001472
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001473 switch (DC->getKind()) {
1474 default:
1475 case ObjCCategoryImpl:
1476 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001477 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001478
1479 // Ivars can only appear in class extension categories.
1480 case ObjCCategory: {
1481 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1482 assert(CD->IsClassExtension() && "invalid container for ivar!");
1483 return CD->getClassInterface();
1484 }
1485
1486 case ObjCImplementation:
1487 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1488
1489 case ObjCInterface:
1490 return cast<ObjCInterfaceDecl>(DC);
1491 }
1492}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001493
1494//===----------------------------------------------------------------------===//
1495// ObjCAtDefsFieldDecl
1496//===----------------------------------------------------------------------===//
1497
David Blaikie68e081d2011-12-20 02:48:34 +00001498void ObjCAtDefsFieldDecl::anchor() { }
1499
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001500ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001501*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1502 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001503 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001504 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001505}
1506
Richard Smithf7981722013-11-22 09:01:48 +00001507ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001508 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001509 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1510 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001511}
1512
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001513//===----------------------------------------------------------------------===//
1514// ObjCProtocolDecl
1515//===----------------------------------------------------------------------===//
1516
David Blaikie68e081d2011-12-20 02:48:34 +00001517void ObjCProtocolDecl::anchor() { }
1518
Douglas Gregor32c17572012-01-01 20:30:41 +00001519ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1520 SourceLocation nameLoc,
1521 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001522 ObjCProtocolDecl *PrevDecl)
1523 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001524{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001525 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001526 if (PrevDecl)
1527 Data = PrevDecl->Data;
1528}
1529
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001530ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001531 IdentifierInfo *Id,
1532 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001533 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001534 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001535 ObjCProtocolDecl *Result =
1536 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001537 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001538 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001539}
1540
Richard Smithf7981722013-11-22 09:01:48 +00001541ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001542 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001543 ObjCProtocolDecl *Result =
1544 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001545 Result->Data.setInt(!C.getLangOpts().Modules);
1546 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001547}
1548
Steve Naroff114aecb2009-03-01 16:12:44 +00001549ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1550 ObjCProtocolDecl *PDecl = this;
1551
1552 if (Name == getIdentifier())
1553 return PDecl;
1554
1555 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1556 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1557 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001558
Steve Naroff114aecb2009-03-01 16:12:44 +00001559 return NULL;
1560}
1561
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001562// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001563// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001564ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1565 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001566 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001567
Douglas Gregoreed49792013-01-17 00:38:46 +00001568 // If there is no definition or the definition is hidden, we don't find
1569 // anything.
1570 const ObjCProtocolDecl *Def = getDefinition();
1571 if (!Def || Def->isHidden())
1572 return NULL;
1573
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001574 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001575 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001576
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001577 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001578 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001579 return MethodDecl;
1580 return NULL;
1581}
1582
Douglas Gregore6e48b12012-01-01 19:29:29 +00001583void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001584 assert(!Data.getPointer() && "Protocol already has a definition!");
1585 Data.setPointer(new (getASTContext()) DefinitionData);
1586 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001587}
1588
1589void ObjCProtocolDecl::startDefinition() {
1590 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001591
1592 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001593 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001594 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001595}
1596
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001597void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1598 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001599
1600 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1601 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1602 E = PDecl->prop_end(); P != E; ++P) {
1603 ObjCPropertyDecl *Prop = *P;
1604 // Insert into PM if not there already.
1605 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001606 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001607 }
1608 // Scan through protocol's protocols.
1609 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1610 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001611 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001612 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001613}
1614
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001615
1616void ObjCProtocolDecl::collectInheritedProtocolProperties(
1617 const ObjCPropertyDecl *Property,
1618 ProtocolPropertyMap &PM) const {
1619 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1620 bool MatchFound = false;
1621 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1622 E = PDecl->prop_end(); P != E; ++P) {
1623 ObjCPropertyDecl *Prop = *P;
1624 if (Prop == Property)
1625 continue;
1626 if (Prop->getIdentifier() == Property->getIdentifier()) {
1627 PM[PDecl] = Prop;
1628 MatchFound = true;
1629 break;
1630 }
1631 }
1632 // Scan through protocol's protocols which did not have a matching property.
1633 if (!MatchFound)
1634 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1635 E = PDecl->protocol_end(); PI != E; ++PI)
1636 (*PI)->collectInheritedProtocolProperties(Property, PM);
1637 }
1638}
Anna Zaks673d76b2012-10-18 19:17:53 +00001639
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001640//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001641// ObjCCategoryDecl
1642//===----------------------------------------------------------------------===//
1643
David Blaikie68e081d2011-12-20 02:48:34 +00001644void ObjCCategoryDecl::anchor() { }
1645
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001646ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001647 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001648 SourceLocation ClassNameLoc,
1649 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001650 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001651 ObjCInterfaceDecl *IDecl,
1652 SourceLocation IvarLBraceLoc,
1653 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001654 ObjCCategoryDecl *CatDecl =
1655 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1656 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001657 if (IDecl) {
1658 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001659 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001660 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001661 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001662 if (ASTMutationListener *L = C.getASTMutationListener())
1663 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1664 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001665 }
1666
1667 return CatDecl;
1668}
1669
Richard Smithf7981722013-11-22 09:01:48 +00001670ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001671 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001672 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1673 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001674}
1675
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001676ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1677 return getASTContext().getObjCImplementation(
1678 const_cast<ObjCCategoryDecl*>(this));
1679}
1680
1681void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1682 getASTContext().setObjCImplementation(this, ImplD);
1683}
1684
1685
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001686//===----------------------------------------------------------------------===//
1687// ObjCCategoryImplDecl
1688//===----------------------------------------------------------------------===//
1689
David Blaikie68e081d2011-12-20 02:48:34 +00001690void ObjCCategoryImplDecl::anchor() { }
1691
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001692ObjCCategoryImplDecl *
1693ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001694 IdentifierInfo *Id,
1695 ObjCInterfaceDecl *ClassInterface,
1696 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001697 SourceLocation atStartLoc,
1698 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001699 if (ClassInterface && ClassInterface->hasDefinition())
1700 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001701 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1702 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001703}
1704
Douglas Gregor72172e92012-01-05 21:55:30 +00001705ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1706 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001707 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1708 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001709}
1710
Steve Narofff406f4d2009-10-29 21:11:04 +00001711ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001712 // The class interface might be NULL if we are working with invalid code.
1713 if (const ObjCInterfaceDecl *ID = getClassInterface())
1714 return ID->FindCategoryDeclaration(getIdentifier());
1715 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001716}
1717
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001718
David Blaikie68e081d2011-12-20 02:48:34 +00001719void ObjCImplDecl::anchor() { }
1720
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001721void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001722 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001723 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001724 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001725}
1726
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001727void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1728 ASTContext &Ctx = getASTContext();
1729
1730 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001731 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001732 if (IFace)
1733 Ctx.setObjCImplementation(IFace, ImplD);
1734
Duncan Sands49c29ee2009-07-21 07:56:29 +00001735 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001736 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1737 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1738 Ctx.setObjCImplementation(CD, ImplD);
1739 }
1740
1741 ClassInterface = IFace;
1742}
1743
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001744/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001745/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001746/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001747///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001748ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001749FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1750 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001751 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001752 if (PID->getPropertyIvarDecl() &&
1753 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1754 return PID;
1755 }
1756 return 0;
1757}
1758
1759/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001760/// added to the list of those properties \@synthesized/\@dynamic in this
1761/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001762///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001763ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001764FindPropertyImplDecl(IdentifierInfo *Id) const {
1765 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001766 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001767 if (PID->getPropertyDecl()->getIdentifier() == Id)
1768 return PID;
1769 }
1770 return 0;
1771}
1772
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001773raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001774 const ObjCCategoryImplDecl &CID) {
1775 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001776 return OS;
1777}
1778
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001779//===----------------------------------------------------------------------===//
1780// ObjCImplementationDecl
1781//===----------------------------------------------------------------------===//
1782
David Blaikie68e081d2011-12-20 02:48:34 +00001783void ObjCImplementationDecl::anchor() { }
1784
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001785ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001786ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001787 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001788 ObjCInterfaceDecl *SuperDecl,
1789 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001790 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001791 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001792 SourceLocation IvarLBraceLoc,
1793 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001794 if (ClassInterface && ClassInterface->hasDefinition())
1795 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001796 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1797 nameLoc, atStartLoc, superLoc,
1798 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001799}
1800
Douglas Gregor72172e92012-01-05 21:55:30 +00001801ObjCImplementationDecl *
1802ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001803 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1804 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001805}
1806
John McCall0410e572011-07-22 04:15:06 +00001807void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1808 CXXCtorInitializer ** initializers,
1809 unsigned numInitializers) {
1810 if (numInitializers > 0) {
1811 NumIvarInitializers = numInitializers;
1812 CXXCtorInitializer **ivarInitializers =
1813 new (C) CXXCtorInitializer*[NumIvarInitializers];
1814 memcpy(ivarInitializers, initializers,
1815 numInitializers * sizeof(CXXCtorInitializer*));
1816 IvarInitializers = ivarInitializers;
1817 }
1818}
1819
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001820raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001821 const ObjCImplementationDecl &ID) {
1822 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001823 return OS;
1824}
1825
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001826//===----------------------------------------------------------------------===//
1827// ObjCCompatibleAliasDecl
1828//===----------------------------------------------------------------------===//
1829
David Blaikie68e081d2011-12-20 02:48:34 +00001830void ObjCCompatibleAliasDecl::anchor() { }
1831
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001832ObjCCompatibleAliasDecl *
1833ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1834 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001835 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001836 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001837 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001838}
1839
Douglas Gregor72172e92012-01-05 21:55:30 +00001840ObjCCompatibleAliasDecl *
1841ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001842 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001843}
1844
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001845//===----------------------------------------------------------------------===//
1846// ObjCPropertyDecl
1847//===----------------------------------------------------------------------===//
1848
David Blaikie68e081d2011-12-20 02:48:34 +00001849void ObjCPropertyDecl::anchor() { }
1850
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001851ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1852 SourceLocation L,
1853 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001854 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001855 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001856 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001857 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001858 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001859}
1860
Richard Smithf7981722013-11-22 09:01:48 +00001861ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001862 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001863 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1864 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001865}
1866
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001867//===----------------------------------------------------------------------===//
1868// ObjCPropertyImplDecl
1869//===----------------------------------------------------------------------===//
1870
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001871ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001872 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001873 SourceLocation atLoc,
1874 SourceLocation L,
1875 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001876 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001877 ObjCIvarDecl *ivar,
1878 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001879 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1880 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001881}
Chris Lattnered0e1642008-03-17 01:19:02 +00001882
Richard Smithf7981722013-11-22 09:01:48 +00001883ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001884 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001885 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1886 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001887}
1888
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001889SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1890 SourceLocation EndLoc = getLocation();
1891 if (IvarLoc.isValid())
1892 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001893
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001894 return SourceRange(AtLoc, EndLoc);
1895}