blob: c53dba3bfdfdb980dbb40d57d9c36af0e30f161a [file] [log] [blame]
Chris Lattner1e03a562008-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 Kyrtzidise6b8d682011-09-01 00:58:55 +000016#include "clang/AST/ASTMutationListener.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/Stmt.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000019#include "llvm/ADT/STLExtras.h"
Anna Zaksad0ce532012-09-27 19:45:11 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner1e03a562008-03-16 00:19:01 +000021using namespace clang;
22
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000023//===----------------------------------------------------------------------===//
Chris Lattner11e1e1a2009-02-20 21:16:26 +000024// ObjCListBase
25//===----------------------------------------------------------------------===//
26
Chris Lattner38af2de2009-02-20 21:35:13 +000027void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Douglas Gregorff331c12010-07-25 18:17:45 +000028 List = 0;
Chris Lattner11e1e1a2009-02-20 21:16:26 +000029 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump1eb44332009-09-09 15:08:12 +000030
31
Chris Lattner4ee413b2009-02-20 21:44:01 +000032 List = new (Ctx) void*[Elts];
Chris Lattner11e1e1a2009-02-20 21:16:26 +000033 NumElts = Elts;
34 memcpy(List, InList, sizeof(void*)*Elts);
35}
36
Douglas Gregor18df52b2010-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 Lattner11e1e1a2009-02-20 21:16:26 +000047//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +000048// ObjCInterfaceDecl
Chris Lattner6c4ae5d2008-03-16 00:49:28 +000049//===----------------------------------------------------------------------===//
50
David Blaikie99ba9e32011-12-20 02:48:34 +000051void ObjCContainerDecl::anchor() { }
52
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000053/// getIvarDecl - This method looks up an ivar in this ContextDecl.
54///
55ObjCIvarDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000056ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
David Blaikie3bc93e32012-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 Jahanian496b5a82009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
63 return 0;
64}
65
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000066// Get the local instance/class method declared in this interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +000067ObjCMethodDecl *
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69 bool AllowHidden) const {
Douglas Gregor0f9b9f32013-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 Kyrtzidis04593d02013-03-29 21:51:48 +000074 if (Def->isHidden() && !AllowHidden)
Douglas Gregor0f9b9f32013-01-17 00:38:46 +000075 return 0;
76 }
77
Steve Naroff0de21fd2009-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 Blaikie3bc93e32012-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 Naroff0de21fd2009-02-22 19:35:57 +000089 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis467c0b12009-07-25 22:15:22 +000090 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroff0de21fd2009-02-22 19:35:57 +000091 return MD;
92 }
Steve Naroff0701bbb2009-01-08 17:28:14 +000093 return 0;
94}
95
Fariborz Jahanian5bdaef52013-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.
Stephen Hines651f13c2014-04-23 16:59:28 -0700115 for (const auto *Cat : ID->visible_categories()) {
Fariborz Jahanian5bdaef52013-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).
Stephen Hines651f13c2014-04-23 16:59:28 -0700124 for (const auto *P : Cat->properties())
Fariborz Jahanian5bdaef52013-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.
Stephen Hines651f13c2014-04-23 16:59:28 -0700133 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +0000134 if (Proto->HasUserDeclaredSetterMethod(Property))
135 return true;
Stephen Hines651f13c2014-04-23 16:59:28 -0700136
Fariborz Jahanian5bdaef52013-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))
Stephen Hines651f13c2014-04-23 16:59:28 -0700146 for (const auto *PI : PD->protocols())
147 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +0000148 return true;
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +0000149 return false;
150}
151
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000152ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000153ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000154 IdentifierInfo *propertyID) {
Douglas Gregor0f9b9f32013-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 Kremenek9f550ff2010-03-15 20:11:46 +0000162
David Blaikie3bc93e32012-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 Kremenek9f550ff2010-03-15 20:11:46 +0000166 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
167 return PD;
168
169 return 0;
170}
171
Anna Zaksad0ce532012-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 Jahanian559c0c42008-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 Jahanian559c0c42008-04-21 19:04:53 +0000184ObjCPropertyDecl *
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000185ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregor0f9b9f32013-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 Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000193 if (ObjCPropertyDecl *PD =
194 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
195 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000197 switch (getKind()) {
198 default:
199 break;
200 case Decl::ObjCProtocol: {
201 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
Stephen Hines651f13c2014-04-23 16:59:28 -0700202 for (const auto *I : PID->protocols())
203 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahanian25760612010-02-15 21:55:26 +0000204 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000205 break;
206 }
207 case Decl::ObjCInterface: {
208 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregord3297242013-01-16 23:00:23 +0000209 // Look through categories (but not extensions).
Stephen Hines651f13c2014-04-23 16:59:28 -0700210 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000211 if (!Cat->IsClassExtension())
212 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
213 return P;
Douglas Gregord3297242013-01-16 23:00:23 +0000214 }
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000215
216 // Look through protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -0700217 for (const auto *I : OID->all_referenced_protocols())
218 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekde09d0c2010-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())
Stephen Hines651f13c2014-04-23 16:59:28 -0700230 for (const auto *I : OCD->protocols())
231 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
232 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000233 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000234 }
235 }
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000236 return 0;
237}
238
David Blaikie99ba9e32011-12-20 02:48:34 +0000239void ObjCInterfaceDecl::anchor() { }
240
Fariborz Jahaniana6f14e12009-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 Kremenek37cafb02010-03-15 20:30:07 +0000243/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000244///
245ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000246ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000247 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-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 Gregor26ac3f32010-12-01 23:49:52 +0000253 LoadExternalDefinition();
254
Ted Kremenek37cafb02010-03-15 20:30:07 +0000255 if (ObjCPropertyDecl *PD =
256 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
257 return PD;
258
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000259 // Look through protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -0700260 for (const auto *I : all_referenced_protocols())
261 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000262 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000263
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000264 return 0;
265}
266
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000267void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
268 PropertyDeclOrder &PO) const {
Stephen Hines651f13c2014-04-23 16:59:28 -0700269 for (auto *Prop : properties()) {
Anna Zaksb36ea372012-10-18 19:17:53 +0000270 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000271 PO.push_back(Prop);
Anna Zaksb36ea372012-10-18 19:17:53 +0000272 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700273 for (const auto *PI : all_referenced_protocols())
274 PI->collectPropertiesToImplement(PM, PO);
Anna Zakse63aedd2012-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 Zaksb36ea372012-10-18 19:17:53 +0000278}
279
Benjamin Kramer2fa67ef2012-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 Jahanian339798e2009-10-05 20:41:32 +0000300void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
301 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
302 ASTContext &C)
303{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000304 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000305 LoadExternalDefinition();
306
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000307 if (data().AllReferencedProtocols.empty() &&
308 data().ReferencedProtocols.empty()) {
309 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000310 return;
311 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000312
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000313 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000314 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000315 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000316 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000317 for (unsigned i = 0; i < ExtNum; i++) {
318 bool protocolExists = false;
319 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Stephen Hines651f13c2014-04-23 16:59:28 -0700320 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian339798e2009-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 Kremenek53b94412010-09-01 01:21:15 +0000328 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000329 ProtocolRefs.push_back(ProtoInExtension);
330 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000331
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000332 if (ProtocolRefs.empty())
333 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000334
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000335 // Merge ProtocolRefs into class's protocol list;
Stephen Hines651f13c2014-04-23 16:59:28 -0700336 for (auto *P : all_referenced_protocols()) {
337 ProtocolRefs.push_back(P);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000338 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000339
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000340 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000341}
342
Stephen Hines651f13c2014-04-23 16:59:28 -0700343const 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
356static 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 }
367 return false;
368}
369
370bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
371 switch (data().InheritedDesignatedInitializers) {
372 case DefinitionData::IDI_Inherited:
373 return true;
374 case DefinitionData::IDI_NotInherited:
375 return false;
376 case DefinitionData::IDI_Unknown: {
377 // If the class introduced initializers we conservatively assume that we
378 // don't know if any of them is a designated initializer to avoid possible
379 // misleading warnings.
380 if (isIntroducingInitializers(this)) {
381 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
382 return false;
383 } else {
384 data().InheritedDesignatedInitializers = DefinitionData::IDI_Inherited;
385 return true;
386 }
387 }
388 }
389
390 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
391}
392
393void ObjCInterfaceDecl::getDesignatedInitializers(
394 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
395 // Check for a complete definition and recover if not so.
396 if (!isThisDeclarationADefinition())
397 return;
398 if (data().ExternallyCompleted)
399 LoadExternalDefinition();
400
401 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
402 if (!IFace)
403 return;
404
405 for (const auto *MD : IFace->instance_methods())
406 if (MD->isThisDeclarationADesignatedInitializer())
407 Methods.push_back(MD);
408 for (const auto *Ext : IFace->visible_extensions()) {
409 for (const auto *MD : Ext->instance_methods())
410 if (MD->isThisDeclarationADesignatedInitializer())
411 Methods.push_back(MD);
412 }
413}
414
415bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
416 const ObjCMethodDecl **InitMethod) const {
417 // Check for a complete definition and recover if not so.
418 if (!isThisDeclarationADefinition())
419 return false;
420 if (data().ExternallyCompleted)
421 LoadExternalDefinition();
422
423 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
424 if (!IFace)
425 return false;
426
427 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
428 if (MD->isThisDeclarationADesignatedInitializer()) {
429 if (InitMethod)
430 *InitMethod = MD;
431 return true;
432 }
433 }
434 for (const auto *Ext : IFace->visible_extensions()) {
435 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
436 if (MD->isThisDeclarationADesignatedInitializer()) {
437 if (InitMethod)
438 *InitMethod = MD;
439 return true;
440 }
441 }
442 }
443 return false;
444}
445
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000446void ObjCInterfaceDecl::allocateDefinitionData() {
447 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor6bd99292013-02-09 01:35:03 +0000448 Data.setPointer(new (getASTContext()) DefinitionData());
449 Data.getPointer()->Definition = this;
Douglas Gregor8d2dbbf2011-12-16 16:34:57 +0000450
451 // Make the type point at the definition, now that we have one.
452 if (TypeForDecl)
453 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregor0af55012011-12-16 03:12:41 +0000454}
455
456void ObjCInterfaceDecl::startDefinition() {
457 allocateDefinitionData();
458
Douglas Gregor53df7a12011-12-15 18:03:09 +0000459 // Update all of the declarations with a pointer to the definition.
Stephen Hines651f13c2014-04-23 16:59:28 -0700460 for (auto RD : redecls()) {
461 if (RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000462 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000463 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000464}
465
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000466ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
467 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000468 // FIXME: Should make sure no callers ever do this.
469 if (!hasDefinition())
470 return 0;
471
472 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000473 LoadExternalDefinition();
474
Chris Lattner1e03a562008-03-16 00:19:01 +0000475 ObjCInterfaceDecl* ClassDecl = this;
476 while (ClassDecl != NULL) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000477 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000478 clsDeclared = ClassDecl;
479 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000480 }
Douglas Gregord3297242013-01-16 23:00:23 +0000481
Stephen Hines651f13c2014-04-23 16:59:28 -0700482 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregord3297242013-01-16 23:00:23 +0000483 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000484 clsDeclared = ClassDecl;
485 return I;
486 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000487 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000488
Chris Lattner1e03a562008-03-16 00:19:01 +0000489 ClassDecl = ClassDecl->getSuperClass();
490 }
491 return NULL;
492}
493
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000494/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
495/// class whose name is passed as argument. If it is not one of the super classes
496/// the it returns NULL.
497ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
498 const IdentifierInfo*ICName) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000499 // FIXME: Should make sure no callers ever do this.
500 if (!hasDefinition())
501 return 0;
502
503 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000504 LoadExternalDefinition();
505
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000506 ObjCInterfaceDecl* ClassDecl = this;
507 while (ClassDecl != NULL) {
508 if (ClassDecl->getIdentifier() == ICName)
509 return ClassDecl;
510 ClassDecl = ClassDecl->getSuperClass();
511 }
512 return NULL;
513}
514
Fariborz Jahanian07b1bbe2013-07-10 21:30:22 +0000515ObjCProtocolDecl *
516ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700517 for (auto *P : all_referenced_protocols())
518 if (P->lookupProtocolNamed(Name))
519 return P;
Fariborz Jahanian07b1bbe2013-07-10 21:30:22 +0000520 ObjCInterfaceDecl *SuperClass = getSuperClass();
521 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
522}
523
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000524/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000525/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000526/// When argument category "C" is specified, any implicit method found
527/// in this category is ignored.
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000528ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Stephen Hines651f13c2014-04-23 16:59:28 -0700529 bool isInstance,
530 bool shallowCategoryLookup,
531 bool followSuper,
532 const ObjCCategoryDecl *C) const
533{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000534 // FIXME: Should make sure no callers ever do this.
535 if (!hasDefinition())
536 return 0;
537
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000538 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner1e03a562008-03-16 00:19:01 +0000539 ObjCMethodDecl *MethodDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000541 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000542 LoadExternalDefinition();
543
Stephen Hines651f13c2014-04-23 16:59:28 -0700544 while (ClassDecl) {
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000545 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000546 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner1e03a562008-03-16 00:19:01 +0000548 // Didn't find one yet - look through protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -0700549 for (const auto *I : ClassDecl->protocols())
550 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000551 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000552
553 // Didn't find one yet - now look through categories.
Stephen Hines651f13c2014-04-23 16:59:28 -0700554 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000555 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Stephen Hines651f13c2014-04-23 16:59:28 -0700556 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +0000557 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000558
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000559 if (!shallowCategoryLookup) {
560 // Didn't find one yet - look through protocols.
561 const ObjCList<ObjCProtocolDecl> &Protocols =
562 Cat->getReferencedProtocols();
563 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
564 E = Protocols.end(); I != E; ++I)
565 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Stephen Hines651f13c2014-04-23 16:59:28 -0700566 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +0000567 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000568 }
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000569 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700570
571 if (!followSuper)
572 return NULL;
573
574 // Get the super class (if any).
Chris Lattner1e03a562008-03-16 00:19:01 +0000575 ClassDecl = ClassDecl->getSuperClass();
576 }
577 return NULL;
578}
579
Anna Zakse61354b2012-07-27 19:07:44 +0000580// Will search "local" class/category implementations for a method decl.
581// If failed, then we search in class's root for an instance method.
582// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000583ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
584 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000585 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000586 // FIXME: Should make sure no callers ever do this.
587 if (!hasDefinition())
588 return 0;
589
590 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000591 LoadExternalDefinition();
592
Steve Naroffd789d3d2009-10-01 23:46:04 +0000593 ObjCMethodDecl *Method = 0;
594 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000595 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
596 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000597
598 // Look through local category implementations associated with the class.
599 if (!Method)
600 Method = Instance ? getCategoryInstanceMethod(Sel)
601 : getCategoryClassMethod(Sel);
602
603 // Before we give up, check if the selector is an instance method.
604 // But only in the root. This matches gcc's behavior and what the
605 // runtime expects.
606 if (!Instance && !Method && !getSuperClass()) {
607 Method = lookupInstanceMethod(Sel);
608 // Look through local category implementations associated
609 // with the root class.
610 if (!Method)
611 Method = lookupPrivateMethod(Sel, true);
612 }
613
Steve Naroffd789d3d2009-10-01 23:46:04 +0000614 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000615 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000616 return Method;
617}
Chris Lattnerab351632009-02-20 20:59:54 +0000618
619//===----------------------------------------------------------------------===//
620// ObjCMethodDecl
621//===----------------------------------------------------------------------===//
622
Stephen Hines651f13c2014-04-23 16:59:28 -0700623ObjCMethodDecl *ObjCMethodDecl::Create(
624 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
625 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
626 DeclContext *contextDecl, bool isInstance, bool isVariadic,
627 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
628 ImplementationControl impControl, bool HasRelatedResultType) {
629 return new (C, contextDecl) ObjCMethodDecl(
630 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
631 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
632 impControl, HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000633}
634
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000635ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700636 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
637 Selector(), QualType(), 0, 0);
638}
639
640bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
641 return getMethodFamily() == OMF_init &&
642 hasAttr<ObjCDesignatedInitializerAttr>();
643}
644
645bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
646 const ObjCMethodDecl **InitMethod) const {
647 if (getMethodFamily() != OMF_init)
648 return false;
649 const DeclContext *DC = getDeclContext();
650 if (isa<ObjCProtocolDecl>(DC))
651 return false;
652 if (const ObjCInterfaceDecl *ID = getClassInterface())
653 return ID->isDesignatedInitializer(getSelector(), InitMethod);
654 return false;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000655}
656
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000657Stmt *ObjCMethodDecl::getBody() const {
658 return Body.get(getASTContext().getExternalSource());
659}
660
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000661void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
662 assert(PrevMethod);
663 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
664 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000665 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000666}
667
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000668void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
669 ArrayRef<ParmVarDecl*> Params,
670 ArrayRef<SourceLocation> SelLocs) {
671 ParamsAndSelLocs = 0;
672 NumParams = Params.size();
673 if (Params.empty() && SelLocs.empty())
674 return;
675
676 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
677 sizeof(SourceLocation) * SelLocs.size();
678 ParamsAndSelLocs = C.Allocate(Size);
679 std::copy(Params.begin(), Params.end(), getParams());
680 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
681}
682
683void ObjCMethodDecl::getSelectorLocs(
684 SmallVectorImpl<SourceLocation> &SelLocs) const {
685 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
686 SelLocs.push_back(getSelectorLoc(i));
687}
688
689void ObjCMethodDecl::setMethodParams(ASTContext &C,
690 ArrayRef<ParmVarDecl*> Params,
691 ArrayRef<SourceLocation> SelLocs) {
692 assert((!SelLocs.empty() || isImplicit()) &&
693 "No selector locs for non-implicit method");
694 if (isImplicit())
Dmitri Gribenko55431692013-05-05 00:41:58 +0000695 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000696
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000697 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
698 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000699 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko55431692013-05-05 00:41:58 +0000700 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000701
702 setParamsAndSelLocs(C, Params, SelLocs);
703}
704
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000705/// \brief A definition will return its interface declaration.
706/// An interface declaration will return its definition.
707/// Otherwise it will return itself.
708ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
709 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000710 ObjCMethodDecl *Redecl = 0;
711 if (HasRedeclaration)
712 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000713 if (Redecl)
714 return Redecl;
715
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000716 Decl *CtxD = cast<Decl>(getDeclContext());
717
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000718 if (!CtxD->isInvalidDecl()) {
719 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
720 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
721 if (!ImplD->isInvalidDecl())
722 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000723
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000724 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
725 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
726 if (!ImplD->isInvalidDecl())
727 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000728
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000729 } else if (ObjCImplementationDecl *ImplD =
730 dyn_cast<ObjCImplementationDecl>(CtxD)) {
731 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
732 if (!IFD->isInvalidDecl())
733 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000734
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000735 } else if (ObjCCategoryImplDecl *CImplD =
736 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
737 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
738 if (!CatD->isInvalidDecl())
739 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
740 }
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000741 }
742
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000743 if (!Redecl && isRedeclaration()) {
744 // This is the last redeclaration, go back to the first method.
745 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
746 isInstanceMethod());
747 }
748
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000749 return Redecl ? Redecl : this;
750}
751
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000752ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
753 Decl *CtxD = cast<Decl>(getDeclContext());
754
755 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
756 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
757 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
758 isInstanceMethod()))
759 return MD;
760
761 } else if (ObjCCategoryImplDecl *CImplD =
762 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000763 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000764 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
765 isInstanceMethod()))
766 return MD;
767 }
768
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000769 if (isRedeclaration())
770 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
771 isInstanceMethod());
772
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000773 return this;
774}
775
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000776SourceLocation ObjCMethodDecl::getLocEnd() const {
777 if (Stmt *Body = getBody())
778 return Body->getLocEnd();
779 return DeclEndLoc;
780}
781
John McCall85f3d762011-03-02 01:50:55 +0000782ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
783 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000784 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000785 return family;
786
John McCalld5313b02011-03-02 11:33:24 +0000787 // Check for an explicit attribute.
788 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
789 // The unfortunate necessity of mapping between enums here is due
790 // to the attributes framework.
791 switch (attr->getFamily()) {
792 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
793 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
794 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
795 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
796 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
797 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
798 }
799 Family = static_cast<unsigned>(family);
800 return family;
801 }
802
John McCall85f3d762011-03-02 01:50:55 +0000803 family = getSelector().getMethodFamily();
804 switch (family) {
805 case OMF_None: break;
806
807 // init only has a conventional meaning for an instance method, and
808 // it has to return an object.
809 case OMF_init:
Stephen Hines651f13c2014-04-23 16:59:28 -0700810 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCall85f3d762011-03-02 01:50:55 +0000811 family = OMF_None;
812 break;
813
814 // alloc/copy/new have a conventional meaning for both class and
815 // instance methods, but they require an object return.
816 case OMF_alloc:
817 case OMF_copy:
818 case OMF_mutableCopy:
819 case OMF_new:
Stephen Hines651f13c2014-04-23 16:59:28 -0700820 if (!getReturnType()->isObjCObjectPointerType())
John McCall85f3d762011-03-02 01:50:55 +0000821 family = OMF_None;
822 break;
823
824 // These selectors have a conventional meaning only for instance methods.
825 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000826 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000827 case OMF_retain:
828 case OMF_release:
829 case OMF_autorelease:
830 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000831 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000832 if (!isInstanceMethod())
833 family = OMF_None;
834 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000835
836 case OMF_performSelector:
Stephen Hines651f13c2014-04-23 16:59:28 -0700837 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000838 family = OMF_None;
839 else {
840 unsigned noParams = param_size();
841 if (noParams < 1 || noParams > 3)
842 family = OMF_None;
843 else {
Stephen Hines651f13c2014-04-23 16:59:28 -0700844 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000845 QualType ArgT = (*it);
846 if (!ArgT->isObjCSelType()) {
847 family = OMF_None;
848 break;
849 }
850 while (--noParams) {
851 it++;
852 ArgT = (*it);
853 if (!ArgT->isObjCIdType()) {
854 family = OMF_None;
855 break;
856 }
857 }
858 }
859 }
860 break;
861
John McCall85f3d762011-03-02 01:50:55 +0000862 }
863
864 // Cache the result.
865 Family = static_cast<unsigned>(family);
866 return family;
867}
868
Mike Stump1eb44332009-09-09 15:08:12 +0000869void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000870 const ObjCInterfaceDecl *OID) {
871 QualType selfTy;
872 if (isInstanceMethod()) {
873 // There may be no interface context due to error in declaration
874 // of the interface (which has been reported). Recover gracefully.
875 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000876 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000877 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000878 } else {
879 selfTy = Context.getObjCIdType();
880 }
881 } else // we have a factory method.
882 selfTy = Context.getObjCClassType();
883
John McCall7acddac2011-06-17 06:42:21 +0000884 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000885 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000886
David Blaikie4e4d0842012-03-11 07:00:24 +0000887 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000888 if (isInstanceMethod()) {
889 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000890
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000891 // 'self' is always __strong. It's actually pseudo-strong except
892 // in init methods (or methods labeled ns_consumes_self), though.
893 Qualifiers qs;
894 qs.setObjCLifetime(Qualifiers::OCL_Strong);
895 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000896
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000897 // In addition, 'self' is const unless this is an init method.
898 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
899 selfTy = selfTy.withConst();
900 selfIsPseudoStrong = true;
901 }
902 }
903 else {
904 assert(isClassMethod());
905 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000906 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000907 selfIsPseudoStrong = true;
908 }
John McCallf85e1932011-06-15 23:02:42 +0000909 }
910
911 ImplicitParamDecl *self
912 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
913 &Context.Idents.get("self"), selfTy);
914 setSelfDecl(self);
915
916 if (selfIsConsumed)
Stephen Hines651f13c2014-04-23 16:59:28 -0700917 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000918
John McCall7acddac2011-06-17 06:42:21 +0000919 if (selfIsPseudoStrong)
920 self->setARCPseudoStrong(true);
921
Mike Stump1eb44332009-09-09 15:08:12 +0000922 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
923 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000924 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000925}
926
Chris Lattnerab351632009-02-20 20:59:54 +0000927ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
928 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
929 return ID;
930 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
931 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000932 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000933 return IMD->getClassInterface();
Stephen Hines651f13c2014-04-23 16:59:28 -0700934 if (isa<ObjCProtocolDecl>(getDeclContext()))
935 return 0;
David Blaikieb219cfc2011-09-23 05:06:16 +0000936 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000937}
938
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000939static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
940 const ObjCMethodDecl *Method,
941 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
942 bool MovedToSuper) {
943 if (!Container)
944 return;
945
946 // In categories look for overriden methods from protocols. A method from
947 // category is not "overriden" since it is considered as the "same" method
948 // (same USR) as the one from the interface.
949 if (const ObjCCategoryDecl *
950 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
951 // Check whether we have a matching method at this category but only if we
952 // are at the super class level.
953 if (MovedToSuper)
954 if (ObjCMethodDecl *
955 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000956 Method->isInstanceMethod(),
957 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000958 if (Method != Overridden) {
959 // We found an override at this category; there is no need to look
960 // into its protocols.
961 Methods.push_back(Overridden);
962 return;
963 }
964
Stephen Hines651f13c2014-04-23 16:59:28 -0700965 for (const auto *P : Category->protocols())
966 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000967 return;
968 }
969
970 // Check whether we have a matching method at this level.
971 if (const ObjCMethodDecl *
972 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000973 Method->isInstanceMethod(),
974 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000975 if (Method != Overridden) {
976 // We found an override at this level; there is no need to look
977 // into other protocols or categories.
978 Methods.push_back(Overridden);
979 return;
980 }
981
982 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Stephen Hines651f13c2014-04-23 16:59:28 -0700983 for (const auto *P : Protocol->protocols())
984 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000985 }
986
987 if (const ObjCInterfaceDecl *
988 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700989 for (const auto *P : Interface->protocols())
990 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000991
Stephen Hines651f13c2014-04-23 16:59:28 -0700992 for (const auto *Cat : Interface->known_categories())
993 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000994
995 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
996 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
997 /*MovedToSuper=*/true);
998 }
999}
1000
1001static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1002 const ObjCMethodDecl *Method,
1003 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1004 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1005 /*MovedToSuper=*/false);
1006}
1007
1008static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1009 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1010 assert(Method->isOverriding());
1011
1012 if (const ObjCProtocolDecl *
1013 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1014 CollectOverriddenMethods(ProtD, Method, overridden);
1015
1016 } else if (const ObjCImplDecl *
1017 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1018 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1019 if (!ID)
1020 return;
1021 // Start searching for overridden methods using the method from the
1022 // interface as starting point.
1023 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00001024 Method->isInstanceMethod(),
1025 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001026 Method = IFaceMeth;
1027 CollectOverriddenMethods(ID, Method, overridden);
1028
1029 } else if (const ObjCCategoryDecl *
1030 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1031 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1032 if (!ID)
1033 return;
1034 // Start searching for overridden methods using the method from the
1035 // interface as starting point.
1036 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00001037 Method->isInstanceMethod(),
1038 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001039 Method = IFaceMeth;
1040 CollectOverriddenMethods(ID, Method, overridden);
1041
1042 } else {
1043 CollectOverriddenMethods(
1044 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1045 Method, overridden);
1046 }
1047}
1048
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001049void ObjCMethodDecl::getOverriddenMethods(
1050 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1051 const ObjCMethodDecl *Method = this;
1052
1053 if (Method->isRedeclaration()) {
1054 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1055 getMethod(Method->getSelector(), Method->isInstanceMethod());
1056 }
1057
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00001058 if (Method->isOverriding()) {
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001059 collectOverriddenMethodsSlow(Method, Overridden);
1060 assert(!Overridden.empty() &&
1061 "ObjCMethodDecl's overriding bit is not as expected");
1062 }
1063}
1064
Jordan Rose04bec392012-10-10 16:42:54 +00001065const ObjCPropertyDecl *
1066ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1067 Selector Sel = getSelector();
1068 unsigned NumArgs = Sel.getNumArgs();
1069 if (NumArgs > 1)
1070 return 0;
1071
Jordan Rose50d2b262012-10-11 16:02:02 +00001072 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose04bec392012-10-10 16:42:54 +00001073 return 0;
1074
1075 if (isPropertyAccessor()) {
1076 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanianc328d9c2013-01-12 00:28:34 +00001077 // If container is class extension, find its primary class.
1078 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1079 if (CatDecl->IsClassExtension())
1080 Container = CatDecl->getClassInterface();
1081
Jordan Rose04bec392012-10-10 16:42:54 +00001082 bool IsGetter = (NumArgs == 0);
1083
Stephen Hines651f13c2014-04-23 16:59:28 -07001084 for (const auto *I : Container->properties()) {
1085 Selector NextSel = IsGetter ? I->getGetterName()
1086 : I->getSetterName();
Jordan Rose04bec392012-10-10 16:42:54 +00001087 if (NextSel == Sel)
Stephen Hines651f13c2014-04-23 16:59:28 -07001088 return I;
Jordan Rose04bec392012-10-10 16:42:54 +00001089 }
1090
1091 llvm_unreachable("Marked as a property accessor but no property found!");
1092 }
1093
1094 if (!CheckOverrides)
1095 return 0;
1096
1097 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1098 OverridesTy Overrides;
1099 getOverriddenMethods(Overrides);
1100 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1101 I != E; ++I) {
1102 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1103 return Prop;
1104 }
1105
1106 return 0;
1107
1108}
1109
Chris Lattnerab351632009-02-20 20:59:54 +00001110//===----------------------------------------------------------------------===//
1111// ObjCInterfaceDecl
1112//===----------------------------------------------------------------------===//
1113
Douglas Gregora6ea10e2012-01-17 18:09:05 +00001114ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +00001115 DeclContext *DC,
1116 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001117 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +00001118 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +00001119 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +00001120 bool isInternal){
Stephen Hines651f13c2014-04-23 16:59:28 -07001121 ObjCInterfaceDecl *Result = new (C, DC)
1122 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001123 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor0af55012011-12-16 03:12:41 +00001124 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +00001125 return Result;
1126}
1127
Stephen Hines651f13c2014-04-23 16:59:28 -07001128ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001129 unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001130 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1131 0, SourceLocation(),
1132 0, false);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001133 Result->Data.setInt(!C.getLangOpts().Modules);
1134 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001135}
1136
1137ObjCInterfaceDecl::
1138ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregorfd002a72011-12-16 22:37:11 +00001139 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1140 bool isInternal)
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001141 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregor7723fec2011-12-15 20:29:51 +00001142 TypeForDecl(0), Data()
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001143{
Rafael Espindolabc650912013-10-17 15:37:26 +00001144 setPreviousDecl(PrevDecl);
Douglas Gregorfd002a72011-12-16 22:37:11 +00001145
1146 // Copy the 'data' pointer over.
1147 if (PrevDecl)
1148 Data = PrevDecl->Data;
1149
Argyrios Kyrtzidis40f57ee2011-11-15 06:20:21 +00001150 setImplicit(isInternal);
Chris Lattnerab351632009-02-20 20:59:54 +00001151}
1152
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001153void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001154 assert(data().ExternallyCompleted && "Class is not externally completed");
1155 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001156 getASTContext().getExternalSource()->CompleteType(
1157 const_cast<ObjCInterfaceDecl *>(this));
1158}
1159
1160void ObjCInterfaceDecl::setExternallyCompleted() {
1161 assert(getASTContext().getExternalSource() &&
1162 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001163 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001164 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001165 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001166}
1167
Stephen Hines651f13c2014-04-23 16:59:28 -07001168void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1169 // Check for a complete definition and recover if not so.
1170 if (!isThisDeclarationADefinition())
1171 return;
1172 data().HasDesignatedInitializers = true;
1173}
1174
1175bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1176 // Check for a complete definition and recover if not so.
1177 if (!isThisDeclarationADefinition())
1178 return false;
1179 if (data().ExternallyCompleted)
1180 LoadExternalDefinition();
1181
1182 return data().HasDesignatedInitializers;
1183}
1184
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001185ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001186 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1187 if (data().ExternallyCompleted)
1188 LoadExternalDefinition();
1189
1190 return getASTContext().getObjCImplementation(
1191 const_cast<ObjCInterfaceDecl*>(Def));
1192 }
1193
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001194 // FIXME: Should make sure no callers ever do this.
Douglas Gregor7723fec2011-12-15 20:29:51 +00001195 return 0;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001196}
1197
1198void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001199 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001200}
1201
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001202namespace {
1203 struct SynthesizeIvarChunk {
1204 uint64_t Size;
1205 ObjCIvarDecl *Ivar;
1206 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1207 : Size(size), Ivar(ivar) {}
1208 };
1209
1210 bool operator<(const SynthesizeIvarChunk & LHS,
1211 const SynthesizeIvarChunk &RHS) {
1212 return LHS.Size < RHS.Size;
1213 }
1214}
1215
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001216/// all_declared_ivar_begin - return first ivar declared in this class,
1217/// its extensions and its implementation. Lazily build the list on first
1218/// access.
Adrian Prantl4919de62013-03-06 22:03:30 +00001219///
1220/// Caveat: The list returned by this method reflects the current
1221/// state of the parser. The cache will be updated for every ivar
1222/// added by an extension or the implementation when they are
1223/// encountered.
1224/// See also ObjCIvarDecl::Create().
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001225ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001226 // FIXME: Should make sure no callers ever do this.
1227 if (!hasDefinition())
1228 return 0;
1229
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001230 ObjCIvarDecl *curIvar = 0;
Adrian Prantl4919de62013-03-06 22:03:30 +00001231 if (!data().IvarList) {
1232 if (!ivar_empty()) {
1233 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1234 data().IvarList = *I; ++I;
1235 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl10b4df72013-02-27 01:31:55 +00001236 curIvar->setNextIvar(*I);
1237 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001238
Stephen Hines651f13c2014-04-23 16:59:28 -07001239 for (const auto *Ext : known_extensions()) {
Adrian Prantl4919de62013-03-06 22:03:30 +00001240 if (!Ext->ivar_empty()) {
1241 ObjCCategoryDecl::ivar_iterator
1242 I = Ext->ivar_begin(),
1243 E = Ext->ivar_end();
1244 if (!data().IvarList) {
1245 data().IvarList = *I; ++I;
1246 curIvar = data().IvarList;
1247 }
1248 for ( ;I != E; curIvar = *I, ++I)
1249 curIvar->setNextIvar(*I);
1250 }
1251 }
1252 data().IvarListMissingImplementation = true;
Adrian Prantl10b4df72013-02-27 01:31:55 +00001253 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001254
1255 // cached and complete!
1256 if (!data().IvarListMissingImplementation)
1257 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001258
1259 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantl4919de62013-03-06 22:03:30 +00001260 data().IvarListMissingImplementation = false;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001261 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001262 SmallVector<SynthesizeIvarChunk, 16> layout;
Stephen Hines651f13c2014-04-23 16:59:28 -07001263 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001264 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1265 layout.push_back(SynthesizeIvarChunk(
1266 IV->getASTContext().getTypeSize(IV->getType()), IV));
1267 continue;
1268 }
1269 if (!data().IvarList)
Stephen Hines651f13c2014-04-23 16:59:28 -07001270 data().IvarList = IV;
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001271 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001272 curIvar->setNextIvar(IV);
1273 curIvar = IV;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001274 }
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001275
1276 if (!layout.empty()) {
1277 // Order synthesized ivars by their size.
1278 std::stable_sort(layout.begin(), layout.end());
1279 unsigned Ix = 0, EIx = layout.size();
1280 if (!data().IvarList) {
1281 data().IvarList = layout[0].Ivar; Ix++;
1282 curIvar = data().IvarList;
1283 }
1284 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1285 curIvar->setNextIvar(layout[Ix].Ivar);
1286 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001287 }
1288 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001289 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001290}
Chris Lattnerab351632009-02-20 20:59:54 +00001291
1292/// FindCategoryDeclaration - Finds category declaration in the list of
1293/// categories for this class and returns it. Name of the category is passed
1294/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001295///
Chris Lattnerab351632009-02-20 20:59:54 +00001296ObjCCategoryDecl *
1297ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001298 // FIXME: Should make sure no callers ever do this.
1299 if (!hasDefinition())
1300 return 0;
1301
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001302 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001303 LoadExternalDefinition();
1304
Stephen Hines651f13c2014-04-23 16:59:28 -07001305 for (auto *Cat : visible_categories())
Douglas Gregord3297242013-01-16 23:00:23 +00001306 if (Cat->getIdentifier() == CategoryId)
Stephen Hines651f13c2014-04-23 16:59:28 -07001307 return Cat;
Douglas Gregord3297242013-01-16 23:00:23 +00001308
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001309 return 0;
1310}
1311
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001312ObjCMethodDecl *
1313ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001314 for (const auto *Cat : visible_categories()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001315 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001316 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1317 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001318 }
1319
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001320 return 0;
1321}
1322
1323ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001324 for (const auto *Cat : visible_categories()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001325 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001326 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1327 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001328 }
1329
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001330 return 0;
1331}
1332
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001333/// ClassImplementsProtocol - Checks that 'lProto' protocol
1334/// has been implemented in IDecl class, its super class or categories (if
1335/// lookupCategory is true).
1336bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1337 bool lookupCategory,
1338 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001339 if (!hasDefinition())
1340 return false;
1341
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001342 ObjCInterfaceDecl *IDecl = this;
1343 // 1st, look up the class.
Stephen Hines651f13c2014-04-23 16:59:28 -07001344 for (auto *PI : IDecl->protocols()){
1345 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001346 return true;
1347 // This is dubious and is added to be compatible with gcc. In gcc, it is
1348 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1349 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1350 // object. This IMO, should be a bug.
1351 // FIXME: Treat this as an extension, and flag this as an error when GCC
1352 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001353 if (RHSIsQualifiedID &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001354 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001355 return true;
1356 }
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001358 // 2nd, look up the category.
1359 if (lookupCategory)
Stephen Hines651f13c2014-04-23 16:59:28 -07001360 for (const auto *Cat : visible_categories()) {
1361 for (auto *PI : Cat->protocols())
1362 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001363 return true;
1364 }
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001366 // 3rd, look up the super class(s)
1367 if (IDecl->getSuperClass())
1368 return
1369 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1370 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001372 return false;
1373}
1374
Chris Lattnerab351632009-02-20 20:59:54 +00001375//===----------------------------------------------------------------------===//
1376// ObjCIvarDecl
1377//===----------------------------------------------------------------------===//
1378
David Blaikie99ba9e32011-12-20 02:48:34 +00001379void ObjCIvarDecl::anchor() { }
1380
Daniel Dunbara0654922010-04-02 20:10:03 +00001381ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001382 SourceLocation StartLoc,
1383 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001384 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001385 AccessControl ac, Expr *BW,
Stephen Hines651f13c2014-04-23 16:59:28 -07001386 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001387 if (DC) {
1388 // Ivar's can only appear in interfaces, implementations (via synthesized
1389 // properties), and class extensions (via direct declaration, or synthesized
1390 // properties).
1391 //
1392 // FIXME: This should really be asserting this:
1393 // (isa<ObjCCategoryDecl>(DC) &&
1394 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1395 // but unfortunately we sometimes place ivars into non-class extension
1396 // categories on error. This breaks an AST invariant, and should not be
1397 // fixed.
1398 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1399 isa<ObjCCategoryDecl>(DC)) &&
1400 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001401 // Once a new ivar is created in any of class/class-extension/implementation
1402 // decl contexts, the previously built IvarList must be rebuilt.
1403 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1404 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001405 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001406 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001407 else
1408 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001409 }
1410 ID->setIvarList(0);
Daniel Dunbara0654922010-04-02 20:10:03 +00001411 }
1412
Stephen Hines651f13c2014-04-23 16:59:28 -07001413 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1414 synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001415}
1416
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001417ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001418 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1419 QualType(), 0, ObjCIvarDecl::None, 0, false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001420}
1421
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001422const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1423 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001424
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001425 switch (DC->getKind()) {
1426 default:
1427 case ObjCCategoryImpl:
1428 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001429 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001430
1431 // Ivars can only appear in class extension categories.
1432 case ObjCCategory: {
1433 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1434 assert(CD->IsClassExtension() && "invalid container for ivar!");
1435 return CD->getClassInterface();
1436 }
1437
1438 case ObjCImplementation:
1439 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1440
1441 case ObjCInterface:
1442 return cast<ObjCInterfaceDecl>(DC);
1443 }
1444}
Chris Lattnerab351632009-02-20 20:59:54 +00001445
1446//===----------------------------------------------------------------------===//
1447// ObjCAtDefsFieldDecl
1448//===----------------------------------------------------------------------===//
1449
David Blaikie99ba9e32011-12-20 02:48:34 +00001450void ObjCAtDefsFieldDecl::anchor() { }
1451
Chris Lattnerab351632009-02-20 20:59:54 +00001452ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001453*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1454 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001455 IdentifierInfo *Id, QualType T, Expr *BW) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001456 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001457}
1458
Stephen Hines651f13c2014-04-23 16:59:28 -07001459ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001460 unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001461 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1462 0, QualType(), 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001463}
1464
Chris Lattnerab351632009-02-20 20:59:54 +00001465//===----------------------------------------------------------------------===//
1466// ObjCProtocolDecl
1467//===----------------------------------------------------------------------===//
1468
David Blaikie99ba9e32011-12-20 02:48:34 +00001469void ObjCProtocolDecl::anchor() { }
1470
Douglas Gregor27c6da22012-01-01 20:30:41 +00001471ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1472 SourceLocation nameLoc,
1473 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001474 ObjCProtocolDecl *PrevDecl)
1475 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor27c6da22012-01-01 20:30:41 +00001476{
Rafael Espindolabc650912013-10-17 15:37:26 +00001477 setPreviousDecl(PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001478 if (PrevDecl)
1479 Data = PrevDecl->Data;
1480}
1481
Chris Lattnerab351632009-02-20 20:59:54 +00001482ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001483 IdentifierInfo *Id,
1484 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001485 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001486 ObjCProtocolDecl *PrevDecl) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001487 ObjCProtocolDecl *Result =
1488 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001489 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001490 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001491}
1492
Stephen Hines651f13c2014-04-23 16:59:28 -07001493ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001494 unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001495 ObjCProtocolDecl *Result =
1496 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001497 Result->Data.setInt(!C.getLangOpts().Modules);
1498 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001499}
1500
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001501ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1502 ObjCProtocolDecl *PDecl = this;
1503
1504 if (Name == getIdentifier())
1505 return PDecl;
1506
Stephen Hines651f13c2014-04-23 16:59:28 -07001507 for (auto *I : protocols())
1508 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001509 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001511 return NULL;
1512}
1513
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001514// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001515// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001516ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1517 bool isInstance) const {
Chris Lattnerab351632009-02-20 20:59:54 +00001518 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Douglas Gregor0f9b9f32013-01-17 00:38:46 +00001520 // If there is no definition or the definition is hidden, we don't find
1521 // anything.
1522 const ObjCProtocolDecl *Def = getDefinition();
1523 if (!Def || Def->isHidden())
1524 return NULL;
1525
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001526 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001527 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Stephen Hines651f13c2014-04-23 16:59:28 -07001529 for (const auto *I : protocols())
1530 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001531 return MethodDecl;
1532 return NULL;
1533}
1534
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001535void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor6bd99292013-02-09 01:35:03 +00001536 assert(!Data.getPointer() && "Protocol already has a definition!");
1537 Data.setPointer(new (getASTContext()) DefinitionData);
1538 Data.getPointer()->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001539}
1540
1541void ObjCProtocolDecl::startDefinition() {
1542 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001543
1544 // Update all of the declarations with a pointer to the definition.
Stephen Hines651f13c2014-04-23 16:59:28 -07001545 for (auto RD : redecls())
Douglas Gregor1d784b22012-01-01 19:51:50 +00001546 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001547}
1548
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001549void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1550 PropertyDeclOrder &PO) const {
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001551
1552 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001553 for (auto *Prop : PDecl->properties()) {
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001554 // Insert into PM if not there already.
1555 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001556 PO.push_back(Prop);
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001557 }
1558 // Scan through protocol's protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -07001559 for (const auto *PI : PDecl->protocols())
1560 PI->collectPropertiesToImplement(PM, PO);
Anna Zaksb36ea372012-10-18 19:17:53 +00001561 }
Anna Zaksb36ea372012-10-18 19:17:53 +00001562}
1563
Fariborz Jahanian8dbda512013-05-20 21:20:24 +00001564
1565void ObjCProtocolDecl::collectInheritedProtocolProperties(
1566 const ObjCPropertyDecl *Property,
1567 ProtocolPropertyMap &PM) const {
1568 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1569 bool MatchFound = false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001570 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian8dbda512013-05-20 21:20:24 +00001571 if (Prop == Property)
1572 continue;
1573 if (Prop->getIdentifier() == Property->getIdentifier()) {
1574 PM[PDecl] = Prop;
1575 MatchFound = true;
1576 break;
1577 }
1578 }
1579 // Scan through protocol's protocols which did not have a matching property.
1580 if (!MatchFound)
Stephen Hines651f13c2014-04-23 16:59:28 -07001581 for (const auto *PI : PDecl->protocols())
1582 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian8dbda512013-05-20 21:20:24 +00001583 }
1584}
Anna Zaksb36ea372012-10-18 19:17:53 +00001585
Chris Lattnerab351632009-02-20 20:59:54 +00001586//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001587// ObjCCategoryDecl
1588//===----------------------------------------------------------------------===//
1589
David Blaikie99ba9e32011-12-20 02:48:34 +00001590void ObjCCategoryDecl::anchor() { }
1591
Chris Lattnerab351632009-02-20 20:59:54 +00001592ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Stephen Hines651f13c2014-04-23 16:59:28 -07001593 SourceLocation AtLoc,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001594 SourceLocation ClassNameLoc,
1595 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001596 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001597 ObjCInterfaceDecl *IDecl,
1598 SourceLocation IvarLBraceLoc,
1599 SourceLocation IvarRBraceLoc) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001600 ObjCCategoryDecl *CatDecl =
1601 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1602 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001603 if (IDecl) {
1604 // Link this category into its class's category list.
Douglas Gregord3297242013-01-16 23:00:23 +00001605 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001606 if (IDecl->hasDefinition()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001607 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001608 if (ASTMutationListener *L = C.getASTMutationListener())
1609 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1610 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001611 }
1612
1613 return CatDecl;
1614}
1615
Stephen Hines651f13c2014-04-23 16:59:28 -07001616ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001617 unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001618 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1619 SourceLocation(), 0, 0);
Chris Lattnerab351632009-02-20 20:59:54 +00001620}
1621
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001622ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1623 return getASTContext().getObjCImplementation(
1624 const_cast<ObjCCategoryDecl*>(this));
1625}
1626
1627void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1628 getASTContext().setObjCImplementation(this, ImplD);
1629}
1630
1631
Chris Lattnerab351632009-02-20 20:59:54 +00001632//===----------------------------------------------------------------------===//
1633// ObjCCategoryImplDecl
1634//===----------------------------------------------------------------------===//
1635
David Blaikie99ba9e32011-12-20 02:48:34 +00001636void ObjCCategoryImplDecl::anchor() { }
1637
Chris Lattnerab351632009-02-20 20:59:54 +00001638ObjCCategoryImplDecl *
1639ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001640 IdentifierInfo *Id,
1641 ObjCInterfaceDecl *ClassInterface,
1642 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001643 SourceLocation atStartLoc,
1644 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001645 if (ClassInterface && ClassInterface->hasDefinition())
1646 ClassInterface = ClassInterface->getDefinition();
Stephen Hines651f13c2014-04-23 16:59:28 -07001647 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1648 atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001649}
1650
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001651ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1652 unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001653 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1654 SourceLocation(), SourceLocation());
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001655}
1656
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001657ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001658 // The class interface might be NULL if we are working with invalid code.
1659 if (const ObjCInterfaceDecl *ID = getClassInterface())
1660 return ID->FindCategoryDeclaration(getIdentifier());
1661 return 0;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001662}
1663
Chris Lattnerab351632009-02-20 20:59:54 +00001664
David Blaikie99ba9e32011-12-20 02:48:34 +00001665void ObjCImplDecl::anchor() { }
1666
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001667void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001668 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001669 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001670 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001671}
1672
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001673void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1674 ASTContext &Ctx = getASTContext();
1675
1676 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001677 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001678 if (IFace)
1679 Ctx.setObjCImplementation(IFace, ImplD);
1680
Duncan Sands98f2cca2009-07-21 07:56:29 +00001681 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001682 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1683 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1684 Ctx.setObjCImplementation(CD, ImplD);
1685 }
1686
1687 ClassInterface = IFace;
1688}
1689
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001690/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian6d1cb5c2013-03-12 17:43:00 +00001691/// properties implemented in this \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001692/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001693///
Chris Lattner3aa18612009-02-28 18:42:10 +00001694ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001695FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001696 for (auto *PID : property_impls())
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001697 if (PID->getPropertyIvarDecl() &&
1698 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1699 return PID;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001700 return 0;
1701}
1702
1703/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001704/// added to the list of those properties \@synthesized/\@dynamic in this
1705/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001706///
Chris Lattner3aa18612009-02-28 18:42:10 +00001707ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001708FindPropertyImplDecl(IdentifierInfo *Id) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001709 for (auto *PID : property_impls())
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001710 if (PID->getPropertyDecl()->getIdentifier() == Id)
1711 return PID;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001712 return 0;
1713}
1714
Chris Lattner5f9e2722011-07-23 10:55:15 +00001715raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001716 const ObjCCategoryImplDecl &CID) {
1717 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001718 return OS;
1719}
1720
Chris Lattnerab351632009-02-20 20:59:54 +00001721//===----------------------------------------------------------------------===//
1722// ObjCImplementationDecl
1723//===----------------------------------------------------------------------===//
1724
David Blaikie99ba9e32011-12-20 02:48:34 +00001725void ObjCImplementationDecl::anchor() { }
1726
Chris Lattnerab351632009-02-20 20:59:54 +00001727ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001728ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001729 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001730 ObjCInterfaceDecl *SuperDecl,
1731 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001732 SourceLocation atStartLoc,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001733 SourceLocation superLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001734 SourceLocation IvarLBraceLoc,
1735 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001736 if (ClassInterface && ClassInterface->hasDefinition())
1737 ClassInterface = ClassInterface->getDefinition();
Stephen Hines651f13c2014-04-23 16:59:28 -07001738 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1739 nameLoc, atStartLoc, superLoc,
1740 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001741}
1742
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001743ObjCImplementationDecl *
1744ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001745 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1746 SourceLocation());
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001747}
1748
John McCallda6d9762011-07-22 04:15:06 +00001749void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1750 CXXCtorInitializer ** initializers,
1751 unsigned numInitializers) {
1752 if (numInitializers > 0) {
1753 NumIvarInitializers = numInitializers;
1754 CXXCtorInitializer **ivarInitializers =
1755 new (C) CXXCtorInitializer*[NumIvarInitializers];
1756 memcpy(ivarInitializers, initializers,
1757 numInitializers * sizeof(CXXCtorInitializer*));
1758 IvarInitializers = ivarInitializers;
1759 }
1760}
1761
Chris Lattner5f9e2722011-07-23 10:55:15 +00001762raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001763 const ObjCImplementationDecl &ID) {
1764 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001765 return OS;
1766}
1767
Chris Lattnerab351632009-02-20 20:59:54 +00001768//===----------------------------------------------------------------------===//
1769// ObjCCompatibleAliasDecl
1770//===----------------------------------------------------------------------===//
1771
David Blaikie99ba9e32011-12-20 02:48:34 +00001772void ObjCCompatibleAliasDecl::anchor() { }
1773
Chris Lattnerab351632009-02-20 20:59:54 +00001774ObjCCompatibleAliasDecl *
1775ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1776 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001777 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001778 ObjCInterfaceDecl* AliasedClass) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001779 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerab351632009-02-20 20:59:54 +00001780}
1781
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001782ObjCCompatibleAliasDecl *
1783ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001784 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001785}
1786
Chris Lattnerab351632009-02-20 20:59:54 +00001787//===----------------------------------------------------------------------===//
1788// ObjCPropertyDecl
1789//===----------------------------------------------------------------------===//
1790
David Blaikie99ba9e32011-12-20 02:48:34 +00001791void ObjCPropertyDecl::anchor() { }
1792
Chris Lattnerab351632009-02-20 20:59:54 +00001793ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1794 SourceLocation L,
1795 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001796 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001797 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001798 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001799 PropertyControl propControl) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001800 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001801}
1802
Stephen Hines651f13c2014-04-23 16:59:28 -07001803ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001804 unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001805 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1806 SourceLocation(), 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001807}
1808
Chris Lattnerab351632009-02-20 20:59:54 +00001809//===----------------------------------------------------------------------===//
1810// ObjCPropertyImplDecl
1811//===----------------------------------------------------------------------===//
1812
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001813ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001814 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001815 SourceLocation atLoc,
1816 SourceLocation L,
1817 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001818 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001819 ObjCIvarDecl *ivar,
1820 SourceLocation ivarLoc) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001821 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1822 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001823}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001824
Stephen Hines651f13c2014-04-23 16:59:28 -07001825ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001826 unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001827 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1828 0, Dynamic, 0, SourceLocation());
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001829}
1830
Douglas Gregora4ffd852010-11-17 01:03:52 +00001831SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1832 SourceLocation EndLoc = getLocation();
1833 if (IvarLoc.isValid())
1834 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001835
Douglas Gregora4ffd852010-11-17 01:03:52 +00001836 return SourceRange(AtLoc, EndLoc);
1837}