blob: 6e7478474d6f9dbcf0f1574cb89d2daad70f0b45 [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) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070028 List = nullptr;
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 {
Stephen Hines0e2c34f2015-03-23 12:09:02 -070057 lookup_result R = lookup(Id);
58 for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
David Blaikie3bc93e32012-12-19 00:45:41 +000059 Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -070063 return nullptr;
Fariborz Jahanian496b5a82009-06-05 18:16:35 +000064}
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)
Stephen Hines6bcf27b2014-05-29 04:14:42 -070075 return nullptr;
Douglas Gregor0f9b9f32013-01-17 00:38:46 +000076 }
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 //
Stephen Hines0e2c34f2015-03-23 12:09:02 -070086 lookup_result R = lookup(Sel);
87 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
David Blaikie3bc93e32012-12-19 00:45:41 +000088 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 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -070093 return nullptr;
Steve Naroff0701bbb2009-01-08 17:28:14 +000094}
95
Stephen Hines0e2c34f2015-03-23 12:09:02 -070096/// \brief This routine returns 'true' if a user declared setter method was
97/// 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'
99/// property. This is because, user must provide a setter method for the
100/// category's 'readwrite' property.
101bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
102 const ObjCPropertyDecl *Property) const {
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +0000103 Selector Sel = Property->getSetterName();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700104 lookup_result R = lookup(Sel);
105 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +0000106 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;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700121 // Also search through the categories looking for a 'readwrite'
122 // declaration of this property. If one found, presumably a setter will
123 // be provided (properties declared in categories will not get
124 // auto-synthesized).
Stephen Hines651f13c2014-04-23 16:59:28 -0700125 for (const auto *P : Cat->properties())
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +0000126 if (P->getIdentifier() == Property->getIdentifier()) {
127 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
128 return true;
129 break;
130 }
131 }
132
133 // Also look into protocols, for a user declared instance method.
Stephen Hines651f13c2014-04-23 16:59:28 -0700134 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +0000135 if (Proto->HasUserDeclaredSetterMethod(Property))
136 return true;
Stephen Hines651f13c2014-04-23 16:59:28 -0700137
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +0000138 // And in its super class.
139 ObjCInterfaceDecl *OSC = ID->getSuperClass();
140 while (OSC) {
141 if (OSC->HasUserDeclaredSetterMethod(Property))
142 return true;
143 OSC = OSC->getSuperClass();
144 }
145 }
146 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
Stephen Hines651f13c2014-04-23 16:59:28 -0700147 for (const auto *PI : PD->protocols())
148 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +0000149 return true;
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +0000150 return false;
151}
152
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000153ObjCPropertyDecl *
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000154ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Stephen Hines176edba2014-12-01 14:53:08 -0800155 const IdentifierInfo *propertyID) {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000156 // If this context is a hidden protocol definition, don't find any
157 // property.
158 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
159 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
160 if (Def->isHidden())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700161 return nullptr;
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000162 }
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000163
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700164 DeclContext::lookup_result R = DC->lookup(propertyID);
165 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
David Blaikie3bc93e32012-12-19 00:45:41 +0000166 ++I)
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000167 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
168 return PD;
169
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700170 return nullptr;
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000171}
172
Anna Zaksad0ce532012-09-27 19:45:11 +0000173IdentifierInfo *
174ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
175 SmallString<128> ivarName;
176 {
177 llvm::raw_svector_ostream os(ivarName);
178 os << '_' << getIdentifier()->getName();
179 }
180 return &Ctx.Idents.get(ivarName.str());
181}
182
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000183/// FindPropertyDeclaration - Finds declaration of the property given its name
184/// in 'PropertyId' and returns it. It returns 0, if not found.
Stephen Hines176edba2014-12-01 14:53:08 -0800185ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
186 const IdentifierInfo *PropertyId) const {
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000187 // Don't find properties within hidden protocol definitions.
188 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
189 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
190 if (Def->isHidden())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700191 return nullptr;
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000192 }
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000194 if (ObjCPropertyDecl *PD =
195 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
196 return PD;
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000198 switch (getKind()) {
199 default:
200 break;
201 case Decl::ObjCProtocol: {
202 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
Stephen Hines651f13c2014-04-23 16:59:28 -0700203 for (const auto *I : PID->protocols())
204 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahanian25760612010-02-15 21:55:26 +0000205 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000206 break;
207 }
208 case Decl::ObjCInterface: {
209 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregord3297242013-01-16 23:00:23 +0000210 // Look through categories (but not extensions).
Stephen Hines651f13c2014-04-23 16:59:28 -0700211 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000212 if (!Cat->IsClassExtension())
213 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
214 return P;
Douglas Gregord3297242013-01-16 23:00:23 +0000215 }
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000216
217 // Look through protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -0700218 for (const auto *I : OID->all_referenced_protocols())
219 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000220 return P;
221
222 // Finally, check the super class.
223 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
224 return superClass->FindPropertyDeclaration(PropertyId);
225 break;
226 }
227 case Decl::ObjCCategory: {
228 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
229 // Look through protocols.
230 if (!OCD->IsClassExtension())
Stephen Hines651f13c2014-04-23 16:59:28 -0700231 for (const auto *I : OCD->protocols())
232 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
233 return P;
Ted Kremenekde09d0c2010-03-15 20:11:53 +0000234 break;
Fariborz Jahanianf034e9c2009-01-19 18:16:19 +0000235 }
236 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700237 return nullptr;
Steve Naroff3d2c22b2008-06-05 13:55:23 +0000238}
239
David Blaikie99ba9e32011-12-20 02:48:34 +0000240void ObjCInterfaceDecl::anchor() { }
241
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000242/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
243/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenek37cafb02010-03-15 20:30:07 +0000244/// (direct or indirect) used by the primary class.
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000245///
246ObjCPropertyDecl *
Ted Kremenek37cafb02010-03-15 20:30:07 +0000247ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000248 IdentifierInfo *PropertyId) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000249 // FIXME: Should make sure no callers ever do this.
250 if (!hasDefinition())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700251 return nullptr;
252
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000253 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000254 LoadExternalDefinition();
255
Ted Kremenek37cafb02010-03-15 20:30:07 +0000256 if (ObjCPropertyDecl *PD =
257 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
258 return PD;
259
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000260 // Look through protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -0700261 for (const auto *I : all_referenced_protocols())
262 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000263 return P;
Ted Kremenek37cafb02010-03-15 20:30:07 +0000264
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700265 return nullptr;
Fariborz Jahaniana6f14e12009-11-02 22:45:15 +0000266}
267
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000268void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
269 PropertyDeclOrder &PO) const {
Stephen Hines651f13c2014-04-23 16:59:28 -0700270 for (auto *Prop : properties()) {
Anna Zaksb36ea372012-10-18 19:17:53 +0000271 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +0000272 PO.push_back(Prop);
Anna Zaksb36ea372012-10-18 19:17:53 +0000273 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700274 for (const auto *PI : all_referenced_protocols())
275 PI->collectPropertiesToImplement(PM, PO);
Anna Zakse63aedd2012-10-31 01:18:22 +0000276 // Note, the properties declared only in class extensions are still copied
277 // into the main @interface's property list, and therefore we don't
278 // explicitly, have to search class extension properties.
Anna Zaksb36ea372012-10-18 19:17:53 +0000279}
280
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000281bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
282 const ObjCInterfaceDecl *Class = this;
283 while (Class) {
284 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
285 return true;
286 Class = Class->getSuperClass();
287 }
288 return false;
289}
290
291const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
292 const ObjCInterfaceDecl *Class = this;
293 while (Class) {
294 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
295 return Class;
296 Class = Class->getSuperClass();
297 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700298 return nullptr;
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000299}
300
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000301void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
302 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
303 ASTContext &C)
304{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000305 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000306 LoadExternalDefinition();
307
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000308 if (data().AllReferencedProtocols.empty() &&
309 data().ReferencedProtocols.empty()) {
310 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000311 return;
312 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000313
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000314 // Check for duplicate protocol in class's protocol list.
Ted Kremenek53b94412010-09-01 01:21:15 +0000315 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000316 // class or its extension are very few.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000317 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000318 for (unsigned i = 0; i < ExtNum; i++) {
319 bool protocolExists = false;
320 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Stephen Hines651f13c2014-04-23 16:59:28 -0700321 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000322 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
323 protocolExists = true;
324 break;
325 }
326 }
327 // Do we want to warn on a protocol in extension class which
328 // already exist in the class? Probably not.
Ted Kremenek53b94412010-09-01 01:21:15 +0000329 if (!protocolExists)
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000330 ProtocolRefs.push_back(ProtoInExtension);
331 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000332
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000333 if (ProtocolRefs.empty())
334 return;
Ted Kremenek53b94412010-09-01 01:21:15 +0000335
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000336 // Merge ProtocolRefs into class's protocol list;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700337 ProtocolRefs.append(all_referenced_protocol_begin(),
338 all_referenced_protocol_end());
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 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700353 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700354}
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 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700367 if (const auto *ImplD = D->getImplementation()) {
368 for (const auto *MD : ImplD->instance_methods()) {
369 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
370 return true;
371 }
372 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700373 return false;
374}
375
376bool 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: {
383 // 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.
386 if (isIntroducingInitializers(this)) {
387 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Stephen Hines651f13c2014-04-23 16:59:28 -0700388 } else {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700389 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 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700398 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700399 assert(data().InheritedDesignatedInitializers
400 != DefinitionData::IDI_Unknown);
401 return data().InheritedDesignatedInitializers ==
402 DefinitionData::IDI_Inherited;
Stephen Hines651f13c2014-04-23 16:59:28 -0700403 }
404 }
405
406 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
407}
408
409void ObjCInterfaceDecl::getDesignatedInitializers(
410 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
411 // Check for a complete definition and recover if not so.
412 if (!isThisDeclarationADefinition())
413 return;
414 if (data().ExternallyCompleted)
415 LoadExternalDefinition();
416
417 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
418 if (!IFace)
419 return;
420
421 for (const auto *MD : IFace->instance_methods())
422 if (MD->isThisDeclarationADesignatedInitializer())
423 Methods.push_back(MD);
424 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 }
429}
430
431bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
432 const ObjCMethodDecl **InitMethod) const {
433 // Check for a complete definition and recover if not so.
434 if (!isThisDeclarationADefinition())
435 return false;
436 if (data().ExternallyCompleted)
437 LoadExternalDefinition();
438
439 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
440 if (!IFace)
441 return false;
442
443 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
444 if (MD->isThisDeclarationADesignatedInitializer()) {
445 if (InitMethod)
446 *InitMethod = MD;
447 return true;
448 }
449 }
450 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 }
459 return false;
460}
461
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000462void ObjCInterfaceDecl::allocateDefinitionData() {
463 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor6bd99292013-02-09 01:35:03 +0000464 Data.setPointer(new (getASTContext()) DefinitionData());
465 Data.getPointer()->Definition = this;
Douglas Gregor8d2dbbf2011-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 Gregor0af55012011-12-16 03:12:41 +0000470}
471
472void ObjCInterfaceDecl::startDefinition() {
473 allocateDefinitionData();
474
Douglas Gregor53df7a12011-12-15 18:03:09 +0000475 // Update all of the declarations with a pointer to the definition.
Stephen Hines651f13c2014-04-23 16:59:28 -0700476 for (auto RD : redecls()) {
477 if (RD != this)
Douglas Gregor26fec632011-12-15 18:17:27 +0000478 RD->Data = Data;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000479 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000480}
481
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000482ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
483 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000484 // FIXME: Should make sure no callers ever do this.
485 if (!hasDefinition())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700486 return nullptr;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000487
488 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000489 LoadExternalDefinition();
490
Chris Lattner1e03a562008-03-16 00:19:01 +0000491 ObjCInterfaceDecl* ClassDecl = this;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700492 while (ClassDecl != nullptr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000493 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian496b5a82009-06-05 18:16:35 +0000494 clsDeclared = ClassDecl;
495 return I;
Chris Lattner1e03a562008-03-16 00:19:01 +0000496 }
Douglas Gregord3297242013-01-16 23:00:23 +0000497
Stephen Hines651f13c2014-04-23 16:59:28 -0700498 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregord3297242013-01-16 23:00:23 +0000499 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000500 clsDeclared = ClassDecl;
501 return I;
502 }
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000503 }
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000504
Chris Lattner1e03a562008-03-16 00:19:01 +0000505 ClassDecl = ClassDecl->getSuperClass();
506 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700507 return nullptr;
Chris Lattner1e03a562008-03-16 00:19:01 +0000508}
509
Fariborz Jahaniancd187622009-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 Gregor2e5c15b2011-12-15 05:27:12 +0000515 // FIXME: Should make sure no callers ever do this.
516 if (!hasDefinition())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700517 return nullptr;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000518
519 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000520 LoadExternalDefinition();
521
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000522 ObjCInterfaceDecl* ClassDecl = this;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700523 while (ClassDecl != nullptr) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000524 if (ClassDecl->getIdentifier() == ICName)
525 return ClassDecl;
526 ClassDecl = ClassDecl->getSuperClass();
527 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700528 return nullptr;
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000529}
530
Fariborz Jahanian07b1bbe2013-07-10 21:30:22 +0000531ObjCProtocolDecl *
532ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700533 for (auto *P : all_referenced_protocols())
534 if (P->lookupProtocolNamed(Name))
535 return P;
Fariborz Jahanian07b1bbe2013-07-10 21:30:22 +0000536 ObjCInterfaceDecl *SuperClass = getSuperClass();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700537 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian07b1bbe2013-07-10 21:30:22 +0000538}
539
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000540/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner1e03a562008-03-16 00:19:01 +0000541/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000542/// When argument category "C" is specified, any implicit method found
543/// in this category is ignored.
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000544ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Stephen Hines651f13c2014-04-23 16:59:28 -0700545 bool isInstance,
546 bool shallowCategoryLookup,
547 bool followSuper,
548 const ObjCCategoryDecl *C) const
549{
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000550 // FIXME: Should make sure no callers ever do this.
551 if (!hasDefinition())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700552 return nullptr;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000553
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000554 const ObjCInterfaceDecl* ClassDecl = this;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700555 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000557 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +0000558 LoadExternalDefinition();
559
Stephen Hines651f13c2014-04-23 16:59:28 -0700560 while (ClassDecl) {
Stephen Hines176edba2014-12-01 14:53:08 -0800561 // 1. Look through primary class.
Argyrios Kyrtzidisaa5420c2009-07-25 22:15:51 +0000562 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner1e03a562008-03-16 00:19:01 +0000563 return MethodDecl;
Fariborz Jahanianbf393be2012-04-05 22:14:12 +0000564
Stephen Hines176edba2014-12-01 14:53:08 -0800565 // 2. Didn't find one yet - now look through categories.
566 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000567 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Stephen Hines651f13c2014-04-23 16:59:28 -0700568 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +0000569 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000570
Stephen Hines176edba2014-12-01 14:53:08 -0800571 // 3. Didn't find one yet - look through primary class's protocols.
572 for (const auto *I : ClassDecl->protocols())
573 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
574 return MethodDecl;
575
576 // 4. Didn't find one yet - now look through categories' protocols
577 if (!shallowCategoryLookup)
578 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000579 // Didn't find one yet - look through protocols.
580 const ObjCList<ObjCProtocolDecl> &Protocols =
Stephen Hines176edba2014-12-01 14:53:08 -0800581 Cat->getReferencedProtocols();
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +0000582 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
583 E = Protocols.end(); I != E; ++I)
584 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Stephen Hines651f13c2014-04-23 16:59:28 -0700585 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +0000586 return MethodDecl;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +0000587 }
Stephen Hines176edba2014-12-01 14:53:08 -0800588
589
Stephen Hines651f13c2014-04-23 16:59:28 -0700590 if (!followSuper)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700591 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700592
Stephen Hines176edba2014-12-01 14:53:08 -0800593 // 5. Get to the super class (if any).
Chris Lattner1e03a562008-03-16 00:19:01 +0000594 ClassDecl = ClassDecl->getSuperClass();
595 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700596 return nullptr;
Chris Lattner1e03a562008-03-16 00:19:01 +0000597}
598
Anna Zakse61354b2012-07-27 19:07:44 +0000599// Will search "local" class/category implementations for a method decl.
600// If failed, then we search in class's root for an instance method.
601// Returns 0 if no method is found.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000602ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
603 const Selector &Sel,
Anna Zaksca93ee72012-07-30 20:31:21 +0000604 bool Instance) const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000605 // FIXME: Should make sure no callers ever do this.
606 if (!hasDefinition())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700607 return nullptr;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000608
609 if (data().ExternallyCompleted)
Argyrios Kyrtzidis7c81c2a2011-10-19 02:25:16 +0000610 LoadExternalDefinition();
611
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700612 ObjCMethodDecl *Method = nullptr;
Steve Naroffd789d3d2009-10-01 23:46:04 +0000613 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000614 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
615 : ImpDecl->getClassMethod(Sel);
Anna Zakse61354b2012-07-27 19:07:44 +0000616
617 // Look through local category implementations associated with the class.
618 if (!Method)
619 Method = Instance ? getCategoryInstanceMethod(Sel)
620 : getCategoryClassMethod(Sel);
621
622 // Before we give up, check if the selector is an instance method.
623 // But only in the root. This matches gcc's behavior and what the
624 // runtime expects.
625 if (!Instance && !Method && !getSuperClass()) {
626 Method = lookupInstanceMethod(Sel);
627 // Look through local category implementations associated
628 // with the root class.
629 if (!Method)
630 Method = lookupPrivateMethod(Sel, true);
631 }
632
Steve Naroffd789d3d2009-10-01 23:46:04 +0000633 if (!Method && getSuperClass())
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000634 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffd789d3d2009-10-01 23:46:04 +0000635 return Method;
636}
Chris Lattnerab351632009-02-20 20:59:54 +0000637
638//===----------------------------------------------------------------------===//
639// ObjCMethodDecl
640//===----------------------------------------------------------------------===//
641
Stephen Hines651f13c2014-04-23 16:59:28 -0700642ObjCMethodDecl *ObjCMethodDecl::Create(
643 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
644 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
645 DeclContext *contextDecl, bool isInstance, bool isVariadic,
646 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
647 ImplementationControl impControl, bool HasRelatedResultType) {
648 return new (C, contextDecl) ObjCMethodDecl(
649 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
650 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
651 impControl, HasRelatedResultType);
Chris Lattner1e03a562008-03-16 00:19:01 +0000652}
653
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000654ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700655 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700656 Selector(), QualType(), nullptr, nullptr);
Stephen Hines651f13c2014-04-23 16:59:28 -0700657}
658
659bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
660 return getMethodFamily() == OMF_init &&
661 hasAttr<ObjCDesignatedInitializerAttr>();
662}
663
664bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
665 const ObjCMethodDecl **InitMethod) const {
666 if (getMethodFamily() != OMF_init)
667 return false;
668 const DeclContext *DC = getDeclContext();
669 if (isa<ObjCProtocolDecl>(DC))
670 return false;
671 if (const ObjCInterfaceDecl *ID = getClassInterface())
672 return ID->isDesignatedInitializer(getSelector(), InitMethod);
673 return false;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000674}
675
Douglas Gregor5456b0fe2012-10-09 17:21:28 +0000676Stmt *ObjCMethodDecl::getBody() const {
677 return Body.get(getASTContext().getExternalSource());
678}
679
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000680void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
681 assert(PrevMethod);
682 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
683 IsRedeclaration = true;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000684 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000685}
686
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000687void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
688 ArrayRef<ParmVarDecl*> Params,
689 ArrayRef<SourceLocation> SelLocs) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700690 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000691 NumParams = Params.size();
692 if (Params.empty() && SelLocs.empty())
693 return;
694
695 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
696 sizeof(SourceLocation) * SelLocs.size();
697 ParamsAndSelLocs = C.Allocate(Size);
698 std::copy(Params.begin(), Params.end(), getParams());
699 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
700}
701
702void ObjCMethodDecl::getSelectorLocs(
703 SmallVectorImpl<SourceLocation> &SelLocs) const {
704 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
705 SelLocs.push_back(getSelectorLoc(i));
706}
707
708void ObjCMethodDecl::setMethodParams(ASTContext &C,
709 ArrayRef<ParmVarDecl*> Params,
710 ArrayRef<SourceLocation> SelLocs) {
711 assert((!SelLocs.empty() || isImplicit()) &&
712 "No selector locs for non-implicit method");
713 if (isImplicit())
Dmitri Gribenko55431692013-05-05 00:41:58 +0000714 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000715
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000716 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
717 DeclEndLoc);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000718 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko55431692013-05-05 00:41:58 +0000719 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000720
721 setParamsAndSelLocs(C, Params, SelLocs);
722}
723
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000724/// \brief A definition will return its interface declaration.
725/// An interface declaration will return its definition.
726/// Otherwise it will return itself.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700727ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000728 ASTContext &Ctx = getASTContext();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700729 ObjCMethodDecl *Redecl = nullptr;
Argyrios Kyrtzidis72b26252011-10-14 17:41:52 +0000730 if (HasRedeclaration)
731 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +0000732 if (Redecl)
733 return Redecl;
734
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000735 Decl *CtxD = cast<Decl>(getDeclContext());
736
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000737 if (!CtxD->isInvalidDecl()) {
738 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
739 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
740 if (!ImplD->isInvalidDecl())
741 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000742
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000743 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
744 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
745 if (!ImplD->isInvalidDecl())
746 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000747
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000748 } else if (ObjCImplementationDecl *ImplD =
749 dyn_cast<ObjCImplementationDecl>(CtxD)) {
750 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
751 if (!IFD->isInvalidDecl())
752 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +0000753
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000754 } else if (ObjCCategoryImplDecl *CImplD =
755 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
756 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
757 if (!CatD->isInvalidDecl())
758 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
759 }
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000760 }
761
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +0000762 if (!Redecl && isRedeclaration()) {
763 // This is the last redeclaration, go back to the first method.
764 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
765 isInstanceMethod());
766 }
767
Argyrios Kyrtzidis57ea6be2009-07-21 00:06:36 +0000768 return Redecl ? Redecl : this;
769}
770
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000771ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
772 Decl *CtxD = cast<Decl>(getDeclContext());
773
774 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
775 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
776 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
777 isInstanceMethod()))
778 return MD;
779
780 } else if (ObjCCategoryImplDecl *CImplD =
781 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000782 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000783 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
784 isInstanceMethod()))
785 return MD;
786 }
787
Argyrios Kyrtzidis6d4740e2011-10-17 19:48:09 +0000788 if (isRedeclaration())
789 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
790 isInstanceMethod());
791
Argyrios Kyrtzidise7f9d302009-07-28 05:11:17 +0000792 return this;
793}
794
Argyrios Kyrtzidisa0cff722012-06-16 00:46:02 +0000795SourceLocation ObjCMethodDecl::getLocEnd() const {
796 if (Stmt *Body = getBody())
797 return Body->getLocEnd();
798 return DeclEndLoc;
799}
800
John McCall85f3d762011-03-02 01:50:55 +0000801ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
802 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCalld976c8e2011-03-02 21:01:41 +0000803 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCall85f3d762011-03-02 01:50:55 +0000804 return family;
805
John McCalld5313b02011-03-02 11:33:24 +0000806 // Check for an explicit attribute.
807 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
808 // The unfortunate necessity of mapping between enums here is due
809 // to the attributes framework.
810 switch (attr->getFamily()) {
811 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
812 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
813 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
814 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
815 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
816 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
817 }
818 Family = static_cast<unsigned>(family);
819 return family;
820 }
821
John McCall85f3d762011-03-02 01:50:55 +0000822 family = getSelector().getMethodFamily();
823 switch (family) {
824 case OMF_None: break;
825
826 // init only has a conventional meaning for an instance method, and
827 // it has to return an object.
828 case OMF_init:
Stephen Hines651f13c2014-04-23 16:59:28 -0700829 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCall85f3d762011-03-02 01:50:55 +0000830 family = OMF_None;
831 break;
832
833 // alloc/copy/new have a conventional meaning for both class and
834 // instance methods, but they require an object return.
835 case OMF_alloc:
836 case OMF_copy:
837 case OMF_mutableCopy:
838 case OMF_new:
Stephen Hines651f13c2014-04-23 16:59:28 -0700839 if (!getReturnType()->isObjCObjectPointerType())
John McCall85f3d762011-03-02 01:50:55 +0000840 family = OMF_None;
841 break;
842
843 // These selectors have a conventional meaning only for instance methods.
844 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000845 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +0000846 case OMF_retain:
847 case OMF_release:
848 case OMF_autorelease:
849 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +0000850 case OMF_self:
John McCall85f3d762011-03-02 01:50:55 +0000851 if (!isInstanceMethod())
852 family = OMF_None;
853 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000854
Stephen Hines176edba2014-12-01 14:53:08 -0800855 case OMF_initialize:
856 if (isInstanceMethod() || !getReturnType()->isVoidType())
857 family = OMF_None;
858 break;
859
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000860 case OMF_performSelector:
Stephen Hines651f13c2014-04-23 16:59:28 -0700861 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000862 family = OMF_None;
863 else {
864 unsigned noParams = param_size();
865 if (noParams < 1 || noParams > 3)
866 family = OMF_None;
867 else {
Stephen Hines651f13c2014-04-23 16:59:28 -0700868 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000869 QualType ArgT = (*it);
870 if (!ArgT->isObjCSelType()) {
871 family = OMF_None;
872 break;
873 }
874 while (--noParams) {
875 it++;
876 ArgT = (*it);
877 if (!ArgT->isObjCIdType()) {
878 family = OMF_None;
879 break;
880 }
881 }
882 }
883 }
884 break;
885
John McCall85f3d762011-03-02 01:50:55 +0000886 }
887
888 // Cache the result.
889 Family = static_cast<unsigned>(family);
890 return family;
891}
892
Mike Stump1eb44332009-09-09 15:08:12 +0000893void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerab351632009-02-20 20:59:54 +0000894 const ObjCInterfaceDecl *OID) {
895 QualType selfTy;
896 if (isInstanceMethod()) {
897 // There may be no interface context due to error in declaration
898 // of the interface (which has been reported). Recover gracefully.
899 if (OID) {
Daniel Dunbar3b3a4582009-04-22 04:34:53 +0000900 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff14108da2009-07-10 23:34:53 +0000901 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerab351632009-02-20 20:59:54 +0000902 } else {
903 selfTy = Context.getObjCIdType();
904 }
905 } else // we have a factory method.
906 selfTy = Context.getObjCClassType();
907
John McCall7acddac2011-06-17 06:42:21 +0000908 bool selfIsPseudoStrong = false;
John McCallf85e1932011-06-15 23:02:42 +0000909 bool selfIsConsumed = false;
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000910
David Blaikie4e4d0842012-03-11 07:00:24 +0000911 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000912 if (isInstanceMethod()) {
913 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCallf85e1932011-06-15 23:02:42 +0000914
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000915 // 'self' is always __strong. It's actually pseudo-strong except
916 // in init methods (or methods labeled ns_consumes_self), though.
917 Qualifiers qs;
918 qs.setObjCLifetime(Qualifiers::OCL_Strong);
919 selfTy = Context.getQualifiedType(selfTy, qs);
John McCallf85e1932011-06-15 23:02:42 +0000920
Ted Kremenek2bbcd5c2011-11-14 21:59:25 +0000921 // In addition, 'self' is const unless this is an init method.
922 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
923 selfTy = selfTy.withConst();
924 selfIsPseudoStrong = true;
925 }
926 }
927 else {
928 assert(isClassMethod());
929 // 'self' is always const in class methods.
John McCallf85e1932011-06-15 23:02:42 +0000930 selfTy = selfTy.withConst();
John McCall7acddac2011-06-17 06:42:21 +0000931 selfIsPseudoStrong = true;
932 }
John McCallf85e1932011-06-15 23:02:42 +0000933 }
934
935 ImplicitParamDecl *self
936 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
937 &Context.Idents.get("self"), selfTy);
938 setSelfDecl(self);
939
940 if (selfIsConsumed)
Stephen Hines651f13c2014-04-23 16:59:28 -0700941 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerab351632009-02-20 20:59:54 +0000942
John McCall7acddac2011-06-17 06:42:21 +0000943 if (selfIsPseudoStrong)
944 self->setARCPseudoStrong(true);
945
Mike Stump1eb44332009-09-09 15:08:12 +0000946 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
947 &Context.Idents.get("_cmd"),
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000948 Context.getObjCSelType()));
Chris Lattnerab351632009-02-20 20:59:54 +0000949}
950
Chris Lattnerab351632009-02-20 20:59:54 +0000951ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
952 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
953 return ID;
954 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
955 return CD->getClassInterface();
Argyrios Kyrtzidisa8530372009-07-28 05:10:52 +0000956 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerab351632009-02-20 20:59:54 +0000957 return IMD->getClassInterface();
Stephen Hines651f13c2014-04-23 16:59:28 -0700958 if (isa<ObjCProtocolDecl>(getDeclContext()))
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700959 return nullptr;
David Blaikieb219cfc2011-09-23 05:06:16 +0000960 llvm_unreachable("unknown method context");
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +0000961}
962
Stephen Hines176edba2014-12-01 14:53:08 -0800963SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
964 const auto *TSI = getReturnTypeSourceInfo();
965 if (TSI)
966 return TSI->getTypeLoc().getSourceRange();
967 return SourceRange();
968}
969
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000970static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
971 const ObjCMethodDecl *Method,
972 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
973 bool MovedToSuper) {
974 if (!Container)
975 return;
976
977 // In categories look for overriden methods from protocols. A method from
978 // category is not "overriden" since it is considered as the "same" method
979 // (same USR) as the one from the interface.
980 if (const ObjCCategoryDecl *
981 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
982 // Check whether we have a matching method at this category but only if we
983 // are at the super class level.
984 if (MovedToSuper)
985 if (ObjCMethodDecl *
986 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +0000987 Method->isInstanceMethod(),
988 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000989 if (Method != Overridden) {
990 // We found an override at this category; there is no need to look
991 // into its protocols.
992 Methods.push_back(Overridden);
993 return;
994 }
995
Stephen Hines651f13c2014-04-23 16:59:28 -0700996 for (const auto *P : Category->protocols())
997 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +0000998 return;
999 }
1000
1001 // Check whether we have a matching method at this level.
1002 if (const ObjCMethodDecl *
1003 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00001004 Method->isInstanceMethod(),
1005 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001006 if (Method != Overridden) {
1007 // We found an override at this level; there is no need to look
1008 // into other protocols or categories.
1009 Methods.push_back(Overridden);
1010 return;
1011 }
1012
1013 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Stephen Hines651f13c2014-04-23 16:59:28 -07001014 for (const auto *P : Protocol->protocols())
1015 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001016 }
1017
1018 if (const ObjCInterfaceDecl *
1019 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001020 for (const auto *P : Interface->protocols())
1021 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001022
Stephen Hines651f13c2014-04-23 16:59:28 -07001023 for (const auto *Cat : Interface->known_categories())
1024 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001025
1026 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1027 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1028 /*MovedToSuper=*/true);
1029 }
1030}
1031
1032static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1033 const ObjCMethodDecl *Method,
1034 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1035 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1036 /*MovedToSuper=*/false);
1037}
1038
1039static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1040 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1041 assert(Method->isOverriding());
1042
1043 if (const ObjCProtocolDecl *
1044 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1045 CollectOverriddenMethods(ProtD, Method, overridden);
1046
1047 } else if (const ObjCImplDecl *
1048 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1049 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1050 if (!ID)
1051 return;
1052 // Start searching for overridden methods using the method from the
1053 // interface as starting point.
1054 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00001055 Method->isInstanceMethod(),
1056 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001057 Method = IFaceMeth;
1058 CollectOverriddenMethods(ID, Method, overridden);
1059
1060 } else if (const ObjCCategoryDecl *
1061 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1062 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1063 if (!ID)
1064 return;
1065 // Start searching for overridden methods using the method from the
1066 // interface as starting point.
1067 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00001068 Method->isInstanceMethod(),
1069 /*AllowHidden=*/true))
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001070 Method = IFaceMeth;
1071 CollectOverriddenMethods(ID, Method, overridden);
1072
1073 } else {
1074 CollectOverriddenMethods(
1075 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1076 Method, overridden);
1077 }
1078}
1079
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001080void ObjCMethodDecl::getOverriddenMethods(
1081 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1082 const ObjCMethodDecl *Method = this;
1083
1084 if (Method->isRedeclaration()) {
1085 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1086 getMethod(Method->getSelector(), Method->isInstanceMethod());
1087 }
1088
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00001089 if (Method->isOverriding()) {
Argyrios Kyrtzidis740ae672012-10-09 18:19:01 +00001090 collectOverriddenMethodsSlow(Method, Overridden);
1091 assert(!Overridden.empty() &&
1092 "ObjCMethodDecl's overriding bit is not as expected");
1093 }
1094}
1095
Jordan Rose04bec392012-10-10 16:42:54 +00001096const ObjCPropertyDecl *
1097ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1098 Selector Sel = getSelector();
1099 unsigned NumArgs = Sel.getNumArgs();
1100 if (NumArgs > 1)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001101 return nullptr;
Jordan Rose04bec392012-10-10 16:42:54 +00001102
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001103 if (!isInstanceMethod())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001104 return nullptr;
1105
Jordan Rose04bec392012-10-10 16:42:54 +00001106 if (isPropertyAccessor()) {
1107 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanianc328d9c2013-01-12 00:28:34 +00001108 // If container is class extension, find its primary class.
1109 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1110 if (CatDecl->IsClassExtension())
1111 Container = CatDecl->getClassInterface();
1112
Jordan Rose04bec392012-10-10 16:42:54 +00001113 bool IsGetter = (NumArgs == 0);
1114
Stephen Hines651f13c2014-04-23 16:59:28 -07001115 for (const auto *I : Container->properties()) {
1116 Selector NextSel = IsGetter ? I->getGetterName()
1117 : I->getSetterName();
Jordan Rose04bec392012-10-10 16:42:54 +00001118 if (NextSel == Sel)
Stephen Hines651f13c2014-04-23 16:59:28 -07001119 return I;
Jordan Rose04bec392012-10-10 16:42:54 +00001120 }
1121
1122 llvm_unreachable("Marked as a property accessor but no property found!");
1123 }
1124
1125 if (!CheckOverrides)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001126 return nullptr;
Jordan Rose04bec392012-10-10 16:42:54 +00001127
1128 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1129 OverridesTy Overrides;
1130 getOverriddenMethods(Overrides);
1131 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1132 I != E; ++I) {
1133 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1134 return Prop;
1135 }
1136
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001137 return nullptr;
Jordan Rose04bec392012-10-10 16:42:54 +00001138}
1139
Chris Lattnerab351632009-02-20 20:59:54 +00001140//===----------------------------------------------------------------------===//
1141// ObjCInterfaceDecl
1142//===----------------------------------------------------------------------===//
1143
Douglas Gregora6ea10e2012-01-17 18:09:05 +00001144ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerab351632009-02-20 20:59:54 +00001145 DeclContext *DC,
1146 SourceLocation atLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001147 IdentifierInfo *Id,
Douglas Gregor0af55012011-12-16 03:12:41 +00001148 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerab351632009-02-20 20:59:54 +00001149 SourceLocation ClassLoc,
Douglas Gregor7723fec2011-12-15 20:29:51 +00001150 bool isInternal){
Stephen Hines651f13c2014-04-23 16:59:28 -07001151 ObjCInterfaceDecl *Result = new (C, DC)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001152 ObjCInterfaceDecl(C, DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001153 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor0af55012011-12-16 03:12:41 +00001154 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregor0af55012011-12-16 03:12:41 +00001155 return Result;
1156}
1157
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001158ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001159 unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001160 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
1161 SourceLocation(),
1162 nullptr,
1163 SourceLocation(),
1164 nullptr, false);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001165 Result->Data.setInt(!C.getLangOpts().Modules);
1166 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001167}
1168
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001169ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1170 SourceLocation AtLoc, IdentifierInfo *Id,
1171 SourceLocation CLoc,
1172 ObjCInterfaceDecl *PrevDecl,
1173 bool IsInternal)
1174 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
1175 redeclarable_base(C), TypeForDecl(nullptr), Data() {
Rafael Espindolabc650912013-10-17 15:37:26 +00001176 setPreviousDecl(PrevDecl);
Douglas Gregorfd002a72011-12-16 22:37:11 +00001177
1178 // Copy the 'data' pointer over.
1179 if (PrevDecl)
1180 Data = PrevDecl->Data;
1181
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001182 setImplicit(IsInternal);
Chris Lattnerab351632009-02-20 20:59:54 +00001183}
1184
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001185void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001186 assert(data().ExternallyCompleted && "Class is not externally completed");
1187 data().ExternallyCompleted = false;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001188 getASTContext().getExternalSource()->CompleteType(
1189 const_cast<ObjCInterfaceDecl *>(this));
1190}
1191
1192void ObjCInterfaceDecl::setExternallyCompleted() {
1193 assert(getASTContext().getExternalSource() &&
1194 "Class can't be externally completed without an external source");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001195 assert(hasDefinition() &&
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001196 "Forward declarations can't be externally completed");
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001197 data().ExternallyCompleted = true;
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001198}
1199
Stephen Hines651f13c2014-04-23 16:59:28 -07001200void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1201 // Check for a complete definition and recover if not so.
1202 if (!isThisDeclarationADefinition())
1203 return;
1204 data().HasDesignatedInitializers = true;
1205}
1206
1207bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1208 // Check for a complete definition and recover if not so.
1209 if (!isThisDeclarationADefinition())
1210 return false;
1211 if (data().ExternallyCompleted)
1212 LoadExternalDefinition();
1213
1214 return data().HasDesignatedInitializers;
1215}
1216
Stephen Hines176edba2014-12-01 14:53:08 -08001217StringRef
1218ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
1219 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1220 return ObjCRTName->getMetadataName();
1221
1222 return getName();
1223}
1224
1225StringRef
1226ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
1227 if (ObjCInterfaceDecl *ID =
1228 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1229 return ID->getObjCRuntimeNameAsString();
1230
1231 return getName();
1232}
1233
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001234ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001235 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1236 if (data().ExternallyCompleted)
1237 LoadExternalDefinition();
1238
1239 return getASTContext().getObjCImplementation(
1240 const_cast<ObjCInterfaceDecl*>(Def));
1241 }
1242
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001243 // FIXME: Should make sure no callers ever do this.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001244 return nullptr;
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001245}
1246
1247void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregor7723fec2011-12-15 20:29:51 +00001248 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001249}
1250
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001251namespace {
1252 struct SynthesizeIvarChunk {
1253 uint64_t Size;
1254 ObjCIvarDecl *Ivar;
1255 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1256 : Size(size), Ivar(ivar) {}
1257 };
1258
1259 bool operator<(const SynthesizeIvarChunk & LHS,
1260 const SynthesizeIvarChunk &RHS) {
1261 return LHS.Size < RHS.Size;
1262 }
1263}
1264
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001265/// all_declared_ivar_begin - return first ivar declared in this class,
1266/// its extensions and its implementation. Lazily build the list on first
1267/// access.
Adrian Prantl4919de62013-03-06 22:03:30 +00001268///
1269/// Caveat: The list returned by this method reflects the current
1270/// state of the parser. The cache will be updated for every ivar
1271/// added by an extension or the implementation when they are
1272/// encountered.
1273/// See also ObjCIvarDecl::Create().
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001274ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001275 // FIXME: Should make sure no callers ever do this.
1276 if (!hasDefinition())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001277 return nullptr;
1278
1279 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantl4919de62013-03-06 22:03:30 +00001280 if (!data().IvarList) {
1281 if (!ivar_empty()) {
1282 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1283 data().IvarList = *I; ++I;
1284 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl10b4df72013-02-27 01:31:55 +00001285 curIvar->setNextIvar(*I);
1286 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001287
Stephen Hines651f13c2014-04-23 16:59:28 -07001288 for (const auto *Ext : known_extensions()) {
Adrian Prantl4919de62013-03-06 22:03:30 +00001289 if (!Ext->ivar_empty()) {
1290 ObjCCategoryDecl::ivar_iterator
1291 I = Ext->ivar_begin(),
1292 E = Ext->ivar_end();
1293 if (!data().IvarList) {
1294 data().IvarList = *I; ++I;
1295 curIvar = data().IvarList;
1296 }
1297 for ( ;I != E; curIvar = *I, ++I)
1298 curIvar->setNextIvar(*I);
1299 }
1300 }
1301 data().IvarListMissingImplementation = true;
Adrian Prantl10b4df72013-02-27 01:31:55 +00001302 }
Adrian Prantl4919de62013-03-06 22:03:30 +00001303
1304 // cached and complete!
1305 if (!data().IvarListMissingImplementation)
1306 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001307
1308 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantl4919de62013-03-06 22:03:30 +00001309 data().IvarListMissingImplementation = false;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001310 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001311 SmallVector<SynthesizeIvarChunk, 16> layout;
Stephen Hines651f13c2014-04-23 16:59:28 -07001312 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001313 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1314 layout.push_back(SynthesizeIvarChunk(
1315 IV->getASTContext().getTypeSize(IV->getType()), IV));
1316 continue;
1317 }
1318 if (!data().IvarList)
Stephen Hines651f13c2014-04-23 16:59:28 -07001319 data().IvarList = IV;
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001320 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001321 curIvar->setNextIvar(IV);
1322 curIvar = IV;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001323 }
Fariborz Jahanian2c5d8452013-02-13 22:50:36 +00001324
1325 if (!layout.empty()) {
1326 // Order synthesized ivars by their size.
1327 std::stable_sort(layout.begin(), layout.end());
1328 unsigned Ix = 0, EIx = layout.size();
1329 if (!data().IvarList) {
1330 data().IvarList = layout[0].Ivar; Ix++;
1331 curIvar = data().IvarList;
1332 }
1333 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1334 curIvar->setNextIvar(layout[Ix].Ivar);
1335 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001336 }
1337 }
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001338 return data().IvarList;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001339}
Chris Lattnerab351632009-02-20 20:59:54 +00001340
1341/// FindCategoryDeclaration - Finds category declaration in the list of
1342/// categories for this class and returns it. Name of the category is passed
1343/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001344///
Chris Lattnerab351632009-02-20 20:59:54 +00001345ObjCCategoryDecl *
1346ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001347 // FIXME: Should make sure no callers ever do this.
1348 if (!hasDefinition())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001349 return nullptr;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +00001350
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001351 if (data().ExternallyCompleted)
Douglas Gregor26ac3f32010-12-01 23:49:52 +00001352 LoadExternalDefinition();
1353
Stephen Hines651f13c2014-04-23 16:59:28 -07001354 for (auto *Cat : visible_categories())
Douglas Gregord3297242013-01-16 23:00:23 +00001355 if (Cat->getIdentifier() == CategoryId)
Stephen Hines651f13c2014-04-23 16:59:28 -07001356 return Cat;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001357
1358 return nullptr;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001359}
1360
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001361ObjCMethodDecl *
1362ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001363 for (const auto *Cat : visible_categories()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001364 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001365 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1366 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001367 }
1368
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001369 return nullptr;
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001370}
1371
1372ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001373 for (const auto *Cat : visible_categories()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001374 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001375 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1376 return MD;
Douglas Gregord3297242013-01-16 23:00:23 +00001377 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001378
1379 return nullptr;
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +00001380}
1381
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001382/// ClassImplementsProtocol - Checks that 'lProto' protocol
1383/// has been implemented in IDecl class, its super class or categories (if
1384/// lookupCategory is true).
1385bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1386 bool lookupCategory,
1387 bool RHSIsQualifiedID) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001388 if (!hasDefinition())
1389 return false;
1390
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001391 ObjCInterfaceDecl *IDecl = this;
1392 // 1st, look up the class.
Stephen Hines651f13c2014-04-23 16:59:28 -07001393 for (auto *PI : IDecl->protocols()){
1394 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001395 return true;
1396 // This is dubious and is added to be compatible with gcc. In gcc, it is
1397 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1398 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1399 // object. This IMO, should be a bug.
1400 // FIXME: Treat this as an extension, and flag this as an error when GCC
1401 // extensions are not enabled.
Mike Stump1eb44332009-09-09 15:08:12 +00001402 if (RHSIsQualifiedID &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001403 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001404 return true;
1405 }
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001407 // 2nd, look up the category.
1408 if (lookupCategory)
Stephen Hines651f13c2014-04-23 16:59:28 -07001409 for (const auto *Cat : visible_categories()) {
1410 for (auto *PI : Cat->protocols())
1411 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001412 return true;
1413 }
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001415 // 3rd, look up the super class(s)
1416 if (IDecl->getSuperClass())
1417 return
1418 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1419 RHSIsQualifiedID);
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00001421 return false;
1422}
1423
Chris Lattnerab351632009-02-20 20:59:54 +00001424//===----------------------------------------------------------------------===//
1425// ObjCIvarDecl
1426//===----------------------------------------------------------------------===//
1427
David Blaikie99ba9e32011-12-20 02:48:34 +00001428void ObjCIvarDecl::anchor() { }
1429
Daniel Dunbara0654922010-04-02 20:10:03 +00001430ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001431 SourceLocation StartLoc,
1432 SourceLocation IdLoc, IdentifierInfo *Id,
John McCalla93c9342009-12-07 02:54:59 +00001433 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001434 AccessControl ac, Expr *BW,
Stephen Hines651f13c2014-04-23 16:59:28 -07001435 bool synthesized) {
Daniel Dunbara0654922010-04-02 20:10:03 +00001436 if (DC) {
1437 // Ivar's can only appear in interfaces, implementations (via synthesized
1438 // properties), and class extensions (via direct declaration, or synthesized
1439 // properties).
1440 //
1441 // FIXME: This should really be asserting this:
1442 // (isa<ObjCCategoryDecl>(DC) &&
1443 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1444 // but unfortunately we sometimes place ivars into non-class extension
1445 // categories on error. This breaks an AST invariant, and should not be
1446 // fixed.
1447 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1448 isa<ObjCCategoryDecl>(DC)) &&
1449 "Invalid ivar decl context!");
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001450 // Once a new ivar is created in any of class/class-extension/implementation
1451 // decl contexts, the previously built IvarList must be rebuilt.
1452 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1453 if (!ID) {
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001454 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001455 ID = IM->getClassInterface();
Eric Christopherffb0c3a2012-07-19 22:22:55 +00001456 else
1457 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001458 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001459 ID->setIvarList(nullptr);
Daniel Dunbara0654922010-04-02 20:10:03 +00001460 }
1461
Stephen Hines651f13c2014-04-23 16:59:28 -07001462 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1463 synthesized);
Chris Lattnerab351632009-02-20 20:59:54 +00001464}
1465
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001466ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001467 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1468 nullptr, QualType(), nullptr,
1469 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001470}
1471
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001472const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1473 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerab351632009-02-20 20:59:54 +00001474
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001475 switch (DC->getKind()) {
1476 default:
1477 case ObjCCategoryImpl:
1478 case ObjCProtocol:
David Blaikieb219cfc2011-09-23 05:06:16 +00001479 llvm_unreachable("invalid ivar container!");
Daniel Dunbar27a961a2010-04-02 21:13:59 +00001480
1481 // Ivars can only appear in class extension categories.
1482 case ObjCCategory: {
1483 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1484 assert(CD->IsClassExtension() && "invalid container for ivar!");
1485 return CD->getClassInterface();
1486 }
1487
1488 case ObjCImplementation:
1489 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1490
1491 case ObjCInterface:
1492 return cast<ObjCInterfaceDecl>(DC);
1493 }
1494}
Chris Lattnerab351632009-02-20 20:59:54 +00001495
1496//===----------------------------------------------------------------------===//
1497// ObjCAtDefsFieldDecl
1498//===----------------------------------------------------------------------===//
1499
David Blaikie99ba9e32011-12-20 02:48:34 +00001500void ObjCAtDefsFieldDecl::anchor() { }
1501
Chris Lattnerab351632009-02-20 20:59:54 +00001502ObjCAtDefsFieldDecl
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001503*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1504 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerab351632009-02-20 20:59:54 +00001505 IdentifierInfo *Id, QualType T, Expr *BW) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001506 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerab351632009-02-20 20:59:54 +00001507}
1508
Stephen Hines651f13c2014-04-23 16:59:28 -07001509ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001510 unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001511 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1512 SourceLocation(), nullptr, QualType(),
1513 nullptr);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001514}
1515
Chris Lattnerab351632009-02-20 20:59:54 +00001516//===----------------------------------------------------------------------===//
1517// ObjCProtocolDecl
1518//===----------------------------------------------------------------------===//
1519
David Blaikie99ba9e32011-12-20 02:48:34 +00001520void ObjCProtocolDecl::anchor() { }
1521
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001522ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1523 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor27c6da22012-01-01 20:30:41 +00001524 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001525 ObjCProtocolDecl *PrevDecl)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001526 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1527 redeclarable_base(C), Data() {
Rafael Espindolabc650912013-10-17 15:37:26 +00001528 setPreviousDecl(PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001529 if (PrevDecl)
1530 Data = PrevDecl->Data;
1531}
1532
Chris Lattnerab351632009-02-20 20:59:54 +00001533ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001534 IdentifierInfo *Id,
1535 SourceLocation nameLoc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00001536 SourceLocation atStartLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00001537 ObjCProtocolDecl *PrevDecl) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001538 ObjCProtocolDecl *Result =
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001539 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001540 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor27c6da22012-01-01 20:30:41 +00001541 return Result;
Chris Lattnerab351632009-02-20 20:59:54 +00001542}
1543
Stephen Hines651f13c2014-04-23 16:59:28 -07001544ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001545 unsigned ID) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001546 ObjCProtocolDecl *Result =
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001547 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
1548 SourceLocation(), nullptr);
Douglas Gregor6bd99292013-02-09 01:35:03 +00001549 Result->Data.setInt(!C.getLangOpts().Modules);
1550 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001551}
1552
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001553ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1554 ObjCProtocolDecl *PDecl = this;
1555
1556 if (Name == getIdentifier())
1557 return PDecl;
1558
Stephen Hines651f13c2014-04-23 16:59:28 -07001559 for (auto *I : protocols())
1560 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001561 return PDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001563 return nullptr;
Steve Naroff91b0b0c2009-03-01 16:12:44 +00001564}
1565
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001566// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerab351632009-02-20 20:59:54 +00001567// it inherited.
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001568ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1569 bool isInstance) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001570 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Douglas Gregor0f9b9f32013-01-17 00:38:46 +00001572 // If there is no definition or the definition is hidden, we don't find
1573 // anything.
1574 const ObjCProtocolDecl *Def = getDefinition();
1575 if (!Def || Def->isHidden())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001576 return nullptr;
Douglas Gregor0f9b9f32013-01-17 00:38:46 +00001577
Argyrios Kyrtzidis094e2bb2009-07-25 22:15:38 +00001578 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001579 return MethodDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Stephen Hines651f13c2014-04-23 16:59:28 -07001581 for (const auto *I : protocols())
1582 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerab351632009-02-20 20:59:54 +00001583 return MethodDecl;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001584 return nullptr;
Chris Lattnerab351632009-02-20 20:59:54 +00001585}
1586
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001587void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor6bd99292013-02-09 01:35:03 +00001588 assert(!Data.getPointer() && "Protocol already has a definition!");
1589 Data.setPointer(new (getASTContext()) DefinitionData);
1590 Data.getPointer()->Definition = this;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00001591}
1592
1593void ObjCProtocolDecl::startDefinition() {
1594 allocateDefinitionData();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001595
1596 // Update all of the declarations with a pointer to the definition.
Stephen Hines651f13c2014-04-23 16:59:28 -07001597 for (auto RD : redecls())
Douglas Gregor1d784b22012-01-01 19:51:50 +00001598 RD->Data = this->Data;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00001599}
1600
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001601void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1602 PropertyDeclOrder &PO) const {
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001603
1604 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001605 for (auto *Prop : PDecl->properties()) {
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001606 // Insert into PM if not there already.
1607 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001608 PO.push_back(Prop);
Fariborz Jahaniancc5a28a2013-01-07 21:31:08 +00001609 }
1610 // Scan through protocol's protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -07001611 for (const auto *PI : PDecl->protocols())
1612 PI->collectPropertiesToImplement(PM, PO);
Anna Zaksb36ea372012-10-18 19:17:53 +00001613 }
Anna Zaksb36ea372012-10-18 19:17:53 +00001614}
1615
Fariborz Jahanian8dbda512013-05-20 21:20:24 +00001616
1617void ObjCProtocolDecl::collectInheritedProtocolProperties(
1618 const ObjCPropertyDecl *Property,
1619 ProtocolPropertyMap &PM) const {
1620 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1621 bool MatchFound = false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001622 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian8dbda512013-05-20 21:20:24 +00001623 if (Prop == Property)
1624 continue;
1625 if (Prop->getIdentifier() == Property->getIdentifier()) {
1626 PM[PDecl] = Prop;
1627 MatchFound = true;
1628 break;
1629 }
1630 }
1631 // Scan through protocol's protocols which did not have a matching property.
1632 if (!MatchFound)
Stephen Hines651f13c2014-04-23 16:59:28 -07001633 for (const auto *PI : PDecl->protocols())
1634 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian8dbda512013-05-20 21:20:24 +00001635 }
1636}
Anna Zaksb36ea372012-10-18 19:17:53 +00001637
Stephen Hines176edba2014-12-01 14:53:08 -08001638StringRef
1639ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
1640 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1641 return ObjCRTName->getMetadataName();
1642
1643 return getName();
1644}
1645
Chris Lattnerab351632009-02-20 20:59:54 +00001646//===----------------------------------------------------------------------===//
Chris Lattnerab351632009-02-20 20:59:54 +00001647// ObjCCategoryDecl
1648//===----------------------------------------------------------------------===//
1649
David Blaikie99ba9e32011-12-20 02:48:34 +00001650void ObjCCategoryDecl::anchor() { }
1651
Chris Lattnerab351632009-02-20 20:59:54 +00001652ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Stephen Hines651f13c2014-04-23 16:59:28 -07001653 SourceLocation AtLoc,
Douglas Gregor3db211b2010-01-16 16:38:58 +00001654 SourceLocation ClassNameLoc,
1655 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001656 IdentifierInfo *Id,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001657 ObjCInterfaceDecl *IDecl,
1658 SourceLocation IvarLBraceLoc,
1659 SourceLocation IvarRBraceLoc) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001660 ObjCCategoryDecl *CatDecl =
1661 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1662 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001663 if (IDecl) {
1664 // Link this category into its class's category list.
Douglas Gregord3297242013-01-16 23:00:23 +00001665 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001666 if (IDecl->hasDefinition()) {
Douglas Gregord3297242013-01-16 23:00:23 +00001667 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001668 if (ASTMutationListener *L = C.getASTMutationListener())
1669 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1670 }
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00001671 }
1672
1673 return CatDecl;
1674}
1675
Stephen Hines651f13c2014-04-23 16:59:28 -07001676ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001677 unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001678 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1679 SourceLocation(), SourceLocation(),
1680 nullptr, nullptr);
Chris Lattnerab351632009-02-20 20:59:54 +00001681}
1682
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001683ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1684 return getASTContext().getObjCImplementation(
1685 const_cast<ObjCCategoryDecl*>(this));
1686}
1687
1688void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1689 getASTContext().setObjCImplementation(this, ImplD);
1690}
1691
1692
Chris Lattnerab351632009-02-20 20:59:54 +00001693//===----------------------------------------------------------------------===//
1694// ObjCCategoryImplDecl
1695//===----------------------------------------------------------------------===//
1696
David Blaikie99ba9e32011-12-20 02:48:34 +00001697void ObjCCategoryImplDecl::anchor() { }
1698
Chris Lattnerab351632009-02-20 20:59:54 +00001699ObjCCategoryImplDecl *
1700ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001701 IdentifierInfo *Id,
1702 ObjCInterfaceDecl *ClassInterface,
1703 SourceLocation nameLoc,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00001704 SourceLocation atStartLoc,
1705 SourceLocation CategoryNameLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001706 if (ClassInterface && ClassInterface->hasDefinition())
1707 ClassInterface = ClassInterface->getDefinition();
Stephen Hines651f13c2014-04-23 16:59:28 -07001708 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1709 atStartLoc, CategoryNameLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001710}
1711
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001712ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1713 unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001714 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1715 SourceLocation(), SourceLocation(),
1716 SourceLocation());
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001717}
1718
Steve Naroff0d69b8c2009-10-29 21:11:04 +00001719ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremenekebfa3392010-03-19 20:39:03 +00001720 // The class interface might be NULL if we are working with invalid code.
1721 if (const ObjCInterfaceDecl *ID = getClassInterface())
1722 return ID->FindCategoryDeclaration(getIdentifier());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001723 return nullptr;
Argyrios Kyrtzidis42920732009-07-28 05:11:05 +00001724}
1725
Chris Lattnerab351632009-02-20 20:59:54 +00001726
David Blaikie99ba9e32011-12-20 02:48:34 +00001727void ObjCImplDecl::anchor() { }
1728
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001729void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor2c2d43c2009-04-23 02:42:49 +00001730 // FIXME: The context should be correct before we get here.
Douglas Gregor653f1b12009-04-23 01:02:12 +00001731 property->setLexicalDeclContext(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001732 addDecl(property);
Douglas Gregor653f1b12009-04-23 01:02:12 +00001733}
1734
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001735void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1736 ASTContext &Ctx = getASTContext();
1737
1738 if (ObjCImplementationDecl *ImplD
Duncan Sands98f2cca2009-07-21 07:56:29 +00001739 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001740 if (IFace)
1741 Ctx.setObjCImplementation(IFace, ImplD);
1742
Duncan Sands98f2cca2009-07-21 07:56:29 +00001743 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001744 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1745 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1746 Ctx.setObjCImplementation(CD, ImplD);
1747 }
1748
1749 ClassInterface = IFace;
1750}
1751
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001752/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahanian6d1cb5c2013-03-12 17:43:00 +00001753/// properties implemented in this \@implementation block and returns
Chris Lattnerd6eed1c2009-02-16 19:24:31 +00001754/// the implemented property that uses it.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001755///
Chris Lattner3aa18612009-02-28 18:42:10 +00001756ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001757FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001758 for (auto *PID : property_impls())
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001759 if (PID->getPropertyIvarDecl() &&
1760 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1761 return PID;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001762 return nullptr;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001763}
1764
1765/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett1c3a46a2012-06-15 22:30:14 +00001766/// added to the list of those properties \@synthesized/\@dynamic in this
1767/// category \@implementation block.
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001768///
Chris Lattner3aa18612009-02-28 18:42:10 +00001769ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001770FindPropertyImplDecl(IdentifierInfo *Id) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001771 for (auto *PID : property_impls())
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001772 if (PID->getPropertyDecl()->getIdentifier() == Id)
1773 return PID;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001774 return nullptr;
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001775}
1776
Chris Lattner5f9e2722011-07-23 10:55:15 +00001777raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001778 const ObjCCategoryImplDecl &CID) {
1779 OS << CID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001780 return OS;
1781}
1782
Chris Lattnerab351632009-02-20 20:59:54 +00001783//===----------------------------------------------------------------------===//
1784// ObjCImplementationDecl
1785//===----------------------------------------------------------------------===//
1786
David Blaikie99ba9e32011-12-20 02:48:34 +00001787void ObjCImplementationDecl::anchor() { }
1788
Chris Lattnerab351632009-02-20 20:59:54 +00001789ObjCImplementationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +00001790ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerab351632009-02-20 20:59:54 +00001791 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001792 ObjCInterfaceDecl *SuperDecl,
1793 SourceLocation nameLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001794 SourceLocation atStartLoc,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001795 SourceLocation superLoc,
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001796 SourceLocation IvarLBraceLoc,
1797 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian712ef872011-12-23 00:31:02 +00001798 if (ClassInterface && ClassInterface->hasDefinition())
1799 ClassInterface = ClassInterface->getDefinition();
Stephen Hines651f13c2014-04-23 16:59:28 -07001800 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1801 nameLoc, atStartLoc, superLoc,
1802 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerab351632009-02-20 20:59:54 +00001803}
1804
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001805ObjCImplementationDecl *
1806ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001807 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
1808 SourceLocation(), SourceLocation());
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001809}
1810
John McCallda6d9762011-07-22 04:15:06 +00001811void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1812 CXXCtorInitializer ** initializers,
1813 unsigned numInitializers) {
1814 if (numInitializers > 0) {
1815 NumIvarInitializers = numInitializers;
1816 CXXCtorInitializer **ivarInitializers =
1817 new (C) CXXCtorInitializer*[NumIvarInitializers];
1818 memcpy(ivarInitializers, initializers,
1819 numInitializers * sizeof(CXXCtorInitializer*));
1820 IvarInitializers = ivarInitializers;
1821 }
1822}
1823
Chris Lattner5f9e2722011-07-23 10:55:15 +00001824raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramerf9780592012-02-07 11:57:45 +00001825 const ObjCImplementationDecl &ID) {
1826 OS << ID.getName();
Benjamin Kramer900fc632010-04-17 09:33:03 +00001827 return OS;
1828}
1829
Chris Lattnerab351632009-02-20 20:59:54 +00001830//===----------------------------------------------------------------------===//
1831// ObjCCompatibleAliasDecl
1832//===----------------------------------------------------------------------===//
1833
David Blaikie99ba9e32011-12-20 02:48:34 +00001834void ObjCCompatibleAliasDecl::anchor() { }
1835
Chris Lattnerab351632009-02-20 20:59:54 +00001836ObjCCompatibleAliasDecl *
1837ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1838 SourceLocation L,
Mike Stump1eb44332009-09-09 15:08:12 +00001839 IdentifierInfo *Id,
Chris Lattnerab351632009-02-20 20:59:54 +00001840 ObjCInterfaceDecl* AliasedClass) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001841 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerab351632009-02-20 20:59:54 +00001842}
1843
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001844ObjCCompatibleAliasDecl *
1845ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001846 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
1847 nullptr, nullptr);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001848}
1849
Chris Lattnerab351632009-02-20 20:59:54 +00001850//===----------------------------------------------------------------------===//
1851// ObjCPropertyDecl
1852//===----------------------------------------------------------------------===//
1853
David Blaikie99ba9e32011-12-20 02:48:34 +00001854void ObjCPropertyDecl::anchor() { }
1855
Chris Lattnerab351632009-02-20 20:59:54 +00001856ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1857 SourceLocation L,
1858 IdentifierInfo *Id,
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001859 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001860 SourceLocation LParenLoc,
John McCall83a230c2010-06-04 20:50:08 +00001861 TypeSourceInfo *T,
Chris Lattnerab351632009-02-20 20:59:54 +00001862 PropertyControl propControl) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001863 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerab351632009-02-20 20:59:54 +00001864}
1865
Stephen Hines651f13c2014-04-23 16:59:28 -07001866ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001867 unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001868 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
1869 SourceLocation(), SourceLocation(),
1870 nullptr);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001871}
1872
Chris Lattnerab351632009-02-20 20:59:54 +00001873//===----------------------------------------------------------------------===//
1874// ObjCPropertyImplDecl
1875//===----------------------------------------------------------------------===//
1876
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001877ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregord0434102009-01-09 00:49:46 +00001878 DeclContext *DC,
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001879 SourceLocation atLoc,
1880 SourceLocation L,
1881 ObjCPropertyDecl *property,
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001882 Kind PK,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001883 ObjCIvarDecl *ivar,
1884 SourceLocation ivarLoc) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001885 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1886 ivarLoc);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001887}
Chris Lattnerf4af5152008-03-17 01:19:02 +00001888
Stephen Hines651f13c2014-04-23 16:59:28 -07001889ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001890 unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001891 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
1892 SourceLocation(), nullptr, Dynamic,
1893 nullptr, SourceLocation());
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001894}
1895
Douglas Gregora4ffd852010-11-17 01:03:52 +00001896SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1897 SourceLocation EndLoc = getLocation();
1898 if (IvarLoc.isValid())
1899 EndLoc = IvarLoc;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001900
Douglas Gregora4ffd852010-11-17 01:03:52 +00001901 return SourceRange(AtLoc, EndLoc);
1902}