blob: e721e1f861fcd14876f4a01028549c5e86c609f6 [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.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +0000558 for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
559 E = ClassDecl->protocol_end();
560 I != E; ++I)
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000561 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000562 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000563
564 // Didn't find one yet - now look through categories.
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000565 for (ObjCInterfaceDecl::visible_categories_iterator
566 Cat = ClassDecl->visible_categories_begin(),
567 CatEnd = ClassDecl->visible_categories_end();
568 Cat != CatEnd; ++Cat) {
569 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
570 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000571 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000572
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000573 if (!shallowCategoryLookup) {
574 // Didn't find one yet - look through protocols.
575 const ObjCList<ObjCProtocolDecl> &Protocols =
576 Cat->getReferencedProtocols();
577 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
578 E = Protocols.end(); I != E; ++I)
579 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
580 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000581 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000582 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000583 }
Ted Kremenek00781502013-11-23 01:01:29 +0000584
585 if (!followSuper)
586 return NULL;
587
588 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000589 ClassDecl = ClassDecl->getSuperClass();
590 }
591 return NULL;
592}
593
Anna Zaksc77a3b12012-07-27 19:07:44 +0000594// Will search "local" class/category implementations for a method decl.
595// If failed, then we search in class's root for an instance method.
596// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000597ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
598 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000599 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000600 // FIXME: Should make sure no callers ever do this.
601 if (!hasDefinition())
602 return 0;
603
604 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000605 LoadExternalDefinition();
606
Steve Naroffbb69c942009-10-01 23:46:04 +0000607 ObjCMethodDecl *Method = 0;
608 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000609 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
610 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000611
612 // Look through local category implementations associated with the class.
613 if (!Method)
614 Method = Instance ? getCategoryInstanceMethod(Sel)
615 : getCategoryClassMethod(Sel);
616
617 // Before we give up, check if the selector is an instance method.
618 // But only in the root. This matches gcc's behavior and what the
619 // runtime expects.
620 if (!Instance && !Method && !getSuperClass()) {
621 Method = lookupInstanceMethod(Sel);
622 // Look through local category implementations associated
623 // with the root class.
624 if (!Method)
625 Method = lookupPrivateMethod(Sel, true);
626 }
627
Steve Naroffbb69c942009-10-01 23:46:04 +0000628 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000629 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000630 return Method;
631}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000632
633//===----------------------------------------------------------------------===//
634// ObjCMethodDecl
635//===----------------------------------------------------------------------===//
636
Alp Toker314cc812014-01-25 16:55:45 +0000637ObjCMethodDecl *ObjCMethodDecl::Create(
638 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
639 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
640 DeclContext *contextDecl, bool isInstance, bool isVariadic,
641 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
642 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000643 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000644 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000645 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
646 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000647}
648
Douglas Gregor72172e92012-01-05 21:55:30 +0000649ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000650 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
651 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000652}
653
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000654bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
655 return getMethodFamily() == OMF_init &&
656 hasAttr<ObjCDesignatedInitializerAttr>();
657}
658
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000659bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
660 const ObjCMethodDecl **InitMethod) const {
661 if (getMethodFamily() != OMF_init)
662 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000663 const DeclContext *DC = getDeclContext();
664 if (isa<ObjCProtocolDecl>(DC))
665 return false;
666 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000667 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000668 return false;
669}
670
Douglas Gregora6017bb2012-10-09 17:21:28 +0000671Stmt *ObjCMethodDecl::getBody() const {
672 return Body.get(getASTContext().getExternalSource());
673}
674
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000675void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
676 assert(PrevMethod);
677 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
678 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000679 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000680}
681
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000682void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
683 ArrayRef<ParmVarDecl*> Params,
684 ArrayRef<SourceLocation> SelLocs) {
685 ParamsAndSelLocs = 0;
686 NumParams = Params.size();
687 if (Params.empty() && SelLocs.empty())
688 return;
689
690 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
691 sizeof(SourceLocation) * SelLocs.size();
692 ParamsAndSelLocs = C.Allocate(Size);
693 std::copy(Params.begin(), Params.end(), getParams());
694 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
695}
696
697void ObjCMethodDecl::getSelectorLocs(
698 SmallVectorImpl<SourceLocation> &SelLocs) const {
699 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
700 SelLocs.push_back(getSelectorLoc(i));
701}
702
703void ObjCMethodDecl::setMethodParams(ASTContext &C,
704 ArrayRef<ParmVarDecl*> Params,
705 ArrayRef<SourceLocation> SelLocs) {
706 assert((!SelLocs.empty() || isImplicit()) &&
707 "No selector locs for non-implicit method");
708 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000709 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000710
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000711 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
712 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000713 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000714 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000715
716 setParamsAndSelLocs(C, Params, SelLocs);
717}
718
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000719/// \brief A definition will return its interface declaration.
720/// An interface declaration will return its definition.
721/// Otherwise it will return itself.
722ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
723 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000724 ObjCMethodDecl *Redecl = 0;
725 if (HasRedeclaration)
726 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000727 if (Redecl)
728 return Redecl;
729
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000730 Decl *CtxD = cast<Decl>(getDeclContext());
731
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000732 if (!CtxD->isInvalidDecl()) {
733 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
734 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
735 if (!ImplD->isInvalidDecl())
736 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000737
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000738 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
739 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
740 if (!ImplD->isInvalidDecl())
741 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000742
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000743 } else if (ObjCImplementationDecl *ImplD =
744 dyn_cast<ObjCImplementationDecl>(CtxD)) {
745 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
746 if (!IFD->isInvalidDecl())
747 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000748
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000749 } else if (ObjCCategoryImplDecl *CImplD =
750 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
751 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
752 if (!CatD->isInvalidDecl())
753 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
754 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000755 }
756
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000757 if (!Redecl && isRedeclaration()) {
758 // This is the last redeclaration, go back to the first method.
759 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
760 isInstanceMethod());
761 }
762
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000763 return Redecl ? Redecl : this;
764}
765
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000766ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
767 Decl *CtxD = cast<Decl>(getDeclContext());
768
769 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
770 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
771 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
772 isInstanceMethod()))
773 return MD;
774
775 } else if (ObjCCategoryImplDecl *CImplD =
776 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000777 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000778 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
779 isInstanceMethod()))
780 return MD;
781 }
782
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000783 if (isRedeclaration())
784 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
785 isInstanceMethod());
786
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000787 return this;
788}
789
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000790SourceLocation ObjCMethodDecl::getLocEnd() const {
791 if (Stmt *Body = getBody())
792 return Body->getLocEnd();
793 return DeclEndLoc;
794}
795
John McCallb4526252011-03-02 01:50:55 +0000796ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
797 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000798 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000799 return family;
800
John McCall86bc21f2011-03-02 11:33:24 +0000801 // Check for an explicit attribute.
802 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
803 // The unfortunate necessity of mapping between enums here is due
804 // to the attributes framework.
805 switch (attr->getFamily()) {
806 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
807 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
808 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
809 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
810 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
811 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
812 }
813 Family = static_cast<unsigned>(family);
814 return family;
815 }
816
John McCallb4526252011-03-02 01:50:55 +0000817 family = getSelector().getMethodFamily();
818 switch (family) {
819 case OMF_None: break;
820
821 // init only has a conventional meaning for an instance method, and
822 // it has to return an object.
823 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000824 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000825 family = OMF_None;
826 break;
827
828 // alloc/copy/new have a conventional meaning for both class and
829 // instance methods, but they require an object return.
830 case OMF_alloc:
831 case OMF_copy:
832 case OMF_mutableCopy:
833 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000834 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000835 family = OMF_None;
836 break;
837
838 // These selectors have a conventional meaning only for instance methods.
839 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000840 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000841 case OMF_retain:
842 case OMF_release:
843 case OMF_autorelease:
844 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000845 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000846 if (!isInstanceMethod())
847 family = OMF_None;
848 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000849
850 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000851 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000852 family = OMF_None;
853 else {
854 unsigned noParams = param_size();
855 if (noParams < 1 || noParams > 3)
856 family = OMF_None;
857 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000858 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000859 QualType ArgT = (*it);
860 if (!ArgT->isObjCSelType()) {
861 family = OMF_None;
862 break;
863 }
864 while (--noParams) {
865 it++;
866 ArgT = (*it);
867 if (!ArgT->isObjCIdType()) {
868 family = OMF_None;
869 break;
870 }
871 }
872 }
873 }
874 break;
875
John McCallb4526252011-03-02 01:50:55 +0000876 }
877
878 // Cache the result.
879 Family = static_cast<unsigned>(family);
880 return family;
881}
882
Mike Stump11289f42009-09-09 15:08:12 +0000883void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000884 const ObjCInterfaceDecl *OID) {
885 QualType selfTy;
886 if (isInstanceMethod()) {
887 // There may be no interface context due to error in declaration
888 // of the interface (which has been reported). Recover gracefully.
889 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000890 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000891 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000892 } else {
893 selfTy = Context.getObjCIdType();
894 }
895 } else // we have a factory method.
896 selfTy = Context.getObjCClassType();
897
John McCalld4631322011-06-17 06:42:21 +0000898 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000899 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000900
David Blaikiebbafb8a2012-03-11 07:00:24 +0000901 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000902 if (isInstanceMethod()) {
903 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000904
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000905 // 'self' is always __strong. It's actually pseudo-strong except
906 // in init methods (or methods labeled ns_consumes_self), though.
907 Qualifiers qs;
908 qs.setObjCLifetime(Qualifiers::OCL_Strong);
909 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000910
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000911 // In addition, 'self' is const unless this is an init method.
912 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
913 selfTy = selfTy.withConst();
914 selfIsPseudoStrong = true;
915 }
916 }
917 else {
918 assert(isClassMethod());
919 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000920 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000921 selfIsPseudoStrong = true;
922 }
John McCall31168b02011-06-15 23:02:42 +0000923 }
924
925 ImplicitParamDecl *self
926 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
927 &Context.Idents.get("self"), selfTy);
928 setSelfDecl(self);
929
930 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000931 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000932
John McCalld4631322011-06-17 06:42:21 +0000933 if (selfIsPseudoStrong)
934 self->setARCPseudoStrong(true);
935
Mike Stump11289f42009-09-09 15:08:12 +0000936 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
937 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000938 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000939}
940
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000941ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
942 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
943 return ID;
944 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
945 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000946 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000947 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000948 if (isa<ObjCProtocolDecl>(getDeclContext()))
949 return 0;
David Blaikie83d382b2011-09-23 05:06:16 +0000950 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000951}
952
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000953static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
954 const ObjCMethodDecl *Method,
955 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
956 bool MovedToSuper) {
957 if (!Container)
958 return;
959
960 // In categories look for overriden methods from protocols. A method from
961 // category is not "overriden" since it is considered as the "same" method
962 // (same USR) as the one from the interface.
963 if (const ObjCCategoryDecl *
964 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
965 // Check whether we have a matching method at this category but only if we
966 // are at the super class level.
967 if (MovedToSuper)
968 if (ObjCMethodDecl *
969 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000970 Method->isInstanceMethod(),
971 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000972 if (Method != Overridden) {
973 // We found an override at this category; there is no need to look
974 // into its protocols.
975 Methods.push_back(Overridden);
976 return;
977 }
978
979 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
980 PEnd = Category->protocol_end();
981 P != PEnd; ++P)
982 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
983 return;
984 }
985
986 // Check whether we have a matching method at this level.
987 if (const ObjCMethodDecl *
988 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000989 Method->isInstanceMethod(),
990 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000991 if (Method != Overridden) {
992 // We found an override at this level; there is no need to look
993 // into other protocols or categories.
994 Methods.push_back(Overridden);
995 return;
996 }
997
998 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
999 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
1000 PEnd = Protocol->protocol_end();
1001 P != PEnd; ++P)
1002 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1003 }
1004
1005 if (const ObjCInterfaceDecl *
1006 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
1007 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
1008 PEnd = Interface->protocol_end();
1009 P != PEnd; ++P)
1010 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
1011
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001012 for (ObjCInterfaceDecl::known_categories_iterator
1013 Cat = Interface->known_categories_begin(),
1014 CatEnd = Interface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001015 Cat != CatEnd; ++Cat) {
1016 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001017 MovedToSuper);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001018 }
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001019
1020 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1021 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1022 /*MovedToSuper=*/true);
1023 }
1024}
1025
1026static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1027 const ObjCMethodDecl *Method,
1028 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1029 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1030 /*MovedToSuper=*/false);
1031}
1032
1033static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1034 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1035 assert(Method->isOverriding());
1036
1037 if (const ObjCProtocolDecl *
1038 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1039 CollectOverriddenMethods(ProtD, Method, overridden);
1040
1041 } else if (const ObjCImplDecl *
1042 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1043 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1044 if (!ID)
1045 return;
1046 // Start searching for overridden methods using the method from the
1047 // interface as starting point.
1048 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001049 Method->isInstanceMethod(),
1050 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001051 Method = IFaceMeth;
1052 CollectOverriddenMethods(ID, Method, overridden);
1053
1054 } else if (const ObjCCategoryDecl *
1055 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1056 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1057 if (!ID)
1058 return;
1059 // Start searching for overridden methods using the method from the
1060 // interface as starting point.
1061 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001062 Method->isInstanceMethod(),
1063 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001064 Method = IFaceMeth;
1065 CollectOverriddenMethods(ID, Method, overridden);
1066
1067 } else {
1068 CollectOverriddenMethods(
1069 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1070 Method, overridden);
1071 }
1072}
1073
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001074void ObjCMethodDecl::getOverriddenMethods(
1075 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1076 const ObjCMethodDecl *Method = this;
1077
1078 if (Method->isRedeclaration()) {
1079 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1080 getMethod(Method->getSelector(), Method->isInstanceMethod());
1081 }
1082
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001083 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001084 collectOverriddenMethodsSlow(Method, Overridden);
1085 assert(!Overridden.empty() &&
1086 "ObjCMethodDecl's overriding bit is not as expected");
1087 }
1088}
1089
Jordan Rose2bd991a2012-10-10 16:42:54 +00001090const ObjCPropertyDecl *
1091ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1092 Selector Sel = getSelector();
1093 unsigned NumArgs = Sel.getNumArgs();
1094 if (NumArgs > 1)
1095 return 0;
1096
Jordan Rose59e34ec2012-10-11 16:02:02 +00001097 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001098 return 0;
1099
1100 if (isPropertyAccessor()) {
1101 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001102 // If container is class extension, find its primary class.
1103 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1104 if (CatDecl->IsClassExtension())
1105 Container = CatDecl->getClassInterface();
1106
Jordan Rose2bd991a2012-10-10 16:42:54 +00001107 bool IsGetter = (NumArgs == 0);
1108
Aaron Ballmand174edf2014-03-13 19:11:50 +00001109 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001110 Selector NextSel = IsGetter ? I->getGetterName()
1111 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001112 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001113 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001114 }
1115
1116 llvm_unreachable("Marked as a property accessor but no property found!");
1117 }
1118
1119 if (!CheckOverrides)
1120 return 0;
1121
1122 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1123 OverridesTy Overrides;
1124 getOverriddenMethods(Overrides);
1125 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1126 I != E; ++I) {
1127 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1128 return Prop;
1129 }
1130
1131 return 0;
1132
1133}
1134
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001135//===----------------------------------------------------------------------===//
1136// ObjCInterfaceDecl
1137//===----------------------------------------------------------------------===//
1138
Douglas Gregord53ae832012-01-17 18:09:05 +00001139ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001140 DeclContext *DC,
1141 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001142 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001143 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001144 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001145 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001146 ObjCInterfaceDecl *Result = new (C, DC)
1147 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001148 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001149 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001150 return Result;
1151}
1152
Richard Smithf7981722013-11-22 09:01:48 +00001153ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001154 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001155 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1156 0, SourceLocation(),
1157 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001158 Result->Data.setInt(!C.getLangOpts().Modules);
1159 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001160}
1161
1162ObjCInterfaceDecl::
1163ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001164 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1165 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001166 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001167 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001168{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001169 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001170
1171 // Copy the 'data' pointer over.
1172 if (PrevDecl)
1173 Data = PrevDecl->Data;
1174
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001175 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001176}
1177
Douglas Gregor73693022010-12-01 23:49:52 +00001178void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001179 assert(data().ExternallyCompleted && "Class is not externally completed");
1180 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001181 getASTContext().getExternalSource()->CompleteType(
1182 const_cast<ObjCInterfaceDecl *>(this));
1183}
1184
1185void ObjCInterfaceDecl::setExternallyCompleted() {
1186 assert(getASTContext().getExternalSource() &&
1187 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001188 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001189 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001190 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001191}
1192
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001193void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001194 // Check for a complete definition and recover if not so.
1195 if (!isThisDeclarationADefinition())
1196 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001197 data().HasDesignatedInitializers = true;
1198}
1199
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001200bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001201 // Check for a complete definition and recover if not so.
1202 if (!isThisDeclarationADefinition())
1203 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001204 if (data().ExternallyCompleted)
1205 LoadExternalDefinition();
1206
1207 return data().HasDesignatedInitializers;
1208}
1209
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001210ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001211 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1212 if (data().ExternallyCompleted)
1213 LoadExternalDefinition();
1214
1215 return getASTContext().getObjCImplementation(
1216 const_cast<ObjCInterfaceDecl*>(Def));
1217 }
1218
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001219 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001220 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001221}
1222
1223void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001224 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001225}
1226
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001227namespace {
1228 struct SynthesizeIvarChunk {
1229 uint64_t Size;
1230 ObjCIvarDecl *Ivar;
1231 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1232 : Size(size), Ivar(ivar) {}
1233 };
1234
1235 bool operator<(const SynthesizeIvarChunk & LHS,
1236 const SynthesizeIvarChunk &RHS) {
1237 return LHS.Size < RHS.Size;
1238 }
1239}
1240
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001241/// all_declared_ivar_begin - return first ivar declared in this class,
1242/// its extensions and its implementation. Lazily build the list on first
1243/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001244///
1245/// Caveat: The list returned by this method reflects the current
1246/// state of the parser. The cache will be updated for every ivar
1247/// added by an extension or the implementation when they are
1248/// encountered.
1249/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001250ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001251 // FIXME: Should make sure no callers ever do this.
1252 if (!hasDefinition())
1253 return 0;
1254
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001255 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001256 if (!data().IvarList) {
1257 if (!ivar_empty()) {
1258 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1259 data().IvarList = *I; ++I;
1260 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001261 curIvar->setNextIvar(*I);
1262 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001263
1264 for (ObjCInterfaceDecl::known_extensions_iterator
1265 Ext = known_extensions_begin(),
1266 ExtEnd = known_extensions_end();
1267 Ext != ExtEnd; ++Ext) {
1268 if (!Ext->ivar_empty()) {
1269 ObjCCategoryDecl::ivar_iterator
1270 I = Ext->ivar_begin(),
1271 E = Ext->ivar_end();
1272 if (!data().IvarList) {
1273 data().IvarList = *I; ++I;
1274 curIvar = data().IvarList;
1275 }
1276 for ( ;I != E; curIvar = *I, ++I)
1277 curIvar->setNextIvar(*I);
1278 }
1279 }
1280 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001281 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001282
1283 // cached and complete!
1284 if (!data().IvarListMissingImplementation)
1285 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001286
1287 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001288 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001289 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001290 SmallVector<SynthesizeIvarChunk, 16> layout;
1291 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1292 E = ImplDecl->ivar_end(); I != E; ++I) {
1293 ObjCIvarDecl *IV = *I;
1294 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1295 layout.push_back(SynthesizeIvarChunk(
1296 IV->getASTContext().getTypeSize(IV->getType()), IV));
1297 continue;
1298 }
1299 if (!data().IvarList)
1300 data().IvarList = *I;
1301 else
1302 curIvar->setNextIvar(*I);
1303 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001304 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001305
1306 if (!layout.empty()) {
1307 // Order synthesized ivars by their size.
1308 std::stable_sort(layout.begin(), layout.end());
1309 unsigned Ix = 0, EIx = layout.size();
1310 if (!data().IvarList) {
1311 data().IvarList = layout[0].Ivar; Ix++;
1312 curIvar = data().IvarList;
1313 }
1314 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1315 curIvar->setNextIvar(layout[Ix].Ivar);
1316 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001317 }
1318 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001319 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001320}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001321
1322/// FindCategoryDeclaration - Finds category declaration in the list of
1323/// categories for this class and returns it. Name of the category is passed
1324/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001325///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001326ObjCCategoryDecl *
1327ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001328 // FIXME: Should make sure no callers ever do this.
1329 if (!hasDefinition())
1330 return 0;
1331
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001332 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001333 LoadExternalDefinition();
1334
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001335 for (visible_categories_iterator Cat = visible_categories_begin(),
1336 CatEnd = visible_categories_end();
1337 Cat != CatEnd;
1338 ++Cat) {
1339 if (Cat->getIdentifier() == CategoryId)
1340 return *Cat;
1341 }
1342
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001343 return 0;
1344}
1345
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001346ObjCMethodDecl *
1347ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001348 for (visible_categories_iterator Cat = visible_categories_begin(),
1349 CatEnd = visible_categories_end();
1350 Cat != CatEnd;
1351 ++Cat) {
1352 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001353 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1354 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001355 }
1356
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001357 return 0;
1358}
1359
1360ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001361 for (visible_categories_iterator Cat = visible_categories_begin(),
1362 CatEnd = visible_categories_end();
1363 Cat != CatEnd;
1364 ++Cat) {
1365 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001366 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1367 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001368 }
1369
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001370 return 0;
1371}
1372
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001373/// ClassImplementsProtocol - Checks that 'lProto' protocol
1374/// has been implemented in IDecl class, its super class or categories (if
1375/// lookupCategory is true).
1376bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1377 bool lookupCategory,
1378 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001379 if (!hasDefinition())
1380 return false;
1381
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001382 ObjCInterfaceDecl *IDecl = this;
1383 // 1st, look up the class.
Argyrios Kyrtzidise7f3ef32012-03-13 01:09:41 +00001384 for (ObjCInterfaceDecl::protocol_iterator
1385 PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001386 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1387 return true;
1388 // This is dubious and is added to be compatible with gcc. In gcc, it is
1389 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1390 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1391 // object. This IMO, should be a bug.
1392 // FIXME: Treat this as an extension, and flag this as an error when GCC
1393 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001394 if (RHSIsQualifiedID &&
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001395 getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1396 return true;
1397 }
Mike Stump11289f42009-09-09 15:08:12 +00001398
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001399 // 2nd, look up the category.
1400 if (lookupCategory)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001401 for (visible_categories_iterator Cat = visible_categories_begin(),
1402 CatEnd = visible_categories_end();
1403 Cat != CatEnd;
1404 ++Cat) {
1405 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1406 E = Cat->protocol_end();
1407 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001408 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1409 return true;
1410 }
Mike Stump11289f42009-09-09 15:08:12 +00001411
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001412 // 3rd, look up the super class(s)
1413 if (IDecl->getSuperClass())
1414 return
1415 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1416 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001417
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001418 return false;
1419}
1420
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001421//===----------------------------------------------------------------------===//
1422// ObjCIvarDecl
1423//===----------------------------------------------------------------------===//
1424
David Blaikie68e081d2011-12-20 02:48:34 +00001425void ObjCIvarDecl::anchor() { }
1426
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001427ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001428 SourceLocation StartLoc,
1429 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001430 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001431 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001432 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001433 if (DC) {
1434 // Ivar's can only appear in interfaces, implementations (via synthesized
1435 // properties), and class extensions (via direct declaration, or synthesized
1436 // properties).
1437 //
1438 // FIXME: This should really be asserting this:
1439 // (isa<ObjCCategoryDecl>(DC) &&
1440 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1441 // but unfortunately we sometimes place ivars into non-class extension
1442 // categories on error. This breaks an AST invariant, and should not be
1443 // fixed.
1444 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1445 isa<ObjCCategoryDecl>(DC)) &&
1446 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001447 // Once a new ivar is created in any of class/class-extension/implementation
1448 // decl contexts, the previously built IvarList must be rebuilt.
1449 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1450 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001451 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001452 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001453 else
1454 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001455 }
1456 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001457 }
1458
Richard Smithf7981722013-11-22 09:01:48 +00001459 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001460 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001461}
1462
Douglas Gregor72172e92012-01-05 21:55:30 +00001463ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001464 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001465 QualType(), 0, ObjCIvarDecl::None, 0, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001466}
1467
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001468const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1469 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001470
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001471 switch (DC->getKind()) {
1472 default:
1473 case ObjCCategoryImpl:
1474 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001475 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001476
1477 // Ivars can only appear in class extension categories.
1478 case ObjCCategory: {
1479 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1480 assert(CD->IsClassExtension() && "invalid container for ivar!");
1481 return CD->getClassInterface();
1482 }
1483
1484 case ObjCImplementation:
1485 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1486
1487 case ObjCInterface:
1488 return cast<ObjCInterfaceDecl>(DC);
1489 }
1490}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001491
1492//===----------------------------------------------------------------------===//
1493// ObjCAtDefsFieldDecl
1494//===----------------------------------------------------------------------===//
1495
David Blaikie68e081d2011-12-20 02:48:34 +00001496void ObjCAtDefsFieldDecl::anchor() { }
1497
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001498ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001499*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1500 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001501 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001502 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001503}
1504
Richard Smithf7981722013-11-22 09:01:48 +00001505ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001506 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001507 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1508 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001509}
1510
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001511//===----------------------------------------------------------------------===//
1512// ObjCProtocolDecl
1513//===----------------------------------------------------------------------===//
1514
David Blaikie68e081d2011-12-20 02:48:34 +00001515void ObjCProtocolDecl::anchor() { }
1516
Douglas Gregor32c17572012-01-01 20:30:41 +00001517ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1518 SourceLocation nameLoc,
1519 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001520 ObjCProtocolDecl *PrevDecl)
1521 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001522{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001523 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001524 if (PrevDecl)
1525 Data = PrevDecl->Data;
1526}
1527
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001528ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001529 IdentifierInfo *Id,
1530 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001531 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001532 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001533 ObjCProtocolDecl *Result =
1534 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001535 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001536 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001537}
1538
Richard Smithf7981722013-11-22 09:01:48 +00001539ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001540 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001541 ObjCProtocolDecl *Result =
1542 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001543 Result->Data.setInt(!C.getLangOpts().Modules);
1544 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001545}
1546
Steve Naroff114aecb2009-03-01 16:12:44 +00001547ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1548 ObjCProtocolDecl *PDecl = this;
1549
1550 if (Name == getIdentifier())
1551 return PDecl;
1552
1553 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1554 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1555 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001556
Steve Naroff114aecb2009-03-01 16:12:44 +00001557 return NULL;
1558}
1559
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001560// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001561// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001562ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1563 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001564 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001565
Douglas Gregoreed49792013-01-17 00:38:46 +00001566 // If there is no definition or the definition is hidden, we don't find
1567 // anything.
1568 const ObjCProtocolDecl *Def = getDefinition();
1569 if (!Def || Def->isHidden())
1570 return NULL;
1571
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001572 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001573 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001574
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001575 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001576 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001577 return MethodDecl;
1578 return NULL;
1579}
1580
Douglas Gregore6e48b12012-01-01 19:29:29 +00001581void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001582 assert(!Data.getPointer() && "Protocol already has a definition!");
1583 Data.setPointer(new (getASTContext()) DefinitionData);
1584 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001585}
1586
1587void ObjCProtocolDecl::startDefinition() {
1588 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001589
1590 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001591 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001592 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001593}
1594
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001595void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1596 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001597
1598 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001599 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001600 // Insert into PM if not there already.
1601 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001602 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001603 }
1604 // Scan through protocol's protocols.
1605 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1606 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001607 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001608 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001609}
1610
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001611
1612void ObjCProtocolDecl::collectInheritedProtocolProperties(
1613 const ObjCPropertyDecl *Property,
1614 ProtocolPropertyMap &PM) const {
1615 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1616 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001617 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001618 if (Prop == Property)
1619 continue;
1620 if (Prop->getIdentifier() == Property->getIdentifier()) {
1621 PM[PDecl] = Prop;
1622 MatchFound = true;
1623 break;
1624 }
1625 }
1626 // Scan through protocol's protocols which did not have a matching property.
1627 if (!MatchFound)
1628 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1629 E = PDecl->protocol_end(); PI != E; ++PI)
1630 (*PI)->collectInheritedProtocolProperties(Property, PM);
1631 }
1632}
Anna Zaks673d76b2012-10-18 19:17:53 +00001633
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001634//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001635// ObjCCategoryDecl
1636//===----------------------------------------------------------------------===//
1637
David Blaikie68e081d2011-12-20 02:48:34 +00001638void ObjCCategoryDecl::anchor() { }
1639
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001640ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001641 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001642 SourceLocation ClassNameLoc,
1643 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001644 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001645 ObjCInterfaceDecl *IDecl,
1646 SourceLocation IvarLBraceLoc,
1647 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001648 ObjCCategoryDecl *CatDecl =
1649 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1650 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001651 if (IDecl) {
1652 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001653 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001654 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001655 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001656 if (ASTMutationListener *L = C.getASTMutationListener())
1657 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1658 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001659 }
1660
1661 return CatDecl;
1662}
1663
Richard Smithf7981722013-11-22 09:01:48 +00001664ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001665 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001666 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1667 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001668}
1669
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001670ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1671 return getASTContext().getObjCImplementation(
1672 const_cast<ObjCCategoryDecl*>(this));
1673}
1674
1675void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1676 getASTContext().setObjCImplementation(this, ImplD);
1677}
1678
1679
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001680//===----------------------------------------------------------------------===//
1681// ObjCCategoryImplDecl
1682//===----------------------------------------------------------------------===//
1683
David Blaikie68e081d2011-12-20 02:48:34 +00001684void ObjCCategoryImplDecl::anchor() { }
1685
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001686ObjCCategoryImplDecl *
1687ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001688 IdentifierInfo *Id,
1689 ObjCInterfaceDecl *ClassInterface,
1690 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001691 SourceLocation atStartLoc,
1692 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001693 if (ClassInterface && ClassInterface->hasDefinition())
1694 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001695 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1696 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001697}
1698
Douglas Gregor72172e92012-01-05 21:55:30 +00001699ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1700 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001701 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1702 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001703}
1704
Steve Narofff406f4d2009-10-29 21:11:04 +00001705ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001706 // The class interface might be NULL if we are working with invalid code.
1707 if (const ObjCInterfaceDecl *ID = getClassInterface())
1708 return ID->FindCategoryDeclaration(getIdentifier());
1709 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001710}
1711
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001712
David Blaikie68e081d2011-12-20 02:48:34 +00001713void ObjCImplDecl::anchor() { }
1714
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001715void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001716 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001717 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001718 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001719}
1720
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001721void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1722 ASTContext &Ctx = getASTContext();
1723
1724 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001725 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001726 if (IFace)
1727 Ctx.setObjCImplementation(IFace, ImplD);
1728
Duncan Sands49c29ee2009-07-21 07:56:29 +00001729 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001730 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1731 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1732 Ctx.setObjCImplementation(CD, ImplD);
1733 }
1734
1735 ClassInterface = IFace;
1736}
1737
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001738/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001739/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001740/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001741///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001742ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001743FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1744 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001745 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001746 if (PID->getPropertyIvarDecl() &&
1747 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1748 return PID;
1749 }
1750 return 0;
1751}
1752
1753/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001754/// added to the list of those properties \@synthesized/\@dynamic in this
1755/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001756///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001757ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001758FindPropertyImplDecl(IdentifierInfo *Id) const {
1759 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001760 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001761 if (PID->getPropertyDecl()->getIdentifier() == Id)
1762 return PID;
1763 }
1764 return 0;
1765}
1766
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001767raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001768 const ObjCCategoryImplDecl &CID) {
1769 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001770 return OS;
1771}
1772
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001773//===----------------------------------------------------------------------===//
1774// ObjCImplementationDecl
1775//===----------------------------------------------------------------------===//
1776
David Blaikie68e081d2011-12-20 02:48:34 +00001777void ObjCImplementationDecl::anchor() { }
1778
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001779ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001780ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001781 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001782 ObjCInterfaceDecl *SuperDecl,
1783 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001784 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001785 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001786 SourceLocation IvarLBraceLoc,
1787 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001788 if (ClassInterface && ClassInterface->hasDefinition())
1789 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001790 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1791 nameLoc, atStartLoc, superLoc,
1792 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001793}
1794
Douglas Gregor72172e92012-01-05 21:55:30 +00001795ObjCImplementationDecl *
1796ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001797 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1798 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001799}
1800
John McCall0410e572011-07-22 04:15:06 +00001801void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1802 CXXCtorInitializer ** initializers,
1803 unsigned numInitializers) {
1804 if (numInitializers > 0) {
1805 NumIvarInitializers = numInitializers;
1806 CXXCtorInitializer **ivarInitializers =
1807 new (C) CXXCtorInitializer*[NumIvarInitializers];
1808 memcpy(ivarInitializers, initializers,
1809 numInitializers * sizeof(CXXCtorInitializer*));
1810 IvarInitializers = ivarInitializers;
1811 }
1812}
1813
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001814raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001815 const ObjCImplementationDecl &ID) {
1816 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001817 return OS;
1818}
1819
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001820//===----------------------------------------------------------------------===//
1821// ObjCCompatibleAliasDecl
1822//===----------------------------------------------------------------------===//
1823
David Blaikie68e081d2011-12-20 02:48:34 +00001824void ObjCCompatibleAliasDecl::anchor() { }
1825
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001826ObjCCompatibleAliasDecl *
1827ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1828 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001829 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001830 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001831 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001832}
1833
Douglas Gregor72172e92012-01-05 21:55:30 +00001834ObjCCompatibleAliasDecl *
1835ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001836 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001837}
1838
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001839//===----------------------------------------------------------------------===//
1840// ObjCPropertyDecl
1841//===----------------------------------------------------------------------===//
1842
David Blaikie68e081d2011-12-20 02:48:34 +00001843void ObjCPropertyDecl::anchor() { }
1844
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001845ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1846 SourceLocation L,
1847 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001848 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001849 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001850 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001851 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001852 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001853}
1854
Richard Smithf7981722013-11-22 09:01:48 +00001855ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001856 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001857 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1858 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001859}
1860
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001861//===----------------------------------------------------------------------===//
1862// ObjCPropertyImplDecl
1863//===----------------------------------------------------------------------===//
1864
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001865ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001866 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001867 SourceLocation atLoc,
1868 SourceLocation L,
1869 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001870 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001871 ObjCIvarDecl *ivar,
1872 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001873 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1874 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001875}
Chris Lattnered0e1642008-03-17 01:19:02 +00001876
Richard Smithf7981722013-11-22 09:01:48 +00001877ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001878 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001879 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1880 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001881}
1882
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001883SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1884 SourceLocation EndLoc = getLocation();
1885 if (IvarLoc.isValid())
1886 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001887
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001888 return SourceRange(AtLoc, EndLoc);
1889}