blob: 4fd152e0902950c0bebf7b8d09247ce4e2319b00 [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).
Aaron Ballmand174edf2014-03-13 19:11:50 +0000128 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000129 if (P->getIdentifier() == Property->getIdentifier()) {
130 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
131 return true;
132 break;
133 }
134 }
135
136 // Also look into protocols, for a user declared instance method.
137 for (ObjCInterfaceDecl::all_protocol_iterator P =
138 ID->all_referenced_protocol_begin(),
139 PE = ID->all_referenced_protocol_end(); P != PE; ++P) {
140 ObjCProtocolDecl *Proto = (*P);
141 if (Proto->HasUserDeclaredSetterMethod(Property))
142 return true;
143 }
144 // And in its super class.
145 ObjCInterfaceDecl *OSC = ID->getSuperClass();
146 while (OSC) {
147 if (OSC->HasUserDeclaredSetterMethod(Property))
148 return true;
149 OSC = OSC->getSuperClass();
150 }
151 }
152 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
153 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
154 E = PD->protocol_end(); PI != E; ++PI) {
155 if ((*PI)->HasUserDeclaredSetterMethod(Property))
156 return true;
157 }
158 return false;
159}
160
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000161ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000162ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000163 IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000164 // If this context is a hidden protocol definition, don't find any
165 // property.
166 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
167 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
168 if (Def->isHidden())
169 return 0;
170 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000171
David Blaikieff7d47a2012-12-19 00:45:41 +0000172 DeclContext::lookup_const_result R = DC->lookup(propertyID);
173 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
174 ++I)
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000175 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
176 return PD;
177
178 return 0;
179}
180
Anna Zaks454477c2012-09-27 19:45:11 +0000181IdentifierInfo *
182ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
183 SmallString<128> ivarName;
184 {
185 llvm::raw_svector_ostream os(ivarName);
186 os << '_' << getIdentifier()->getName();
187 }
188 return &Ctx.Idents.get(ivarName.str());
189}
190
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000191/// FindPropertyDeclaration - Finds declaration of the property given its name
192/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000193ObjCPropertyDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000194ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000195 // Don't find properties within hidden protocol definitions.
196 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
197 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
198 if (Def->isHidden())
199 return 0;
200 }
Mike Stump11289f42009-09-09 15:08:12 +0000201
Ted Kremenekddcd1092010-03-15 20:11:53 +0000202 if (ObjCPropertyDecl *PD =
203 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
204 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000205
Ted Kremenekddcd1092010-03-15 20:11:53 +0000206 switch (getKind()) {
207 default:
208 break;
209 case Decl::ObjCProtocol: {
210 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
211 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
212 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000213 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
214 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000215 break;
216 }
217 case Decl::ObjCInterface: {
218 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000219 // Look through categories (but not extensions).
220 for (ObjCInterfaceDecl::visible_categories_iterator
221 Cat = OID->visible_categories_begin(),
222 CatEnd = OID->visible_categories_end();
223 Cat != CatEnd; ++Cat) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000224 if (!Cat->IsClassExtension())
225 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
226 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000227 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000228
229 // Look through protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000230 for (ObjCInterfaceDecl::all_protocol_iterator
231 I = OID->all_referenced_protocol_begin(),
232 E = OID->all_referenced_protocol_end(); I != E; ++I)
Ted Kremenekddcd1092010-03-15 20:11:53 +0000233 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
234 return P;
235
236 // Finally, check the super class.
237 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
238 return superClass->FindPropertyDeclaration(PropertyId);
239 break;
240 }
241 case Decl::ObjCCategory: {
242 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
243 // Look through protocols.
244 if (!OCD->IsClassExtension())
245 for (ObjCCategoryDecl::protocol_iterator
246 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
247 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
248 return P;
249
250 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000251 }
252 }
Steve Narofff9c65242008-06-05 13:55:23 +0000253 return 0;
254}
255
David Blaikie68e081d2011-12-20 02:48:34 +0000256void ObjCInterfaceDecl::anchor() { }
257
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000258/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
259/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000260/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000261///
262ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000263ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000264 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000265 // FIXME: Should make sure no callers ever do this.
266 if (!hasDefinition())
267 return 0;
268
269 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000270 LoadExternalDefinition();
271
Ted Kremenekd133a862010-03-15 20:30:07 +0000272 if (ObjCPropertyDecl *PD =
273 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
274 return PD;
275
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000276 // Look through protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000277 for (ObjCInterfaceDecl::all_protocol_iterator
278 I = all_referenced_protocol_begin(),
279 E = all_referenced_protocol_end(); I != E; ++I)
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000280 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
281 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000282
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000283 return 0;
284}
285
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000286void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
287 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000288 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000289 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000290 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000291 }
292 for (ObjCInterfaceDecl::all_protocol_iterator
293 PI = all_referenced_protocol_begin(),
294 E = all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000295 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000296 // Note, the properties declared only in class extensions are still copied
297 // into the main @interface's property list, and therefore we don't
298 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000299}
300
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000301bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
302 const ObjCInterfaceDecl *Class = this;
303 while (Class) {
304 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
305 return true;
306 Class = Class->getSuperClass();
307 }
308 return false;
309}
310
311const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
312 const ObjCInterfaceDecl *Class = this;
313 while (Class) {
314 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
315 return Class;
316 Class = Class->getSuperClass();
317 }
318 return 0;
319}
320
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000321void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
322 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
323 ASTContext &C)
324{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000325 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000326 LoadExternalDefinition();
327
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000328 if (data().AllReferencedProtocols.empty() &&
329 data().ReferencedProtocols.empty()) {
330 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000331 return;
332 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000333
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000334 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000335 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000336 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000337 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000338 for (unsigned i = 0; i < ExtNum; i++) {
339 bool protocolExists = false;
340 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000341 for (all_protocol_iterator
342 p = all_referenced_protocol_begin(),
343 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000344 ObjCProtocolDecl *Proto = (*p);
345 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
346 protocolExists = true;
347 break;
348 }
349 }
350 // Do we want to warn on a protocol in extension class which
351 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000352 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000353 ProtocolRefs.push_back(ProtoInExtension);
354 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000355
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000356 if (ProtocolRefs.empty())
357 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000358
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000359 // Merge ProtocolRefs into class's protocol list;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000360 for (all_protocol_iterator p = all_referenced_protocol_begin(),
361 e = all_referenced_protocol_end(); p != e; ++p) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000362 ProtocolRefs.push_back(*p);
Douglas Gregor002b6712010-01-16 15:02:53 +0000363 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000364
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000365 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000366}
367
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000368const ObjCInterfaceDecl *
369ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
370 const ObjCInterfaceDecl *IFace = this;
371 while (IFace) {
372 if (IFace->hasDesignatedInitializers())
373 return IFace;
374 if (!IFace->inheritsDesignatedInitializers())
375 break;
376 IFace = IFace->getSuperClass();
377 }
378 return 0;
379}
380
381bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
382 switch (data().InheritedDesignatedInitializers) {
383 case DefinitionData::IDI_Inherited:
384 return true;
385 case DefinitionData::IDI_NotInherited:
386 return false;
387 case DefinitionData::IDI_Unknown: {
388 bool isIntroducingInitializers = false;
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000389 for (const auto *MD : instance_methods()) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000390 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding()) {
391 isIntroducingInitializers = true;
392 break;
393 }
394 }
395 // If the class introduced initializers we conservatively assume that we
396 // don't know if any of them is a designated initializer to avoid possible
397 // misleading warnings.
398 if (isIntroducingInitializers) {
399 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
400 return false;
401 } else {
402 data().InheritedDesignatedInitializers = DefinitionData::IDI_Inherited;
403 return true;
404 }
405 }
406 }
407
408 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
409}
410
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000411void ObjCInterfaceDecl::getDesignatedInitializers(
412 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000413 // Check for a complete definition and recover if not so.
414 if (!isThisDeclarationADefinition())
415 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000416 if (data().ExternallyCompleted)
417 LoadExternalDefinition();
418
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000419 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000420 if (!IFace)
421 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000422
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000423 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000424 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000425 Methods.push_back(MD);
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000426}
427
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000428bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
429 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000430 // Check for a complete definition and recover if not so.
431 if (!isThisDeclarationADefinition())
432 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000433 if (data().ExternallyCompleted)
434 LoadExternalDefinition();
435
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000436 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000437 if (!IFace)
438 return false;
439
Argyrios Kyrtzidise919fc22013-12-10 18:36:43 +0000440 if (const ObjCMethodDecl *MD = IFace->getMethod(Sel, /*isInstance=*/true)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000441 if (MD->isThisDeclarationADesignatedInitializer()) {
442 if (InitMethod)
443 *InitMethod = MD;
444 return true;
445 }
446 }
447 return false;
448}
449
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000450void ObjCInterfaceDecl::allocateDefinitionData() {
451 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000452 Data.setPointer(new (getASTContext()) DefinitionData());
453 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000454
455 // Make the type point at the definition, now that we have one.
456 if (TypeForDecl)
457 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000458}
459
460void ObjCInterfaceDecl::startDefinition() {
461 allocateDefinitionData();
462
Douglas Gregor66b310c2011-12-15 18:03:09 +0000463 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000464 for (auto RD : redecls()) {
465 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000466 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000467 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000468}
469
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000470ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
471 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000472 // FIXME: Should make sure no callers ever do this.
473 if (!hasDefinition())
474 return 0;
475
476 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000477 LoadExternalDefinition();
478
Chris Lattner89375192008-03-16 00:19:01 +0000479 ObjCInterfaceDecl* ClassDecl = this;
480 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000481 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000482 clsDeclared = ClassDecl;
483 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000484 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000485
486 for (ObjCInterfaceDecl::visible_extensions_iterator
487 Ext = ClassDecl->visible_extensions_begin(),
488 ExtEnd = ClassDecl->visible_extensions_end();
489 Ext != ExtEnd; ++Ext) {
490 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000491 clsDeclared = ClassDecl;
492 return I;
493 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000494 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000495
Chris Lattner89375192008-03-16 00:19:01 +0000496 ClassDecl = ClassDecl->getSuperClass();
497 }
498 return NULL;
499}
500
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000501/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
502/// class whose name is passed as argument. If it is not one of the super classes
503/// the it returns NULL.
504ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
505 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000506 // FIXME: Should make sure no callers ever do this.
507 if (!hasDefinition())
508 return 0;
509
510 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000511 LoadExternalDefinition();
512
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000513 ObjCInterfaceDecl* ClassDecl = this;
514 while (ClassDecl != NULL) {
515 if (ClassDecl->getIdentifier() == ICName)
516 return ClassDecl;
517 ClassDecl = ClassDecl->getSuperClass();
518 }
519 return NULL;
520}
521
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000522ObjCProtocolDecl *
523ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
524 for (ObjCInterfaceDecl::all_protocol_iterator P =
525 all_referenced_protocol_begin(), PE = all_referenced_protocol_end();
526 P != PE; ++P)
527 if ((*P)->lookupProtocolNamed(Name))
528 return (*P);
529 ObjCInterfaceDecl *SuperClass = getSuperClass();
530 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
531}
532
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000533/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000534/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000535/// When argument category "C" is specified, any implicit method found
536/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000537ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000538 bool isInstance,
539 bool shallowCategoryLookup,
540 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000541 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000542{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000543 // FIXME: Should make sure no callers ever do this.
544 if (!hasDefinition())
545 return 0;
546
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000547 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000548 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000549
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000550 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000551 LoadExternalDefinition();
552
Ted Kremenek00781502013-11-23 01:01:29 +0000553 while (ClassDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000554 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000555 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000556
Chris Lattner89375192008-03-16 00:19:01 +0000557 // Didn't find one yet - look through protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000558 for (const auto *I : ClassDecl->protocols())
559 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000560 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000561
562 // Didn't find one yet - now look through categories.
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000563 for (ObjCInterfaceDecl::visible_categories_iterator
564 Cat = ClassDecl->visible_categories_begin(),
565 CatEnd = ClassDecl->visible_categories_end();
566 Cat != CatEnd; ++Cat) {
567 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
568 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000569 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000570
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000571 if (!shallowCategoryLookup) {
572 // Didn't find one yet - look through protocols.
573 const ObjCList<ObjCProtocolDecl> &Protocols =
574 Cat->getReferencedProtocols();
575 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
576 E = Protocols.end(); I != E; ++I)
577 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
578 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000579 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000580 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000581 }
Ted Kremenek00781502013-11-23 01:01:29 +0000582
583 if (!followSuper)
584 return NULL;
585
586 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000587 ClassDecl = ClassDecl->getSuperClass();
588 }
589 return NULL;
590}
591
Anna Zaksc77a3b12012-07-27 19:07:44 +0000592// Will search "local" class/category implementations for a method decl.
593// If failed, then we search in class's root for an instance method.
594// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000595ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
596 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000597 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000598 // FIXME: Should make sure no callers ever do this.
599 if (!hasDefinition())
600 return 0;
601
602 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000603 LoadExternalDefinition();
604
Steve Naroffbb69c942009-10-01 23:46:04 +0000605 ObjCMethodDecl *Method = 0;
606 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000607 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
608 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000609
610 // Look through local category implementations associated with the class.
611 if (!Method)
612 Method = Instance ? getCategoryInstanceMethod(Sel)
613 : getCategoryClassMethod(Sel);
614
615 // Before we give up, check if the selector is an instance method.
616 // But only in the root. This matches gcc's behavior and what the
617 // runtime expects.
618 if (!Instance && !Method && !getSuperClass()) {
619 Method = lookupInstanceMethod(Sel);
620 // Look through local category implementations associated
621 // with the root class.
622 if (!Method)
623 Method = lookupPrivateMethod(Sel, true);
624 }
625
Steve Naroffbb69c942009-10-01 23:46:04 +0000626 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000627 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000628 return Method;
629}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000630
631//===----------------------------------------------------------------------===//
632// ObjCMethodDecl
633//===----------------------------------------------------------------------===//
634
Alp Toker314cc812014-01-25 16:55:45 +0000635ObjCMethodDecl *ObjCMethodDecl::Create(
636 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
637 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
638 DeclContext *contextDecl, bool isInstance, bool isVariadic,
639 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
640 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000641 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000642 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000643 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
644 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000645}
646
Douglas Gregor72172e92012-01-05 21:55:30 +0000647ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000648 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
649 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000650}
651
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000652bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
653 return getMethodFamily() == OMF_init &&
654 hasAttr<ObjCDesignatedInitializerAttr>();
655}
656
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000657bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
658 const ObjCMethodDecl **InitMethod) const {
659 if (getMethodFamily() != OMF_init)
660 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000661 const DeclContext *DC = getDeclContext();
662 if (isa<ObjCProtocolDecl>(DC))
663 return false;
664 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000665 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000666 return false;
667}
668
Douglas Gregora6017bb2012-10-09 17:21:28 +0000669Stmt *ObjCMethodDecl::getBody() const {
670 return Body.get(getASTContext().getExternalSource());
671}
672
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000673void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
674 assert(PrevMethod);
675 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
676 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000677 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000678}
679
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000680void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
681 ArrayRef<ParmVarDecl*> Params,
682 ArrayRef<SourceLocation> SelLocs) {
683 ParamsAndSelLocs = 0;
684 NumParams = Params.size();
685 if (Params.empty() && SelLocs.empty())
686 return;
687
688 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
689 sizeof(SourceLocation) * SelLocs.size();
690 ParamsAndSelLocs = C.Allocate(Size);
691 std::copy(Params.begin(), Params.end(), getParams());
692 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
693}
694
695void ObjCMethodDecl::getSelectorLocs(
696 SmallVectorImpl<SourceLocation> &SelLocs) const {
697 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
698 SelLocs.push_back(getSelectorLoc(i));
699}
700
701void ObjCMethodDecl::setMethodParams(ASTContext &C,
702 ArrayRef<ParmVarDecl*> Params,
703 ArrayRef<SourceLocation> SelLocs) {
704 assert((!SelLocs.empty() || isImplicit()) &&
705 "No selector locs for non-implicit method");
706 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000707 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000708
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000709 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
710 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000711 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000712 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000713
714 setParamsAndSelLocs(C, Params, SelLocs);
715}
716
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000717/// \brief A definition will return its interface declaration.
718/// An interface declaration will return its definition.
719/// Otherwise it will return itself.
720ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
721 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000722 ObjCMethodDecl *Redecl = 0;
723 if (HasRedeclaration)
724 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000725 if (Redecl)
726 return Redecl;
727
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000728 Decl *CtxD = cast<Decl>(getDeclContext());
729
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000730 if (!CtxD->isInvalidDecl()) {
731 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
732 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
733 if (!ImplD->isInvalidDecl())
734 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000735
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000736 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
737 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
738 if (!ImplD->isInvalidDecl())
739 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000740
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000741 } else if (ObjCImplementationDecl *ImplD =
742 dyn_cast<ObjCImplementationDecl>(CtxD)) {
743 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
744 if (!IFD->isInvalidDecl())
745 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000746
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000747 } else if (ObjCCategoryImplDecl *CImplD =
748 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
749 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
750 if (!CatD->isInvalidDecl())
751 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
752 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000753 }
754
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000755 if (!Redecl && isRedeclaration()) {
756 // This is the last redeclaration, go back to the first method.
757 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
758 isInstanceMethod());
759 }
760
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000761 return Redecl ? Redecl : this;
762}
763
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000764ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
765 Decl *CtxD = cast<Decl>(getDeclContext());
766
767 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
768 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
769 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
770 isInstanceMethod()))
771 return MD;
772
773 } else if (ObjCCategoryImplDecl *CImplD =
774 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000775 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000776 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
777 isInstanceMethod()))
778 return MD;
779 }
780
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000781 if (isRedeclaration())
782 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
783 isInstanceMethod());
784
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000785 return this;
786}
787
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000788SourceLocation ObjCMethodDecl::getLocEnd() const {
789 if (Stmt *Body = getBody())
790 return Body->getLocEnd();
791 return DeclEndLoc;
792}
793
John McCallb4526252011-03-02 01:50:55 +0000794ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
795 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000796 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000797 return family;
798
John McCall86bc21f2011-03-02 11:33:24 +0000799 // Check for an explicit attribute.
800 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
801 // The unfortunate necessity of mapping between enums here is due
802 // to the attributes framework.
803 switch (attr->getFamily()) {
804 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
805 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
806 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
807 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
808 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
809 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
810 }
811 Family = static_cast<unsigned>(family);
812 return family;
813 }
814
John McCallb4526252011-03-02 01:50:55 +0000815 family = getSelector().getMethodFamily();
816 switch (family) {
817 case OMF_None: break;
818
819 // init only has a conventional meaning for an instance method, and
820 // it has to return an object.
821 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000822 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000823 family = OMF_None;
824 break;
825
826 // alloc/copy/new have a conventional meaning for both class and
827 // instance methods, but they require an object return.
828 case OMF_alloc:
829 case OMF_copy:
830 case OMF_mutableCopy:
831 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000832 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000833 family = OMF_None;
834 break;
835
836 // These selectors have a conventional meaning only for instance methods.
837 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000838 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000839 case OMF_retain:
840 case OMF_release:
841 case OMF_autorelease:
842 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000843 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000844 if (!isInstanceMethod())
845 family = OMF_None;
846 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000847
848 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000849 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000850 family = OMF_None;
851 else {
852 unsigned noParams = param_size();
853 if (noParams < 1 || noParams > 3)
854 family = OMF_None;
855 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000856 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000857 QualType ArgT = (*it);
858 if (!ArgT->isObjCSelType()) {
859 family = OMF_None;
860 break;
861 }
862 while (--noParams) {
863 it++;
864 ArgT = (*it);
865 if (!ArgT->isObjCIdType()) {
866 family = OMF_None;
867 break;
868 }
869 }
870 }
871 }
872 break;
873
John McCallb4526252011-03-02 01:50:55 +0000874 }
875
876 // Cache the result.
877 Family = static_cast<unsigned>(family);
878 return family;
879}
880
Mike Stump11289f42009-09-09 15:08:12 +0000881void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000882 const ObjCInterfaceDecl *OID) {
883 QualType selfTy;
884 if (isInstanceMethod()) {
885 // There may be no interface context due to error in declaration
886 // of the interface (which has been reported). Recover gracefully.
887 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000888 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000889 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000890 } else {
891 selfTy = Context.getObjCIdType();
892 }
893 } else // we have a factory method.
894 selfTy = Context.getObjCClassType();
895
John McCalld4631322011-06-17 06:42:21 +0000896 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000897 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000898
David Blaikiebbafb8a2012-03-11 07:00:24 +0000899 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000900 if (isInstanceMethod()) {
901 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000902
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000903 // 'self' is always __strong. It's actually pseudo-strong except
904 // in init methods (or methods labeled ns_consumes_self), though.
905 Qualifiers qs;
906 qs.setObjCLifetime(Qualifiers::OCL_Strong);
907 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000908
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000909 // In addition, 'self' is const unless this is an init method.
910 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
911 selfTy = selfTy.withConst();
912 selfIsPseudoStrong = true;
913 }
914 }
915 else {
916 assert(isClassMethod());
917 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000918 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000919 selfIsPseudoStrong = true;
920 }
John McCall31168b02011-06-15 23:02:42 +0000921 }
922
923 ImplicitParamDecl *self
924 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
925 &Context.Idents.get("self"), selfTy);
926 setSelfDecl(self);
927
928 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000929 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000930
John McCalld4631322011-06-17 06:42:21 +0000931 if (selfIsPseudoStrong)
932 self->setARCPseudoStrong(true);
933
Mike Stump11289f42009-09-09 15:08:12 +0000934 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
935 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000936 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000937}
938
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000939ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
940 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
941 return ID;
942 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
943 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000944 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000945 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000946 if (isa<ObjCProtocolDecl>(getDeclContext()))
947 return 0;
David Blaikie83d382b2011-09-23 05:06:16 +0000948 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000949}
950
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000951static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
952 const ObjCMethodDecl *Method,
953 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
954 bool MovedToSuper) {
955 if (!Container)
956 return;
957
958 // In categories look for overriden methods from protocols. A method from
959 // category is not "overriden" since it is considered as the "same" method
960 // (same USR) as the one from the interface.
961 if (const ObjCCategoryDecl *
962 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
963 // Check whether we have a matching method at this category but only if we
964 // are at the super class level.
965 if (MovedToSuper)
966 if (ObjCMethodDecl *
967 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000968 Method->isInstanceMethod(),
969 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000970 if (Method != Overridden) {
971 // We found an override at this category; there is no need to look
972 // into its protocols.
973 Methods.push_back(Overridden);
974 return;
975 }
976
977 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
978 PEnd = Category->protocol_end();
979 P != PEnd; ++P)
980 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
981 return;
982 }
983
984 // Check whether we have a matching method at this level.
985 if (const ObjCMethodDecl *
986 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000987 Method->isInstanceMethod(),
988 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000989 if (Method != Overridden) {
990 // We found an override at this level; there is no need to look
991 // into other protocols or categories.
992 Methods.push_back(Overridden);
993 return;
994 }
995
996 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
997 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
998 PEnd = Protocol->protocol_end();
999 P != PEnd; ++P)
1000 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1001 }
1002
1003 if (const ObjCInterfaceDecl *
1004 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001005 for (const auto *P : Interface->protocols())
1006 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001007
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001008 for (ObjCInterfaceDecl::known_categories_iterator
1009 Cat = Interface->known_categories_begin(),
1010 CatEnd = Interface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001011 Cat != CatEnd; ++Cat) {
1012 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001013 MovedToSuper);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001014 }
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001015
1016 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1017 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1018 /*MovedToSuper=*/true);
1019 }
1020}
1021
1022static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1023 const ObjCMethodDecl *Method,
1024 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1025 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1026 /*MovedToSuper=*/false);
1027}
1028
1029static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1030 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1031 assert(Method->isOverriding());
1032
1033 if (const ObjCProtocolDecl *
1034 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1035 CollectOverriddenMethods(ProtD, Method, overridden);
1036
1037 } else if (const ObjCImplDecl *
1038 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1039 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1040 if (!ID)
1041 return;
1042 // Start searching for overridden methods using the method from the
1043 // interface as starting point.
1044 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001045 Method->isInstanceMethod(),
1046 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001047 Method = IFaceMeth;
1048 CollectOverriddenMethods(ID, Method, overridden);
1049
1050 } else if (const ObjCCategoryDecl *
1051 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1052 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1053 if (!ID)
1054 return;
1055 // Start searching for overridden methods using the method from the
1056 // interface as starting point.
1057 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001058 Method->isInstanceMethod(),
1059 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001060 Method = IFaceMeth;
1061 CollectOverriddenMethods(ID, Method, overridden);
1062
1063 } else {
1064 CollectOverriddenMethods(
1065 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1066 Method, overridden);
1067 }
1068}
1069
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001070void ObjCMethodDecl::getOverriddenMethods(
1071 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1072 const ObjCMethodDecl *Method = this;
1073
1074 if (Method->isRedeclaration()) {
1075 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1076 getMethod(Method->getSelector(), Method->isInstanceMethod());
1077 }
1078
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001079 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001080 collectOverriddenMethodsSlow(Method, Overridden);
1081 assert(!Overridden.empty() &&
1082 "ObjCMethodDecl's overriding bit is not as expected");
1083 }
1084}
1085
Jordan Rose2bd991a2012-10-10 16:42:54 +00001086const ObjCPropertyDecl *
1087ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1088 Selector Sel = getSelector();
1089 unsigned NumArgs = Sel.getNumArgs();
1090 if (NumArgs > 1)
1091 return 0;
1092
Jordan Rose59e34ec2012-10-11 16:02:02 +00001093 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001094 return 0;
1095
1096 if (isPropertyAccessor()) {
1097 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001098 // If container is class extension, find its primary class.
1099 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1100 if (CatDecl->IsClassExtension())
1101 Container = CatDecl->getClassInterface();
1102
Jordan Rose2bd991a2012-10-10 16:42:54 +00001103 bool IsGetter = (NumArgs == 0);
1104
Aaron Ballmand174edf2014-03-13 19:11:50 +00001105 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001106 Selector NextSel = IsGetter ? I->getGetterName()
1107 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001108 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001109 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001110 }
1111
1112 llvm_unreachable("Marked as a property accessor but no property found!");
1113 }
1114
1115 if (!CheckOverrides)
1116 return 0;
1117
1118 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1119 OverridesTy Overrides;
1120 getOverriddenMethods(Overrides);
1121 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1122 I != E; ++I) {
1123 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1124 return Prop;
1125 }
1126
1127 return 0;
1128
1129}
1130
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001131//===----------------------------------------------------------------------===//
1132// ObjCInterfaceDecl
1133//===----------------------------------------------------------------------===//
1134
Douglas Gregord53ae832012-01-17 18:09:05 +00001135ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001136 DeclContext *DC,
1137 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001138 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001139 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001140 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001141 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001142 ObjCInterfaceDecl *Result = new (C, DC)
1143 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001144 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001145 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001146 return Result;
1147}
1148
Richard Smithf7981722013-11-22 09:01:48 +00001149ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001150 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001151 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1152 0, SourceLocation(),
1153 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001154 Result->Data.setInt(!C.getLangOpts().Modules);
1155 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001156}
1157
1158ObjCInterfaceDecl::
1159ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001160 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1161 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001162 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001163 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001164{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001165 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001166
1167 // Copy the 'data' pointer over.
1168 if (PrevDecl)
1169 Data = PrevDecl->Data;
1170
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001171 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001172}
1173
Douglas Gregor73693022010-12-01 23:49:52 +00001174void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001175 assert(data().ExternallyCompleted && "Class is not externally completed");
1176 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001177 getASTContext().getExternalSource()->CompleteType(
1178 const_cast<ObjCInterfaceDecl *>(this));
1179}
1180
1181void ObjCInterfaceDecl::setExternallyCompleted() {
1182 assert(getASTContext().getExternalSource() &&
1183 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001184 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001185 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001186 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001187}
1188
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001189void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001190 // Check for a complete definition and recover if not so.
1191 if (!isThisDeclarationADefinition())
1192 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001193 data().HasDesignatedInitializers = true;
1194}
1195
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001196bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001197 // Check for a complete definition and recover if not so.
1198 if (!isThisDeclarationADefinition())
1199 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001200 if (data().ExternallyCompleted)
1201 LoadExternalDefinition();
1202
1203 return data().HasDesignatedInitializers;
1204}
1205
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001206ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001207 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1208 if (data().ExternallyCompleted)
1209 LoadExternalDefinition();
1210
1211 return getASTContext().getObjCImplementation(
1212 const_cast<ObjCInterfaceDecl*>(Def));
1213 }
1214
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001215 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001216 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001217}
1218
1219void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001220 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001221}
1222
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001223namespace {
1224 struct SynthesizeIvarChunk {
1225 uint64_t Size;
1226 ObjCIvarDecl *Ivar;
1227 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1228 : Size(size), Ivar(ivar) {}
1229 };
1230
1231 bool operator<(const SynthesizeIvarChunk & LHS,
1232 const SynthesizeIvarChunk &RHS) {
1233 return LHS.Size < RHS.Size;
1234 }
1235}
1236
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001237/// all_declared_ivar_begin - return first ivar declared in this class,
1238/// its extensions and its implementation. Lazily build the list on first
1239/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001240///
1241/// Caveat: The list returned by this method reflects the current
1242/// state of the parser. The cache will be updated for every ivar
1243/// added by an extension or the implementation when they are
1244/// encountered.
1245/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001246ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001247 // FIXME: Should make sure no callers ever do this.
1248 if (!hasDefinition())
1249 return 0;
1250
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001251 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001252 if (!data().IvarList) {
1253 if (!ivar_empty()) {
1254 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1255 data().IvarList = *I; ++I;
1256 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001257 curIvar->setNextIvar(*I);
1258 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001259
1260 for (ObjCInterfaceDecl::known_extensions_iterator
1261 Ext = known_extensions_begin(),
1262 ExtEnd = known_extensions_end();
1263 Ext != ExtEnd; ++Ext) {
1264 if (!Ext->ivar_empty()) {
1265 ObjCCategoryDecl::ivar_iterator
1266 I = Ext->ivar_begin(),
1267 E = Ext->ivar_end();
1268 if (!data().IvarList) {
1269 data().IvarList = *I; ++I;
1270 curIvar = data().IvarList;
1271 }
1272 for ( ;I != E; curIvar = *I, ++I)
1273 curIvar->setNextIvar(*I);
1274 }
1275 }
1276 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001277 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001278
1279 // cached and complete!
1280 if (!data().IvarListMissingImplementation)
1281 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001282
1283 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001284 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001285 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001286 SmallVector<SynthesizeIvarChunk, 16> layout;
1287 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1288 E = ImplDecl->ivar_end(); I != E; ++I) {
1289 ObjCIvarDecl *IV = *I;
1290 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1291 layout.push_back(SynthesizeIvarChunk(
1292 IV->getASTContext().getTypeSize(IV->getType()), IV));
1293 continue;
1294 }
1295 if (!data().IvarList)
1296 data().IvarList = *I;
1297 else
1298 curIvar->setNextIvar(*I);
1299 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001300 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001301
1302 if (!layout.empty()) {
1303 // Order synthesized ivars by their size.
1304 std::stable_sort(layout.begin(), layout.end());
1305 unsigned Ix = 0, EIx = layout.size();
1306 if (!data().IvarList) {
1307 data().IvarList = layout[0].Ivar; Ix++;
1308 curIvar = data().IvarList;
1309 }
1310 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1311 curIvar->setNextIvar(layout[Ix].Ivar);
1312 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001313 }
1314 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001315 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001316}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001317
1318/// FindCategoryDeclaration - Finds category declaration in the list of
1319/// categories for this class and returns it. Name of the category is passed
1320/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001321///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001322ObjCCategoryDecl *
1323ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001324 // FIXME: Should make sure no callers ever do this.
1325 if (!hasDefinition())
1326 return 0;
1327
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001328 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001329 LoadExternalDefinition();
1330
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001331 for (visible_categories_iterator Cat = visible_categories_begin(),
1332 CatEnd = visible_categories_end();
1333 Cat != CatEnd;
1334 ++Cat) {
1335 if (Cat->getIdentifier() == CategoryId)
1336 return *Cat;
1337 }
1338
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001339 return 0;
1340}
1341
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001342ObjCMethodDecl *
1343ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001344 for (visible_categories_iterator Cat = visible_categories_begin(),
1345 CatEnd = visible_categories_end();
1346 Cat != CatEnd;
1347 ++Cat) {
1348 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001349 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1350 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001351 }
1352
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001353 return 0;
1354}
1355
1356ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001357 for (visible_categories_iterator Cat = visible_categories_begin(),
1358 CatEnd = visible_categories_end();
1359 Cat != CatEnd;
1360 ++Cat) {
1361 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001362 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1363 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001364 }
1365
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001366 return 0;
1367}
1368
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001369/// ClassImplementsProtocol - Checks that 'lProto' protocol
1370/// has been implemented in IDecl class, its super class or categories (if
1371/// lookupCategory is true).
1372bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1373 bool lookupCategory,
1374 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001375 if (!hasDefinition())
1376 return false;
1377
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001378 ObjCInterfaceDecl *IDecl = this;
1379 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001380 for (auto *PI : IDecl->protocols()){
1381 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001382 return true;
1383 // This is dubious and is added to be compatible with gcc. In gcc, it is
1384 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1385 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1386 // object. This IMO, should be a bug.
1387 // FIXME: Treat this as an extension, and flag this as an error when GCC
1388 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001389 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001390 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001391 return true;
1392 }
Mike Stump11289f42009-09-09 15:08:12 +00001393
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001394 // 2nd, look up the category.
1395 if (lookupCategory)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001396 for (visible_categories_iterator Cat = visible_categories_begin(),
1397 CatEnd = visible_categories_end();
1398 Cat != CatEnd;
1399 ++Cat) {
1400 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1401 E = Cat->protocol_end();
1402 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001403 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1404 return true;
1405 }
Mike Stump11289f42009-09-09 15:08:12 +00001406
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001407 // 3rd, look up the super class(s)
1408 if (IDecl->getSuperClass())
1409 return
1410 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1411 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001412
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001413 return false;
1414}
1415
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001416//===----------------------------------------------------------------------===//
1417// ObjCIvarDecl
1418//===----------------------------------------------------------------------===//
1419
David Blaikie68e081d2011-12-20 02:48:34 +00001420void ObjCIvarDecl::anchor() { }
1421
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001422ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001423 SourceLocation StartLoc,
1424 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001425 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001426 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001427 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001428 if (DC) {
1429 // Ivar's can only appear in interfaces, implementations (via synthesized
1430 // properties), and class extensions (via direct declaration, or synthesized
1431 // properties).
1432 //
1433 // FIXME: This should really be asserting this:
1434 // (isa<ObjCCategoryDecl>(DC) &&
1435 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1436 // but unfortunately we sometimes place ivars into non-class extension
1437 // categories on error. This breaks an AST invariant, and should not be
1438 // fixed.
1439 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1440 isa<ObjCCategoryDecl>(DC)) &&
1441 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001442 // Once a new ivar is created in any of class/class-extension/implementation
1443 // decl contexts, the previously built IvarList must be rebuilt.
1444 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1445 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001446 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001447 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001448 else
1449 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001450 }
1451 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001452 }
1453
Richard Smithf7981722013-11-22 09:01:48 +00001454 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001455 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001456}
1457
Douglas Gregor72172e92012-01-05 21:55:30 +00001458ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001459 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001460 QualType(), 0, ObjCIvarDecl::None, 0, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001461}
1462
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001463const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1464 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001465
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001466 switch (DC->getKind()) {
1467 default:
1468 case ObjCCategoryImpl:
1469 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001470 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001471
1472 // Ivars can only appear in class extension categories.
1473 case ObjCCategory: {
1474 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1475 assert(CD->IsClassExtension() && "invalid container for ivar!");
1476 return CD->getClassInterface();
1477 }
1478
1479 case ObjCImplementation:
1480 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1481
1482 case ObjCInterface:
1483 return cast<ObjCInterfaceDecl>(DC);
1484 }
1485}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001486
1487//===----------------------------------------------------------------------===//
1488// ObjCAtDefsFieldDecl
1489//===----------------------------------------------------------------------===//
1490
David Blaikie68e081d2011-12-20 02:48:34 +00001491void ObjCAtDefsFieldDecl::anchor() { }
1492
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001493ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001494*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1495 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001496 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001497 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001498}
1499
Richard Smithf7981722013-11-22 09:01:48 +00001500ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001501 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001502 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1503 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001504}
1505
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001506//===----------------------------------------------------------------------===//
1507// ObjCProtocolDecl
1508//===----------------------------------------------------------------------===//
1509
David Blaikie68e081d2011-12-20 02:48:34 +00001510void ObjCProtocolDecl::anchor() { }
1511
Douglas Gregor32c17572012-01-01 20:30:41 +00001512ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1513 SourceLocation nameLoc,
1514 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001515 ObjCProtocolDecl *PrevDecl)
1516 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001517{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001518 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001519 if (PrevDecl)
1520 Data = PrevDecl->Data;
1521}
1522
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001523ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001524 IdentifierInfo *Id,
1525 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001526 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001527 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001528 ObjCProtocolDecl *Result =
1529 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001530 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001531 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001532}
1533
Richard Smithf7981722013-11-22 09:01:48 +00001534ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001535 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001536 ObjCProtocolDecl *Result =
1537 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001538 Result->Data.setInt(!C.getLangOpts().Modules);
1539 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001540}
1541
Steve Naroff114aecb2009-03-01 16:12:44 +00001542ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1543 ObjCProtocolDecl *PDecl = this;
1544
1545 if (Name == getIdentifier())
1546 return PDecl;
1547
1548 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1549 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1550 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001551
Steve Naroff114aecb2009-03-01 16:12:44 +00001552 return NULL;
1553}
1554
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001555// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001556// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001557ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1558 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001559 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001560
Douglas Gregoreed49792013-01-17 00:38:46 +00001561 // If there is no definition or the definition is hidden, we don't find
1562 // anything.
1563 const ObjCProtocolDecl *Def = getDefinition();
1564 if (!Def || Def->isHidden())
1565 return NULL;
1566
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001567 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001568 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001569
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001570 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001571 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001572 return MethodDecl;
1573 return NULL;
1574}
1575
Douglas Gregore6e48b12012-01-01 19:29:29 +00001576void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001577 assert(!Data.getPointer() && "Protocol already has a definition!");
1578 Data.setPointer(new (getASTContext()) DefinitionData);
1579 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001580}
1581
1582void ObjCProtocolDecl::startDefinition() {
1583 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001584
1585 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001586 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001587 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001588}
1589
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001590void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1591 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001592
1593 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001594 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001595 // Insert into PM if not there already.
1596 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001597 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001598 }
1599 // Scan through protocol's protocols.
1600 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1601 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001602 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001603 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001604}
1605
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001606
1607void ObjCProtocolDecl::collectInheritedProtocolProperties(
1608 const ObjCPropertyDecl *Property,
1609 ProtocolPropertyMap &PM) const {
1610 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1611 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001612 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001613 if (Prop == Property)
1614 continue;
1615 if (Prop->getIdentifier() == Property->getIdentifier()) {
1616 PM[PDecl] = Prop;
1617 MatchFound = true;
1618 break;
1619 }
1620 }
1621 // Scan through protocol's protocols which did not have a matching property.
1622 if (!MatchFound)
1623 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1624 E = PDecl->protocol_end(); PI != E; ++PI)
1625 (*PI)->collectInheritedProtocolProperties(Property, PM);
1626 }
1627}
Anna Zaks673d76b2012-10-18 19:17:53 +00001628
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001629//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001630// ObjCCategoryDecl
1631//===----------------------------------------------------------------------===//
1632
David Blaikie68e081d2011-12-20 02:48:34 +00001633void ObjCCategoryDecl::anchor() { }
1634
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001635ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001636 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001637 SourceLocation ClassNameLoc,
1638 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001639 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001640 ObjCInterfaceDecl *IDecl,
1641 SourceLocation IvarLBraceLoc,
1642 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001643 ObjCCategoryDecl *CatDecl =
1644 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1645 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001646 if (IDecl) {
1647 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001648 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001649 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001650 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001651 if (ASTMutationListener *L = C.getASTMutationListener())
1652 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1653 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001654 }
1655
1656 return CatDecl;
1657}
1658
Richard Smithf7981722013-11-22 09:01:48 +00001659ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001660 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001661 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1662 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001663}
1664
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001665ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1666 return getASTContext().getObjCImplementation(
1667 const_cast<ObjCCategoryDecl*>(this));
1668}
1669
1670void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1671 getASTContext().setObjCImplementation(this, ImplD);
1672}
1673
1674
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001675//===----------------------------------------------------------------------===//
1676// ObjCCategoryImplDecl
1677//===----------------------------------------------------------------------===//
1678
David Blaikie68e081d2011-12-20 02:48:34 +00001679void ObjCCategoryImplDecl::anchor() { }
1680
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001681ObjCCategoryImplDecl *
1682ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001683 IdentifierInfo *Id,
1684 ObjCInterfaceDecl *ClassInterface,
1685 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001686 SourceLocation atStartLoc,
1687 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001688 if (ClassInterface && ClassInterface->hasDefinition())
1689 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001690 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1691 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001692}
1693
Douglas Gregor72172e92012-01-05 21:55:30 +00001694ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1695 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001696 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1697 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001698}
1699
Steve Narofff406f4d2009-10-29 21:11:04 +00001700ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001701 // The class interface might be NULL if we are working with invalid code.
1702 if (const ObjCInterfaceDecl *ID = getClassInterface())
1703 return ID->FindCategoryDeclaration(getIdentifier());
1704 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001705}
1706
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001707
David Blaikie68e081d2011-12-20 02:48:34 +00001708void ObjCImplDecl::anchor() { }
1709
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001710void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001711 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001712 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001713 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001714}
1715
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001716void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1717 ASTContext &Ctx = getASTContext();
1718
1719 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001720 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001721 if (IFace)
1722 Ctx.setObjCImplementation(IFace, ImplD);
1723
Duncan Sands49c29ee2009-07-21 07:56:29 +00001724 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001725 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1726 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1727 Ctx.setObjCImplementation(CD, ImplD);
1728 }
1729
1730 ClassInterface = IFace;
1731}
1732
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001733/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001734/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001735/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001736///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001737ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001738FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1739 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001740 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001741 if (PID->getPropertyIvarDecl() &&
1742 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1743 return PID;
1744 }
1745 return 0;
1746}
1747
1748/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001749/// added to the list of those properties \@synthesized/\@dynamic in this
1750/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001751///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001752ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001753FindPropertyImplDecl(IdentifierInfo *Id) const {
1754 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001755 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001756 if (PID->getPropertyDecl()->getIdentifier() == Id)
1757 return PID;
1758 }
1759 return 0;
1760}
1761
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001762raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001763 const ObjCCategoryImplDecl &CID) {
1764 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001765 return OS;
1766}
1767
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001768//===----------------------------------------------------------------------===//
1769// ObjCImplementationDecl
1770//===----------------------------------------------------------------------===//
1771
David Blaikie68e081d2011-12-20 02:48:34 +00001772void ObjCImplementationDecl::anchor() { }
1773
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001774ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001775ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001776 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001777 ObjCInterfaceDecl *SuperDecl,
1778 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001779 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001780 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001781 SourceLocation IvarLBraceLoc,
1782 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001783 if (ClassInterface && ClassInterface->hasDefinition())
1784 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001785 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1786 nameLoc, atStartLoc, superLoc,
1787 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001788}
1789
Douglas Gregor72172e92012-01-05 21:55:30 +00001790ObjCImplementationDecl *
1791ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001792 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1793 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001794}
1795
John McCall0410e572011-07-22 04:15:06 +00001796void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1797 CXXCtorInitializer ** initializers,
1798 unsigned numInitializers) {
1799 if (numInitializers > 0) {
1800 NumIvarInitializers = numInitializers;
1801 CXXCtorInitializer **ivarInitializers =
1802 new (C) CXXCtorInitializer*[NumIvarInitializers];
1803 memcpy(ivarInitializers, initializers,
1804 numInitializers * sizeof(CXXCtorInitializer*));
1805 IvarInitializers = ivarInitializers;
1806 }
1807}
1808
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001809raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001810 const ObjCImplementationDecl &ID) {
1811 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001812 return OS;
1813}
1814
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001815//===----------------------------------------------------------------------===//
1816// ObjCCompatibleAliasDecl
1817//===----------------------------------------------------------------------===//
1818
David Blaikie68e081d2011-12-20 02:48:34 +00001819void ObjCCompatibleAliasDecl::anchor() { }
1820
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001821ObjCCompatibleAliasDecl *
1822ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1823 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001824 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001825 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001826 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001827}
1828
Douglas Gregor72172e92012-01-05 21:55:30 +00001829ObjCCompatibleAliasDecl *
1830ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001831 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001832}
1833
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001834//===----------------------------------------------------------------------===//
1835// ObjCPropertyDecl
1836//===----------------------------------------------------------------------===//
1837
David Blaikie68e081d2011-12-20 02:48:34 +00001838void ObjCPropertyDecl::anchor() { }
1839
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001840ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1841 SourceLocation L,
1842 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001843 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001844 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001845 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001846 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001847 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001848}
1849
Richard Smithf7981722013-11-22 09:01:48 +00001850ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001851 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001852 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1853 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001854}
1855
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001856//===----------------------------------------------------------------------===//
1857// ObjCPropertyImplDecl
1858//===----------------------------------------------------------------------===//
1859
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001860ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001861 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001862 SourceLocation atLoc,
1863 SourceLocation L,
1864 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001865 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001866 ObjCIvarDecl *ivar,
1867 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001868 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1869 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001870}
Chris Lattnered0e1642008-03-17 01:19:02 +00001871
Richard Smithf7981722013-11-22 09:01:48 +00001872ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001873 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001874 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1875 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001876}
1877
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001878SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1879 SourceLocation EndLoc = getLocation();
1880 if (IvarLoc.isValid())
1881 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001882
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001883 return SourceRange(AtLoc, EndLoc);
1884}