blob: 4bea9f948b7477d23fee3cab05118e62752ebec8 [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()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000368 for (const auto *MD : ImplD->instance_methods()) {
369 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
370 return true;
371 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000372 }
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;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000388 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000389 if (auto SuperD = getSuperClass()) {
390 data().InheritedDesignatedInitializers =
391 SuperD->declaresOrInheritsDesignatedInitializers() ?
392 DefinitionData::IDI_Inherited :
393 DefinitionData::IDI_NotInherited;
394 } else {
395 data().InheritedDesignatedInitializers =
396 DefinitionData::IDI_NotInherited;
397 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000398 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000399 assert(data().InheritedDesignatedInitializers
400 != DefinitionData::IDI_Unknown);
401 return data().InheritedDesignatedInitializers ==
402 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000403 }
404 }
405
406 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
407}
408
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000409void ObjCInterfaceDecl::getDesignatedInitializers(
410 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000411 // Check for a complete definition and recover if not so.
412 if (!isThisDeclarationADefinition())
413 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000414 if (data().ExternallyCompleted)
415 LoadExternalDefinition();
416
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000417 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000418 if (!IFace)
419 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000420
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000421 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000422 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000423 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000424 for (const auto *Ext : IFace->visible_extensions()) {
425 for (const auto *MD : Ext->instance_methods())
426 if (MD->isThisDeclarationADesignatedInitializer())
427 Methods.push_back(MD);
428 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000429}
430
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000431bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
432 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000433 // Check for a complete definition and recover if not so.
434 if (!isThisDeclarationADefinition())
435 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000436 if (data().ExternallyCompleted)
437 LoadExternalDefinition();
438
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000439 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000440 if (!IFace)
441 return false;
442
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000443 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000444 if (MD->isThisDeclarationADesignatedInitializer()) {
445 if (InitMethod)
446 *InitMethod = MD;
447 return true;
448 }
449 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000450 for (const auto *Ext : IFace->visible_extensions()) {
451 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
452 if (MD->isThisDeclarationADesignatedInitializer()) {
453 if (InitMethod)
454 *InitMethod = MD;
455 return true;
456 }
457 }
458 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000459 return false;
460}
461
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000462void ObjCInterfaceDecl::allocateDefinitionData() {
463 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000464 Data.setPointer(new (getASTContext()) DefinitionData());
465 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000466
467 // Make the type point at the definition, now that we have one.
468 if (TypeForDecl)
469 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000470}
471
472void ObjCInterfaceDecl::startDefinition() {
473 allocateDefinitionData();
474
Douglas Gregor66b310c2011-12-15 18:03:09 +0000475 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000476 for (auto RD : redecls()) {
477 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000478 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000479 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000480}
481
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000482ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
483 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000484 // FIXME: Should make sure no callers ever do this.
485 if (!hasDefinition())
486 return 0;
487
488 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000489 LoadExternalDefinition();
490
Chris Lattner89375192008-03-16 00:19:01 +0000491 ObjCInterfaceDecl* ClassDecl = this;
492 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000493 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000494 clsDeclared = ClassDecl;
495 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000496 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000497
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000498 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000499 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000500 clsDeclared = ClassDecl;
501 return I;
502 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000503 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000504
Chris Lattner89375192008-03-16 00:19:01 +0000505 ClassDecl = ClassDecl->getSuperClass();
506 }
507 return NULL;
508}
509
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000510/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
511/// class whose name is passed as argument. If it is not one of the super classes
512/// the it returns NULL.
513ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
514 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000515 // FIXME: Should make sure no callers ever do this.
516 if (!hasDefinition())
517 return 0;
518
519 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000520 LoadExternalDefinition();
521
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000522 ObjCInterfaceDecl* ClassDecl = this;
523 while (ClassDecl != NULL) {
524 if (ClassDecl->getIdentifier() == ICName)
525 return ClassDecl;
526 ClassDecl = ClassDecl->getSuperClass();
527 }
528 return NULL;
529}
530
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000531ObjCProtocolDecl *
532ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000533 for (auto *P : all_referenced_protocols())
534 if (P->lookupProtocolNamed(Name))
535 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000536 ObjCInterfaceDecl *SuperClass = getSuperClass();
537 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
538}
539
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000540/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000541/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000542/// When argument category "C" is specified, any implicit method found
543/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000544ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000545 bool isInstance,
546 bool shallowCategoryLookup,
547 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000548 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000549{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000550 // FIXME: Should make sure no callers ever do this.
551 if (!hasDefinition())
552 return 0;
553
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000554 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000555 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000556
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000557 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000558 LoadExternalDefinition();
559
Ted Kremenek00781502013-11-23 01:01:29 +0000560 while (ClassDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000561 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000562 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000563
Chris Lattner89375192008-03-16 00:19:01 +0000564 // Didn't find one yet - look through protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000565 for (const auto *I : ClassDecl->protocols())
566 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000567 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000568
569 // Didn't find one yet - now look through categories.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000570 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000571 if ((MethodDecl = Cat->getMethod(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 if (!shallowCategoryLookup) {
576 // Didn't find one yet - look through protocols.
577 const ObjCList<ObjCProtocolDecl> &Protocols =
578 Cat->getReferencedProtocols();
579 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
580 E = Protocols.end(); I != E; ++I)
581 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000582 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000583 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000584 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000585 }
Ted Kremenek00781502013-11-23 01:01:29 +0000586
587 if (!followSuper)
588 return NULL;
589
590 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000591 ClassDecl = ClassDecl->getSuperClass();
592 }
593 return NULL;
594}
595
Anna Zaksc77a3b12012-07-27 19:07:44 +0000596// Will search "local" class/category implementations for a method decl.
597// If failed, then we search in class's root for an instance method.
598// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000599ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
600 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000601 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000602 // FIXME: Should make sure no callers ever do this.
603 if (!hasDefinition())
604 return 0;
605
606 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000607 LoadExternalDefinition();
608
Steve Naroffbb69c942009-10-01 23:46:04 +0000609 ObjCMethodDecl *Method = 0;
610 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000611 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
612 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000613
614 // Look through local category implementations associated with the class.
615 if (!Method)
616 Method = Instance ? getCategoryInstanceMethod(Sel)
617 : getCategoryClassMethod(Sel);
618
619 // Before we give up, check if the selector is an instance method.
620 // But only in the root. This matches gcc's behavior and what the
621 // runtime expects.
622 if (!Instance && !Method && !getSuperClass()) {
623 Method = lookupInstanceMethod(Sel);
624 // Look through local category implementations associated
625 // with the root class.
626 if (!Method)
627 Method = lookupPrivateMethod(Sel, true);
628 }
629
Steve Naroffbb69c942009-10-01 23:46:04 +0000630 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000631 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000632 return Method;
633}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000634
635//===----------------------------------------------------------------------===//
636// ObjCMethodDecl
637//===----------------------------------------------------------------------===//
638
Alp Toker314cc812014-01-25 16:55:45 +0000639ObjCMethodDecl *ObjCMethodDecl::Create(
640 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
641 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
642 DeclContext *contextDecl, bool isInstance, bool isVariadic,
643 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
644 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000645 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000646 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000647 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
648 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000649}
650
Douglas Gregor72172e92012-01-05 21:55:30 +0000651ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000652 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
653 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000654}
655
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000656bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
657 return getMethodFamily() == OMF_init &&
658 hasAttr<ObjCDesignatedInitializerAttr>();
659}
660
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000661bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
662 const ObjCMethodDecl **InitMethod) const {
663 if (getMethodFamily() != OMF_init)
664 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000665 const DeclContext *DC = getDeclContext();
666 if (isa<ObjCProtocolDecl>(DC))
667 return false;
668 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000669 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000670 return false;
671}
672
Douglas Gregora6017bb2012-10-09 17:21:28 +0000673Stmt *ObjCMethodDecl::getBody() const {
674 return Body.get(getASTContext().getExternalSource());
675}
676
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000677void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
678 assert(PrevMethod);
679 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
680 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000681 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000682}
683
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000684void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
685 ArrayRef<ParmVarDecl*> Params,
686 ArrayRef<SourceLocation> SelLocs) {
687 ParamsAndSelLocs = 0;
688 NumParams = Params.size();
689 if (Params.empty() && SelLocs.empty())
690 return;
691
692 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
693 sizeof(SourceLocation) * SelLocs.size();
694 ParamsAndSelLocs = C.Allocate(Size);
695 std::copy(Params.begin(), Params.end(), getParams());
696 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
697}
698
699void ObjCMethodDecl::getSelectorLocs(
700 SmallVectorImpl<SourceLocation> &SelLocs) const {
701 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
702 SelLocs.push_back(getSelectorLoc(i));
703}
704
705void ObjCMethodDecl::setMethodParams(ASTContext &C,
706 ArrayRef<ParmVarDecl*> Params,
707 ArrayRef<SourceLocation> SelLocs) {
708 assert((!SelLocs.empty() || isImplicit()) &&
709 "No selector locs for non-implicit method");
710 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000711 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000712
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000713 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
714 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000715 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000716 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000717
718 setParamsAndSelLocs(C, Params, SelLocs);
719}
720
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000721/// \brief A definition will return its interface declaration.
722/// An interface declaration will return its definition.
723/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000724ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000725 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000726 ObjCMethodDecl *Redecl = 0;
727 if (HasRedeclaration)
728 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000729 if (Redecl)
730 return Redecl;
731
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000732 Decl *CtxD = cast<Decl>(getDeclContext());
733
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000734 if (!CtxD->isInvalidDecl()) {
735 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
736 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
737 if (!ImplD->isInvalidDecl())
738 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000739
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000740 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
741 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
742 if (!ImplD->isInvalidDecl())
743 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000744
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000745 } else if (ObjCImplementationDecl *ImplD =
746 dyn_cast<ObjCImplementationDecl>(CtxD)) {
747 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
748 if (!IFD->isInvalidDecl())
749 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000750
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000751 } else if (ObjCCategoryImplDecl *CImplD =
752 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
753 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
754 if (!CatD->isInvalidDecl())
755 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
756 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000757 }
758
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000759 if (!Redecl && isRedeclaration()) {
760 // This is the last redeclaration, go back to the first method.
761 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
762 isInstanceMethod());
763 }
764
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000765 return Redecl ? Redecl : this;
766}
767
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000768ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
769 Decl *CtxD = cast<Decl>(getDeclContext());
770
771 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
772 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
773 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
774 isInstanceMethod()))
775 return MD;
776
777 } else if (ObjCCategoryImplDecl *CImplD =
778 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000779 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000780 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
781 isInstanceMethod()))
782 return MD;
783 }
784
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000785 if (isRedeclaration())
786 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
787 isInstanceMethod());
788
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000789 return this;
790}
791
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000792SourceLocation ObjCMethodDecl::getLocEnd() const {
793 if (Stmt *Body = getBody())
794 return Body->getLocEnd();
795 return DeclEndLoc;
796}
797
John McCallb4526252011-03-02 01:50:55 +0000798ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
799 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000800 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000801 return family;
802
John McCall86bc21f2011-03-02 11:33:24 +0000803 // Check for an explicit attribute.
804 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
805 // The unfortunate necessity of mapping between enums here is due
806 // to the attributes framework.
807 switch (attr->getFamily()) {
808 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
809 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
810 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
811 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
812 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
813 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
814 }
815 Family = static_cast<unsigned>(family);
816 return family;
817 }
818
John McCallb4526252011-03-02 01:50:55 +0000819 family = getSelector().getMethodFamily();
820 switch (family) {
821 case OMF_None: break;
822
823 // init only has a conventional meaning for an instance method, and
824 // it has to return an object.
825 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000826 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000827 family = OMF_None;
828 break;
829
830 // alloc/copy/new have a conventional meaning for both class and
831 // instance methods, but they require an object return.
832 case OMF_alloc:
833 case OMF_copy:
834 case OMF_mutableCopy:
835 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000836 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000837 family = OMF_None;
838 break;
839
840 // These selectors have a conventional meaning only for instance methods.
841 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000842 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000843 case OMF_retain:
844 case OMF_release:
845 case OMF_autorelease:
846 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000847 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000848 if (!isInstanceMethod())
849 family = OMF_None;
850 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000851
852 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000853 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000854 family = OMF_None;
855 else {
856 unsigned noParams = param_size();
857 if (noParams < 1 || noParams > 3)
858 family = OMF_None;
859 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000860 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000861 QualType ArgT = (*it);
862 if (!ArgT->isObjCSelType()) {
863 family = OMF_None;
864 break;
865 }
866 while (--noParams) {
867 it++;
868 ArgT = (*it);
869 if (!ArgT->isObjCIdType()) {
870 family = OMF_None;
871 break;
872 }
873 }
874 }
875 }
876 break;
877
John McCallb4526252011-03-02 01:50:55 +0000878 }
879
880 // Cache the result.
881 Family = static_cast<unsigned>(family);
882 return family;
883}
884
Mike Stump11289f42009-09-09 15:08:12 +0000885void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000886 const ObjCInterfaceDecl *OID) {
887 QualType selfTy;
888 if (isInstanceMethod()) {
889 // There may be no interface context due to error in declaration
890 // of the interface (which has been reported). Recover gracefully.
891 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000892 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000893 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000894 } else {
895 selfTy = Context.getObjCIdType();
896 }
897 } else // we have a factory method.
898 selfTy = Context.getObjCClassType();
899
John McCalld4631322011-06-17 06:42:21 +0000900 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000901 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000902
David Blaikiebbafb8a2012-03-11 07:00:24 +0000903 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000904 if (isInstanceMethod()) {
905 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000906
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000907 // 'self' is always __strong. It's actually pseudo-strong except
908 // in init methods (or methods labeled ns_consumes_self), though.
909 Qualifiers qs;
910 qs.setObjCLifetime(Qualifiers::OCL_Strong);
911 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000912
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000913 // In addition, 'self' is const unless this is an init method.
914 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
915 selfTy = selfTy.withConst();
916 selfIsPseudoStrong = true;
917 }
918 }
919 else {
920 assert(isClassMethod());
921 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000922 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000923 selfIsPseudoStrong = true;
924 }
John McCall31168b02011-06-15 23:02:42 +0000925 }
926
927 ImplicitParamDecl *self
928 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
929 &Context.Idents.get("self"), selfTy);
930 setSelfDecl(self);
931
932 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000933 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000934
John McCalld4631322011-06-17 06:42:21 +0000935 if (selfIsPseudoStrong)
936 self->setARCPseudoStrong(true);
937
Mike Stump11289f42009-09-09 15:08:12 +0000938 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
939 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000940 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000941}
942
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000943ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
944 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
945 return ID;
946 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
947 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000948 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000949 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000950 if (isa<ObjCProtocolDecl>(getDeclContext()))
951 return 0;
David Blaikie83d382b2011-09-23 05:06:16 +0000952 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000953}
954
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000955static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
956 const ObjCMethodDecl *Method,
957 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
958 bool MovedToSuper) {
959 if (!Container)
960 return;
961
962 // In categories look for overriden methods from protocols. A method from
963 // category is not "overriden" since it is considered as the "same" method
964 // (same USR) as the one from the interface.
965 if (const ObjCCategoryDecl *
966 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
967 // Check whether we have a matching method at this category but only if we
968 // are at the super class level.
969 if (MovedToSuper)
970 if (ObjCMethodDecl *
971 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000972 Method->isInstanceMethod(),
973 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000974 if (Method != Overridden) {
975 // We found an override at this category; there is no need to look
976 // into its protocols.
977 Methods.push_back(Overridden);
978 return;
979 }
980
Aaron Ballman19a41762014-03-14 12:55:57 +0000981 for (const auto *P : Category->protocols())
982 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000983 return;
984 }
985
986 // Check whether we have a matching method at this level.
987 if (const ObjCMethodDecl *
988 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000989 Method->isInstanceMethod(),
990 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000991 if (Method != Overridden) {
992 // We found an override at this level; there is no need to look
993 // into other protocols or categories.
994 Methods.push_back(Overridden);
995 return;
996 }
997
998 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000999 for (const auto *P : Protocol->protocols())
1000 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001001 }
1002
1003 if (const ObjCInterfaceDecl *
1004 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001005 for (const auto *P : Interface->protocols())
1006 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001007
Aaron Ballman15063e12014-03-13 21:35:02 +00001008 for (const auto *Cat : Interface->known_categories())
1009 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001010
1011 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1012 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1013 /*MovedToSuper=*/true);
1014 }
1015}
1016
1017static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1018 const ObjCMethodDecl *Method,
1019 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1020 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1021 /*MovedToSuper=*/false);
1022}
1023
1024static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1025 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1026 assert(Method->isOverriding());
1027
1028 if (const ObjCProtocolDecl *
1029 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1030 CollectOverriddenMethods(ProtD, Method, overridden);
1031
1032 } else if (const ObjCImplDecl *
1033 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1034 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1035 if (!ID)
1036 return;
1037 // Start searching for overridden methods using the method from the
1038 // interface as starting point.
1039 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001040 Method->isInstanceMethod(),
1041 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001042 Method = IFaceMeth;
1043 CollectOverriddenMethods(ID, Method, overridden);
1044
1045 } else if (const ObjCCategoryDecl *
1046 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1047 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1048 if (!ID)
1049 return;
1050 // Start searching for overridden methods using the method from the
1051 // interface as starting point.
1052 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001053 Method->isInstanceMethod(),
1054 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001055 Method = IFaceMeth;
1056 CollectOverriddenMethods(ID, Method, overridden);
1057
1058 } else {
1059 CollectOverriddenMethods(
1060 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1061 Method, overridden);
1062 }
1063}
1064
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001065void ObjCMethodDecl::getOverriddenMethods(
1066 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1067 const ObjCMethodDecl *Method = this;
1068
1069 if (Method->isRedeclaration()) {
1070 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1071 getMethod(Method->getSelector(), Method->isInstanceMethod());
1072 }
1073
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001074 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001075 collectOverriddenMethodsSlow(Method, Overridden);
1076 assert(!Overridden.empty() &&
1077 "ObjCMethodDecl's overriding bit is not as expected");
1078 }
1079}
1080
Jordan Rose2bd991a2012-10-10 16:42:54 +00001081const ObjCPropertyDecl *
1082ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1083 Selector Sel = getSelector();
1084 unsigned NumArgs = Sel.getNumArgs();
1085 if (NumArgs > 1)
1086 return 0;
1087
Jordan Rose59e34ec2012-10-11 16:02:02 +00001088 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001089 return 0;
1090
1091 if (isPropertyAccessor()) {
1092 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001093 // If container is class extension, find its primary class.
1094 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1095 if (CatDecl->IsClassExtension())
1096 Container = CatDecl->getClassInterface();
1097
Jordan Rose2bd991a2012-10-10 16:42:54 +00001098 bool IsGetter = (NumArgs == 0);
1099
Aaron Ballmand174edf2014-03-13 19:11:50 +00001100 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001101 Selector NextSel = IsGetter ? I->getGetterName()
1102 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001103 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001104 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001105 }
1106
1107 llvm_unreachable("Marked as a property accessor but no property found!");
1108 }
1109
1110 if (!CheckOverrides)
1111 return 0;
1112
1113 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1114 OverridesTy Overrides;
1115 getOverriddenMethods(Overrides);
1116 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1117 I != E; ++I) {
1118 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1119 return Prop;
1120 }
1121
1122 return 0;
1123
1124}
1125
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001126//===----------------------------------------------------------------------===//
1127// ObjCInterfaceDecl
1128//===----------------------------------------------------------------------===//
1129
Douglas Gregord53ae832012-01-17 18:09:05 +00001130ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001131 DeclContext *DC,
1132 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001133 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001134 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001135 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001136 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001137 ObjCInterfaceDecl *Result = new (C, DC)
1138 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001139 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001140 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001141 return Result;
1142}
1143
Richard Smithf7981722013-11-22 09:01:48 +00001144ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001145 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001146 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1147 0, SourceLocation(),
1148 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001149 Result->Data.setInt(!C.getLangOpts().Modules);
1150 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001151}
1152
1153ObjCInterfaceDecl::
1154ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001155 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1156 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001157 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001158 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001159{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001160 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001161
1162 // Copy the 'data' pointer over.
1163 if (PrevDecl)
1164 Data = PrevDecl->Data;
1165
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001166 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001167}
1168
Douglas Gregor73693022010-12-01 23:49:52 +00001169void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001170 assert(data().ExternallyCompleted && "Class is not externally completed");
1171 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001172 getASTContext().getExternalSource()->CompleteType(
1173 const_cast<ObjCInterfaceDecl *>(this));
1174}
1175
1176void ObjCInterfaceDecl::setExternallyCompleted() {
1177 assert(getASTContext().getExternalSource() &&
1178 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001179 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001180 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001181 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001182}
1183
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001184void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001185 // Check for a complete definition and recover if not so.
1186 if (!isThisDeclarationADefinition())
1187 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001188 data().HasDesignatedInitializers = true;
1189}
1190
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001191bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001192 // Check for a complete definition and recover if not so.
1193 if (!isThisDeclarationADefinition())
1194 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001195 if (data().ExternallyCompleted)
1196 LoadExternalDefinition();
1197
1198 return data().HasDesignatedInitializers;
1199}
1200
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001201ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001202 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1203 if (data().ExternallyCompleted)
1204 LoadExternalDefinition();
1205
1206 return getASTContext().getObjCImplementation(
1207 const_cast<ObjCInterfaceDecl*>(Def));
1208 }
1209
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001210 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001211 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001212}
1213
1214void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001215 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001216}
1217
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001218namespace {
1219 struct SynthesizeIvarChunk {
1220 uint64_t Size;
1221 ObjCIvarDecl *Ivar;
1222 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1223 : Size(size), Ivar(ivar) {}
1224 };
1225
1226 bool operator<(const SynthesizeIvarChunk & LHS,
1227 const SynthesizeIvarChunk &RHS) {
1228 return LHS.Size < RHS.Size;
1229 }
1230}
1231
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001232/// all_declared_ivar_begin - return first ivar declared in this class,
1233/// its extensions and its implementation. Lazily build the list on first
1234/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001235///
1236/// Caveat: The list returned by this method reflects the current
1237/// state of the parser. The cache will be updated for every ivar
1238/// added by an extension or the implementation when they are
1239/// encountered.
1240/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001241ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001242 // FIXME: Should make sure no callers ever do this.
1243 if (!hasDefinition())
1244 return 0;
1245
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001246 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001247 if (!data().IvarList) {
1248 if (!ivar_empty()) {
1249 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1250 data().IvarList = *I; ++I;
1251 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001252 curIvar->setNextIvar(*I);
1253 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001254
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001255 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001256 if (!Ext->ivar_empty()) {
1257 ObjCCategoryDecl::ivar_iterator
1258 I = Ext->ivar_begin(),
1259 E = Ext->ivar_end();
1260 if (!data().IvarList) {
1261 data().IvarList = *I; ++I;
1262 curIvar = data().IvarList;
1263 }
1264 for ( ;I != E; curIvar = *I, ++I)
1265 curIvar->setNextIvar(*I);
1266 }
1267 }
1268 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001269 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001270
1271 // cached and complete!
1272 if (!data().IvarListMissingImplementation)
1273 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001274
1275 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001276 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001277 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001278 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001279 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001280 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1281 layout.push_back(SynthesizeIvarChunk(
1282 IV->getASTContext().getTypeSize(IV->getType()), IV));
1283 continue;
1284 }
1285 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001286 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001287 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001288 curIvar->setNextIvar(IV);
1289 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001290 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001291
1292 if (!layout.empty()) {
1293 // Order synthesized ivars by their size.
1294 std::stable_sort(layout.begin(), layout.end());
1295 unsigned Ix = 0, EIx = layout.size();
1296 if (!data().IvarList) {
1297 data().IvarList = layout[0].Ivar; Ix++;
1298 curIvar = data().IvarList;
1299 }
1300 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1301 curIvar->setNextIvar(layout[Ix].Ivar);
1302 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001303 }
1304 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001305 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001306}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001307
1308/// FindCategoryDeclaration - Finds category declaration in the list of
1309/// categories for this class and returns it. Name of the category is passed
1310/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001311///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001312ObjCCategoryDecl *
1313ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001314 // FIXME: Should make sure no callers ever do this.
1315 if (!hasDefinition())
1316 return 0;
1317
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001318 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001319 LoadExternalDefinition();
1320
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001321 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001322 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001323 return Cat;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001324
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001325 return 0;
1326}
1327
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001328ObjCMethodDecl *
1329ObjCInterfaceDecl::getCategoryInstanceMethod(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->getInstanceMethod(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
1339ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001340 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001341 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001342 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1343 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001344 }
1345
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001346 return 0;
1347}
1348
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001349/// ClassImplementsProtocol - Checks that 'lProto' protocol
1350/// has been implemented in IDecl class, its super class or categories (if
1351/// lookupCategory is true).
1352bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1353 bool lookupCategory,
1354 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001355 if (!hasDefinition())
1356 return false;
1357
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001358 ObjCInterfaceDecl *IDecl = this;
1359 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001360 for (auto *PI : IDecl->protocols()){
1361 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001362 return true;
1363 // This is dubious and is added to be compatible with gcc. In gcc, it is
1364 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1365 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1366 // object. This IMO, should be a bug.
1367 // FIXME: Treat this as an extension, and flag this as an error when GCC
1368 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001369 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001370 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001371 return true;
1372 }
Mike Stump11289f42009-09-09 15:08:12 +00001373
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001374 // 2nd, look up the category.
1375 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001376 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001377 for (auto *PI : Cat->protocols())
1378 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001379 return true;
1380 }
Mike Stump11289f42009-09-09 15:08:12 +00001381
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001382 // 3rd, look up the super class(s)
1383 if (IDecl->getSuperClass())
1384 return
1385 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1386 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001387
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001388 return false;
1389}
1390
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001391//===----------------------------------------------------------------------===//
1392// ObjCIvarDecl
1393//===----------------------------------------------------------------------===//
1394
David Blaikie68e081d2011-12-20 02:48:34 +00001395void ObjCIvarDecl::anchor() { }
1396
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001397ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001398 SourceLocation StartLoc,
1399 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001400 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001401 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001402 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001403 if (DC) {
1404 // Ivar's can only appear in interfaces, implementations (via synthesized
1405 // properties), and class extensions (via direct declaration, or synthesized
1406 // properties).
1407 //
1408 // FIXME: This should really be asserting this:
1409 // (isa<ObjCCategoryDecl>(DC) &&
1410 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1411 // but unfortunately we sometimes place ivars into non-class extension
1412 // categories on error. This breaks an AST invariant, and should not be
1413 // fixed.
1414 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1415 isa<ObjCCategoryDecl>(DC)) &&
1416 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001417 // Once a new ivar is created in any of class/class-extension/implementation
1418 // decl contexts, the previously built IvarList must be rebuilt.
1419 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1420 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001421 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001422 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001423 else
1424 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001425 }
1426 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001427 }
1428
Richard Smithf7981722013-11-22 09:01:48 +00001429 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001430 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001431}
1432
Douglas Gregor72172e92012-01-05 21:55:30 +00001433ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001434 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001435 QualType(), 0, ObjCIvarDecl::None, 0, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001436}
1437
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001438const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1439 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001440
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001441 switch (DC->getKind()) {
1442 default:
1443 case ObjCCategoryImpl:
1444 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001445 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001446
1447 // Ivars can only appear in class extension categories.
1448 case ObjCCategory: {
1449 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1450 assert(CD->IsClassExtension() && "invalid container for ivar!");
1451 return CD->getClassInterface();
1452 }
1453
1454 case ObjCImplementation:
1455 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1456
1457 case ObjCInterface:
1458 return cast<ObjCInterfaceDecl>(DC);
1459 }
1460}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001461
1462//===----------------------------------------------------------------------===//
1463// ObjCAtDefsFieldDecl
1464//===----------------------------------------------------------------------===//
1465
David Blaikie68e081d2011-12-20 02:48:34 +00001466void ObjCAtDefsFieldDecl::anchor() { }
1467
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001468ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001469*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1470 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001471 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001472 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001473}
1474
Richard Smithf7981722013-11-22 09:01:48 +00001475ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001476 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001477 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1478 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001479}
1480
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001481//===----------------------------------------------------------------------===//
1482// ObjCProtocolDecl
1483//===----------------------------------------------------------------------===//
1484
David Blaikie68e081d2011-12-20 02:48:34 +00001485void ObjCProtocolDecl::anchor() { }
1486
Douglas Gregor32c17572012-01-01 20:30:41 +00001487ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1488 SourceLocation nameLoc,
1489 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001490 ObjCProtocolDecl *PrevDecl)
1491 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001492{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001493 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001494 if (PrevDecl)
1495 Data = PrevDecl->Data;
1496}
1497
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001498ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001499 IdentifierInfo *Id,
1500 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001501 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001502 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001503 ObjCProtocolDecl *Result =
1504 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001505 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001506 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001507}
1508
Richard Smithf7981722013-11-22 09:01:48 +00001509ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001510 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001511 ObjCProtocolDecl *Result =
1512 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001513 Result->Data.setInt(!C.getLangOpts().Modules);
1514 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001515}
1516
Steve Naroff114aecb2009-03-01 16:12:44 +00001517ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1518 ObjCProtocolDecl *PDecl = this;
1519
1520 if (Name == getIdentifier())
1521 return PDecl;
1522
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001523 for (auto *I : protocols())
1524 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001525 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001526
Steve Naroff114aecb2009-03-01 16:12:44 +00001527 return NULL;
1528}
1529
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001530// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001531// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001532ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1533 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001534 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001535
Douglas Gregoreed49792013-01-17 00:38:46 +00001536 // If there is no definition or the definition is hidden, we don't find
1537 // anything.
1538 const ObjCProtocolDecl *Def = getDefinition();
1539 if (!Def || Def->isHidden())
1540 return NULL;
1541
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001542 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001543 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001544
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001545 for (const auto *I : protocols())
1546 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001547 return MethodDecl;
1548 return NULL;
1549}
1550
Douglas Gregore6e48b12012-01-01 19:29:29 +00001551void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001552 assert(!Data.getPointer() && "Protocol already has a definition!");
1553 Data.setPointer(new (getASTContext()) DefinitionData);
1554 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001555}
1556
1557void ObjCProtocolDecl::startDefinition() {
1558 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001559
1560 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001561 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001562 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001563}
1564
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001565void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1566 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001567
1568 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001569 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001570 // Insert into PM if not there already.
1571 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001572 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001573 }
1574 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001575 for (const auto *PI : PDecl->protocols())
1576 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001577 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001578}
1579
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001580
1581void ObjCProtocolDecl::collectInheritedProtocolProperties(
1582 const ObjCPropertyDecl *Property,
1583 ProtocolPropertyMap &PM) const {
1584 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1585 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001586 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001587 if (Prop == Property)
1588 continue;
1589 if (Prop->getIdentifier() == Property->getIdentifier()) {
1590 PM[PDecl] = Prop;
1591 MatchFound = true;
1592 break;
1593 }
1594 }
1595 // Scan through protocol's protocols which did not have a matching property.
1596 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001597 for (const auto *PI : PDecl->protocols())
1598 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001599 }
1600}
Anna Zaks673d76b2012-10-18 19:17:53 +00001601
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001602//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001603// ObjCCategoryDecl
1604//===----------------------------------------------------------------------===//
1605
David Blaikie68e081d2011-12-20 02:48:34 +00001606void ObjCCategoryDecl::anchor() { }
1607
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001608ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001609 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001610 SourceLocation ClassNameLoc,
1611 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001612 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001613 ObjCInterfaceDecl *IDecl,
1614 SourceLocation IvarLBraceLoc,
1615 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001616 ObjCCategoryDecl *CatDecl =
1617 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1618 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001619 if (IDecl) {
1620 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001621 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001622 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001623 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001624 if (ASTMutationListener *L = C.getASTMutationListener())
1625 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1626 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001627 }
1628
1629 return CatDecl;
1630}
1631
Richard Smithf7981722013-11-22 09:01:48 +00001632ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001633 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001634 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1635 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001636}
1637
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001638ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1639 return getASTContext().getObjCImplementation(
1640 const_cast<ObjCCategoryDecl*>(this));
1641}
1642
1643void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1644 getASTContext().setObjCImplementation(this, ImplD);
1645}
1646
1647
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001648//===----------------------------------------------------------------------===//
1649// ObjCCategoryImplDecl
1650//===----------------------------------------------------------------------===//
1651
David Blaikie68e081d2011-12-20 02:48:34 +00001652void ObjCCategoryImplDecl::anchor() { }
1653
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001654ObjCCategoryImplDecl *
1655ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001656 IdentifierInfo *Id,
1657 ObjCInterfaceDecl *ClassInterface,
1658 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001659 SourceLocation atStartLoc,
1660 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001661 if (ClassInterface && ClassInterface->hasDefinition())
1662 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001663 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1664 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001665}
1666
Douglas Gregor72172e92012-01-05 21:55:30 +00001667ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1668 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001669 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1670 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001671}
1672
Steve Narofff406f4d2009-10-29 21:11:04 +00001673ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001674 // The class interface might be NULL if we are working with invalid code.
1675 if (const ObjCInterfaceDecl *ID = getClassInterface())
1676 return ID->FindCategoryDeclaration(getIdentifier());
1677 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001678}
1679
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001680
David Blaikie68e081d2011-12-20 02:48:34 +00001681void ObjCImplDecl::anchor() { }
1682
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001683void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001684 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001685 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001686 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001687}
1688
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001689void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1690 ASTContext &Ctx = getASTContext();
1691
1692 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001693 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001694 if (IFace)
1695 Ctx.setObjCImplementation(IFace, ImplD);
1696
Duncan Sands49c29ee2009-07-21 07:56:29 +00001697 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001698 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1699 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1700 Ctx.setObjCImplementation(CD, ImplD);
1701 }
1702
1703 ClassInterface = IFace;
1704}
1705
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001706/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001707/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001708/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001709///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001710ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001711FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001712 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001713 if (PID->getPropertyIvarDecl() &&
1714 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1715 return PID;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001716 return 0;
1717}
1718
1719/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001720/// added to the list of those properties \@synthesized/\@dynamic in this
1721/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001722///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001723ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001724FindPropertyImplDecl(IdentifierInfo *Id) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001725 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001726 if (PID->getPropertyDecl()->getIdentifier() == Id)
1727 return PID;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001728 return 0;
1729}
1730
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001731raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001732 const ObjCCategoryImplDecl &CID) {
1733 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001734 return OS;
1735}
1736
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001737//===----------------------------------------------------------------------===//
1738// ObjCImplementationDecl
1739//===----------------------------------------------------------------------===//
1740
David Blaikie68e081d2011-12-20 02:48:34 +00001741void ObjCImplementationDecl::anchor() { }
1742
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001743ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001744ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001745 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001746 ObjCInterfaceDecl *SuperDecl,
1747 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001748 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001749 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001750 SourceLocation IvarLBraceLoc,
1751 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001752 if (ClassInterface && ClassInterface->hasDefinition())
1753 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001754 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1755 nameLoc, atStartLoc, superLoc,
1756 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001757}
1758
Douglas Gregor72172e92012-01-05 21:55:30 +00001759ObjCImplementationDecl *
1760ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001761 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1762 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001763}
1764
John McCall0410e572011-07-22 04:15:06 +00001765void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1766 CXXCtorInitializer ** initializers,
1767 unsigned numInitializers) {
1768 if (numInitializers > 0) {
1769 NumIvarInitializers = numInitializers;
1770 CXXCtorInitializer **ivarInitializers =
1771 new (C) CXXCtorInitializer*[NumIvarInitializers];
1772 memcpy(ivarInitializers, initializers,
1773 numInitializers * sizeof(CXXCtorInitializer*));
1774 IvarInitializers = ivarInitializers;
1775 }
1776}
1777
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001778raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001779 const ObjCImplementationDecl &ID) {
1780 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001781 return OS;
1782}
1783
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001784//===----------------------------------------------------------------------===//
1785// ObjCCompatibleAliasDecl
1786//===----------------------------------------------------------------------===//
1787
David Blaikie68e081d2011-12-20 02:48:34 +00001788void ObjCCompatibleAliasDecl::anchor() { }
1789
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001790ObjCCompatibleAliasDecl *
1791ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1792 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001793 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001794 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001795 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001796}
1797
Douglas Gregor72172e92012-01-05 21:55:30 +00001798ObjCCompatibleAliasDecl *
1799ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001800 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001801}
1802
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001803//===----------------------------------------------------------------------===//
1804// ObjCPropertyDecl
1805//===----------------------------------------------------------------------===//
1806
David Blaikie68e081d2011-12-20 02:48:34 +00001807void ObjCPropertyDecl::anchor() { }
1808
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001809ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1810 SourceLocation L,
1811 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001812 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001813 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001814 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001815 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001816 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001817}
1818
Richard Smithf7981722013-11-22 09:01:48 +00001819ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001820 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001821 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1822 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001823}
1824
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001825//===----------------------------------------------------------------------===//
1826// ObjCPropertyImplDecl
1827//===----------------------------------------------------------------------===//
1828
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001829ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001830 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001831 SourceLocation atLoc,
1832 SourceLocation L,
1833 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001834 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001835 ObjCIvarDecl *ivar,
1836 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001837 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1838 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001839}
Chris Lattnered0e1642008-03-17 01:19:02 +00001840
Richard Smithf7981722013-11-22 09:01:48 +00001841ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001842 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001843 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1844 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001845}
1846
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001847SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1848 SourceLocation EndLoc = getLocation();
1849 if (IvarLoc.isValid())
1850 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001851
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001852 return SourceRange(AtLoc, EndLoc);
1853}