blob: a1600cb1101480c4b40e28d41d56b8a2d28d24e3 [file] [log] [blame]
Chris Lattner89375192008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000016#include "clang/AST/ASTMutationListener.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/Stmt.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000019#include "llvm/ADT/STLExtras.h"
Anna Zaks454477c2012-09-27 19:45:11 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner89375192008-03-16 00:19:01 +000021using namespace clang;
22
Chris Lattner8d8829e2008-03-16 00:49:28 +000023//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000024// ObjCListBase
25//===----------------------------------------------------------------------===//
26
Chris Lattner22298722009-02-20 21:35:13 +000027void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Craig Topper36250ad2014-05-12 05:36:57 +000028 List = nullptr;
Chris Lattner4d1eb762009-02-20 21:16:26 +000029 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000030
31
Chris Lattner7c981a72009-02-20 21:44:01 +000032 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000033 NumElts = Elts;
34 memcpy(List, InList, sizeof(void*)*Elts);
35}
36
Douglas Gregor002b6712010-01-16 15:02:53 +000037void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
38 const SourceLocation *Locs, ASTContext &Ctx) {
39 if (Elts == 0)
40 return;
41
42 Locations = new (Ctx) SourceLocation[Elts];
43 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
44 set(InList, Elts, Ctx);
45}
46
Chris Lattner4d1eb762009-02-20 21:16:26 +000047//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000048// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000049//===----------------------------------------------------------------------===//
50
David Blaikie68e081d2011-12-20 02:48:34 +000051void ObjCContainerDecl::anchor() { }
52
Fariborz Jahanian68453832009-06-05 18:16:35 +000053/// getIvarDecl - This method looks up an ivar in this ContextDecl.
54///
55ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000056ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Richard Smithcf4bdde2015-02-21 02:45:19 +000057 lookup_result R = lookup(Id);
58 for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000059 Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian68453832009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
Craig Topper36250ad2014-05-12 05:36:57 +000063 return nullptr;
Fariborz Jahanian68453832009-06-05 18:16:35 +000064}
65
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000066// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000067ObjCMethodDecl *
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69 bool AllowHidden) const {
Douglas Gregoreed49792013-01-17 00:38:46 +000070 // If this context is a hidden protocol definition, don't find any
71 // methods there.
72 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
73 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000074 if (Def->isHidden() && !AllowHidden)
Craig Topper36250ad2014-05-12 05:36:57 +000075 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +000076 }
77
Steve Naroffc4173fa2009-02-22 19:35:57 +000078 // Since instance & class methods can have the same name, the loop below
79 // ensures we get the correct method.
80 //
81 // @interface Whatever
82 // - (int) class_method;
83 // + (float) class_method;
84 // @end
85 //
Richard Smithcf4bdde2015-02-21 02:45:19 +000086 lookup_result R = lookup(Sel);
87 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000088 Meth != MethEnd; ++Meth) {
Steve Naroffc4173fa2009-02-22 19:35:57 +000089 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000090 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +000091 return MD;
92 }
Craig Topper36250ad2014-05-12 05:36:57 +000093 return nullptr;
Steve Naroff35c62ae2009-01-08 17:28:14 +000094}
95
Nico Weber4701ffd2014-12-22 05:21:03 +000096/// \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 Jahanian1446b342013-03-21 20:50:53 +0000103 Selector Sel = Property->getSetterName();
Richard Smithcf4bdde2015-02-21 02:45:19 +0000104 lookup_result R = lookup(Sel);
105 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
Fariborz Jahanian1446b342013-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.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000115 for (const auto *Cat : ID->visible_categories()) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000116 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
117 if (!MD->isImplicit())
118 return true;
119 if (Cat->IsClassExtension())
120 continue;
Nico Weber4701ffd2014-12-22 05:21:03 +0000121 // 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).
Aaron Ballmand174edf2014-03-13 19:11:50 +0000125 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-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.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000134 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000135 if (Proto->HasUserDeclaredSetterMethod(Property))
136 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000137
Fariborz Jahanian1446b342013-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))
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000147 for (const auto *PI : PD->protocols())
148 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000149 return true;
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000150 return false;
151}
152
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000153ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000154ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Jordan Rose210bfe92014-11-19 22:03:46 +0000155 const IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-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())
Craig Topper36250ad2014-05-12 05:36:57 +0000161 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000162 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000163
Richard Smithcf4bdde2015-02-21 02:45:19 +0000164 DeclContext::lookup_result R = DC->lookup(propertyID);
165 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
David Blaikieff7d47a2012-12-19 00:45:41 +0000166 ++I)
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000167 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
168 return PD;
169
Craig Topper36250ad2014-05-12 05:36:57 +0000170 return nullptr;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000171}
172
Anna Zaks454477c2012-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 Jahaniana054e992008-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.
Jordan Rose210bfe92014-11-19 22:03:46 +0000185ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
186 const IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-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())
Craig Topper36250ad2014-05-12 05:36:57 +0000191 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Ted Kremenekddcd1092010-03-15 20:11:53 +0000194 if (ObjCPropertyDecl *PD =
195 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
196 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Ted Kremenekddcd1092010-03-15 20:11:53 +0000198 switch (getKind()) {
199 default:
200 break;
201 case Decl::ObjCProtocol: {
202 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000203 for (const auto *I : PID->protocols())
204 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000205 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000206 break;
207 }
208 case Decl::ObjCInterface: {
209 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000210 // Look through categories (but not extensions).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000211 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000212 if (!Cat->IsClassExtension())
213 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
214 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000215 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000216
217 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000218 for (const auto *I : OID->all_referenced_protocols())
219 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekddcd1092010-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())
Aaron Ballman19a41762014-03-14 12:55:57 +0000231 for (const auto *I : OCD->protocols())
232 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
233 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000234 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000235 }
236 }
Craig Topper36250ad2014-05-12 05:36:57 +0000237 return nullptr;
Steve Narofff9c65242008-06-05 13:55:23 +0000238}
239
David Blaikie68e081d2011-12-20 02:48:34 +0000240void ObjCInterfaceDecl::anchor() { }
241
Fariborz Jahaniande8db162009-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 Kremenekd133a862010-03-15 20:30:07 +0000244/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000245///
246ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000247ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000248 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000249 // FIXME: Should make sure no callers ever do this.
250 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000251 return nullptr;
252
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000253 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000254 LoadExternalDefinition();
255
Ted Kremenekd133a862010-03-15 20:30:07 +0000256 if (ObjCPropertyDecl *PD =
257 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
258 return PD;
259
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000260 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000261 for (const auto *I : all_referenced_protocols())
262 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000263 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000264
Craig Topper36250ad2014-05-12 05:36:57 +0000265 return nullptr;
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000266}
267
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000268void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
269 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000270 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000271 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000272 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000273 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000274 for (const auto *PI : all_referenced_protocols())
275 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-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 Zaks673d76b2012-10-18 19:17:53 +0000279}
280
Benjamin Kramerea70eb32012-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 }
Craig Topper36250ad2014-05-12 05:36:57 +0000298 return nullptr;
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000299}
300
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000301void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
302 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
303 ASTContext &C)
304{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000305 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000306 LoadExternalDefinition();
307
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000308 if (data().AllReferencedProtocols.empty() &&
309 data().ReferencedProtocols.empty()) {
310 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000311 return;
312 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000313
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000314 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000315 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000316 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000317 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000318 for (unsigned i = 0; i < ExtNum; i++) {
319 bool protocolExists = false;
320 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000321 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-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 Kremenek0ef508d2010-09-01 01:21:15 +0000329 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000330 ProtocolRefs.push_back(ProtoInExtension);
331 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000332
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000333 if (ProtocolRefs.empty())
334 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000335
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000336 // Merge ProtocolRefs into class's protocol list;
Benjamin Kramerf9890422015-02-17 16:48:30 +0000337 ProtocolRefs.append(all_referenced_protocol_begin(),
338 all_referenced_protocol_end());
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000339
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000340 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000341}
342
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000343const ObjCInterfaceDecl *
344ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
345 const ObjCInterfaceDecl *IFace = this;
346 while (IFace) {
347 if (IFace->hasDesignatedInitializers())
348 return IFace;
349 if (!IFace->inheritsDesignatedInitializers())
350 break;
351 IFace = IFace->getSuperClass();
352 }
Craig Topper36250ad2014-05-12 05:36:57 +0000353 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000354}
355
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000356static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
357 for (const auto *MD : D->instance_methods()) {
358 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
359 return true;
360 }
361 for (const auto *Ext : D->visible_extensions()) {
362 for (const auto *MD : Ext->instance_methods()) {
363 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
364 return true;
365 }
366 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000367 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000368 for (const auto *MD : ImplD->instance_methods()) {
369 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
370 return true;
371 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000372 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000373 return false;
374}
375
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000376bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
377 switch (data().InheritedDesignatedInitializers) {
378 case DefinitionData::IDI_Inherited:
379 return true;
380 case DefinitionData::IDI_NotInherited:
381 return false;
382 case DefinitionData::IDI_Unknown: {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000383 // If the class introduced initializers we conservatively assume that we
384 // don't know if any of them is a designated initializer to avoid possible
385 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000386 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000387 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000388 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000389 if (auto SuperD = getSuperClass()) {
390 data().InheritedDesignatedInitializers =
391 SuperD->declaresOrInheritsDesignatedInitializers() ?
392 DefinitionData::IDI_Inherited :
393 DefinitionData::IDI_NotInherited;
394 } else {
395 data().InheritedDesignatedInitializers =
396 DefinitionData::IDI_NotInherited;
397 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000398 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000399 assert(data().InheritedDesignatedInitializers
400 != DefinitionData::IDI_Unknown);
401 return data().InheritedDesignatedInitializers ==
402 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000403 }
404 }
405
406 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
407}
408
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000409void ObjCInterfaceDecl::getDesignatedInitializers(
410 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000411 // Check for a complete definition and recover if not so.
412 if (!isThisDeclarationADefinition())
413 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000414 if (data().ExternallyCompleted)
415 LoadExternalDefinition();
416
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000417 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000418 if (!IFace)
419 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000420
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000421 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000422 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000423 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000424 for (const auto *Ext : IFace->visible_extensions()) {
425 for (const auto *MD : Ext->instance_methods())
426 if (MD->isThisDeclarationADesignatedInitializer())
427 Methods.push_back(MD);
428 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000429}
430
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000431bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
432 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000433 // Check for a complete definition and recover if not so.
434 if (!isThisDeclarationADefinition())
435 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000436 if (data().ExternallyCompleted)
437 LoadExternalDefinition();
438
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000439 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000440 if (!IFace)
441 return false;
442
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000443 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000444 if (MD->isThisDeclarationADesignatedInitializer()) {
445 if (InitMethod)
446 *InitMethod = MD;
447 return true;
448 }
449 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000450 for (const auto *Ext : IFace->visible_extensions()) {
451 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
452 if (MD->isThisDeclarationADesignatedInitializer()) {
453 if (InitMethod)
454 *InitMethod = MD;
455 return true;
456 }
457 }
458 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000459 return false;
460}
461
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000462void ObjCInterfaceDecl::allocateDefinitionData() {
463 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000464 Data.setPointer(new (getASTContext()) DefinitionData());
465 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000466
467 // Make the type point at the definition, now that we have one.
468 if (TypeForDecl)
469 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000470}
471
472void ObjCInterfaceDecl::startDefinition() {
473 allocateDefinitionData();
474
Douglas Gregor66b310c2011-12-15 18:03:09 +0000475 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000476 for (auto RD : redecls()) {
477 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000478 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000479 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000480}
481
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000482ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
483 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000484 // FIXME: Should make sure no callers ever do this.
485 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000486 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000487
488 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000489 LoadExternalDefinition();
490
Chris Lattner89375192008-03-16 00:19:01 +0000491 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000492 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000493 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000494 clsDeclared = ClassDecl;
495 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000496 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000497
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000498 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000499 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000500 clsDeclared = ClassDecl;
501 return I;
502 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000503 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000504
Chris Lattner89375192008-03-16 00:19:01 +0000505 ClassDecl = ClassDecl->getSuperClass();
506 }
Craig Topper36250ad2014-05-12 05:36:57 +0000507 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000508}
509
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000510/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
511/// class whose name is passed as argument. If it is not one of the super classes
512/// the it returns NULL.
513ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
514 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000515 // FIXME: Should make sure no callers ever do this.
516 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000517 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000518
519 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000520 LoadExternalDefinition();
521
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000522 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000523 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000524 if (ClassDecl->getIdentifier() == ICName)
525 return ClassDecl;
526 ClassDecl = ClassDecl->getSuperClass();
527 }
Craig Topper36250ad2014-05-12 05:36:57 +0000528 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000529}
530
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000531ObjCProtocolDecl *
532ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000533 for (auto *P : all_referenced_protocols())
534 if (P->lookupProtocolNamed(Name))
535 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000536 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000537 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000538}
539
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000540/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000541/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000542/// When argument category "C" is specified, any implicit method found
543/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000544ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000545 bool isInstance,
546 bool shallowCategoryLookup,
547 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000548 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000549{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000550 // FIXME: Should make sure no callers ever do this.
551 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000552 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000553
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000554 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000555 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000556
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000557 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000558 LoadExternalDefinition();
559
Ted Kremenek00781502013-11-23 01:01:29 +0000560 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000561 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000562 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000563 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000564
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000565 // 2. Didn't find one yet - now look through categories.
566 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000567 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000568 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000569 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000570
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000571 // 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 Jahanian73e244a2013-04-25 21:59:34 +0000579 // Didn't find one yet - look through protocols.
580 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000581 Cat->getReferencedProtocols();
Fariborz Jahanian73e244a2013-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)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000585 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000586 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000587 }
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000588
589
Ted Kremenek00781502013-11-23 01:01:29 +0000590 if (!followSuper)
Craig Topper36250ad2014-05-12 05:36:57 +0000591 return nullptr;
Ted Kremenek00781502013-11-23 01:01:29 +0000592
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000593 // 5. Get to the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000594 ClassDecl = ClassDecl->getSuperClass();
595 }
Craig Topper36250ad2014-05-12 05:36:57 +0000596 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000597}
598
Anna Zaksc77a3b12012-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 Jahanianecbbb6e2010-12-03 23:37:08 +0000602ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
603 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000604 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000605 // FIXME: Should make sure no callers ever do this.
606 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000607 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000608
609 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000610 LoadExternalDefinition();
611
Craig Topper36250ad2014-05-12 05:36:57 +0000612 ObjCMethodDecl *Method = nullptr;
Steve Naroffbb69c942009-10-01 23:46:04 +0000613 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000614 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
615 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000616
617 // Look through local category implementations associated with the class.
618 if (!Method)
Nico Weberf6098392015-03-02 01:12:28 +0000619 Method = getCategoryMethod(Sel, Instance);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000620
621 // Before we give up, check if the selector is an instance method.
622 // But only in the root. This matches gcc's behavior and what the
623 // runtime expects.
624 if (!Instance && !Method && !getSuperClass()) {
625 Method = lookupInstanceMethod(Sel);
626 // Look through local category implementations associated
627 // with the root class.
628 if (!Method)
629 Method = lookupPrivateMethod(Sel, true);
630 }
631
Steve Naroffbb69c942009-10-01 23:46:04 +0000632 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000633 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000634 return Method;
635}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000636
637//===----------------------------------------------------------------------===//
638// ObjCMethodDecl
639//===----------------------------------------------------------------------===//
640
Alp Toker314cc812014-01-25 16:55:45 +0000641ObjCMethodDecl *ObjCMethodDecl::Create(
642 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
643 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
644 DeclContext *contextDecl, bool isInstance, bool isVariadic,
645 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
646 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000647 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000648 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000649 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
650 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000651}
652
Douglas Gregor72172e92012-01-05 21:55:30 +0000653ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000654 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000655 Selector(), QualType(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000656}
657
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000658bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
659 return getMethodFamily() == OMF_init &&
660 hasAttr<ObjCDesignatedInitializerAttr>();
661}
662
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000663bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
664 const ObjCMethodDecl **InitMethod) const {
665 if (getMethodFamily() != OMF_init)
666 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000667 const DeclContext *DC = getDeclContext();
668 if (isa<ObjCProtocolDecl>(DC))
669 return false;
670 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000671 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000672 return false;
673}
674
Douglas Gregora6017bb2012-10-09 17:21:28 +0000675Stmt *ObjCMethodDecl::getBody() const {
676 return Body.get(getASTContext().getExternalSource());
677}
678
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000679void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
680 assert(PrevMethod);
681 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
682 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000683 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000684}
685
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000686void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
687 ArrayRef<ParmVarDecl*> Params,
688 ArrayRef<SourceLocation> SelLocs) {
Craig Topper36250ad2014-05-12 05:36:57 +0000689 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000690 NumParams = Params.size();
691 if (Params.empty() && SelLocs.empty())
692 return;
693
694 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
695 sizeof(SourceLocation) * SelLocs.size();
696 ParamsAndSelLocs = C.Allocate(Size);
697 std::copy(Params.begin(), Params.end(), getParams());
698 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
699}
700
701void ObjCMethodDecl::getSelectorLocs(
702 SmallVectorImpl<SourceLocation> &SelLocs) const {
703 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
704 SelLocs.push_back(getSelectorLoc(i));
705}
706
707void ObjCMethodDecl::setMethodParams(ASTContext &C,
708 ArrayRef<ParmVarDecl*> Params,
709 ArrayRef<SourceLocation> SelLocs) {
710 assert((!SelLocs.empty() || isImplicit()) &&
711 "No selector locs for non-implicit method");
712 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000713 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000714
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000715 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
716 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000717 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000718 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000719
720 setParamsAndSelLocs(C, Params, SelLocs);
721}
722
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000723/// \brief A definition will return its interface declaration.
724/// An interface declaration will return its definition.
725/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000726ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000727 ASTContext &Ctx = getASTContext();
Craig Topper36250ad2014-05-12 05:36:57 +0000728 ObjCMethodDecl *Redecl = nullptr;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000729 if (HasRedeclaration)
730 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000731 if (Redecl)
732 return Redecl;
733
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000734 Decl *CtxD = cast<Decl>(getDeclContext());
735
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000736 if (!CtxD->isInvalidDecl()) {
737 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
738 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
739 if (!ImplD->isInvalidDecl())
740 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000741
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000742 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
743 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
744 if (!ImplD->isInvalidDecl())
745 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000746
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000747 } else if (ObjCImplementationDecl *ImplD =
748 dyn_cast<ObjCImplementationDecl>(CtxD)) {
749 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
750 if (!IFD->isInvalidDecl())
751 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000752
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000753 } else if (ObjCCategoryImplDecl *CImplD =
754 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
755 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
756 if (!CatD->isInvalidDecl())
757 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
758 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000759 }
760
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000761 if (!Redecl && isRedeclaration()) {
762 // This is the last redeclaration, go back to the first method.
763 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
764 isInstanceMethod());
765 }
766
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000767 return Redecl ? Redecl : this;
768}
769
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000770ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
771 Decl *CtxD = cast<Decl>(getDeclContext());
772
773 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
774 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
775 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
776 isInstanceMethod()))
777 return MD;
778
779 } else if (ObjCCategoryImplDecl *CImplD =
780 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000781 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000782 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
783 isInstanceMethod()))
784 return MD;
785 }
786
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000787 if (isRedeclaration())
788 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
789 isInstanceMethod());
790
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000791 return this;
792}
793
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000794SourceLocation ObjCMethodDecl::getLocEnd() const {
795 if (Stmt *Body = getBody())
796 return Body->getLocEnd();
797 return DeclEndLoc;
798}
799
John McCallb4526252011-03-02 01:50:55 +0000800ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
801 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000802 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000803 return family;
804
John McCall86bc21f2011-03-02 11:33:24 +0000805 // Check for an explicit attribute.
806 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
807 // The unfortunate necessity of mapping between enums here is due
808 // to the attributes framework.
809 switch (attr->getFamily()) {
810 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
811 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
812 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
813 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
814 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
815 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
816 }
817 Family = static_cast<unsigned>(family);
818 return family;
819 }
820
John McCallb4526252011-03-02 01:50:55 +0000821 family = getSelector().getMethodFamily();
822 switch (family) {
823 case OMF_None: break;
824
825 // init only has a conventional meaning for an instance method, and
826 // it has to return an object.
827 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000828 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000829 family = OMF_None;
830 break;
831
832 // alloc/copy/new have a conventional meaning for both class and
833 // instance methods, but they require an object return.
834 case OMF_alloc:
835 case OMF_copy:
836 case OMF_mutableCopy:
837 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000838 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000839 family = OMF_None;
840 break;
841
842 // These selectors have a conventional meaning only for instance methods.
843 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000844 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000845 case OMF_retain:
846 case OMF_release:
847 case OMF_autorelease:
848 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000849 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000850 if (!isInstanceMethod())
851 family = OMF_None;
852 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000853
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000854 case OMF_initialize:
855 if (isInstanceMethod() || !getReturnType()->isVoidType())
856 family = OMF_None;
857 break;
858
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000859 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000860 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000861 family = OMF_None;
862 else {
863 unsigned noParams = param_size();
864 if (noParams < 1 || noParams > 3)
865 family = OMF_None;
866 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000867 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000868 QualType ArgT = (*it);
869 if (!ArgT->isObjCSelType()) {
870 family = OMF_None;
871 break;
872 }
873 while (--noParams) {
874 it++;
875 ArgT = (*it);
876 if (!ArgT->isObjCIdType()) {
877 family = OMF_None;
878 break;
879 }
880 }
881 }
882 }
883 break;
884
John McCallb4526252011-03-02 01:50:55 +0000885 }
886
887 // Cache the result.
888 Family = static_cast<unsigned>(family);
889 return family;
890}
891
Mike Stump11289f42009-09-09 15:08:12 +0000892void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000893 const ObjCInterfaceDecl *OID) {
894 QualType selfTy;
895 if (isInstanceMethod()) {
896 // There may be no interface context due to error in declaration
897 // of the interface (which has been reported). Recover gracefully.
898 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000899 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000900 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000901 } else {
902 selfTy = Context.getObjCIdType();
903 }
904 } else // we have a factory method.
905 selfTy = Context.getObjCClassType();
906
John McCalld4631322011-06-17 06:42:21 +0000907 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000908 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000909
David Blaikiebbafb8a2012-03-11 07:00:24 +0000910 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000911 if (isInstanceMethod()) {
912 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000913
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000914 // 'self' is always __strong. It's actually pseudo-strong except
915 // in init methods (or methods labeled ns_consumes_self), though.
916 Qualifiers qs;
917 qs.setObjCLifetime(Qualifiers::OCL_Strong);
918 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000919
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000920 // In addition, 'self' is const unless this is an init method.
921 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
922 selfTy = selfTy.withConst();
923 selfIsPseudoStrong = true;
924 }
925 }
926 else {
927 assert(isClassMethod());
928 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000929 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000930 selfIsPseudoStrong = true;
931 }
John McCall31168b02011-06-15 23:02:42 +0000932 }
933
934 ImplicitParamDecl *self
935 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
936 &Context.Idents.get("self"), selfTy);
937 setSelfDecl(self);
938
939 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000940 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000941
John McCalld4631322011-06-17 06:42:21 +0000942 if (selfIsPseudoStrong)
943 self->setARCPseudoStrong(true);
944
Mike Stump11289f42009-09-09 15:08:12 +0000945 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
946 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000947 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000948}
949
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000950ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
951 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
952 return ID;
953 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
954 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000955 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000956 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000957 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +0000958 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +0000959 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000960}
961
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000962SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
963 const auto *TSI = getReturnTypeSourceInfo();
964 if (TSI)
965 return TSI->getTypeLoc().getSourceRange();
966 return SourceRange();
967}
968
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000969static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
970 const ObjCMethodDecl *Method,
971 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
972 bool MovedToSuper) {
973 if (!Container)
974 return;
975
976 // In categories look for overriden methods from protocols. A method from
977 // category is not "overriden" since it is considered as the "same" method
978 // (same USR) as the one from the interface.
979 if (const ObjCCategoryDecl *
980 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
981 // Check whether we have a matching method at this category but only if we
982 // are at the super class level.
983 if (MovedToSuper)
984 if (ObjCMethodDecl *
985 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000986 Method->isInstanceMethod(),
987 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000988 if (Method != Overridden) {
989 // We found an override at this category; there is no need to look
990 // into its protocols.
991 Methods.push_back(Overridden);
992 return;
993 }
994
Aaron Ballman19a41762014-03-14 12:55:57 +0000995 for (const auto *P : Category->protocols())
996 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000997 return;
998 }
999
1000 // Check whether we have a matching method at this level.
1001 if (const ObjCMethodDecl *
1002 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001003 Method->isInstanceMethod(),
1004 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001005 if (Method != Overridden) {
1006 // We found an override at this level; there is no need to look
1007 // into other protocols or categories.
1008 Methods.push_back(Overridden);
1009 return;
1010 }
1011
1012 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001013 for (const auto *P : Protocol->protocols())
1014 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001015 }
1016
1017 if (const ObjCInterfaceDecl *
1018 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001019 for (const auto *P : Interface->protocols())
1020 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001021
Aaron Ballman15063e12014-03-13 21:35:02 +00001022 for (const auto *Cat : Interface->known_categories())
1023 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001024
1025 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1026 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1027 /*MovedToSuper=*/true);
1028 }
1029}
1030
1031static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1032 const ObjCMethodDecl *Method,
1033 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1034 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1035 /*MovedToSuper=*/false);
1036}
1037
1038static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1039 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1040 assert(Method->isOverriding());
1041
1042 if (const ObjCProtocolDecl *
1043 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1044 CollectOverriddenMethods(ProtD, Method, overridden);
1045
1046 } else if (const ObjCImplDecl *
1047 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1048 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1049 if (!ID)
1050 return;
1051 // Start searching for overridden methods using the method from the
1052 // interface as starting point.
1053 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001054 Method->isInstanceMethod(),
1055 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001056 Method = IFaceMeth;
1057 CollectOverriddenMethods(ID, Method, overridden);
1058
1059 } else if (const ObjCCategoryDecl *
1060 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1061 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1062 if (!ID)
1063 return;
1064 // Start searching for overridden methods using the method from the
1065 // interface as starting point.
1066 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001067 Method->isInstanceMethod(),
1068 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001069 Method = IFaceMeth;
1070 CollectOverriddenMethods(ID, Method, overridden);
1071
1072 } else {
1073 CollectOverriddenMethods(
1074 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1075 Method, overridden);
1076 }
1077}
1078
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001079void ObjCMethodDecl::getOverriddenMethods(
1080 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1081 const ObjCMethodDecl *Method = this;
1082
1083 if (Method->isRedeclaration()) {
1084 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1085 getMethod(Method->getSelector(), Method->isInstanceMethod());
1086 }
1087
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001088 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001089 collectOverriddenMethodsSlow(Method, Overridden);
1090 assert(!Overridden.empty() &&
1091 "ObjCMethodDecl's overriding bit is not as expected");
1092 }
1093}
1094
Jordan Rose2bd991a2012-10-10 16:42:54 +00001095const ObjCPropertyDecl *
1096ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1097 Selector Sel = getSelector();
1098 unsigned NumArgs = Sel.getNumArgs();
1099 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001100 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001101
Jordan Rose16ba5532015-01-16 23:04:26 +00001102 if (!isInstanceMethod())
Craig Topper36250ad2014-05-12 05:36:57 +00001103 return nullptr;
1104
Jordan Rose2bd991a2012-10-10 16:42:54 +00001105 if (isPropertyAccessor()) {
1106 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001107 // If container is class extension, find its primary class.
1108 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1109 if (CatDecl->IsClassExtension())
1110 Container = CatDecl->getClassInterface();
1111
Jordan Rose2bd991a2012-10-10 16:42:54 +00001112 bool IsGetter = (NumArgs == 0);
1113
Aaron Ballmand174edf2014-03-13 19:11:50 +00001114 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001115 Selector NextSel = IsGetter ? I->getGetterName()
1116 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001117 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001118 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001119 }
1120
1121 llvm_unreachable("Marked as a property accessor but no property found!");
1122 }
1123
1124 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001125 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001126
1127 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1128 OverridesTy Overrides;
1129 getOverriddenMethods(Overrides);
1130 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1131 I != E; ++I) {
1132 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1133 return Prop;
1134 }
1135
Craig Topper36250ad2014-05-12 05:36:57 +00001136 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001137}
1138
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001139//===----------------------------------------------------------------------===//
1140// ObjCInterfaceDecl
1141//===----------------------------------------------------------------------===//
1142
Douglas Gregord53ae832012-01-17 18:09:05 +00001143ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001144 DeclContext *DC,
1145 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001146 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001147 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001148 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001149 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001150 ObjCInterfaceDecl *Result = new (C, DC)
Richard Smith053f6c62014-05-16 23:01:30 +00001151 ObjCInterfaceDecl(C, DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001152 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001153 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001154 return Result;
1155}
1156
Richard Smith053f6c62014-05-16 23:01:30 +00001157ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001158 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001159 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001160 SourceLocation(),
1161 nullptr,
1162 SourceLocation(),
1163 nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001164 Result->Data.setInt(!C.getLangOpts().Modules);
1165 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001166}
1167
Richard Smith053f6c62014-05-16 23:01:30 +00001168ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1169 SourceLocation AtLoc, IdentifierInfo *Id,
1170 SourceLocation CLoc,
1171 ObjCInterfaceDecl *PrevDecl,
1172 bool IsInternal)
1173 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
1174 redeclarable_base(C), TypeForDecl(nullptr), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001175 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001176
1177 // Copy the 'data' pointer over.
1178 if (PrevDecl)
1179 Data = PrevDecl->Data;
1180
Richard Smith053f6c62014-05-16 23:01:30 +00001181 setImplicit(IsInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001182}
1183
Douglas Gregor73693022010-12-01 23:49:52 +00001184void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001185 assert(data().ExternallyCompleted && "Class is not externally completed");
1186 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001187 getASTContext().getExternalSource()->CompleteType(
1188 const_cast<ObjCInterfaceDecl *>(this));
1189}
1190
1191void ObjCInterfaceDecl::setExternallyCompleted() {
1192 assert(getASTContext().getExternalSource() &&
1193 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001194 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001195 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001196 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001197}
1198
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001199void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001200 // Check for a complete definition and recover if not so.
1201 if (!isThisDeclarationADefinition())
1202 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001203 data().HasDesignatedInitializers = true;
1204}
1205
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001206bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001207 // Check for a complete definition and recover if not so.
1208 if (!isThisDeclarationADefinition())
1209 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001210 if (data().ExternallyCompleted)
1211 LoadExternalDefinition();
1212
1213 return data().HasDesignatedInitializers;
1214}
1215
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001216StringRef
1217ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001218 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1219 return ObjCRTName->getMetadataName();
1220
1221 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001222}
1223
1224StringRef
1225ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001226 if (ObjCInterfaceDecl *ID =
1227 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1228 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001229
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001230 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001231}
1232
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001233ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001234 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1235 if (data().ExternallyCompleted)
1236 LoadExternalDefinition();
1237
1238 return getASTContext().getObjCImplementation(
1239 const_cast<ObjCInterfaceDecl*>(Def));
1240 }
1241
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001242 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001243 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001244}
1245
1246void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001247 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001248}
1249
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001250namespace {
1251 struct SynthesizeIvarChunk {
1252 uint64_t Size;
1253 ObjCIvarDecl *Ivar;
1254 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1255 : Size(size), Ivar(ivar) {}
1256 };
1257
1258 bool operator<(const SynthesizeIvarChunk & LHS,
1259 const SynthesizeIvarChunk &RHS) {
1260 return LHS.Size < RHS.Size;
1261 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001262}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001263
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001264/// all_declared_ivar_begin - return first ivar declared in this class,
1265/// its extensions and its implementation. Lazily build the list on first
1266/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001267///
1268/// Caveat: The list returned by this method reflects the current
1269/// state of the parser. The cache will be updated for every ivar
1270/// added by an extension or the implementation when they are
1271/// encountered.
1272/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001273ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001274 // FIXME: Should make sure no callers ever do this.
1275 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001276 return nullptr;
1277
1278 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001279 if (!data().IvarList) {
1280 if (!ivar_empty()) {
1281 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1282 data().IvarList = *I; ++I;
1283 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001284 curIvar->setNextIvar(*I);
1285 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001286
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001287 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001288 if (!Ext->ivar_empty()) {
1289 ObjCCategoryDecl::ivar_iterator
1290 I = Ext->ivar_begin(),
1291 E = Ext->ivar_end();
1292 if (!data().IvarList) {
1293 data().IvarList = *I; ++I;
1294 curIvar = data().IvarList;
1295 }
1296 for ( ;I != E; curIvar = *I, ++I)
1297 curIvar->setNextIvar(*I);
1298 }
1299 }
1300 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001301 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001302
1303 // cached and complete!
1304 if (!data().IvarListMissingImplementation)
1305 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001306
1307 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001308 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001309 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001310 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001311 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001312 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1313 layout.push_back(SynthesizeIvarChunk(
1314 IV->getASTContext().getTypeSize(IV->getType()), IV));
1315 continue;
1316 }
1317 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001318 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001319 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001320 curIvar->setNextIvar(IV);
1321 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001322 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001323
1324 if (!layout.empty()) {
1325 // Order synthesized ivars by their size.
1326 std::stable_sort(layout.begin(), layout.end());
1327 unsigned Ix = 0, EIx = layout.size();
1328 if (!data().IvarList) {
1329 data().IvarList = layout[0].Ivar; Ix++;
1330 curIvar = data().IvarList;
1331 }
1332 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1333 curIvar->setNextIvar(layout[Ix].Ivar);
1334 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001335 }
1336 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001337 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001338}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001339
1340/// FindCategoryDeclaration - Finds category declaration in the list of
1341/// categories for this class and returns it. Name of the category is passed
1342/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001343///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001344ObjCCategoryDecl *
1345ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001346 // FIXME: Should make sure no callers ever do this.
1347 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001348 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001349
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001350 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001351 LoadExternalDefinition();
1352
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001353 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001354 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001355 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001356
1357 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001358}
1359
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001360ObjCMethodDecl *
1361ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001362 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001363 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001364 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1365 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001366 }
1367
Craig Topper36250ad2014-05-12 05:36:57 +00001368 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001369}
1370
1371ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001372 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001373 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001374 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1375 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001376 }
Craig Topper36250ad2014-05-12 05:36:57 +00001377
1378 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001379}
1380
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001381/// ClassImplementsProtocol - Checks that 'lProto' protocol
1382/// has been implemented in IDecl class, its super class or categories (if
1383/// lookupCategory is true).
1384bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1385 bool lookupCategory,
1386 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001387 if (!hasDefinition())
1388 return false;
1389
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001390 ObjCInterfaceDecl *IDecl = this;
1391 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001392 for (auto *PI : IDecl->protocols()){
1393 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001394 return true;
1395 // This is dubious and is added to be compatible with gcc. In gcc, it is
1396 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1397 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1398 // object. This IMO, should be a bug.
1399 // FIXME: Treat this as an extension, and flag this as an error when GCC
1400 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001401 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001402 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001403 return true;
1404 }
Mike Stump11289f42009-09-09 15:08:12 +00001405
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001406 // 2nd, look up the category.
1407 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001408 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001409 for (auto *PI : Cat->protocols())
1410 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001411 return true;
1412 }
Mike Stump11289f42009-09-09 15:08:12 +00001413
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001414 // 3rd, look up the super class(s)
1415 if (IDecl->getSuperClass())
1416 return
1417 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1418 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001419
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001420 return false;
1421}
1422
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001423//===----------------------------------------------------------------------===//
1424// ObjCIvarDecl
1425//===----------------------------------------------------------------------===//
1426
David Blaikie68e081d2011-12-20 02:48:34 +00001427void ObjCIvarDecl::anchor() { }
1428
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001429ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001430 SourceLocation StartLoc,
1431 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001432 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001433 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001434 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001435 if (DC) {
1436 // Ivar's can only appear in interfaces, implementations (via synthesized
1437 // properties), and class extensions (via direct declaration, or synthesized
1438 // properties).
1439 //
1440 // FIXME: This should really be asserting this:
1441 // (isa<ObjCCategoryDecl>(DC) &&
1442 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1443 // but unfortunately we sometimes place ivars into non-class extension
1444 // categories on error. This breaks an AST invariant, and should not be
1445 // fixed.
1446 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1447 isa<ObjCCategoryDecl>(DC)) &&
1448 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001449 // Once a new ivar is created in any of class/class-extension/implementation
1450 // decl contexts, the previously built IvarList must be rebuilt.
1451 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1452 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001453 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001454 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001455 else
1456 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001457 }
Craig Topper36250ad2014-05-12 05:36:57 +00001458 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001459 }
1460
Richard Smithf7981722013-11-22 09:01:48 +00001461 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001462 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001463}
1464
Douglas Gregor72172e92012-01-05 21:55:30 +00001465ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001466 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1467 nullptr, QualType(), nullptr,
1468 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001469}
1470
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001471const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1472 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001473
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001474 switch (DC->getKind()) {
1475 default:
1476 case ObjCCategoryImpl:
1477 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001478 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001479
1480 // Ivars can only appear in class extension categories.
1481 case ObjCCategory: {
1482 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1483 assert(CD->IsClassExtension() && "invalid container for ivar!");
1484 return CD->getClassInterface();
1485 }
1486
1487 case ObjCImplementation:
1488 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1489
1490 case ObjCInterface:
1491 return cast<ObjCInterfaceDecl>(DC);
1492 }
1493}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001494
1495//===----------------------------------------------------------------------===//
1496// ObjCAtDefsFieldDecl
1497//===----------------------------------------------------------------------===//
1498
David Blaikie68e081d2011-12-20 02:48:34 +00001499void ObjCAtDefsFieldDecl::anchor() { }
1500
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001501ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001502*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1503 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001504 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001505 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001506}
1507
Richard Smithf7981722013-11-22 09:01:48 +00001508ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001509 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001510 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1511 SourceLocation(), nullptr, QualType(),
1512 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001513}
1514
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001515//===----------------------------------------------------------------------===//
1516// ObjCProtocolDecl
1517//===----------------------------------------------------------------------===//
1518
David Blaikie68e081d2011-12-20 02:48:34 +00001519void ObjCProtocolDecl::anchor() { }
1520
Richard Smith053f6c62014-05-16 23:01:30 +00001521ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1522 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001523 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001524 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001525 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1526 redeclarable_base(C), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001527 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001528 if (PrevDecl)
1529 Data = PrevDecl->Data;
1530}
1531
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001532ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001533 IdentifierInfo *Id,
1534 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001535 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001536 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001537 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001538 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001539 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001540 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001541}
1542
Richard Smithf7981722013-11-22 09:01:48 +00001543ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001544 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001545 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001546 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001547 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001548 Result->Data.setInt(!C.getLangOpts().Modules);
1549 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001550}
1551
Steve Naroff114aecb2009-03-01 16:12:44 +00001552ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1553 ObjCProtocolDecl *PDecl = this;
1554
1555 if (Name == getIdentifier())
1556 return PDecl;
1557
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001558 for (auto *I : protocols())
1559 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001560 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001561
Craig Topper36250ad2014-05-12 05:36:57 +00001562 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001563}
1564
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001565// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001566// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001567ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1568 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001569 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001570
Douglas Gregoreed49792013-01-17 00:38:46 +00001571 // If there is no definition or the definition is hidden, we don't find
1572 // anything.
1573 const ObjCProtocolDecl *Def = getDefinition();
1574 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001575 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001576
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001577 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001578 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001579
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001580 for (const auto *I : protocols())
1581 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001582 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001583 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001584}
1585
Douglas Gregore6e48b12012-01-01 19:29:29 +00001586void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001587 assert(!Data.getPointer() && "Protocol already has a definition!");
1588 Data.setPointer(new (getASTContext()) DefinitionData);
1589 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001590}
1591
1592void ObjCProtocolDecl::startDefinition() {
1593 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001594
1595 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001596 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001597 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001598}
1599
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001600void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1601 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001602
1603 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001604 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001605 // Insert into PM if not there already.
1606 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001607 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001608 }
1609 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001610 for (const auto *PI : PDecl->protocols())
1611 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001612 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001613}
1614
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001615
1616void ObjCProtocolDecl::collectInheritedProtocolProperties(
1617 const ObjCPropertyDecl *Property,
1618 ProtocolPropertyMap &PM) const {
1619 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1620 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001621 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001622 if (Prop == Property)
1623 continue;
1624 if (Prop->getIdentifier() == Property->getIdentifier()) {
1625 PM[PDecl] = Prop;
1626 MatchFound = true;
1627 break;
1628 }
1629 }
1630 // Scan through protocol's protocols which did not have a matching property.
1631 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001632 for (const auto *PI : PDecl->protocols())
1633 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001634 }
1635}
Anna Zaks673d76b2012-10-18 19:17:53 +00001636
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001637StringRef
1638ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001639 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1640 return ObjCRTName->getMetadataName();
1641
1642 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001643}
1644
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001645//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001646// ObjCCategoryDecl
1647//===----------------------------------------------------------------------===//
1648
David Blaikie68e081d2011-12-20 02:48:34 +00001649void ObjCCategoryDecl::anchor() { }
1650
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001651ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001652 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001653 SourceLocation ClassNameLoc,
1654 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001655 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001656 ObjCInterfaceDecl *IDecl,
1657 SourceLocation IvarLBraceLoc,
1658 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001659 ObjCCategoryDecl *CatDecl =
1660 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1661 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001662 if (IDecl) {
1663 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001664 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001665 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001666 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001667 if (ASTMutationListener *L = C.getASTMutationListener())
1668 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1669 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001670 }
1671
1672 return CatDecl;
1673}
1674
Richard Smithf7981722013-11-22 09:01:48 +00001675ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001676 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001677 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1678 SourceLocation(), SourceLocation(),
1679 nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001680}
1681
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001682ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1683 return getASTContext().getObjCImplementation(
1684 const_cast<ObjCCategoryDecl*>(this));
1685}
1686
1687void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1688 getASTContext().setObjCImplementation(this, ImplD);
1689}
1690
1691
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001692//===----------------------------------------------------------------------===//
1693// ObjCCategoryImplDecl
1694//===----------------------------------------------------------------------===//
1695
David Blaikie68e081d2011-12-20 02:48:34 +00001696void ObjCCategoryImplDecl::anchor() { }
1697
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001698ObjCCategoryImplDecl *
1699ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001700 IdentifierInfo *Id,
1701 ObjCInterfaceDecl *ClassInterface,
1702 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001703 SourceLocation atStartLoc,
1704 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001705 if (ClassInterface && ClassInterface->hasDefinition())
1706 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001707 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1708 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001709}
1710
Douglas Gregor72172e92012-01-05 21:55:30 +00001711ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1712 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001713 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1714 SourceLocation(), SourceLocation(),
1715 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001716}
1717
Steve Narofff406f4d2009-10-29 21:11:04 +00001718ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001719 // The class interface might be NULL if we are working with invalid code.
1720 if (const ObjCInterfaceDecl *ID = getClassInterface())
1721 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00001722 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001723}
1724
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001725
David Blaikie68e081d2011-12-20 02:48:34 +00001726void ObjCImplDecl::anchor() { }
1727
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001728void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001729 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001730 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001731 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001732}
1733
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001734void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1735 ASTContext &Ctx = getASTContext();
1736
1737 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001738 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001739 if (IFace)
1740 Ctx.setObjCImplementation(IFace, ImplD);
1741
Duncan Sands49c29ee2009-07-21 07:56:29 +00001742 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001743 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1744 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1745 Ctx.setObjCImplementation(CD, ImplD);
1746 }
1747
1748 ClassInterface = IFace;
1749}
1750
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001751/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001752/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001753/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001754///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001755ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001756FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001757 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001758 if (PID->getPropertyIvarDecl() &&
1759 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1760 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001761 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001762}
1763
1764/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001765/// added to the list of those properties \@synthesized/\@dynamic in this
1766/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001767///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001768ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001769FindPropertyImplDecl(IdentifierInfo *Id) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001770 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001771 if (PID->getPropertyDecl()->getIdentifier() == Id)
1772 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001773 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001774}
1775
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001776raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001777 const ObjCCategoryImplDecl &CID) {
1778 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001779 return OS;
1780}
1781
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001782//===----------------------------------------------------------------------===//
1783// ObjCImplementationDecl
1784//===----------------------------------------------------------------------===//
1785
David Blaikie68e081d2011-12-20 02:48:34 +00001786void ObjCImplementationDecl::anchor() { }
1787
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001788ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001789ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001790 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001791 ObjCInterfaceDecl *SuperDecl,
1792 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001793 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001794 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001795 SourceLocation IvarLBraceLoc,
1796 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001797 if (ClassInterface && ClassInterface->hasDefinition())
1798 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001799 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1800 nameLoc, atStartLoc, superLoc,
1801 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001802}
1803
Douglas Gregor72172e92012-01-05 21:55:30 +00001804ObjCImplementationDecl *
1805ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001806 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
1807 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001808}
1809
John McCall0410e572011-07-22 04:15:06 +00001810void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1811 CXXCtorInitializer ** initializers,
1812 unsigned numInitializers) {
1813 if (numInitializers > 0) {
1814 NumIvarInitializers = numInitializers;
1815 CXXCtorInitializer **ivarInitializers =
1816 new (C) CXXCtorInitializer*[NumIvarInitializers];
1817 memcpy(ivarInitializers, initializers,
1818 numInitializers * sizeof(CXXCtorInitializer*));
1819 IvarInitializers = ivarInitializers;
1820 }
1821}
1822
Richard Smithc2bb8182015-03-24 06:36:48 +00001823ObjCImplementationDecl::init_const_iterator
1824ObjCImplementationDecl::init_begin() const {
1825 return IvarInitializers.get(getASTContext().getExternalSource());
1826}
1827
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001828raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001829 const ObjCImplementationDecl &ID) {
1830 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001831 return OS;
1832}
1833
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001834//===----------------------------------------------------------------------===//
1835// ObjCCompatibleAliasDecl
1836//===----------------------------------------------------------------------===//
1837
David Blaikie68e081d2011-12-20 02:48:34 +00001838void ObjCCompatibleAliasDecl::anchor() { }
1839
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001840ObjCCompatibleAliasDecl *
1841ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1842 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001843 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001844 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001845 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001846}
1847
Douglas Gregor72172e92012-01-05 21:55:30 +00001848ObjCCompatibleAliasDecl *
1849ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001850 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
1851 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001852}
1853
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001854//===----------------------------------------------------------------------===//
1855// ObjCPropertyDecl
1856//===----------------------------------------------------------------------===//
1857
David Blaikie68e081d2011-12-20 02:48:34 +00001858void ObjCPropertyDecl::anchor() { }
1859
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001860ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1861 SourceLocation L,
1862 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001863 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001864 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00001865 QualType T,
1866 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001867 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001868 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
1869 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001870}
1871
Richard Smithf7981722013-11-22 09:01:48 +00001872ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001873 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001874 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
1875 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00001876 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00001877}
1878
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001879//===----------------------------------------------------------------------===//
1880// ObjCPropertyImplDecl
1881//===----------------------------------------------------------------------===//
1882
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001883ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001884 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001885 SourceLocation atLoc,
1886 SourceLocation L,
1887 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001888 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001889 ObjCIvarDecl *ivar,
1890 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001891 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1892 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001893}
Chris Lattnered0e1642008-03-17 01:19:02 +00001894
Richard Smithf7981722013-11-22 09:01:48 +00001895ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001896 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001897 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
1898 SourceLocation(), nullptr, Dynamic,
1899 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001900}
1901
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001902SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1903 SourceLocation EndLoc = getLocation();
1904 if (IvarLoc.isValid())
1905 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001906
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001907 return SourceRange(AtLoc, EndLoc);
1908}