blob: ed5367514c300135c365daebe0a0e9456402aad0 [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 {
David Blaikieff7d47a2012-12-19 00:45:41 +000057 lookup_const_result R = lookup(Id);
58 for (lookup_const_iterator Ivar = R.begin(), IvarEnd = R.end();
59 Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian68453832009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
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 //
David Blaikieff7d47a2012-12-19 00:45:41 +000086 lookup_const_result R = lookup(Sel);
87 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
88 Meth != MethEnd; ++Meth) {
Steve Naroffc4173fa2009-02-22 19:35:57 +000089 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000090 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +000091 return MD;
92 }
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();
104 lookup_const_result R = lookup(Sel);
105 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
106 Meth != MethEnd; ++Meth) {
107 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
108 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
109 return true;
110 }
111
112 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
113 // Also look into categories, including class extensions, looking
114 // for a user declared instance method.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000115 for (const auto *Cat : ID->visible_categories()) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000116 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
117 if (!MD->isImplicit())
118 return true;
119 if (Cat->IsClassExtension())
120 continue;
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
David Blaikieff7d47a2012-12-19 00:45:41 +0000164 DeclContext::lookup_const_result R = DC->lookup(propertyID);
165 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
166 ++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;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000337 for (auto *P : all_referenced_protocols()) {
338 ProtocolRefs.push_back(P);
Douglas Gregor002b6712010-01-16 15:02:53 +0000339 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000340
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000341 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000342}
343
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000344const ObjCInterfaceDecl *
345ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
346 const ObjCInterfaceDecl *IFace = this;
347 while (IFace) {
348 if (IFace->hasDesignatedInitializers())
349 return IFace;
350 if (!IFace->inheritsDesignatedInitializers())
351 break;
352 IFace = IFace->getSuperClass();
353 }
Craig Topper36250ad2014-05-12 05:36:57 +0000354 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000355}
356
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000357static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
358 for (const auto *MD : D->instance_methods()) {
359 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
360 return true;
361 }
362 for (const auto *Ext : D->visible_extensions()) {
363 for (const auto *MD : Ext->instance_methods()) {
364 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
365 return true;
366 }
367 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000368 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000369 for (const auto *MD : ImplD->instance_methods()) {
370 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
371 return true;
372 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000373 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000374 return false;
375}
376
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000377bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
378 switch (data().InheritedDesignatedInitializers) {
379 case DefinitionData::IDI_Inherited:
380 return true;
381 case DefinitionData::IDI_NotInherited:
382 return false;
383 case DefinitionData::IDI_Unknown: {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000384 // If the class introduced initializers we conservatively assume that we
385 // don't know if any of them is a designated initializer to avoid possible
386 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000387 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000388 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000389 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000390 if (auto SuperD = getSuperClass()) {
391 data().InheritedDesignatedInitializers =
392 SuperD->declaresOrInheritsDesignatedInitializers() ?
393 DefinitionData::IDI_Inherited :
394 DefinitionData::IDI_NotInherited;
395 } else {
396 data().InheritedDesignatedInitializers =
397 DefinitionData::IDI_NotInherited;
398 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000399 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000400 assert(data().InheritedDesignatedInitializers
401 != DefinitionData::IDI_Unknown);
402 return data().InheritedDesignatedInitializers ==
403 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000404 }
405 }
406
407 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
408}
409
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000410void ObjCInterfaceDecl::getDesignatedInitializers(
411 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000412 // Check for a complete definition and recover if not so.
413 if (!isThisDeclarationADefinition())
414 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000415 if (data().ExternallyCompleted)
416 LoadExternalDefinition();
417
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000418 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000419 if (!IFace)
420 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000421
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000422 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000423 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000424 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000425 for (const auto *Ext : IFace->visible_extensions()) {
426 for (const auto *MD : Ext->instance_methods())
427 if (MD->isThisDeclarationADesignatedInitializer())
428 Methods.push_back(MD);
429 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000430}
431
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000432bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
433 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000434 // Check for a complete definition and recover if not so.
435 if (!isThisDeclarationADefinition())
436 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000437 if (data().ExternallyCompleted)
438 LoadExternalDefinition();
439
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000440 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000441 if (!IFace)
442 return false;
443
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000444 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000445 if (MD->isThisDeclarationADesignatedInitializer()) {
446 if (InitMethod)
447 *InitMethod = MD;
448 return true;
449 }
450 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000451 for (const auto *Ext : IFace->visible_extensions()) {
452 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
453 if (MD->isThisDeclarationADesignatedInitializer()) {
454 if (InitMethod)
455 *InitMethod = MD;
456 return true;
457 }
458 }
459 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000460 return false;
461}
462
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000463void ObjCInterfaceDecl::allocateDefinitionData() {
464 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000465 Data.setPointer(new (getASTContext()) DefinitionData());
466 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000467
468 // Make the type point at the definition, now that we have one.
469 if (TypeForDecl)
470 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000471}
472
473void ObjCInterfaceDecl::startDefinition() {
474 allocateDefinitionData();
475
Douglas Gregor66b310c2011-12-15 18:03:09 +0000476 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000477 for (auto RD : redecls()) {
478 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000479 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000480 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000481}
482
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000483ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
484 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000485 // FIXME: Should make sure no callers ever do this.
486 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000487 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000488
489 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000490 LoadExternalDefinition();
491
Chris Lattner89375192008-03-16 00:19:01 +0000492 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000493 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000494 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000495 clsDeclared = ClassDecl;
496 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000497 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000498
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000499 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000500 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000501 clsDeclared = ClassDecl;
502 return I;
503 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000504 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000505
Chris Lattner89375192008-03-16 00:19:01 +0000506 ClassDecl = ClassDecl->getSuperClass();
507 }
Craig Topper36250ad2014-05-12 05:36:57 +0000508 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000509}
510
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000511/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
512/// class whose name is passed as argument. If it is not one of the super classes
513/// the it returns NULL.
514ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
515 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000516 // FIXME: Should make sure no callers ever do this.
517 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000518 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000519
520 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000521 LoadExternalDefinition();
522
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000523 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000524 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000525 if (ClassDecl->getIdentifier() == ICName)
526 return ClassDecl;
527 ClassDecl = ClassDecl->getSuperClass();
528 }
Craig Topper36250ad2014-05-12 05:36:57 +0000529 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000530}
531
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000532ObjCProtocolDecl *
533ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000534 for (auto *P : all_referenced_protocols())
535 if (P->lookupProtocolNamed(Name))
536 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000537 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000538 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000539}
540
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000541/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000542/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000543/// When argument category "C" is specified, any implicit method found
544/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000545ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000546 bool isInstance,
547 bool shallowCategoryLookup,
548 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000549 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000550{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000551 // FIXME: Should make sure no callers ever do this.
552 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000553 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000554
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000555 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000556 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000557
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000558 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000559 LoadExternalDefinition();
560
Ted Kremenek00781502013-11-23 01:01:29 +0000561 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000562 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000563 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000564 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000565
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000566 // 2. Didn't find one yet - now look through categories.
567 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000568 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000569 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000570 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000571
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000572 // 3. Didn't find one yet - look through primary class's protocols.
573 for (const auto *I : ClassDecl->protocols())
574 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
575 return MethodDecl;
576
577 // 4. Didn't find one yet - now look through categories' protocols
578 if (!shallowCategoryLookup)
579 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000580 // Didn't find one yet - look through protocols.
581 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000582 Cat->getReferencedProtocols();
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000583 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
584 E = Protocols.end(); I != E; ++I)
585 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000586 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000587 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000588 }
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000589
590
Ted Kremenek00781502013-11-23 01:01:29 +0000591 if (!followSuper)
Craig Topper36250ad2014-05-12 05:36:57 +0000592 return nullptr;
Ted Kremenek00781502013-11-23 01:01:29 +0000593
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000594 // 5. Get to the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000595 ClassDecl = ClassDecl->getSuperClass();
596 }
Craig Topper36250ad2014-05-12 05:36:57 +0000597 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000598}
599
Anna Zaksc77a3b12012-07-27 19:07:44 +0000600// Will search "local" class/category implementations for a method decl.
601// If failed, then we search in class's root for an instance method.
602// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000603ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
604 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000605 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000606 // FIXME: Should make sure no callers ever do this.
607 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000608 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000609
610 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000611 LoadExternalDefinition();
612
Craig Topper36250ad2014-05-12 05:36:57 +0000613 ObjCMethodDecl *Method = nullptr;
Steve Naroffbb69c942009-10-01 23:46:04 +0000614 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000615 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
616 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000617
618 // Look through local category implementations associated with the class.
619 if (!Method)
620 Method = Instance ? getCategoryInstanceMethod(Sel)
621 : getCategoryClassMethod(Sel);
622
623 // Before we give up, check if the selector is an instance method.
624 // But only in the root. This matches gcc's behavior and what the
625 // runtime expects.
626 if (!Instance && !Method && !getSuperClass()) {
627 Method = lookupInstanceMethod(Sel);
628 // Look through local category implementations associated
629 // with the root class.
630 if (!Method)
631 Method = lookupPrivateMethod(Sel, true);
632 }
633
Steve Naroffbb69c942009-10-01 23:46:04 +0000634 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000635 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000636 return Method;
637}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000638
639//===----------------------------------------------------------------------===//
640// ObjCMethodDecl
641//===----------------------------------------------------------------------===//
642
Alp Toker314cc812014-01-25 16:55:45 +0000643ObjCMethodDecl *ObjCMethodDecl::Create(
644 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
645 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
646 DeclContext *contextDecl, bool isInstance, bool isVariadic,
647 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
648 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000649 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000650 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000651 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
652 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000653}
654
Douglas Gregor72172e92012-01-05 21:55:30 +0000655ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000656 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000657 Selector(), QualType(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000658}
659
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000660bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
661 return getMethodFamily() == OMF_init &&
662 hasAttr<ObjCDesignatedInitializerAttr>();
663}
664
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000665bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
666 const ObjCMethodDecl **InitMethod) const {
667 if (getMethodFamily() != OMF_init)
668 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000669 const DeclContext *DC = getDeclContext();
670 if (isa<ObjCProtocolDecl>(DC))
671 return false;
672 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000673 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000674 return false;
675}
676
Douglas Gregora6017bb2012-10-09 17:21:28 +0000677Stmt *ObjCMethodDecl::getBody() const {
678 return Body.get(getASTContext().getExternalSource());
679}
680
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000681void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
682 assert(PrevMethod);
683 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
684 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000685 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000686}
687
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000688void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
689 ArrayRef<ParmVarDecl*> Params,
690 ArrayRef<SourceLocation> SelLocs) {
Craig Topper36250ad2014-05-12 05:36:57 +0000691 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000692 NumParams = Params.size();
693 if (Params.empty() && SelLocs.empty())
694 return;
695
696 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
697 sizeof(SourceLocation) * SelLocs.size();
698 ParamsAndSelLocs = C.Allocate(Size);
699 std::copy(Params.begin(), Params.end(), getParams());
700 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
701}
702
703void ObjCMethodDecl::getSelectorLocs(
704 SmallVectorImpl<SourceLocation> &SelLocs) const {
705 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
706 SelLocs.push_back(getSelectorLoc(i));
707}
708
709void ObjCMethodDecl::setMethodParams(ASTContext &C,
710 ArrayRef<ParmVarDecl*> Params,
711 ArrayRef<SourceLocation> SelLocs) {
712 assert((!SelLocs.empty() || isImplicit()) &&
713 "No selector locs for non-implicit method");
714 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000715 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000716
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000717 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
718 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000719 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000720 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000721
722 setParamsAndSelLocs(C, Params, SelLocs);
723}
724
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000725/// \brief A definition will return its interface declaration.
726/// An interface declaration will return its definition.
727/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000728ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000729 ASTContext &Ctx = getASTContext();
Craig Topper36250ad2014-05-12 05:36:57 +0000730 ObjCMethodDecl *Redecl = nullptr;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000731 if (HasRedeclaration)
732 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000733 if (Redecl)
734 return Redecl;
735
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000736 Decl *CtxD = cast<Decl>(getDeclContext());
737
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000738 if (!CtxD->isInvalidDecl()) {
739 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
740 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
741 if (!ImplD->isInvalidDecl())
742 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000743
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000744 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
745 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
746 if (!ImplD->isInvalidDecl())
747 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000748
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000749 } else if (ObjCImplementationDecl *ImplD =
750 dyn_cast<ObjCImplementationDecl>(CtxD)) {
751 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
752 if (!IFD->isInvalidDecl())
753 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000754
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000755 } else if (ObjCCategoryImplDecl *CImplD =
756 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
757 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
758 if (!CatD->isInvalidDecl())
759 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
760 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000761 }
762
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000763 if (!Redecl && isRedeclaration()) {
764 // This is the last redeclaration, go back to the first method.
765 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
766 isInstanceMethod());
767 }
768
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000769 return Redecl ? Redecl : this;
770}
771
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000772ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
773 Decl *CtxD = cast<Decl>(getDeclContext());
774
775 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
776 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
777 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
778 isInstanceMethod()))
779 return MD;
780
781 } else if (ObjCCategoryImplDecl *CImplD =
782 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000783 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000784 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
785 isInstanceMethod()))
786 return MD;
787 }
788
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000789 if (isRedeclaration())
790 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
791 isInstanceMethod());
792
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000793 return this;
794}
795
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000796SourceLocation ObjCMethodDecl::getLocEnd() const {
797 if (Stmt *Body = getBody())
798 return Body->getLocEnd();
799 return DeclEndLoc;
800}
801
John McCallb4526252011-03-02 01:50:55 +0000802ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
803 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000804 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000805 return family;
806
John McCall86bc21f2011-03-02 11:33:24 +0000807 // Check for an explicit attribute.
808 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
809 // The unfortunate necessity of mapping between enums here is due
810 // to the attributes framework.
811 switch (attr->getFamily()) {
812 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
813 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
814 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
815 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
816 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
817 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
818 }
819 Family = static_cast<unsigned>(family);
820 return family;
821 }
822
John McCallb4526252011-03-02 01:50:55 +0000823 family = getSelector().getMethodFamily();
824 switch (family) {
825 case OMF_None: break;
826
827 // init only has a conventional meaning for an instance method, and
828 // it has to return an object.
829 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000830 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000831 family = OMF_None;
832 break;
833
834 // alloc/copy/new have a conventional meaning for both class and
835 // instance methods, but they require an object return.
836 case OMF_alloc:
837 case OMF_copy:
838 case OMF_mutableCopy:
839 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000840 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000841 family = OMF_None;
842 break;
843
844 // These selectors have a conventional meaning only for instance methods.
845 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000846 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000847 case OMF_retain:
848 case OMF_release:
849 case OMF_autorelease:
850 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000851 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000852 if (!isInstanceMethod())
853 family = OMF_None;
854 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000855
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000856 case OMF_initialize:
857 if (isInstanceMethod() || !getReturnType()->isVoidType())
858 family = OMF_None;
859 break;
860
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000861 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000862 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000863 family = OMF_None;
864 else {
865 unsigned noParams = param_size();
866 if (noParams < 1 || noParams > 3)
867 family = OMF_None;
868 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000869 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000870 QualType ArgT = (*it);
871 if (!ArgT->isObjCSelType()) {
872 family = OMF_None;
873 break;
874 }
875 while (--noParams) {
876 it++;
877 ArgT = (*it);
878 if (!ArgT->isObjCIdType()) {
879 family = OMF_None;
880 break;
881 }
882 }
883 }
884 }
885 break;
886
John McCallb4526252011-03-02 01:50:55 +0000887 }
888
889 // Cache the result.
890 Family = static_cast<unsigned>(family);
891 return family;
892}
893
Mike Stump11289f42009-09-09 15:08:12 +0000894void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000895 const ObjCInterfaceDecl *OID) {
896 QualType selfTy;
897 if (isInstanceMethod()) {
898 // There may be no interface context due to error in declaration
899 // of the interface (which has been reported). Recover gracefully.
900 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000901 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000902 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000903 } else {
904 selfTy = Context.getObjCIdType();
905 }
906 } else // we have a factory method.
907 selfTy = Context.getObjCClassType();
908
John McCalld4631322011-06-17 06:42:21 +0000909 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000910 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000911
David Blaikiebbafb8a2012-03-11 07:00:24 +0000912 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000913 if (isInstanceMethod()) {
914 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000915
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000916 // 'self' is always __strong. It's actually pseudo-strong except
917 // in init methods (or methods labeled ns_consumes_self), though.
918 Qualifiers qs;
919 qs.setObjCLifetime(Qualifiers::OCL_Strong);
920 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000921
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000922 // In addition, 'self' is const unless this is an init method.
923 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
924 selfTy = selfTy.withConst();
925 selfIsPseudoStrong = true;
926 }
927 }
928 else {
929 assert(isClassMethod());
930 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000931 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000932 selfIsPseudoStrong = true;
933 }
John McCall31168b02011-06-15 23:02:42 +0000934 }
935
936 ImplicitParamDecl *self
937 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
938 &Context.Idents.get("self"), selfTy);
939 setSelfDecl(self);
940
941 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000942 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000943
John McCalld4631322011-06-17 06:42:21 +0000944 if (selfIsPseudoStrong)
945 self->setARCPseudoStrong(true);
946
Mike Stump11289f42009-09-09 15:08:12 +0000947 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
948 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000949 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000950}
951
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000952ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
953 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
954 return ID;
955 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
956 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000957 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000958 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000959 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +0000960 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +0000961 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000962}
963
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000964SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
965 const auto *TSI = getReturnTypeSourceInfo();
966 if (TSI)
967 return TSI->getTypeLoc().getSourceRange();
968 return SourceRange();
969}
970
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000971static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
972 const ObjCMethodDecl *Method,
973 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
974 bool MovedToSuper) {
975 if (!Container)
976 return;
977
978 // In categories look for overriden methods from protocols. A method from
979 // category is not "overriden" since it is considered as the "same" method
980 // (same USR) as the one from the interface.
981 if (const ObjCCategoryDecl *
982 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
983 // Check whether we have a matching method at this category but only if we
984 // are at the super class level.
985 if (MovedToSuper)
986 if (ObjCMethodDecl *
987 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000988 Method->isInstanceMethod(),
989 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000990 if (Method != Overridden) {
991 // We found an override at this category; there is no need to look
992 // into its protocols.
993 Methods.push_back(Overridden);
994 return;
995 }
996
Aaron Ballman19a41762014-03-14 12:55:57 +0000997 for (const auto *P : Category->protocols())
998 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000999 return;
1000 }
1001
1002 // Check whether we have a matching method at this level.
1003 if (const ObjCMethodDecl *
1004 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001005 Method->isInstanceMethod(),
1006 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001007 if (Method != Overridden) {
1008 // We found an override at this level; there is no need to look
1009 // into other protocols or categories.
1010 Methods.push_back(Overridden);
1011 return;
1012 }
1013
1014 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001015 for (const auto *P : Protocol->protocols())
1016 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001017 }
1018
1019 if (const ObjCInterfaceDecl *
1020 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001021 for (const auto *P : Interface->protocols())
1022 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001023
Aaron Ballman15063e12014-03-13 21:35:02 +00001024 for (const auto *Cat : Interface->known_categories())
1025 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001026
1027 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1028 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1029 /*MovedToSuper=*/true);
1030 }
1031}
1032
1033static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1034 const ObjCMethodDecl *Method,
1035 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1036 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1037 /*MovedToSuper=*/false);
1038}
1039
1040static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1041 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1042 assert(Method->isOverriding());
1043
1044 if (const ObjCProtocolDecl *
1045 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1046 CollectOverriddenMethods(ProtD, Method, overridden);
1047
1048 } else if (const ObjCImplDecl *
1049 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1050 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1051 if (!ID)
1052 return;
1053 // Start searching for overridden methods using the method from the
1054 // interface as starting point.
1055 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001056 Method->isInstanceMethod(),
1057 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001058 Method = IFaceMeth;
1059 CollectOverriddenMethods(ID, Method, overridden);
1060
1061 } else if (const ObjCCategoryDecl *
1062 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1063 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1064 if (!ID)
1065 return;
1066 // Start searching for overridden methods using the method from the
1067 // interface as starting point.
1068 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001069 Method->isInstanceMethod(),
1070 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001071 Method = IFaceMeth;
1072 CollectOverriddenMethods(ID, Method, overridden);
1073
1074 } else {
1075 CollectOverriddenMethods(
1076 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1077 Method, overridden);
1078 }
1079}
1080
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001081void ObjCMethodDecl::getOverriddenMethods(
1082 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1083 const ObjCMethodDecl *Method = this;
1084
1085 if (Method->isRedeclaration()) {
1086 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1087 getMethod(Method->getSelector(), Method->isInstanceMethod());
1088 }
1089
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001090 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001091 collectOverriddenMethodsSlow(Method, Overridden);
1092 assert(!Overridden.empty() &&
1093 "ObjCMethodDecl's overriding bit is not as expected");
1094 }
1095}
1096
Jordan Rose2bd991a2012-10-10 16:42:54 +00001097const ObjCPropertyDecl *
1098ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1099 Selector Sel = getSelector();
1100 unsigned NumArgs = Sel.getNumArgs();
1101 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001102 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001103
Jordan Rose59e34ec2012-10-11 16:02:02 +00001104 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Craig Topper36250ad2014-05-12 05:36:57 +00001105 return nullptr;
1106
Jordan Rose2bd991a2012-10-10 16:42:54 +00001107 if (isPropertyAccessor()) {
1108 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001109 // If container is class extension, find its primary class.
1110 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1111 if (CatDecl->IsClassExtension())
1112 Container = CatDecl->getClassInterface();
1113
Jordan Rose2bd991a2012-10-10 16:42:54 +00001114 bool IsGetter = (NumArgs == 0);
1115
Aaron Ballmand174edf2014-03-13 19:11:50 +00001116 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001117 Selector NextSel = IsGetter ? I->getGetterName()
1118 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001119 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001120 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001121 }
1122
1123 llvm_unreachable("Marked as a property accessor but no property found!");
1124 }
1125
1126 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001127 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001128
1129 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1130 OverridesTy Overrides;
1131 getOverriddenMethods(Overrides);
1132 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1133 I != E; ++I) {
1134 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1135 return Prop;
1136 }
1137
Craig Topper36250ad2014-05-12 05:36:57 +00001138 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001139}
1140
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001141//===----------------------------------------------------------------------===//
1142// ObjCInterfaceDecl
1143//===----------------------------------------------------------------------===//
1144
Douglas Gregord53ae832012-01-17 18:09:05 +00001145ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001146 DeclContext *DC,
1147 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001148 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001149 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001150 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001151 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001152 ObjCInterfaceDecl *Result = new (C, DC)
Richard Smith053f6c62014-05-16 23:01:30 +00001153 ObjCInterfaceDecl(C, DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001154 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001155 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001156 return Result;
1157}
1158
Richard Smith053f6c62014-05-16 23:01:30 +00001159ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001160 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001161 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001162 SourceLocation(),
1163 nullptr,
1164 SourceLocation(),
1165 nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001166 Result->Data.setInt(!C.getLangOpts().Modules);
1167 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001168}
1169
Richard Smith053f6c62014-05-16 23:01:30 +00001170ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1171 SourceLocation AtLoc, IdentifierInfo *Id,
1172 SourceLocation CLoc,
1173 ObjCInterfaceDecl *PrevDecl,
1174 bool IsInternal)
1175 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
1176 redeclarable_base(C), TypeForDecl(nullptr), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001177 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001178
1179 // Copy the 'data' pointer over.
1180 if (PrevDecl)
1181 Data = PrevDecl->Data;
1182
Richard Smith053f6c62014-05-16 23:01:30 +00001183 setImplicit(IsInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001184}
1185
Douglas Gregor73693022010-12-01 23:49:52 +00001186void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001187 assert(data().ExternallyCompleted && "Class is not externally completed");
1188 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001189 getASTContext().getExternalSource()->CompleteType(
1190 const_cast<ObjCInterfaceDecl *>(this));
1191}
1192
1193void ObjCInterfaceDecl::setExternallyCompleted() {
1194 assert(getASTContext().getExternalSource() &&
1195 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001196 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001197 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001198 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001199}
1200
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001201void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001202 // Check for a complete definition and recover if not so.
1203 if (!isThisDeclarationADefinition())
1204 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001205 data().HasDesignatedInitializers = true;
1206}
1207
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001208bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001209 // Check for a complete definition and recover if not so.
1210 if (!isThisDeclarationADefinition())
1211 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001212 if (data().ExternallyCompleted)
1213 LoadExternalDefinition();
1214
1215 return data().HasDesignatedInitializers;
1216}
1217
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001218StringRef
1219ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001220 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1221 return ObjCRTName->getMetadataName();
1222
1223 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001224}
1225
1226StringRef
1227ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001228 if (ObjCInterfaceDecl *ID =
1229 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1230 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001231
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001232 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001233}
1234
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001235ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001236 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1237 if (data().ExternallyCompleted)
1238 LoadExternalDefinition();
1239
1240 return getASTContext().getObjCImplementation(
1241 const_cast<ObjCInterfaceDecl*>(Def));
1242 }
1243
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001244 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001245 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001246}
1247
1248void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001249 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001250}
1251
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001252namespace {
1253 struct SynthesizeIvarChunk {
1254 uint64_t Size;
1255 ObjCIvarDecl *Ivar;
1256 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1257 : Size(size), Ivar(ivar) {}
1258 };
1259
1260 bool operator<(const SynthesizeIvarChunk & LHS,
1261 const SynthesizeIvarChunk &RHS) {
1262 return LHS.Size < RHS.Size;
1263 }
1264}
1265
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001266/// all_declared_ivar_begin - return first ivar declared in this class,
1267/// its extensions and its implementation. Lazily build the list on first
1268/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001269///
1270/// Caveat: The list returned by this method reflects the current
1271/// state of the parser. The cache will be updated for every ivar
1272/// added by an extension or the implementation when they are
1273/// encountered.
1274/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001275ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001276 // FIXME: Should make sure no callers ever do this.
1277 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001278 return nullptr;
1279
1280 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001281 if (!data().IvarList) {
1282 if (!ivar_empty()) {
1283 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1284 data().IvarList = *I; ++I;
1285 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001286 curIvar->setNextIvar(*I);
1287 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001288
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001289 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001290 if (!Ext->ivar_empty()) {
1291 ObjCCategoryDecl::ivar_iterator
1292 I = Ext->ivar_begin(),
1293 E = Ext->ivar_end();
1294 if (!data().IvarList) {
1295 data().IvarList = *I; ++I;
1296 curIvar = data().IvarList;
1297 }
1298 for ( ;I != E; curIvar = *I, ++I)
1299 curIvar->setNextIvar(*I);
1300 }
1301 }
1302 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001303 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001304
1305 // cached and complete!
1306 if (!data().IvarListMissingImplementation)
1307 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001308
1309 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001310 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001311 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001312 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001313 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001314 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1315 layout.push_back(SynthesizeIvarChunk(
1316 IV->getASTContext().getTypeSize(IV->getType()), IV));
1317 continue;
1318 }
1319 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001320 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001321 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001322 curIvar->setNextIvar(IV);
1323 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001324 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001325
1326 if (!layout.empty()) {
1327 // Order synthesized ivars by their size.
1328 std::stable_sort(layout.begin(), layout.end());
1329 unsigned Ix = 0, EIx = layout.size();
1330 if (!data().IvarList) {
1331 data().IvarList = layout[0].Ivar; Ix++;
1332 curIvar = data().IvarList;
1333 }
1334 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1335 curIvar->setNextIvar(layout[Ix].Ivar);
1336 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001337 }
1338 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001339 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001340}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001341
1342/// FindCategoryDeclaration - Finds category declaration in the list of
1343/// categories for this class and returns it. Name of the category is passed
1344/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001345///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001346ObjCCategoryDecl *
1347ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001348 // FIXME: Should make sure no callers ever do this.
1349 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001350 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001351
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001352 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001353 LoadExternalDefinition();
1354
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001355 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001356 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001357 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001358
1359 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001360}
1361
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001362ObjCMethodDecl *
1363ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001364 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001365 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001366 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1367 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001368 }
1369
Craig Topper36250ad2014-05-12 05:36:57 +00001370 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001371}
1372
1373ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001374 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001375 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001376 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1377 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001378 }
Craig Topper36250ad2014-05-12 05:36:57 +00001379
1380 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001381}
1382
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001383/// ClassImplementsProtocol - Checks that 'lProto' protocol
1384/// has been implemented in IDecl class, its super class or categories (if
1385/// lookupCategory is true).
1386bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1387 bool lookupCategory,
1388 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001389 if (!hasDefinition())
1390 return false;
1391
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001392 ObjCInterfaceDecl *IDecl = this;
1393 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001394 for (auto *PI : IDecl->protocols()){
1395 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001396 return true;
1397 // This is dubious and is added to be compatible with gcc. In gcc, it is
1398 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1399 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1400 // object. This IMO, should be a bug.
1401 // FIXME: Treat this as an extension, and flag this as an error when GCC
1402 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001403 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001404 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001405 return true;
1406 }
Mike Stump11289f42009-09-09 15:08:12 +00001407
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001408 // 2nd, look up the category.
1409 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001410 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001411 for (auto *PI : Cat->protocols())
1412 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001413 return true;
1414 }
Mike Stump11289f42009-09-09 15:08:12 +00001415
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001416 // 3rd, look up the super class(s)
1417 if (IDecl->getSuperClass())
1418 return
1419 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1420 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001421
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001422 return false;
1423}
1424
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001425//===----------------------------------------------------------------------===//
1426// ObjCIvarDecl
1427//===----------------------------------------------------------------------===//
1428
David Blaikie68e081d2011-12-20 02:48:34 +00001429void ObjCIvarDecl::anchor() { }
1430
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001431ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001432 SourceLocation StartLoc,
1433 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001434 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001435 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001436 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001437 if (DC) {
1438 // Ivar's can only appear in interfaces, implementations (via synthesized
1439 // properties), and class extensions (via direct declaration, or synthesized
1440 // properties).
1441 //
1442 // FIXME: This should really be asserting this:
1443 // (isa<ObjCCategoryDecl>(DC) &&
1444 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1445 // but unfortunately we sometimes place ivars into non-class extension
1446 // categories on error. This breaks an AST invariant, and should not be
1447 // fixed.
1448 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1449 isa<ObjCCategoryDecl>(DC)) &&
1450 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001451 // Once a new ivar is created in any of class/class-extension/implementation
1452 // decl contexts, the previously built IvarList must be rebuilt.
1453 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1454 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001455 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001456 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001457 else
1458 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001459 }
Craig Topper36250ad2014-05-12 05:36:57 +00001460 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001461 }
1462
Richard Smithf7981722013-11-22 09:01:48 +00001463 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001464 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001465}
1466
Douglas Gregor72172e92012-01-05 21:55:30 +00001467ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001468 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1469 nullptr, QualType(), nullptr,
1470 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001471}
1472
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001473const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1474 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001475
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001476 switch (DC->getKind()) {
1477 default:
1478 case ObjCCategoryImpl:
1479 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001480 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001481
1482 // Ivars can only appear in class extension categories.
1483 case ObjCCategory: {
1484 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1485 assert(CD->IsClassExtension() && "invalid container for ivar!");
1486 return CD->getClassInterface();
1487 }
1488
1489 case ObjCImplementation:
1490 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1491
1492 case ObjCInterface:
1493 return cast<ObjCInterfaceDecl>(DC);
1494 }
1495}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001496
1497//===----------------------------------------------------------------------===//
1498// ObjCAtDefsFieldDecl
1499//===----------------------------------------------------------------------===//
1500
David Blaikie68e081d2011-12-20 02:48:34 +00001501void ObjCAtDefsFieldDecl::anchor() { }
1502
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001503ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001504*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1505 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001506 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001507 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001508}
1509
Richard Smithf7981722013-11-22 09:01:48 +00001510ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001511 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001512 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1513 SourceLocation(), nullptr, QualType(),
1514 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001515}
1516
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001517//===----------------------------------------------------------------------===//
1518// ObjCProtocolDecl
1519//===----------------------------------------------------------------------===//
1520
David Blaikie68e081d2011-12-20 02:48:34 +00001521void ObjCProtocolDecl::anchor() { }
1522
Richard Smith053f6c62014-05-16 23:01:30 +00001523ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1524 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001525 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001526 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001527 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1528 redeclarable_base(C), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001529 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001530 if (PrevDecl)
1531 Data = PrevDecl->Data;
1532}
1533
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001534ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001535 IdentifierInfo *Id,
1536 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001537 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001538 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001539 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001540 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001541 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001542 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001543}
1544
Richard Smithf7981722013-11-22 09:01:48 +00001545ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001546 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001547 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001548 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001549 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001550 Result->Data.setInt(!C.getLangOpts().Modules);
1551 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001552}
1553
Steve Naroff114aecb2009-03-01 16:12:44 +00001554ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1555 ObjCProtocolDecl *PDecl = this;
1556
1557 if (Name == getIdentifier())
1558 return PDecl;
1559
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001560 for (auto *I : protocols())
1561 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001562 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001563
Craig Topper36250ad2014-05-12 05:36:57 +00001564 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001565}
1566
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001567// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001568// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001569ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1570 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001571 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001572
Douglas Gregoreed49792013-01-17 00:38:46 +00001573 // If there is no definition or the definition is hidden, we don't find
1574 // anything.
1575 const ObjCProtocolDecl *Def = getDefinition();
1576 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001577 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001578
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001579 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001580 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001581
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001582 for (const auto *I : protocols())
1583 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001584 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001585 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001586}
1587
Douglas Gregore6e48b12012-01-01 19:29:29 +00001588void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001589 assert(!Data.getPointer() && "Protocol already has a definition!");
1590 Data.setPointer(new (getASTContext()) DefinitionData);
1591 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001592}
1593
1594void ObjCProtocolDecl::startDefinition() {
1595 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001596
1597 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001598 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001599 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001600}
1601
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001602void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1603 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001604
1605 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001606 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001607 // Insert into PM if not there already.
1608 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001609 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001610 }
1611 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001612 for (const auto *PI : PDecl->protocols())
1613 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001614 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001615}
1616
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001617
1618void ObjCProtocolDecl::collectInheritedProtocolProperties(
1619 const ObjCPropertyDecl *Property,
1620 ProtocolPropertyMap &PM) const {
1621 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1622 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001623 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001624 if (Prop == Property)
1625 continue;
1626 if (Prop->getIdentifier() == Property->getIdentifier()) {
1627 PM[PDecl] = Prop;
1628 MatchFound = true;
1629 break;
1630 }
1631 }
1632 // Scan through protocol's protocols which did not have a matching property.
1633 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001634 for (const auto *PI : PDecl->protocols())
1635 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001636 }
1637}
Anna Zaks673d76b2012-10-18 19:17:53 +00001638
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001639StringRef
1640ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001641 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1642 return ObjCRTName->getMetadataName();
1643
1644 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001645}
1646
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001647//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001648// ObjCCategoryDecl
1649//===----------------------------------------------------------------------===//
1650
David Blaikie68e081d2011-12-20 02:48:34 +00001651void ObjCCategoryDecl::anchor() { }
1652
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001653ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001654 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001655 SourceLocation ClassNameLoc,
1656 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001657 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001658 ObjCInterfaceDecl *IDecl,
1659 SourceLocation IvarLBraceLoc,
1660 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001661 ObjCCategoryDecl *CatDecl =
1662 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1663 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001664 if (IDecl) {
1665 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001666 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001667 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001668 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001669 if (ASTMutationListener *L = C.getASTMutationListener())
1670 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1671 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001672 }
1673
1674 return CatDecl;
1675}
1676
Richard Smithf7981722013-11-22 09:01:48 +00001677ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001678 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001679 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1680 SourceLocation(), SourceLocation(),
1681 nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001682}
1683
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001684ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1685 return getASTContext().getObjCImplementation(
1686 const_cast<ObjCCategoryDecl*>(this));
1687}
1688
1689void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1690 getASTContext().setObjCImplementation(this, ImplD);
1691}
1692
1693
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001694//===----------------------------------------------------------------------===//
1695// ObjCCategoryImplDecl
1696//===----------------------------------------------------------------------===//
1697
David Blaikie68e081d2011-12-20 02:48:34 +00001698void ObjCCategoryImplDecl::anchor() { }
1699
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001700ObjCCategoryImplDecl *
1701ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001702 IdentifierInfo *Id,
1703 ObjCInterfaceDecl *ClassInterface,
1704 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001705 SourceLocation atStartLoc,
1706 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001707 if (ClassInterface && ClassInterface->hasDefinition())
1708 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001709 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1710 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001711}
1712
Douglas Gregor72172e92012-01-05 21:55:30 +00001713ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1714 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001715 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1716 SourceLocation(), SourceLocation(),
1717 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001718}
1719
Steve Narofff406f4d2009-10-29 21:11:04 +00001720ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001721 // The class interface might be NULL if we are working with invalid code.
1722 if (const ObjCInterfaceDecl *ID = getClassInterface())
1723 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00001724 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001725}
1726
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001727
David Blaikie68e081d2011-12-20 02:48:34 +00001728void ObjCImplDecl::anchor() { }
1729
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001730void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001731 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001732 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001733 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001734}
1735
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001736void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1737 ASTContext &Ctx = getASTContext();
1738
1739 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001740 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001741 if (IFace)
1742 Ctx.setObjCImplementation(IFace, ImplD);
1743
Duncan Sands49c29ee2009-07-21 07:56:29 +00001744 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001745 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1746 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1747 Ctx.setObjCImplementation(CD, ImplD);
1748 }
1749
1750 ClassInterface = IFace;
1751}
1752
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001753/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001754/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001755/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001756///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001757ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001758FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001759 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001760 if (PID->getPropertyIvarDecl() &&
1761 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1762 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001763 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001764}
1765
1766/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001767/// added to the list of those properties \@synthesized/\@dynamic in this
1768/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001769///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001770ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001771FindPropertyImplDecl(IdentifierInfo *Id) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001772 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001773 if (PID->getPropertyDecl()->getIdentifier() == Id)
1774 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001775 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001776}
1777
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001778raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001779 const ObjCCategoryImplDecl &CID) {
1780 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001781 return OS;
1782}
1783
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001784//===----------------------------------------------------------------------===//
1785// ObjCImplementationDecl
1786//===----------------------------------------------------------------------===//
1787
David Blaikie68e081d2011-12-20 02:48:34 +00001788void ObjCImplementationDecl::anchor() { }
1789
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001790ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001791ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001792 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001793 ObjCInterfaceDecl *SuperDecl,
1794 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001795 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001796 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001797 SourceLocation IvarLBraceLoc,
1798 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001799 if (ClassInterface && ClassInterface->hasDefinition())
1800 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001801 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1802 nameLoc, atStartLoc, superLoc,
1803 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001804}
1805
Douglas Gregor72172e92012-01-05 21:55:30 +00001806ObjCImplementationDecl *
1807ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001808 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
1809 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001810}
1811
John McCall0410e572011-07-22 04:15:06 +00001812void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1813 CXXCtorInitializer ** initializers,
1814 unsigned numInitializers) {
1815 if (numInitializers > 0) {
1816 NumIvarInitializers = numInitializers;
1817 CXXCtorInitializer **ivarInitializers =
1818 new (C) CXXCtorInitializer*[NumIvarInitializers];
1819 memcpy(ivarInitializers, initializers,
1820 numInitializers * sizeof(CXXCtorInitializer*));
1821 IvarInitializers = ivarInitializers;
1822 }
1823}
1824
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001825raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001826 const ObjCImplementationDecl &ID) {
1827 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001828 return OS;
1829}
1830
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001831//===----------------------------------------------------------------------===//
1832// ObjCCompatibleAliasDecl
1833//===----------------------------------------------------------------------===//
1834
David Blaikie68e081d2011-12-20 02:48:34 +00001835void ObjCCompatibleAliasDecl::anchor() { }
1836
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001837ObjCCompatibleAliasDecl *
1838ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1839 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001840 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001841 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001842 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001843}
1844
Douglas Gregor72172e92012-01-05 21:55:30 +00001845ObjCCompatibleAliasDecl *
1846ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001847 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
1848 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001849}
1850
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001851//===----------------------------------------------------------------------===//
1852// ObjCPropertyDecl
1853//===----------------------------------------------------------------------===//
1854
David Blaikie68e081d2011-12-20 02:48:34 +00001855void ObjCPropertyDecl::anchor() { }
1856
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001857ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1858 SourceLocation L,
1859 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001860 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001861 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001862 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001863 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001864 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001865}
1866
Richard Smithf7981722013-11-22 09:01:48 +00001867ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001868 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001869 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
1870 SourceLocation(), SourceLocation(),
1871 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001872}
1873
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001874//===----------------------------------------------------------------------===//
1875// ObjCPropertyImplDecl
1876//===----------------------------------------------------------------------===//
1877
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001878ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001879 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001880 SourceLocation atLoc,
1881 SourceLocation L,
1882 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001883 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001884 ObjCIvarDecl *ivar,
1885 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001886 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1887 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001888}
Chris Lattnered0e1642008-03-17 01:19:02 +00001889
Richard Smithf7981722013-11-22 09:01:48 +00001890ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001891 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001892 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
1893 SourceLocation(), nullptr, Dynamic,
1894 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001895}
1896
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001897SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1898 SourceLocation EndLoc = getLocation();
1899 if (IvarLoc.isValid())
1900 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001901
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001902 return SourceRange(AtLoc, EndLoc);
1903}