blob: 429b235315dcd71a4b8141ffdf056cfc5ae8f185 [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.
468 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
469 RD != RDEnd; ++RD) {
470 if (*RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000471 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000472 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000473}
474
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000475ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
476 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000477 // FIXME: Should make sure no callers ever do this.
478 if (!hasDefinition())
479 return 0;
480
481 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000482 LoadExternalDefinition();
483
Chris Lattner89375192008-03-16 00:19:01 +0000484 ObjCInterfaceDecl* ClassDecl = this;
485 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000486 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000487 clsDeclared = ClassDecl;
488 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000489 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000490
491 for (ObjCInterfaceDecl::visible_extensions_iterator
492 Ext = ClassDecl->visible_extensions_begin(),
493 ExtEnd = ClassDecl->visible_extensions_end();
494 Ext != ExtEnd; ++Ext) {
495 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000496 clsDeclared = ClassDecl;
497 return I;
498 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000499 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000500
Chris Lattner89375192008-03-16 00:19:01 +0000501 ClassDecl = ClassDecl->getSuperClass();
502 }
503 return NULL;
504}
505
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000506/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
507/// class whose name is passed as argument. If it is not one of the super classes
508/// the it returns NULL.
509ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
510 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000511 // FIXME: Should make sure no callers ever do this.
512 if (!hasDefinition())
513 return 0;
514
515 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000516 LoadExternalDefinition();
517
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000518 ObjCInterfaceDecl* ClassDecl = this;
519 while (ClassDecl != NULL) {
520 if (ClassDecl->getIdentifier() == ICName)
521 return ClassDecl;
522 ClassDecl = ClassDecl->getSuperClass();
523 }
524 return NULL;
525}
526
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000527ObjCProtocolDecl *
528ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
529 for (ObjCInterfaceDecl::all_protocol_iterator P =
530 all_referenced_protocol_begin(), PE = all_referenced_protocol_end();
531 P != PE; ++P)
532 if ((*P)->lookupProtocolNamed(Name))
533 return (*P);
534 ObjCInterfaceDecl *SuperClass = getSuperClass();
535 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
536}
537
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000538/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000539/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000540/// When argument category "C" is specified, any implicit method found
541/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000542ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000543 bool isInstance,
544 bool shallowCategoryLookup,
545 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000546 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000547{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000548 // FIXME: Should make sure no callers ever do this.
549 if (!hasDefinition())
550 return 0;
551
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000552 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000553 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000554
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000555 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000556 LoadExternalDefinition();
557
Ted Kremenek00781502013-11-23 01:01:29 +0000558 while (ClassDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000559 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000560 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000561
Chris Lattner89375192008-03-16 00:19:01 +0000562 // Didn't find one yet - look through protocols.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +0000563 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
564 E = ClassDecl->protocol_end();
565 I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000566 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000567 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000568
569 // Didn't find one yet - now look through categories.
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000570 for (ObjCInterfaceDecl::visible_categories_iterator
571 Cat = ClassDecl->visible_categories_begin(),
572 CatEnd = ClassDecl->visible_categories_end();
573 Cat != CatEnd; ++Cat) {
574 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
575 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000576 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000577
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000578 if (!shallowCategoryLookup) {
579 // Didn't find one yet - look through protocols.
580 const ObjCList<ObjCProtocolDecl> &Protocols =
581 Cat->getReferencedProtocols();
582 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
583 E = Protocols.end(); I != E; ++I)
584 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
585 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000586 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000587 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000588 }
Ted Kremenek00781502013-11-23 01:01:29 +0000589
590 if (!followSuper)
591 return NULL;
592
593 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000594 ClassDecl = ClassDecl->getSuperClass();
595 }
596 return NULL;
597}
598
Anna Zaksc77a3b12012-07-27 19:07:44 +0000599// Will search "local" class/category implementations for a method decl.
600// If failed, then we search in class's root for an instance method.
601// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000602ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
603 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000604 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000605 // FIXME: Should make sure no callers ever do this.
606 if (!hasDefinition())
607 return 0;
608
609 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000610 LoadExternalDefinition();
611
Steve Naroffbb69c942009-10-01 23:46:04 +0000612 ObjCMethodDecl *Method = 0;
613 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000614 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
615 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000616
617 // Look through local category implementations associated with the class.
618 if (!Method)
619 Method = Instance ? getCategoryInstanceMethod(Sel)
620 : getCategoryClassMethod(Sel);
621
622 // Before we give up, check if the selector is an instance method.
623 // But only in the root. This matches gcc's behavior and what the
624 // runtime expects.
625 if (!Instance && !Method && !getSuperClass()) {
626 Method = lookupInstanceMethod(Sel);
627 // Look through local category implementations associated
628 // with the root class.
629 if (!Method)
630 Method = lookupPrivateMethod(Sel, true);
631 }
632
Steve Naroffbb69c942009-10-01 23:46:04 +0000633 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000634 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000635 return Method;
636}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000637
638//===----------------------------------------------------------------------===//
639// ObjCMethodDecl
640//===----------------------------------------------------------------------===//
641
642ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000643 SourceLocation beginLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000644 SourceLocation endLoc,
645 Selector SelInfo, QualType T,
Douglas Gregor12852d92010-03-08 14:59:44 +0000646 TypeSourceInfo *ResultTInfo,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000647 DeclContext *contextDecl,
648 bool isInstance,
649 bool isVariadic,
Jordan Rosed01e83a2012-10-10 16:42:25 +0000650 bool isPropertyAccessor,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +0000651 bool isImplicitlyDeclared,
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000652 bool isDefined,
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000653 ImplementationControl impControl,
Argyrios Kyrtzidis38493942011-10-03 06:36:29 +0000654 bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000655 return new (C, contextDecl) ObjCMethodDecl(
656 beginLoc, endLoc, SelInfo, T, ResultTInfo, contextDecl, isInstance,
657 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
658 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000659}
660
Douglas Gregor72172e92012-01-05 21:55:30 +0000661ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000662 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
663 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000664}
665
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000666bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
667 return getMethodFamily() == OMF_init &&
668 hasAttr<ObjCDesignatedInitializerAttr>();
669}
670
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000671bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
672 const ObjCMethodDecl **InitMethod) const {
673 if (getMethodFamily() != OMF_init)
674 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000675 const DeclContext *DC = getDeclContext();
676 if (isa<ObjCProtocolDecl>(DC))
677 return false;
678 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000679 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000680 return false;
681}
682
Douglas Gregora6017bb2012-10-09 17:21:28 +0000683Stmt *ObjCMethodDecl::getBody() const {
684 return Body.get(getASTContext().getExternalSource());
685}
686
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000687void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
688 assert(PrevMethod);
689 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
690 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000691 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000692}
693
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000694void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
695 ArrayRef<ParmVarDecl*> Params,
696 ArrayRef<SourceLocation> SelLocs) {
697 ParamsAndSelLocs = 0;
698 NumParams = Params.size();
699 if (Params.empty() && SelLocs.empty())
700 return;
701
702 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
703 sizeof(SourceLocation) * SelLocs.size();
704 ParamsAndSelLocs = C.Allocate(Size);
705 std::copy(Params.begin(), Params.end(), getParams());
706 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
707}
708
709void ObjCMethodDecl::getSelectorLocs(
710 SmallVectorImpl<SourceLocation> &SelLocs) const {
711 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
712 SelLocs.push_back(getSelectorLoc(i));
713}
714
715void ObjCMethodDecl::setMethodParams(ASTContext &C,
716 ArrayRef<ParmVarDecl*> Params,
717 ArrayRef<SourceLocation> SelLocs) {
718 assert((!SelLocs.empty() || isImplicit()) &&
719 "No selector locs for non-implicit method");
720 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000721 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000722
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000723 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
724 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000725 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000726 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000727
728 setParamsAndSelLocs(C, Params, SelLocs);
729}
730
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000731/// \brief A definition will return its interface declaration.
732/// An interface declaration will return its definition.
733/// Otherwise it will return itself.
734ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
735 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000736 ObjCMethodDecl *Redecl = 0;
737 if (HasRedeclaration)
738 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000739 if (Redecl)
740 return Redecl;
741
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000742 Decl *CtxD = cast<Decl>(getDeclContext());
743
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000744 if (!CtxD->isInvalidDecl()) {
745 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
746 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
747 if (!ImplD->isInvalidDecl())
748 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000749
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000750 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
751 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
752 if (!ImplD->isInvalidDecl())
753 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000754
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000755 } else if (ObjCImplementationDecl *ImplD =
756 dyn_cast<ObjCImplementationDecl>(CtxD)) {
757 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
758 if (!IFD->isInvalidDecl())
759 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000760
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000761 } else if (ObjCCategoryImplDecl *CImplD =
762 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
763 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
764 if (!CatD->isInvalidDecl())
765 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
766 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000767 }
768
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000769 if (!Redecl && isRedeclaration()) {
770 // This is the last redeclaration, go back to the first method.
771 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
772 isInstanceMethod());
773 }
774
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000775 return Redecl ? Redecl : this;
776}
777
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000778ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
779 Decl *CtxD = cast<Decl>(getDeclContext());
780
781 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
782 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
783 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
784 isInstanceMethod()))
785 return MD;
786
787 } else if (ObjCCategoryImplDecl *CImplD =
788 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000789 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000790 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
791 isInstanceMethod()))
792 return MD;
793 }
794
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000795 if (isRedeclaration())
796 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
797 isInstanceMethod());
798
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000799 return this;
800}
801
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000802SourceLocation ObjCMethodDecl::getLocEnd() const {
803 if (Stmt *Body = getBody())
804 return Body->getLocEnd();
805 return DeclEndLoc;
806}
807
John McCallb4526252011-03-02 01:50:55 +0000808ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
809 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000810 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000811 return family;
812
John McCall86bc21f2011-03-02 11:33:24 +0000813 // Check for an explicit attribute.
814 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
815 // The unfortunate necessity of mapping between enums here is due
816 // to the attributes framework.
817 switch (attr->getFamily()) {
818 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
819 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
820 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
821 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
822 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
823 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
824 }
825 Family = static_cast<unsigned>(family);
826 return family;
827 }
828
John McCallb4526252011-03-02 01:50:55 +0000829 family = getSelector().getMethodFamily();
830 switch (family) {
831 case OMF_None: break;
832
833 // init only has a conventional meaning for an instance method, and
834 // it has to return an object.
835 case OMF_init:
836 if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
837 family = OMF_None;
838 break;
839
840 // alloc/copy/new have a conventional meaning for both class and
841 // instance methods, but they require an object return.
842 case OMF_alloc:
843 case OMF_copy:
844 case OMF_mutableCopy:
845 case OMF_new:
846 if (!getResultType()->isObjCObjectPointerType())
847 family = OMF_None;
848 break;
849
850 // These selectors have a conventional meaning only for instance methods.
851 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000852 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000853 case OMF_retain:
854 case OMF_release:
855 case OMF_autorelease:
856 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000857 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000858 if (!isInstanceMethod())
859 family = OMF_None;
860 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000861
862 case OMF_performSelector:
863 if (!isInstanceMethod() ||
864 !getResultType()->isObjCIdType())
865 family = OMF_None;
866 else {
867 unsigned noParams = param_size();
868 if (noParams < 1 || noParams > 3)
869 family = OMF_None;
870 else {
871 ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
872 QualType ArgT = (*it);
873 if (!ArgT->isObjCSelType()) {
874 family = OMF_None;
875 break;
876 }
877 while (--noParams) {
878 it++;
879 ArgT = (*it);
880 if (!ArgT->isObjCIdType()) {
881 family = OMF_None;
882 break;
883 }
884 }
885 }
886 }
887 break;
888
John McCallb4526252011-03-02 01:50:55 +0000889 }
890
891 // Cache the result.
892 Family = static_cast<unsigned>(family);
893 return family;
894}
895
Mike Stump11289f42009-09-09 15:08:12 +0000896void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000897 const ObjCInterfaceDecl *OID) {
898 QualType selfTy;
899 if (isInstanceMethod()) {
900 // There may be no interface context due to error in declaration
901 // of the interface (which has been reported). Recover gracefully.
902 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000903 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000904 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000905 } else {
906 selfTy = Context.getObjCIdType();
907 }
908 } else // we have a factory method.
909 selfTy = Context.getObjCClassType();
910
John McCalld4631322011-06-17 06:42:21 +0000911 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000912 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000913
David Blaikiebbafb8a2012-03-11 07:00:24 +0000914 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000915 if (isInstanceMethod()) {
916 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000917
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000918 // 'self' is always __strong. It's actually pseudo-strong except
919 // in init methods (or methods labeled ns_consumes_self), though.
920 Qualifiers qs;
921 qs.setObjCLifetime(Qualifiers::OCL_Strong);
922 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000923
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000924 // In addition, 'self' is const unless this is an init method.
925 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
926 selfTy = selfTy.withConst();
927 selfIsPseudoStrong = true;
928 }
929 }
930 else {
931 assert(isClassMethod());
932 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000933 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000934 selfIsPseudoStrong = true;
935 }
John McCall31168b02011-06-15 23:02:42 +0000936 }
937
938 ImplicitParamDecl *self
939 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
940 &Context.Idents.get("self"), selfTy);
941 setSelfDecl(self);
942
943 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000944 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000945
John McCalld4631322011-06-17 06:42:21 +0000946 if (selfIsPseudoStrong)
947 self->setARCPseudoStrong(true);
948
Mike Stump11289f42009-09-09 15:08:12 +0000949 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
950 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000951 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000952}
953
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000954ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
955 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
956 return ID;
957 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
958 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000959 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000960 return IMD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000961
962 assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
David Blaikie83d382b2011-09-23 05:06:16 +0000963 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000964}
965
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000966static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
967 const ObjCMethodDecl *Method,
968 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
969 bool MovedToSuper) {
970 if (!Container)
971 return;
972
973 // In categories look for overriden methods from protocols. A method from
974 // category is not "overriden" since it is considered as the "same" method
975 // (same USR) as the one from the interface.
976 if (const ObjCCategoryDecl *
977 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
978 // Check whether we have a matching method at this category but only if we
979 // are at the super class level.
980 if (MovedToSuper)
981 if (ObjCMethodDecl *
982 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000983 Method->isInstanceMethod(),
984 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000985 if (Method != Overridden) {
986 // We found an override at this category; there is no need to look
987 // into its protocols.
988 Methods.push_back(Overridden);
989 return;
990 }
991
992 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
993 PEnd = Category->protocol_end();
994 P != PEnd; ++P)
995 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
996 return;
997 }
998
999 // Check whether we have a matching method at this level.
1000 if (const ObjCMethodDecl *
1001 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001002 Method->isInstanceMethod(),
1003 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001004 if (Method != Overridden) {
1005 // We found an override at this level; there is no need to look
1006 // into other protocols or categories.
1007 Methods.push_back(Overridden);
1008 return;
1009 }
1010
1011 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
1012 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
1013 PEnd = Protocol->protocol_end();
1014 P != PEnd; ++P)
1015 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1016 }
1017
1018 if (const ObjCInterfaceDecl *
1019 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
1020 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
1021 PEnd = Interface->protocol_end();
1022 P != PEnd; ++P)
1023 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1024
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001025 for (ObjCInterfaceDecl::known_categories_iterator
1026 Cat = Interface->known_categories_begin(),
1027 CatEnd = Interface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001028 Cat != CatEnd; ++Cat) {
1029 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001030 MovedToSuper);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001031 }
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001032
1033 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1034 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1035 /*MovedToSuper=*/true);
1036 }
1037}
1038
1039static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1040 const ObjCMethodDecl *Method,
1041 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1042 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1043 /*MovedToSuper=*/false);
1044}
1045
1046static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1047 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1048 assert(Method->isOverriding());
1049
1050 if (const ObjCProtocolDecl *
1051 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1052 CollectOverriddenMethods(ProtD, Method, overridden);
1053
1054 } else if (const ObjCImplDecl *
1055 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1056 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1057 if (!ID)
1058 return;
1059 // Start searching for overridden methods using the method from the
1060 // interface as starting point.
1061 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001062 Method->isInstanceMethod(),
1063 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001064 Method = IFaceMeth;
1065 CollectOverriddenMethods(ID, Method, overridden);
1066
1067 } else if (const ObjCCategoryDecl *
1068 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1069 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1070 if (!ID)
1071 return;
1072 // Start searching for overridden methods using the method from the
1073 // interface as starting point.
1074 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001075 Method->isInstanceMethod(),
1076 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001077 Method = IFaceMeth;
1078 CollectOverriddenMethods(ID, Method, overridden);
1079
1080 } else {
1081 CollectOverriddenMethods(
1082 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1083 Method, overridden);
1084 }
1085}
1086
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001087void ObjCMethodDecl::getOverriddenMethods(
1088 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1089 const ObjCMethodDecl *Method = this;
1090
1091 if (Method->isRedeclaration()) {
1092 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1093 getMethod(Method->getSelector(), Method->isInstanceMethod());
1094 }
1095
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001096 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001097 collectOverriddenMethodsSlow(Method, Overridden);
1098 assert(!Overridden.empty() &&
1099 "ObjCMethodDecl's overriding bit is not as expected");
1100 }
1101}
1102
Jordan Rose2bd991a2012-10-10 16:42:54 +00001103const ObjCPropertyDecl *
1104ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1105 Selector Sel = getSelector();
1106 unsigned NumArgs = Sel.getNumArgs();
1107 if (NumArgs > 1)
1108 return 0;
1109
Jordan Rose59e34ec2012-10-11 16:02:02 +00001110 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001111 return 0;
1112
1113 if (isPropertyAccessor()) {
1114 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001115 // If container is class extension, find its primary class.
1116 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1117 if (CatDecl->IsClassExtension())
1118 Container = CatDecl->getClassInterface();
1119
Jordan Rose2bd991a2012-10-10 16:42:54 +00001120 bool IsGetter = (NumArgs == 0);
1121
1122 for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1123 E = Container->prop_end();
1124 I != E; ++I) {
1125 Selector NextSel = IsGetter ? (*I)->getGetterName()
1126 : (*I)->getSetterName();
1127 if (NextSel == Sel)
1128 return *I;
1129 }
1130
1131 llvm_unreachable("Marked as a property accessor but no property found!");
1132 }
1133
1134 if (!CheckOverrides)
1135 return 0;
1136
1137 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1138 OverridesTy Overrides;
1139 getOverriddenMethods(Overrides);
1140 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1141 I != E; ++I) {
1142 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1143 return Prop;
1144 }
1145
1146 return 0;
1147
1148}
1149
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001150//===----------------------------------------------------------------------===//
1151// ObjCInterfaceDecl
1152//===----------------------------------------------------------------------===//
1153
Douglas Gregord53ae832012-01-17 18:09:05 +00001154ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001155 DeclContext *DC,
1156 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001157 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001158 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001159 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001160 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001161 ObjCInterfaceDecl *Result = new (C, DC)
1162 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001163 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001164 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001165 return Result;
1166}
1167
Richard Smithf7981722013-11-22 09:01:48 +00001168ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001169 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001170 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1171 0, SourceLocation(),
1172 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001173 Result->Data.setInt(!C.getLangOpts().Modules);
1174 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001175}
1176
1177ObjCInterfaceDecl::
1178ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001179 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1180 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001181 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001182 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001183{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001184 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001185
1186 // Copy the 'data' pointer over.
1187 if (PrevDecl)
1188 Data = PrevDecl->Data;
1189
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001190 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001191}
1192
Douglas Gregor73693022010-12-01 23:49:52 +00001193void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001194 assert(data().ExternallyCompleted && "Class is not externally completed");
1195 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001196 getASTContext().getExternalSource()->CompleteType(
1197 const_cast<ObjCInterfaceDecl *>(this));
1198}
1199
1200void ObjCInterfaceDecl::setExternallyCompleted() {
1201 assert(getASTContext().getExternalSource() &&
1202 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001203 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001204 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001205 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001206}
1207
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001208void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1209 assert(hasDefinition() && "Forward declarations can't contain methods");
1210 data().HasDesignatedInitializers = true;
1211}
1212
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001213bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1214 assert(hasDefinition() && "Forward declarations can't contain methods");
1215 if (data().ExternallyCompleted)
1216 LoadExternalDefinition();
1217
1218 return data().HasDesignatedInitializers;
1219}
1220
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001221ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001222 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1223 if (data().ExternallyCompleted)
1224 LoadExternalDefinition();
1225
1226 return getASTContext().getObjCImplementation(
1227 const_cast<ObjCInterfaceDecl*>(Def));
1228 }
1229
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001230 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001231 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001232}
1233
1234void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001235 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001236}
1237
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001238namespace {
1239 struct SynthesizeIvarChunk {
1240 uint64_t Size;
1241 ObjCIvarDecl *Ivar;
1242 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1243 : Size(size), Ivar(ivar) {}
1244 };
1245
1246 bool operator<(const SynthesizeIvarChunk & LHS,
1247 const SynthesizeIvarChunk &RHS) {
1248 return LHS.Size < RHS.Size;
1249 }
1250}
1251
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001252/// all_declared_ivar_begin - return first ivar declared in this class,
1253/// its extensions and its implementation. Lazily build the list on first
1254/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001255///
1256/// Caveat: The list returned by this method reflects the current
1257/// state of the parser. The cache will be updated for every ivar
1258/// added by an extension or the implementation when they are
1259/// encountered.
1260/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001261ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001262 // FIXME: Should make sure no callers ever do this.
1263 if (!hasDefinition())
1264 return 0;
1265
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001266 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001267 if (!data().IvarList) {
1268 if (!ivar_empty()) {
1269 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1270 data().IvarList = *I; ++I;
1271 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001272 curIvar->setNextIvar(*I);
1273 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001274
1275 for (ObjCInterfaceDecl::known_extensions_iterator
1276 Ext = known_extensions_begin(),
1277 ExtEnd = known_extensions_end();
1278 Ext != ExtEnd; ++Ext) {
1279 if (!Ext->ivar_empty()) {
1280 ObjCCategoryDecl::ivar_iterator
1281 I = Ext->ivar_begin(),
1282 E = Ext->ivar_end();
1283 if (!data().IvarList) {
1284 data().IvarList = *I; ++I;
1285 curIvar = data().IvarList;
1286 }
1287 for ( ;I != E; curIvar = *I, ++I)
1288 curIvar->setNextIvar(*I);
1289 }
1290 }
1291 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001292 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001293
1294 // cached and complete!
1295 if (!data().IvarListMissingImplementation)
1296 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001297
1298 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001299 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001300 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001301 SmallVector<SynthesizeIvarChunk, 16> layout;
1302 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1303 E = ImplDecl->ivar_end(); I != E; ++I) {
1304 ObjCIvarDecl *IV = *I;
1305 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1306 layout.push_back(SynthesizeIvarChunk(
1307 IV->getASTContext().getTypeSize(IV->getType()), IV));
1308 continue;
1309 }
1310 if (!data().IvarList)
1311 data().IvarList = *I;
1312 else
1313 curIvar->setNextIvar(*I);
1314 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001315 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001316
1317 if (!layout.empty()) {
1318 // Order synthesized ivars by their size.
1319 std::stable_sort(layout.begin(), layout.end());
1320 unsigned Ix = 0, EIx = layout.size();
1321 if (!data().IvarList) {
1322 data().IvarList = layout[0].Ivar; Ix++;
1323 curIvar = data().IvarList;
1324 }
1325 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1326 curIvar->setNextIvar(layout[Ix].Ivar);
1327 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001328 }
1329 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001330 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001331}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001332
1333/// FindCategoryDeclaration - Finds category declaration in the list of
1334/// categories for this class and returns it. Name of the category is passed
1335/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001336///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001337ObjCCategoryDecl *
1338ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001339 // FIXME: Should make sure no callers ever do this.
1340 if (!hasDefinition())
1341 return 0;
1342
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001343 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001344 LoadExternalDefinition();
1345
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001346 for (visible_categories_iterator Cat = visible_categories_begin(),
1347 CatEnd = visible_categories_end();
1348 Cat != CatEnd;
1349 ++Cat) {
1350 if (Cat->getIdentifier() == CategoryId)
1351 return *Cat;
1352 }
1353
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001354 return 0;
1355}
1356
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001357ObjCMethodDecl *
1358ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001359 for (visible_categories_iterator Cat = visible_categories_begin(),
1360 CatEnd = visible_categories_end();
1361 Cat != CatEnd;
1362 ++Cat) {
1363 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001364 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1365 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001366 }
1367
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001368 return 0;
1369}
1370
1371ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001372 for (visible_categories_iterator Cat = visible_categories_begin(),
1373 CatEnd = visible_categories_end();
1374 Cat != CatEnd;
1375 ++Cat) {
1376 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001377 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1378 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001379 }
1380
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001381 return 0;
1382}
1383
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001384/// ClassImplementsProtocol - Checks that 'lProto' protocol
1385/// has been implemented in IDecl class, its super class or categories (if
1386/// lookupCategory is true).
1387bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1388 bool lookupCategory,
1389 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001390 if (!hasDefinition())
1391 return false;
1392
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001393 ObjCInterfaceDecl *IDecl = this;
1394 // 1st, look up the class.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +00001395 for (ObjCInterfaceDecl::protocol_iterator
1396 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001397 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1398 return true;
1399 // This is dubious and is added to be compatible with gcc. In gcc, it is
1400 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1401 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1402 // object. This IMO, should be a bug.
1403 // FIXME: Treat this as an extension, and flag this as an error when GCC
1404 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001405 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001406 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1407 return true;
1408 }
Mike Stump11289f42009-09-09 15:08:12 +00001409
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001410 // 2nd, look up the category.
1411 if (lookupCategory)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001412 for (visible_categories_iterator Cat = visible_categories_begin(),
1413 CatEnd = visible_categories_end();
1414 Cat != CatEnd;
1415 ++Cat) {
1416 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1417 E = Cat->protocol_end();
1418 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001419 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1420 return true;
1421 }
Mike Stump11289f42009-09-09 15:08:12 +00001422
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001423 // 3rd, look up the super class(s)
1424 if (IDecl->getSuperClass())
1425 return
1426 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1427 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001428
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001429 return false;
1430}
1431
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001432//===----------------------------------------------------------------------===//
1433// ObjCIvarDecl
1434//===----------------------------------------------------------------------===//
1435
David Blaikie68e081d2011-12-20 02:48:34 +00001436void ObjCIvarDecl::anchor() { }
1437
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001438ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001439 SourceLocation StartLoc,
1440 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001441 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001442 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001443 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001444 if (DC) {
1445 // Ivar's can only appear in interfaces, implementations (via synthesized
1446 // properties), and class extensions (via direct declaration, or synthesized
1447 // properties).
1448 //
1449 // FIXME: This should really be asserting this:
1450 // (isa<ObjCCategoryDecl>(DC) &&
1451 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1452 // but unfortunately we sometimes place ivars into non-class extension
1453 // categories on error. This breaks an AST invariant, and should not be
1454 // fixed.
1455 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1456 isa<ObjCCategoryDecl>(DC)) &&
1457 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001458 // Once a new ivar is created in any of class/class-extension/implementation
1459 // decl contexts, the previously built IvarList must be rebuilt.
1460 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1461 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001462 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001463 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001464 else
1465 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001466 }
1467 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001468 }
1469
Richard Smithf7981722013-11-22 09:01:48 +00001470 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001471 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001472}
1473
Douglas Gregor72172e92012-01-05 21:55:30 +00001474ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001475 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001476 QualType(), 0, ObjCIvarDecl::None, 0, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001477}
1478
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001479const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1480 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001481
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001482 switch (DC->getKind()) {
1483 default:
1484 case ObjCCategoryImpl:
1485 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001486 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001487
1488 // Ivars can only appear in class extension categories.
1489 case ObjCCategory: {
1490 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1491 assert(CD->IsClassExtension() && "invalid container for ivar!");
1492 return CD->getClassInterface();
1493 }
1494
1495 case ObjCImplementation:
1496 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1497
1498 case ObjCInterface:
1499 return cast<ObjCInterfaceDecl>(DC);
1500 }
1501}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001502
1503//===----------------------------------------------------------------------===//
1504// ObjCAtDefsFieldDecl
1505//===----------------------------------------------------------------------===//
1506
David Blaikie68e081d2011-12-20 02:48:34 +00001507void ObjCAtDefsFieldDecl::anchor() { }
1508
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001509ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001510*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1511 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001512 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001513 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001514}
1515
Richard Smithf7981722013-11-22 09:01:48 +00001516ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001517 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001518 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1519 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001520}
1521
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001522//===----------------------------------------------------------------------===//
1523// ObjCProtocolDecl
1524//===----------------------------------------------------------------------===//
1525
David Blaikie68e081d2011-12-20 02:48:34 +00001526void ObjCProtocolDecl::anchor() { }
1527
Douglas Gregor32c17572012-01-01 20:30:41 +00001528ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1529 SourceLocation nameLoc,
1530 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001531 ObjCProtocolDecl *PrevDecl)
1532 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001533{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001534 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001535 if (PrevDecl)
1536 Data = PrevDecl->Data;
1537}
1538
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001539ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001540 IdentifierInfo *Id,
1541 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001542 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001543 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001544 ObjCProtocolDecl *Result =
1545 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001546 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001547 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001548}
1549
Richard Smithf7981722013-11-22 09:01:48 +00001550ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001551 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001552 ObjCProtocolDecl *Result =
1553 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001554 Result->Data.setInt(!C.getLangOpts().Modules);
1555 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001556}
1557
Steve Naroff114aecb2009-03-01 16:12:44 +00001558ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1559 ObjCProtocolDecl *PDecl = this;
1560
1561 if (Name == getIdentifier())
1562 return PDecl;
1563
1564 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1565 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1566 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001567
Steve Naroff114aecb2009-03-01 16:12:44 +00001568 return NULL;
1569}
1570
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001571// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001572// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001573ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1574 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001575 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001576
Douglas Gregoreed49792013-01-17 00:38:46 +00001577 // If there is no definition or the definition is hidden, we don't find
1578 // anything.
1579 const ObjCProtocolDecl *Def = getDefinition();
1580 if (!Def || Def->isHidden())
1581 return NULL;
1582
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001583 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001584 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001585
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001586 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001587 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001588 return MethodDecl;
1589 return NULL;
1590}
1591
Douglas Gregore6e48b12012-01-01 19:29:29 +00001592void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001593 assert(!Data.getPointer() && "Protocol already has a definition!");
1594 Data.setPointer(new (getASTContext()) DefinitionData);
1595 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001596}
1597
1598void ObjCProtocolDecl::startDefinition() {
1599 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001600
1601 // Update all of the declarations with a pointer to the definition.
1602 for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1603 RD != RDEnd; ++RD)
1604 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001605}
1606
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001607void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1608 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001609
1610 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1611 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1612 E = PDecl->prop_end(); P != E; ++P) {
1613 ObjCPropertyDecl *Prop = *P;
1614 // Insert into PM if not there already.
1615 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001616 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001617 }
1618 // Scan through protocol's protocols.
1619 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1620 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001621 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001622 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001623}
1624
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001625
1626void ObjCProtocolDecl::collectInheritedProtocolProperties(
1627 const ObjCPropertyDecl *Property,
1628 ProtocolPropertyMap &PM) const {
1629 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1630 bool MatchFound = false;
1631 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1632 E = PDecl->prop_end(); P != E; ++P) {
1633 ObjCPropertyDecl *Prop = *P;
1634 if (Prop == Property)
1635 continue;
1636 if (Prop->getIdentifier() == Property->getIdentifier()) {
1637 PM[PDecl] = Prop;
1638 MatchFound = true;
1639 break;
1640 }
1641 }
1642 // Scan through protocol's protocols which did not have a matching property.
1643 if (!MatchFound)
1644 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1645 E = PDecl->protocol_end(); PI != E; ++PI)
1646 (*PI)->collectInheritedProtocolProperties(Property, PM);
1647 }
1648}
Anna Zaks673d76b2012-10-18 19:17:53 +00001649
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001650//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001651// ObjCCategoryDecl
1652//===----------------------------------------------------------------------===//
1653
David Blaikie68e081d2011-12-20 02:48:34 +00001654void ObjCCategoryDecl::anchor() { }
1655
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001656ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001657 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001658 SourceLocation ClassNameLoc,
1659 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001660 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001661 ObjCInterfaceDecl *IDecl,
1662 SourceLocation IvarLBraceLoc,
1663 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001664 ObjCCategoryDecl *CatDecl =
1665 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1666 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001667 if (IDecl) {
1668 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001669 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001670 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001671 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001672 if (ASTMutationListener *L = C.getASTMutationListener())
1673 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1674 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001675 }
1676
1677 return CatDecl;
1678}
1679
Richard Smithf7981722013-11-22 09:01:48 +00001680ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001681 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001682 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1683 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001684}
1685
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001686ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1687 return getASTContext().getObjCImplementation(
1688 const_cast<ObjCCategoryDecl*>(this));
1689}
1690
1691void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1692 getASTContext().setObjCImplementation(this, ImplD);
1693}
1694
1695
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001696//===----------------------------------------------------------------------===//
1697// ObjCCategoryImplDecl
1698//===----------------------------------------------------------------------===//
1699
David Blaikie68e081d2011-12-20 02:48:34 +00001700void ObjCCategoryImplDecl::anchor() { }
1701
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001702ObjCCategoryImplDecl *
1703ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001704 IdentifierInfo *Id,
1705 ObjCInterfaceDecl *ClassInterface,
1706 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001707 SourceLocation atStartLoc,
1708 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001709 if (ClassInterface && ClassInterface->hasDefinition())
1710 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001711 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1712 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001713}
1714
Douglas Gregor72172e92012-01-05 21:55:30 +00001715ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1716 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001717 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1718 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001719}
1720
Steve Narofff406f4d2009-10-29 21:11:04 +00001721ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001722 // The class interface might be NULL if we are working with invalid code.
1723 if (const ObjCInterfaceDecl *ID = getClassInterface())
1724 return ID->FindCategoryDeclaration(getIdentifier());
1725 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001726}
1727
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001728
David Blaikie68e081d2011-12-20 02:48:34 +00001729void ObjCImplDecl::anchor() { }
1730
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001731void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001732 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001733 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001734 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001735}
1736
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001737void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1738 ASTContext &Ctx = getASTContext();
1739
1740 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001741 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001742 if (IFace)
1743 Ctx.setObjCImplementation(IFace, ImplD);
1744
Duncan Sands49c29ee2009-07-21 07:56:29 +00001745 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001746 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1747 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1748 Ctx.setObjCImplementation(CD, ImplD);
1749 }
1750
1751 ClassInterface = IFace;
1752}
1753
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001754/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001755/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001756/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001757///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001758ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001759FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1760 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001761 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001762 if (PID->getPropertyIvarDecl() &&
1763 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1764 return PID;
1765 }
1766 return 0;
1767}
1768
1769/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001770/// added to the list of those properties \@synthesized/\@dynamic in this
1771/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001772///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001773ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001774FindPropertyImplDecl(IdentifierInfo *Id) const {
1775 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001776 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001777 if (PID->getPropertyDecl()->getIdentifier() == Id)
1778 return PID;
1779 }
1780 return 0;
1781}
1782
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001783raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001784 const ObjCCategoryImplDecl &CID) {
1785 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001786 return OS;
1787}
1788
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001789//===----------------------------------------------------------------------===//
1790// ObjCImplementationDecl
1791//===----------------------------------------------------------------------===//
1792
David Blaikie68e081d2011-12-20 02:48:34 +00001793void ObjCImplementationDecl::anchor() { }
1794
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001795ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001796ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001797 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001798 ObjCInterfaceDecl *SuperDecl,
1799 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001800 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001801 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001802 SourceLocation IvarLBraceLoc,
1803 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001804 if (ClassInterface && ClassInterface->hasDefinition())
1805 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001806 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1807 nameLoc, atStartLoc, superLoc,
1808 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001809}
1810
Douglas Gregor72172e92012-01-05 21:55:30 +00001811ObjCImplementationDecl *
1812ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001813 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1814 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001815}
1816
John McCall0410e572011-07-22 04:15:06 +00001817void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1818 CXXCtorInitializer ** initializers,
1819 unsigned numInitializers) {
1820 if (numInitializers > 0) {
1821 NumIvarInitializers = numInitializers;
1822 CXXCtorInitializer **ivarInitializers =
1823 new (C) CXXCtorInitializer*[NumIvarInitializers];
1824 memcpy(ivarInitializers, initializers,
1825 numInitializers * sizeof(CXXCtorInitializer*));
1826 IvarInitializers = ivarInitializers;
1827 }
1828}
1829
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001830raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001831 const ObjCImplementationDecl &ID) {
1832 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001833 return OS;
1834}
1835
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001836//===----------------------------------------------------------------------===//
1837// ObjCCompatibleAliasDecl
1838//===----------------------------------------------------------------------===//
1839
David Blaikie68e081d2011-12-20 02:48:34 +00001840void ObjCCompatibleAliasDecl::anchor() { }
1841
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001842ObjCCompatibleAliasDecl *
1843ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1844 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001845 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001846 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001847 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001848}
1849
Douglas Gregor72172e92012-01-05 21:55:30 +00001850ObjCCompatibleAliasDecl *
1851ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001852 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001853}
1854
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001855//===----------------------------------------------------------------------===//
1856// ObjCPropertyDecl
1857//===----------------------------------------------------------------------===//
1858
David Blaikie68e081d2011-12-20 02:48:34 +00001859void ObjCPropertyDecl::anchor() { }
1860
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001861ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1862 SourceLocation L,
1863 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001864 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001865 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001866 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001867 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001868 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001869}
1870
Richard Smithf7981722013-11-22 09:01:48 +00001871ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001872 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001873 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1874 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001875}
1876
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001877//===----------------------------------------------------------------------===//
1878// ObjCPropertyImplDecl
1879//===----------------------------------------------------------------------===//
1880
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001881ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001882 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001883 SourceLocation atLoc,
1884 SourceLocation L,
1885 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001886 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001887 ObjCIvarDecl *ivar,
1888 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001889 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1890 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001891}
Chris Lattnered0e1642008-03-17 01:19:02 +00001892
Richard Smithf7981722013-11-22 09:01:48 +00001893ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001894 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001895 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1896 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001897}
1898
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001899SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1900 SourceLocation EndLoc = getLocation();
1901 if (IvarLoc.isValid())
1902 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001903
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001904 return SourceRange(AtLoc, EndLoc);
1905}