blob: 34c87ab982485670369194981f9fd4a84551c537 [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.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000115 for (const auto *Cat : ID->visible_categories()) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000116 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
117 if (!MD->isImplicit())
118 return true;
119 if (Cat->IsClassExtension())
120 continue;
121 // Also search through the categories looking for a 'readwrite' declaration
122 // of this property. If one found, presumably a setter will be provided
123 // (properties declared in categories will not get auto-synthesized).
Aaron Ballmand174edf2014-03-13 19:11:50 +0000124 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000125 if (P->getIdentifier() == Property->getIdentifier()) {
126 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
127 return true;
128 break;
129 }
130 }
131
132 // Also look into protocols, for a user declared instance method.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000133 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000134 if (Proto->HasUserDeclaredSetterMethod(Property))
135 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000136
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000137 // And in its super class.
138 ObjCInterfaceDecl *OSC = ID->getSuperClass();
139 while (OSC) {
140 if (OSC->HasUserDeclaredSetterMethod(Property))
141 return true;
142 OSC = OSC->getSuperClass();
143 }
144 }
145 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000146 for (const auto *PI : PD->protocols())
147 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000148 return true;
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000149 return false;
150}
151
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000152ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000153ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000154 IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000155 // If this context is a hidden protocol definition, don't find any
156 // property.
157 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
158 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
159 if (Def->isHidden())
160 return 0;
161 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000162
David Blaikieff7d47a2012-12-19 00:45:41 +0000163 DeclContext::lookup_const_result R = DC->lookup(propertyID);
164 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
165 ++I)
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000166 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
167 return PD;
168
169 return 0;
170}
171
Anna Zaks454477c2012-09-27 19:45:11 +0000172IdentifierInfo *
173ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
174 SmallString<128> ivarName;
175 {
176 llvm::raw_svector_ostream os(ivarName);
177 os << '_' << getIdentifier()->getName();
178 }
179 return &Ctx.Idents.get(ivarName.str());
180}
181
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000182/// FindPropertyDeclaration - Finds declaration of the property given its name
183/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000184ObjCPropertyDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000185ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000186 // Don't find properties within hidden protocol definitions.
187 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
188 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
189 if (Def->isHidden())
190 return 0;
191 }
Mike Stump11289f42009-09-09 15:08:12 +0000192
Ted Kremenekddcd1092010-03-15 20:11:53 +0000193 if (ObjCPropertyDecl *PD =
194 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
195 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000196
Ted Kremenekddcd1092010-03-15 20:11:53 +0000197 switch (getKind()) {
198 default:
199 break;
200 case Decl::ObjCProtocol: {
201 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000202 for (const auto *I : PID->protocols())
203 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000204 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000205 break;
206 }
207 case Decl::ObjCInterface: {
208 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000209 // Look through categories (but not extensions).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000210 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000211 if (!Cat->IsClassExtension())
212 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
213 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000214 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000215
216 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000217 for (const auto *I : OID->all_referenced_protocols())
218 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000219 return P;
220
221 // Finally, check the super class.
222 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
223 return superClass->FindPropertyDeclaration(PropertyId);
224 break;
225 }
226 case Decl::ObjCCategory: {
227 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
228 // Look through protocols.
229 if (!OCD->IsClassExtension())
Aaron Ballman19a41762014-03-14 12:55:57 +0000230 for (const auto *I : OCD->protocols())
231 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
232 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000233 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000234 }
235 }
Steve Narofff9c65242008-06-05 13:55:23 +0000236 return 0;
237}
238
David Blaikie68e081d2011-12-20 02:48:34 +0000239void ObjCInterfaceDecl::anchor() { }
240
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000241/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
242/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000243/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000244///
245ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000246ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000247 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000248 // FIXME: Should make sure no callers ever do this.
249 if (!hasDefinition())
250 return 0;
251
252 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000253 LoadExternalDefinition();
254
Ted Kremenekd133a862010-03-15 20:30:07 +0000255 if (ObjCPropertyDecl *PD =
256 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
257 return PD;
258
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000259 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000260 for (const auto *I : all_referenced_protocols())
261 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000262 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000263
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000264 return 0;
265}
266
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000267void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
268 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000269 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000270 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000271 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000272 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000273 for (const auto *PI : all_referenced_protocols())
274 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000275 // Note, the properties declared only in class extensions are still copied
276 // into the main @interface's property list, and therefore we don't
277 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000278}
279
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000280bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
281 const ObjCInterfaceDecl *Class = this;
282 while (Class) {
283 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
284 return true;
285 Class = Class->getSuperClass();
286 }
287 return false;
288}
289
290const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
291 const ObjCInterfaceDecl *Class = this;
292 while (Class) {
293 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
294 return Class;
295 Class = Class->getSuperClass();
296 }
297 return 0;
298}
299
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000300void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
301 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
302 ASTContext &C)
303{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000304 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000305 LoadExternalDefinition();
306
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000307 if (data().AllReferencedProtocols.empty() &&
308 data().ReferencedProtocols.empty()) {
309 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000310 return;
311 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000312
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000313 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000314 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000315 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000316 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000317 for (unsigned i = 0; i < ExtNum; i++) {
318 bool protocolExists = false;
319 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000320 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000321 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
322 protocolExists = true;
323 break;
324 }
325 }
326 // Do we want to warn on a protocol in extension class which
327 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000328 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000329 ProtocolRefs.push_back(ProtoInExtension);
330 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000331
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000332 if (ProtocolRefs.empty())
333 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000334
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000335 // Merge ProtocolRefs into class's protocol list;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000336 for (auto *P : all_referenced_protocols()) {
337 ProtocolRefs.push_back(P);
Douglas Gregor002b6712010-01-16 15:02:53 +0000338 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000339
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000340 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000341}
342
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000343const ObjCInterfaceDecl *
344ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
345 const ObjCInterfaceDecl *IFace = this;
346 while (IFace) {
347 if (IFace->hasDesignatedInitializers())
348 return IFace;
349 if (!IFace->inheritsDesignatedInitializers())
350 break;
351 IFace = IFace->getSuperClass();
352 }
353 return 0;
354}
355
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000356static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
357 for (const auto *MD : D->instance_methods()) {
358 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
359 return true;
360 }
361 for (const auto *Ext : D->visible_extensions()) {
362 for (const auto *MD : Ext->instance_methods()) {
363 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
364 return true;
365 }
366 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000367 if (const auto *ImplD = D->getImplementation()) {
368 for (const auto *MD : ImplD->instance_methods()) {
369 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
370 return true;
371 }
372 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000373 return false;
374}
375
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000376bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
377 switch (data().InheritedDesignatedInitializers) {
378 case DefinitionData::IDI_Inherited:
379 return true;
380 case DefinitionData::IDI_NotInherited:
381 return false;
382 case DefinitionData::IDI_Unknown: {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000383 // If the class introduced initializers we conservatively assume that we
384 // don't know if any of them is a designated initializer to avoid possible
385 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000386 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000387 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
388 return false;
389 } else {
390 data().InheritedDesignatedInitializers = DefinitionData::IDI_Inherited;
391 return true;
392 }
393 }
394 }
395
396 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
397}
398
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000399void ObjCInterfaceDecl::getDesignatedInitializers(
400 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000401 // Check for a complete definition and recover if not so.
402 if (!isThisDeclarationADefinition())
403 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000404 if (data().ExternallyCompleted)
405 LoadExternalDefinition();
406
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000407 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000408 if (!IFace)
409 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000410
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000411 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000412 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000413 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000414 for (const auto *Ext : IFace->visible_extensions()) {
415 for (const auto *MD : Ext->instance_methods())
416 if (MD->isThisDeclarationADesignatedInitializer())
417 Methods.push_back(MD);
418 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000419}
420
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000421bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
422 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000423 // Check for a complete definition and recover if not so.
424 if (!isThisDeclarationADefinition())
425 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000426 if (data().ExternallyCompleted)
427 LoadExternalDefinition();
428
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000429 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000430 if (!IFace)
431 return false;
432
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000433 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000434 if (MD->isThisDeclarationADesignatedInitializer()) {
435 if (InitMethod)
436 *InitMethod = MD;
437 return true;
438 }
439 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000440 for (const auto *Ext : IFace->visible_extensions()) {
441 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
442 if (MD->isThisDeclarationADesignatedInitializer()) {
443 if (InitMethod)
444 *InitMethod = MD;
445 return true;
446 }
447 }
448 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000449 return false;
450}
451
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000452void ObjCInterfaceDecl::allocateDefinitionData() {
453 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000454 Data.setPointer(new (getASTContext()) DefinitionData());
455 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000456
457 // Make the type point at the definition, now that we have one.
458 if (TypeForDecl)
459 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000460}
461
462void ObjCInterfaceDecl::startDefinition() {
463 allocateDefinitionData();
464
Douglas Gregor66b310c2011-12-15 18:03:09 +0000465 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000466 for (auto RD : redecls()) {
467 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000468 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000469 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000470}
471
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000472ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
473 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000474 // FIXME: Should make sure no callers ever do this.
475 if (!hasDefinition())
476 return 0;
477
478 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000479 LoadExternalDefinition();
480
Chris Lattner89375192008-03-16 00:19:01 +0000481 ObjCInterfaceDecl* ClassDecl = this;
482 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000483 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000484 clsDeclared = ClassDecl;
485 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000486 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000487
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000488 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000489 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000490 clsDeclared = ClassDecl;
491 return I;
492 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000493 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000494
Chris Lattner89375192008-03-16 00:19:01 +0000495 ClassDecl = ClassDecl->getSuperClass();
496 }
497 return NULL;
498}
499
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000500/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
501/// class whose name is passed as argument. If it is not one of the super classes
502/// the it returns NULL.
503ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
504 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000505 // FIXME: Should make sure no callers ever do this.
506 if (!hasDefinition())
507 return 0;
508
509 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000510 LoadExternalDefinition();
511
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000512 ObjCInterfaceDecl* ClassDecl = this;
513 while (ClassDecl != NULL) {
514 if (ClassDecl->getIdentifier() == ICName)
515 return ClassDecl;
516 ClassDecl = ClassDecl->getSuperClass();
517 }
518 return NULL;
519}
520
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000521ObjCProtocolDecl *
522ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000523 for (auto *P : all_referenced_protocols())
524 if (P->lookupProtocolNamed(Name))
525 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000526 ObjCInterfaceDecl *SuperClass = getSuperClass();
527 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
528}
529
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000530/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000531/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000532/// When argument category "C" is specified, any implicit method found
533/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000534ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000535 bool isInstance,
536 bool shallowCategoryLookup,
537 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000538 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000539{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000540 // FIXME: Should make sure no callers ever do this.
541 if (!hasDefinition())
542 return 0;
543
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000544 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000545 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000546
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000547 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000548 LoadExternalDefinition();
549
Ted Kremenek00781502013-11-23 01:01:29 +0000550 while (ClassDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000551 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000552 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000553
Chris Lattner89375192008-03-16 00:19:01 +0000554 // Didn't find one yet - look through protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000555 for (const auto *I : ClassDecl->protocols())
556 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000557 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000558
559 // Didn't find one yet - now look through categories.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000560 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000561 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000562 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000563 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000564
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000565 if (!shallowCategoryLookup) {
566 // Didn't find one yet - look through protocols.
567 const ObjCList<ObjCProtocolDecl> &Protocols =
568 Cat->getReferencedProtocols();
569 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
570 E = Protocols.end(); I != E; ++I)
571 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000572 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000573 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000574 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000575 }
Ted Kremenek00781502013-11-23 01:01:29 +0000576
577 if (!followSuper)
578 return NULL;
579
580 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000581 ClassDecl = ClassDecl->getSuperClass();
582 }
583 return NULL;
584}
585
Anna Zaksc77a3b12012-07-27 19:07:44 +0000586// Will search "local" class/category implementations for a method decl.
587// If failed, then we search in class's root for an instance method.
588// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000589ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
590 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000591 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000592 // FIXME: Should make sure no callers ever do this.
593 if (!hasDefinition())
594 return 0;
595
596 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000597 LoadExternalDefinition();
598
Steve Naroffbb69c942009-10-01 23:46:04 +0000599 ObjCMethodDecl *Method = 0;
600 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000601 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
602 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000603
604 // Look through local category implementations associated with the class.
605 if (!Method)
606 Method = Instance ? getCategoryInstanceMethod(Sel)
607 : getCategoryClassMethod(Sel);
608
609 // Before we give up, check if the selector is an instance method.
610 // But only in the root. This matches gcc's behavior and what the
611 // runtime expects.
612 if (!Instance && !Method && !getSuperClass()) {
613 Method = lookupInstanceMethod(Sel);
614 // Look through local category implementations associated
615 // with the root class.
616 if (!Method)
617 Method = lookupPrivateMethod(Sel, true);
618 }
619
Steve Naroffbb69c942009-10-01 23:46:04 +0000620 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000621 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000622 return Method;
623}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000624
625//===----------------------------------------------------------------------===//
626// ObjCMethodDecl
627//===----------------------------------------------------------------------===//
628
Alp Toker314cc812014-01-25 16:55:45 +0000629ObjCMethodDecl *ObjCMethodDecl::Create(
630 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
631 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
632 DeclContext *contextDecl, bool isInstance, bool isVariadic,
633 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
634 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000635 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000636 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000637 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
638 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000639}
640
Douglas Gregor72172e92012-01-05 21:55:30 +0000641ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000642 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
643 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000644}
645
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000646bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
647 return getMethodFamily() == OMF_init &&
648 hasAttr<ObjCDesignatedInitializerAttr>();
649}
650
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000651bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
652 const ObjCMethodDecl **InitMethod) const {
653 if (getMethodFamily() != OMF_init)
654 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000655 const DeclContext *DC = getDeclContext();
656 if (isa<ObjCProtocolDecl>(DC))
657 return false;
658 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000659 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000660 return false;
661}
662
Douglas Gregora6017bb2012-10-09 17:21:28 +0000663Stmt *ObjCMethodDecl::getBody() const {
664 return Body.get(getASTContext().getExternalSource());
665}
666
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000667void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
668 assert(PrevMethod);
669 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
670 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000671 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000672}
673
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000674void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
675 ArrayRef<ParmVarDecl*> Params,
676 ArrayRef<SourceLocation> SelLocs) {
677 ParamsAndSelLocs = 0;
678 NumParams = Params.size();
679 if (Params.empty() && SelLocs.empty())
680 return;
681
682 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
683 sizeof(SourceLocation) * SelLocs.size();
684 ParamsAndSelLocs = C.Allocate(Size);
685 std::copy(Params.begin(), Params.end(), getParams());
686 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
687}
688
689void ObjCMethodDecl::getSelectorLocs(
690 SmallVectorImpl<SourceLocation> &SelLocs) const {
691 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
692 SelLocs.push_back(getSelectorLoc(i));
693}
694
695void ObjCMethodDecl::setMethodParams(ASTContext &C,
696 ArrayRef<ParmVarDecl*> Params,
697 ArrayRef<SourceLocation> SelLocs) {
698 assert((!SelLocs.empty() || isImplicit()) &&
699 "No selector locs for non-implicit method");
700 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000701 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000702
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000703 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
704 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000705 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000706 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000707
708 setParamsAndSelLocs(C, Params, SelLocs);
709}
710
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000711/// \brief A definition will return its interface declaration.
712/// An interface declaration will return its definition.
713/// Otherwise it will return itself.
714ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
715 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000716 ObjCMethodDecl *Redecl = 0;
717 if (HasRedeclaration)
718 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000719 if (Redecl)
720 return Redecl;
721
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000722 Decl *CtxD = cast<Decl>(getDeclContext());
723
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000724 if (!CtxD->isInvalidDecl()) {
725 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
726 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
727 if (!ImplD->isInvalidDecl())
728 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000729
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000730 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
731 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
732 if (!ImplD->isInvalidDecl())
733 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000734
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000735 } else if (ObjCImplementationDecl *ImplD =
736 dyn_cast<ObjCImplementationDecl>(CtxD)) {
737 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
738 if (!IFD->isInvalidDecl())
739 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000740
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000741 } else if (ObjCCategoryImplDecl *CImplD =
742 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
743 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
744 if (!CatD->isInvalidDecl())
745 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
746 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000747 }
748
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000749 if (!Redecl && isRedeclaration()) {
750 // This is the last redeclaration, go back to the first method.
751 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
752 isInstanceMethod());
753 }
754
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000755 return Redecl ? Redecl : this;
756}
757
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000758ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
759 Decl *CtxD = cast<Decl>(getDeclContext());
760
761 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
762 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
763 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
764 isInstanceMethod()))
765 return MD;
766
767 } else if (ObjCCategoryImplDecl *CImplD =
768 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000769 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000770 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
771 isInstanceMethod()))
772 return MD;
773 }
774
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000775 if (isRedeclaration())
776 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
777 isInstanceMethod());
778
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000779 return this;
780}
781
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000782SourceLocation ObjCMethodDecl::getLocEnd() const {
783 if (Stmt *Body = getBody())
784 return Body->getLocEnd();
785 return DeclEndLoc;
786}
787
John McCallb4526252011-03-02 01:50:55 +0000788ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
789 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000790 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000791 return family;
792
John McCall86bc21f2011-03-02 11:33:24 +0000793 // Check for an explicit attribute.
794 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
795 // The unfortunate necessity of mapping between enums here is due
796 // to the attributes framework.
797 switch (attr->getFamily()) {
798 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
799 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
800 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
801 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
802 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
803 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
804 }
805 Family = static_cast<unsigned>(family);
806 return family;
807 }
808
John McCallb4526252011-03-02 01:50:55 +0000809 family = getSelector().getMethodFamily();
810 switch (family) {
811 case OMF_None: break;
812
813 // init only has a conventional meaning for an instance method, and
814 // it has to return an object.
815 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000816 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000817 family = OMF_None;
818 break;
819
820 // alloc/copy/new have a conventional meaning for both class and
821 // instance methods, but they require an object return.
822 case OMF_alloc:
823 case OMF_copy:
824 case OMF_mutableCopy:
825 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000826 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000827 family = OMF_None;
828 break;
829
830 // These selectors have a conventional meaning only for instance methods.
831 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000832 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000833 case OMF_retain:
834 case OMF_release:
835 case OMF_autorelease:
836 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000837 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000838 if (!isInstanceMethod())
839 family = OMF_None;
840 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000841
842 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000843 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000844 family = OMF_None;
845 else {
846 unsigned noParams = param_size();
847 if (noParams < 1 || noParams > 3)
848 family = OMF_None;
849 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000850 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000851 QualType ArgT = (*it);
852 if (!ArgT->isObjCSelType()) {
853 family = OMF_None;
854 break;
855 }
856 while (--noParams) {
857 it++;
858 ArgT = (*it);
859 if (!ArgT->isObjCIdType()) {
860 family = OMF_None;
861 break;
862 }
863 }
864 }
865 }
866 break;
867
John McCallb4526252011-03-02 01:50:55 +0000868 }
869
870 // Cache the result.
871 Family = static_cast<unsigned>(family);
872 return family;
873}
874
Mike Stump11289f42009-09-09 15:08:12 +0000875void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000876 const ObjCInterfaceDecl *OID) {
877 QualType selfTy;
878 if (isInstanceMethod()) {
879 // There may be no interface context due to error in declaration
880 // of the interface (which has been reported). Recover gracefully.
881 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000882 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000883 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000884 } else {
885 selfTy = Context.getObjCIdType();
886 }
887 } else // we have a factory method.
888 selfTy = Context.getObjCClassType();
889
John McCalld4631322011-06-17 06:42:21 +0000890 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000891 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000892
David Blaikiebbafb8a2012-03-11 07:00:24 +0000893 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000894 if (isInstanceMethod()) {
895 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000896
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000897 // 'self' is always __strong. It's actually pseudo-strong except
898 // in init methods (or methods labeled ns_consumes_self), though.
899 Qualifiers qs;
900 qs.setObjCLifetime(Qualifiers::OCL_Strong);
901 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000902
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000903 // In addition, 'self' is const unless this is an init method.
904 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
905 selfTy = selfTy.withConst();
906 selfIsPseudoStrong = true;
907 }
908 }
909 else {
910 assert(isClassMethod());
911 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000912 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000913 selfIsPseudoStrong = true;
914 }
John McCall31168b02011-06-15 23:02:42 +0000915 }
916
917 ImplicitParamDecl *self
918 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
919 &Context.Idents.get("self"), selfTy);
920 setSelfDecl(self);
921
922 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000923 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000924
John McCalld4631322011-06-17 06:42:21 +0000925 if (selfIsPseudoStrong)
926 self->setARCPseudoStrong(true);
927
Mike Stump11289f42009-09-09 15:08:12 +0000928 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
929 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000930 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000931}
932
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000933ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
934 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
935 return ID;
936 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
937 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000938 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000939 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000940 if (isa<ObjCProtocolDecl>(getDeclContext()))
941 return 0;
David Blaikie83d382b2011-09-23 05:06:16 +0000942 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000943}
944
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000945static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
946 const ObjCMethodDecl *Method,
947 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
948 bool MovedToSuper) {
949 if (!Container)
950 return;
951
952 // In categories look for overriden methods from protocols. A method from
953 // category is not "overriden" since it is considered as the "same" method
954 // (same USR) as the one from the interface.
955 if (const ObjCCategoryDecl *
956 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
957 // Check whether we have a matching method at this category but only if we
958 // are at the super class level.
959 if (MovedToSuper)
960 if (ObjCMethodDecl *
961 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000962 Method->isInstanceMethod(),
963 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000964 if (Method != Overridden) {
965 // We found an override at this category; there is no need to look
966 // into its protocols.
967 Methods.push_back(Overridden);
968 return;
969 }
970
Aaron Ballman19a41762014-03-14 12:55:57 +0000971 for (const auto *P : Category->protocols())
972 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000973 return;
974 }
975
976 // Check whether we have a matching method at this level.
977 if (const ObjCMethodDecl *
978 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000979 Method->isInstanceMethod(),
980 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000981 if (Method != Overridden) {
982 // We found an override at this level; there is no need to look
983 // into other protocols or categories.
984 Methods.push_back(Overridden);
985 return;
986 }
987
988 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000989 for (const auto *P : Protocol->protocols())
990 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000991 }
992
993 if (const ObjCInterfaceDecl *
994 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +0000995 for (const auto *P : Interface->protocols())
996 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000997
Aaron Ballman15063e12014-03-13 21:35:02 +0000998 for (const auto *Cat : Interface->known_categories())
999 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001000
1001 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1002 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1003 /*MovedToSuper=*/true);
1004 }
1005}
1006
1007static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1008 const ObjCMethodDecl *Method,
1009 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1010 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1011 /*MovedToSuper=*/false);
1012}
1013
1014static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1015 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1016 assert(Method->isOverriding());
1017
1018 if (const ObjCProtocolDecl *
1019 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1020 CollectOverriddenMethods(ProtD, Method, overridden);
1021
1022 } else if (const ObjCImplDecl *
1023 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1024 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1025 if (!ID)
1026 return;
1027 // Start searching for overridden methods using the method from the
1028 // interface as starting point.
1029 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001030 Method->isInstanceMethod(),
1031 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001032 Method = IFaceMeth;
1033 CollectOverriddenMethods(ID, Method, overridden);
1034
1035 } else if (const ObjCCategoryDecl *
1036 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1037 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1038 if (!ID)
1039 return;
1040 // Start searching for overridden methods using the method from the
1041 // interface as starting point.
1042 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001043 Method->isInstanceMethod(),
1044 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001045 Method = IFaceMeth;
1046 CollectOverriddenMethods(ID, Method, overridden);
1047
1048 } else {
1049 CollectOverriddenMethods(
1050 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1051 Method, overridden);
1052 }
1053}
1054
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001055void ObjCMethodDecl::getOverriddenMethods(
1056 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1057 const ObjCMethodDecl *Method = this;
1058
1059 if (Method->isRedeclaration()) {
1060 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1061 getMethod(Method->getSelector(), Method->isInstanceMethod());
1062 }
1063
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001064 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001065 collectOverriddenMethodsSlow(Method, Overridden);
1066 assert(!Overridden.empty() &&
1067 "ObjCMethodDecl's overriding bit is not as expected");
1068 }
1069}
1070
Jordan Rose2bd991a2012-10-10 16:42:54 +00001071const ObjCPropertyDecl *
1072ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1073 Selector Sel = getSelector();
1074 unsigned NumArgs = Sel.getNumArgs();
1075 if (NumArgs > 1)
1076 return 0;
1077
Jordan Rose59e34ec2012-10-11 16:02:02 +00001078 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001079 return 0;
1080
1081 if (isPropertyAccessor()) {
1082 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001083 // If container is class extension, find its primary class.
1084 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1085 if (CatDecl->IsClassExtension())
1086 Container = CatDecl->getClassInterface();
1087
Jordan Rose2bd991a2012-10-10 16:42:54 +00001088 bool IsGetter = (NumArgs == 0);
1089
Aaron Ballmand174edf2014-03-13 19:11:50 +00001090 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001091 Selector NextSel = IsGetter ? I->getGetterName()
1092 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001093 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001094 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001095 }
1096
1097 llvm_unreachable("Marked as a property accessor but no property found!");
1098 }
1099
1100 if (!CheckOverrides)
1101 return 0;
1102
1103 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1104 OverridesTy Overrides;
1105 getOverriddenMethods(Overrides);
1106 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1107 I != E; ++I) {
1108 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1109 return Prop;
1110 }
1111
1112 return 0;
1113
1114}
1115
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001116//===----------------------------------------------------------------------===//
1117// ObjCInterfaceDecl
1118//===----------------------------------------------------------------------===//
1119
Douglas Gregord53ae832012-01-17 18:09:05 +00001120ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001121 DeclContext *DC,
1122 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001123 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001124 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001125 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001126 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001127 ObjCInterfaceDecl *Result = new (C, DC)
1128 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001129 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001130 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001131 return Result;
1132}
1133
Richard Smithf7981722013-11-22 09:01:48 +00001134ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001135 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001136 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1137 0, SourceLocation(),
1138 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001139 Result->Data.setInt(!C.getLangOpts().Modules);
1140 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001141}
1142
1143ObjCInterfaceDecl::
1144ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001145 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1146 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001147 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001148 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001149{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001150 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001151
1152 // Copy the 'data' pointer over.
1153 if (PrevDecl)
1154 Data = PrevDecl->Data;
1155
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001156 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001157}
1158
Douglas Gregor73693022010-12-01 23:49:52 +00001159void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001160 assert(data().ExternallyCompleted && "Class is not externally completed");
1161 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001162 getASTContext().getExternalSource()->CompleteType(
1163 const_cast<ObjCInterfaceDecl *>(this));
1164}
1165
1166void ObjCInterfaceDecl::setExternallyCompleted() {
1167 assert(getASTContext().getExternalSource() &&
1168 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001169 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001170 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001171 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001172}
1173
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001174void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001175 // Check for a complete definition and recover if not so.
1176 if (!isThisDeclarationADefinition())
1177 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001178 data().HasDesignatedInitializers = true;
1179}
1180
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001181bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001182 // Check for a complete definition and recover if not so.
1183 if (!isThisDeclarationADefinition())
1184 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001185 if (data().ExternallyCompleted)
1186 LoadExternalDefinition();
1187
1188 return data().HasDesignatedInitializers;
1189}
1190
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001191ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001192 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1193 if (data().ExternallyCompleted)
1194 LoadExternalDefinition();
1195
1196 return getASTContext().getObjCImplementation(
1197 const_cast<ObjCInterfaceDecl*>(Def));
1198 }
1199
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001200 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001201 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001202}
1203
1204void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001205 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001206}
1207
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001208namespace {
1209 struct SynthesizeIvarChunk {
1210 uint64_t Size;
1211 ObjCIvarDecl *Ivar;
1212 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1213 : Size(size), Ivar(ivar) {}
1214 };
1215
1216 bool operator<(const SynthesizeIvarChunk & LHS,
1217 const SynthesizeIvarChunk &RHS) {
1218 return LHS.Size < RHS.Size;
1219 }
1220}
1221
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001222/// all_declared_ivar_begin - return first ivar declared in this class,
1223/// its extensions and its implementation. Lazily build the list on first
1224/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001225///
1226/// Caveat: The list returned by this method reflects the current
1227/// state of the parser. The cache will be updated for every ivar
1228/// added by an extension or the implementation when they are
1229/// encountered.
1230/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001231ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001232 // FIXME: Should make sure no callers ever do this.
1233 if (!hasDefinition())
1234 return 0;
1235
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001236 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001237 if (!data().IvarList) {
1238 if (!ivar_empty()) {
1239 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1240 data().IvarList = *I; ++I;
1241 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001242 curIvar->setNextIvar(*I);
1243 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001244
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001245 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001246 if (!Ext->ivar_empty()) {
1247 ObjCCategoryDecl::ivar_iterator
1248 I = Ext->ivar_begin(),
1249 E = Ext->ivar_end();
1250 if (!data().IvarList) {
1251 data().IvarList = *I; ++I;
1252 curIvar = data().IvarList;
1253 }
1254 for ( ;I != E; curIvar = *I, ++I)
1255 curIvar->setNextIvar(*I);
1256 }
1257 }
1258 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001259 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001260
1261 // cached and complete!
1262 if (!data().IvarListMissingImplementation)
1263 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001264
1265 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001266 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001267 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001268 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001269 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001270 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1271 layout.push_back(SynthesizeIvarChunk(
1272 IV->getASTContext().getTypeSize(IV->getType()), IV));
1273 continue;
1274 }
1275 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001276 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001277 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001278 curIvar->setNextIvar(IV);
1279 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001280 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001281
1282 if (!layout.empty()) {
1283 // Order synthesized ivars by their size.
1284 std::stable_sort(layout.begin(), layout.end());
1285 unsigned Ix = 0, EIx = layout.size();
1286 if (!data().IvarList) {
1287 data().IvarList = layout[0].Ivar; Ix++;
1288 curIvar = data().IvarList;
1289 }
1290 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1291 curIvar->setNextIvar(layout[Ix].Ivar);
1292 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001293 }
1294 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001295 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001296}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001297
1298/// FindCategoryDeclaration - Finds category declaration in the list of
1299/// categories for this class and returns it. Name of the category is passed
1300/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001301///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001302ObjCCategoryDecl *
1303ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001304 // FIXME: Should make sure no callers ever do this.
1305 if (!hasDefinition())
1306 return 0;
1307
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001308 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001309 LoadExternalDefinition();
1310
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001311 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001312 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001313 return Cat;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001314
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001315 return 0;
1316}
1317
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001318ObjCMethodDecl *
1319ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001320 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001321 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001322 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1323 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001324 }
1325
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001326 return 0;
1327}
1328
1329ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001330 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001331 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001332 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1333 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001334 }
1335
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001336 return 0;
1337}
1338
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001339/// ClassImplementsProtocol - Checks that 'lProto' protocol
1340/// has been implemented in IDecl class, its super class or categories (if
1341/// lookupCategory is true).
1342bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1343 bool lookupCategory,
1344 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001345 if (!hasDefinition())
1346 return false;
1347
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001348 ObjCInterfaceDecl *IDecl = this;
1349 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001350 for (auto *PI : IDecl->protocols()){
1351 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001352 return true;
1353 // This is dubious and is added to be compatible with gcc. In gcc, it is
1354 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1355 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1356 // object. This IMO, should be a bug.
1357 // FIXME: Treat this as an extension, and flag this as an error when GCC
1358 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001359 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001360 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001361 return true;
1362 }
Mike Stump11289f42009-09-09 15:08:12 +00001363
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001364 // 2nd, look up the category.
1365 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001366 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001367 for (auto *PI : Cat->protocols())
1368 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001369 return true;
1370 }
Mike Stump11289f42009-09-09 15:08:12 +00001371
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001372 // 3rd, look up the super class(s)
1373 if (IDecl->getSuperClass())
1374 return
1375 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1376 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001377
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001378 return false;
1379}
1380
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001381//===----------------------------------------------------------------------===//
1382// ObjCIvarDecl
1383//===----------------------------------------------------------------------===//
1384
David Blaikie68e081d2011-12-20 02:48:34 +00001385void ObjCIvarDecl::anchor() { }
1386
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001387ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001388 SourceLocation StartLoc,
1389 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001390 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001391 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001392 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001393 if (DC) {
1394 // Ivar's can only appear in interfaces, implementations (via synthesized
1395 // properties), and class extensions (via direct declaration, or synthesized
1396 // properties).
1397 //
1398 // FIXME: This should really be asserting this:
1399 // (isa<ObjCCategoryDecl>(DC) &&
1400 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1401 // but unfortunately we sometimes place ivars into non-class extension
1402 // categories on error. This breaks an AST invariant, and should not be
1403 // fixed.
1404 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1405 isa<ObjCCategoryDecl>(DC)) &&
1406 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001407 // Once a new ivar is created in any of class/class-extension/implementation
1408 // decl contexts, the previously built IvarList must be rebuilt.
1409 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1410 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001411 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001412 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001413 else
1414 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001415 }
1416 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001417 }
1418
Richard Smithf7981722013-11-22 09:01:48 +00001419 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001420 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001421}
1422
Douglas Gregor72172e92012-01-05 21:55:30 +00001423ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001424 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001425 QualType(), 0, ObjCIvarDecl::None, 0, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001426}
1427
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001428const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1429 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001430
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001431 switch (DC->getKind()) {
1432 default:
1433 case ObjCCategoryImpl:
1434 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001435 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001436
1437 // Ivars can only appear in class extension categories.
1438 case ObjCCategory: {
1439 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1440 assert(CD->IsClassExtension() && "invalid container for ivar!");
1441 return CD->getClassInterface();
1442 }
1443
1444 case ObjCImplementation:
1445 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1446
1447 case ObjCInterface:
1448 return cast<ObjCInterfaceDecl>(DC);
1449 }
1450}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001451
1452//===----------------------------------------------------------------------===//
1453// ObjCAtDefsFieldDecl
1454//===----------------------------------------------------------------------===//
1455
David Blaikie68e081d2011-12-20 02:48:34 +00001456void ObjCAtDefsFieldDecl::anchor() { }
1457
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001458ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001459*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1460 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001461 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001462 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001463}
1464
Richard Smithf7981722013-11-22 09:01:48 +00001465ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001466 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001467 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1468 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001469}
1470
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001471//===----------------------------------------------------------------------===//
1472// ObjCProtocolDecl
1473//===----------------------------------------------------------------------===//
1474
David Blaikie68e081d2011-12-20 02:48:34 +00001475void ObjCProtocolDecl::anchor() { }
1476
Douglas Gregor32c17572012-01-01 20:30:41 +00001477ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1478 SourceLocation nameLoc,
1479 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001480 ObjCProtocolDecl *PrevDecl)
1481 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001482{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001483 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001484 if (PrevDecl)
1485 Data = PrevDecl->Data;
1486}
1487
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001488ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001489 IdentifierInfo *Id,
1490 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001491 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001492 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001493 ObjCProtocolDecl *Result =
1494 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001495 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001496 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001497}
1498
Richard Smithf7981722013-11-22 09:01:48 +00001499ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001500 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001501 ObjCProtocolDecl *Result =
1502 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001503 Result->Data.setInt(!C.getLangOpts().Modules);
1504 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001505}
1506
Steve Naroff114aecb2009-03-01 16:12:44 +00001507ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1508 ObjCProtocolDecl *PDecl = this;
1509
1510 if (Name == getIdentifier())
1511 return PDecl;
1512
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001513 for (auto *I : protocols())
1514 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001515 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001516
Steve Naroff114aecb2009-03-01 16:12:44 +00001517 return NULL;
1518}
1519
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001520// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001521// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001522ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1523 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001524 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001525
Douglas Gregoreed49792013-01-17 00:38:46 +00001526 // If there is no definition or the definition is hidden, we don't find
1527 // anything.
1528 const ObjCProtocolDecl *Def = getDefinition();
1529 if (!Def || Def->isHidden())
1530 return NULL;
1531
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001532 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001533 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001534
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001535 for (const auto *I : protocols())
1536 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001537 return MethodDecl;
1538 return NULL;
1539}
1540
Douglas Gregore6e48b12012-01-01 19:29:29 +00001541void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001542 assert(!Data.getPointer() && "Protocol already has a definition!");
1543 Data.setPointer(new (getASTContext()) DefinitionData);
1544 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001545}
1546
1547void ObjCProtocolDecl::startDefinition() {
1548 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001549
1550 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001551 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001552 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001553}
1554
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001555void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1556 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001557
1558 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001559 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001560 // Insert into PM if not there already.
1561 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001562 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001563 }
1564 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001565 for (const auto *PI : PDecl->protocols())
1566 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001567 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001568}
1569
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001570
1571void ObjCProtocolDecl::collectInheritedProtocolProperties(
1572 const ObjCPropertyDecl *Property,
1573 ProtocolPropertyMap &PM) const {
1574 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1575 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001576 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001577 if (Prop == Property)
1578 continue;
1579 if (Prop->getIdentifier() == Property->getIdentifier()) {
1580 PM[PDecl] = Prop;
1581 MatchFound = true;
1582 break;
1583 }
1584 }
1585 // Scan through protocol's protocols which did not have a matching property.
1586 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001587 for (const auto *PI : PDecl->protocols())
1588 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001589 }
1590}
Anna Zaks673d76b2012-10-18 19:17:53 +00001591
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001592//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001593// ObjCCategoryDecl
1594//===----------------------------------------------------------------------===//
1595
David Blaikie68e081d2011-12-20 02:48:34 +00001596void ObjCCategoryDecl::anchor() { }
1597
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001598ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001599 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001600 SourceLocation ClassNameLoc,
1601 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001602 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001603 ObjCInterfaceDecl *IDecl,
1604 SourceLocation IvarLBraceLoc,
1605 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001606 ObjCCategoryDecl *CatDecl =
1607 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1608 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001609 if (IDecl) {
1610 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001611 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001612 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001613 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001614 if (ASTMutationListener *L = C.getASTMutationListener())
1615 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1616 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001617 }
1618
1619 return CatDecl;
1620}
1621
Richard Smithf7981722013-11-22 09:01:48 +00001622ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001623 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001624 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1625 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001626}
1627
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001628ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1629 return getASTContext().getObjCImplementation(
1630 const_cast<ObjCCategoryDecl*>(this));
1631}
1632
1633void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1634 getASTContext().setObjCImplementation(this, ImplD);
1635}
1636
1637
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001638//===----------------------------------------------------------------------===//
1639// ObjCCategoryImplDecl
1640//===----------------------------------------------------------------------===//
1641
David Blaikie68e081d2011-12-20 02:48:34 +00001642void ObjCCategoryImplDecl::anchor() { }
1643
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001644ObjCCategoryImplDecl *
1645ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001646 IdentifierInfo *Id,
1647 ObjCInterfaceDecl *ClassInterface,
1648 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001649 SourceLocation atStartLoc,
1650 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001651 if (ClassInterface && ClassInterface->hasDefinition())
1652 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001653 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1654 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001655}
1656
Douglas Gregor72172e92012-01-05 21:55:30 +00001657ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1658 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001659 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1660 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001661}
1662
Steve Narofff406f4d2009-10-29 21:11:04 +00001663ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001664 // The class interface might be NULL if we are working with invalid code.
1665 if (const ObjCInterfaceDecl *ID = getClassInterface())
1666 return ID->FindCategoryDeclaration(getIdentifier());
1667 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001668}
1669
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001670
David Blaikie68e081d2011-12-20 02:48:34 +00001671void ObjCImplDecl::anchor() { }
1672
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001673void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001674 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001675 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001676 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001677}
1678
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001679void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1680 ASTContext &Ctx = getASTContext();
1681
1682 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001683 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001684 if (IFace)
1685 Ctx.setObjCImplementation(IFace, ImplD);
1686
Duncan Sands49c29ee2009-07-21 07:56:29 +00001687 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001688 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1689 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1690 Ctx.setObjCImplementation(CD, ImplD);
1691 }
1692
1693 ClassInterface = IFace;
1694}
1695
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001696/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001697/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001698/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001699///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001700ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001701FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001702 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001703 if (PID->getPropertyIvarDecl() &&
1704 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1705 return PID;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001706 return 0;
1707}
1708
1709/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001710/// added to the list of those properties \@synthesized/\@dynamic in this
1711/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001712///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001713ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001714FindPropertyImplDecl(IdentifierInfo *Id) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001715 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001716 if (PID->getPropertyDecl()->getIdentifier() == Id)
1717 return PID;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001718 return 0;
1719}
1720
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001721raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001722 const ObjCCategoryImplDecl &CID) {
1723 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001724 return OS;
1725}
1726
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001727//===----------------------------------------------------------------------===//
1728// ObjCImplementationDecl
1729//===----------------------------------------------------------------------===//
1730
David Blaikie68e081d2011-12-20 02:48:34 +00001731void ObjCImplementationDecl::anchor() { }
1732
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001733ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001734ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001735 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001736 ObjCInterfaceDecl *SuperDecl,
1737 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001738 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001739 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001740 SourceLocation IvarLBraceLoc,
1741 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001742 if (ClassInterface && ClassInterface->hasDefinition())
1743 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001744 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1745 nameLoc, atStartLoc, superLoc,
1746 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001747}
1748
Douglas Gregor72172e92012-01-05 21:55:30 +00001749ObjCImplementationDecl *
1750ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001751 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1752 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001753}
1754
John McCall0410e572011-07-22 04:15:06 +00001755void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1756 CXXCtorInitializer ** initializers,
1757 unsigned numInitializers) {
1758 if (numInitializers > 0) {
1759 NumIvarInitializers = numInitializers;
1760 CXXCtorInitializer **ivarInitializers =
1761 new (C) CXXCtorInitializer*[NumIvarInitializers];
1762 memcpy(ivarInitializers, initializers,
1763 numInitializers * sizeof(CXXCtorInitializer*));
1764 IvarInitializers = ivarInitializers;
1765 }
1766}
1767
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001768raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001769 const ObjCImplementationDecl &ID) {
1770 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001771 return OS;
1772}
1773
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001774//===----------------------------------------------------------------------===//
1775// ObjCCompatibleAliasDecl
1776//===----------------------------------------------------------------------===//
1777
David Blaikie68e081d2011-12-20 02:48:34 +00001778void ObjCCompatibleAliasDecl::anchor() { }
1779
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001780ObjCCompatibleAliasDecl *
1781ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1782 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001783 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001784 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001785 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001786}
1787
Douglas Gregor72172e92012-01-05 21:55:30 +00001788ObjCCompatibleAliasDecl *
1789ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001790 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001791}
1792
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001793//===----------------------------------------------------------------------===//
1794// ObjCPropertyDecl
1795//===----------------------------------------------------------------------===//
1796
David Blaikie68e081d2011-12-20 02:48:34 +00001797void ObjCPropertyDecl::anchor() { }
1798
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001799ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1800 SourceLocation L,
1801 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001802 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001803 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001804 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001805 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001806 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001807}
1808
Richard Smithf7981722013-11-22 09:01:48 +00001809ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001810 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001811 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1812 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001813}
1814
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001815//===----------------------------------------------------------------------===//
1816// ObjCPropertyImplDecl
1817//===----------------------------------------------------------------------===//
1818
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001819ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001820 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001821 SourceLocation atLoc,
1822 SourceLocation L,
1823 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001824 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001825 ObjCIvarDecl *ivar,
1826 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001827 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1828 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001829}
Chris Lattnered0e1642008-03-17 01:19:02 +00001830
Richard Smithf7981722013-11-22 09:01:48 +00001831ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001832 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001833 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1834 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001835}
1836
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001837SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1838 SourceLocation EndLoc = getLocation();
1839 if (IvarLoc.isValid())
1840 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001841
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001842 return SourceRange(AtLoc, EndLoc);
1843}