blob: 79054c7862f97acb8d6353f53574c79ef42ce3a0 [file] [log] [blame]
Chris Lattner89375192008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000016#include "clang/AST/ASTMutationListener.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/Stmt.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000019#include "llvm/ADT/STLExtras.h"
Anna Zaks454477c2012-09-27 19:45:11 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner89375192008-03-16 00:19:01 +000021using namespace clang;
22
Chris Lattner8d8829e2008-03-16 00:49:28 +000023//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000024// ObjCListBase
25//===----------------------------------------------------------------------===//
26
Chris Lattner22298722009-02-20 21:35:13 +000027void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Craig Topper36250ad2014-05-12 05:36:57 +000028 List = nullptr;
Chris Lattner4d1eb762009-02-20 21:16:26 +000029 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000030
31
Chris Lattner7c981a72009-02-20 21:44:01 +000032 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000033 NumElts = Elts;
34 memcpy(List, InList, sizeof(void*)*Elts);
35}
36
Douglas Gregor002b6712010-01-16 15:02:53 +000037void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
38 const SourceLocation *Locs, ASTContext &Ctx) {
39 if (Elts == 0)
40 return;
41
42 Locations = new (Ctx) SourceLocation[Elts];
43 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
44 set(InList, Elts, Ctx);
45}
46
Chris Lattner4d1eb762009-02-20 21:16:26 +000047//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000048// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000049//===----------------------------------------------------------------------===//
50
David Blaikie68e081d2011-12-20 02:48:34 +000051void ObjCContainerDecl::anchor() { }
52
Fariborz Jahanian68453832009-06-05 18:16:35 +000053/// getIvarDecl - This method looks up an ivar in this ContextDecl.
54///
55ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000056ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Richard Smithcf4bdde2015-02-21 02:45:19 +000057 lookup_result R = lookup(Id);
58 for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000059 Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian68453832009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
Craig Topper36250ad2014-05-12 05:36:57 +000063 return nullptr;
Fariborz Jahanian68453832009-06-05 18:16:35 +000064}
65
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000066// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000067ObjCMethodDecl *
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69 bool AllowHidden) const {
Douglas Gregoreed49792013-01-17 00:38:46 +000070 // If this context is a hidden protocol definition, don't find any
71 // methods there.
72 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
73 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000074 if (Def->isHidden() && !AllowHidden)
Craig Topper36250ad2014-05-12 05:36:57 +000075 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +000076 }
77
Steve Naroffc4173fa2009-02-22 19:35:57 +000078 // Since instance & class methods can have the same name, the loop below
79 // ensures we get the correct method.
80 //
81 // @interface Whatever
82 // - (int) class_method;
83 // + (float) class_method;
84 // @end
85 //
Richard Smithcf4bdde2015-02-21 02:45:19 +000086 lookup_result R = lookup(Sel);
87 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000088 Meth != MethEnd; ++Meth) {
Steve Naroffc4173fa2009-02-22 19:35:57 +000089 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000090 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +000091 return MD;
92 }
Craig Topper36250ad2014-05-12 05:36:57 +000093 return nullptr;
Steve Naroff35c62ae2009-01-08 17:28:14 +000094}
95
Nico Weber4701ffd2014-12-22 05:21:03 +000096/// \brief This routine returns 'true' if a user declared setter method was
97/// found in the class, its protocols, its super classes or categories.
98/// It also returns 'true' if one of its categories has declared a 'readwrite'
99/// property. This is because, user must provide a setter method for the
100/// category's 'readwrite' property.
101bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
102 const ObjCPropertyDecl *Property) const {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000103 Selector Sel = Property->getSetterName();
Richard Smithcf4bdde2015-02-21 02:45:19 +0000104 lookup_result R = lookup(Sel);
105 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000106 Meth != MethEnd; ++Meth) {
107 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
108 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
109 return true;
110 }
111
112 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
113 // Also look into categories, including class extensions, looking
114 // for a user declared instance method.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000115 for (const auto *Cat : ID->visible_categories()) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000116 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
117 if (!MD->isImplicit())
118 return true;
119 if (Cat->IsClassExtension())
120 continue;
Nico Weber4701ffd2014-12-22 05:21:03 +0000121 // Also search through the categories looking for a 'readwrite'
122 // declaration of this property. If one found, presumably a setter will
123 // be provided (properties declared in categories will not get
124 // auto-synthesized).
Aaron Ballmand174edf2014-03-13 19:11:50 +0000125 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000126 if (P->getIdentifier() == Property->getIdentifier()) {
127 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
128 return true;
129 break;
130 }
131 }
132
133 // Also look into protocols, for a user declared instance method.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000134 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000135 if (Proto->HasUserDeclaredSetterMethod(Property))
136 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000137
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000138 // And in its super class.
139 ObjCInterfaceDecl *OSC = ID->getSuperClass();
140 while (OSC) {
141 if (OSC->HasUserDeclaredSetterMethod(Property))
142 return true;
143 OSC = OSC->getSuperClass();
144 }
145 }
146 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000147 for (const auto *PI : PD->protocols())
148 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000149 return true;
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000150 return false;
151}
152
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000153ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000154ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Jordan Rose210bfe92014-11-19 22:03:46 +0000155 const IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000156 // If this context is a hidden protocol definition, don't find any
157 // property.
158 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
159 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
160 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000161 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000162 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000163
Richard Smithcf4bdde2015-02-21 02:45:19 +0000164 DeclContext::lookup_result R = DC->lookup(propertyID);
165 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
David Blaikieff7d47a2012-12-19 00:45:41 +0000166 ++I)
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000167 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
168 return PD;
169
Craig Topper36250ad2014-05-12 05:36:57 +0000170 return nullptr;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000171}
172
Anna Zaks454477c2012-09-27 19:45:11 +0000173IdentifierInfo *
174ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
175 SmallString<128> ivarName;
176 {
177 llvm::raw_svector_ostream os(ivarName);
178 os << '_' << getIdentifier()->getName();
179 }
180 return &Ctx.Idents.get(ivarName.str());
181}
182
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000183/// FindPropertyDeclaration - Finds declaration of the property given its name
184/// in 'PropertyId' and returns it. It returns 0, if not found.
Jordan Rose210bfe92014-11-19 22:03:46 +0000185ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
186 const IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000187 // Don't find properties within hidden protocol definitions.
188 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
189 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
190 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000191 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Ted Kremenekddcd1092010-03-15 20:11:53 +0000194 if (ObjCPropertyDecl *PD =
195 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
196 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Ted Kremenekddcd1092010-03-15 20:11:53 +0000198 switch (getKind()) {
199 default:
200 break;
201 case Decl::ObjCProtocol: {
202 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000203 for (const auto *I : PID->protocols())
204 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000205 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000206 break;
207 }
208 case Decl::ObjCInterface: {
209 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000210 // Look through categories (but not extensions).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000211 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000212 if (!Cat->IsClassExtension())
213 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
214 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000215 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000216
217 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000218 for (const auto *I : OID->all_referenced_protocols())
219 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000220 return P;
221
222 // Finally, check the super class.
223 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
224 return superClass->FindPropertyDeclaration(PropertyId);
225 break;
226 }
227 case Decl::ObjCCategory: {
228 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
229 // Look through protocols.
230 if (!OCD->IsClassExtension())
Aaron Ballman19a41762014-03-14 12:55:57 +0000231 for (const auto *I : OCD->protocols())
232 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
233 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000234 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000235 }
236 }
Craig Topper36250ad2014-05-12 05:36:57 +0000237 return nullptr;
Steve Narofff9c65242008-06-05 13:55:23 +0000238}
239
David Blaikie68e081d2011-12-20 02:48:34 +0000240void ObjCInterfaceDecl::anchor() { }
241
Douglas Gregor85f3f952015-07-07 03:57:15 +0000242ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
243 // If this particular declaration has a type parameter list, return it.
244 if (ObjCTypeParamList *written = getTypeParamListAsWritten())
245 return written;
246
247 // If there is a definition, return its type parameter list.
248 if (const ObjCInterfaceDecl *def = getDefinition())
249 return def->getTypeParamListAsWritten();
250
251 // Otherwise, look at previous declarations to determine whether any
252 // of them has a type parameter list, skipping over those
253 // declarations that do not.
254 for (auto decl = getPreviousDecl(); decl; decl = decl->getPreviousDecl()) {
255 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
256 return written;
257 }
258
259 return nullptr;
260}
261
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000262/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
263/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000264/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000265///
266ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000267ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000268 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000269 // FIXME: Should make sure no callers ever do this.
270 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000271 return nullptr;
272
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000273 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000274 LoadExternalDefinition();
275
Ted Kremenekd133a862010-03-15 20:30:07 +0000276 if (ObjCPropertyDecl *PD =
277 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
278 return PD;
279
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000280 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000281 for (const auto *I : all_referenced_protocols())
282 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000283 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000284
Craig Topper36250ad2014-05-12 05:36:57 +0000285 return nullptr;
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000286}
287
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000288void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
289 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000290 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000291 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000292 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000293 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000294 for (const auto *PI : all_referenced_protocols())
295 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000296 // Note, the properties declared only in class extensions are still copied
297 // into the main @interface's property list, and therefore we don't
298 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000299}
300
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000301bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
302 const ObjCInterfaceDecl *Class = this;
303 while (Class) {
304 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
305 return true;
306 Class = Class->getSuperClass();
307 }
308 return false;
309}
310
311const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
312 const ObjCInterfaceDecl *Class = this;
313 while (Class) {
314 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
315 return Class;
316 Class = Class->getSuperClass();
317 }
Craig Topper36250ad2014-05-12 05:36:57 +0000318 return nullptr;
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000319}
320
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000321void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
322 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
323 ASTContext &C)
324{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000325 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000326 LoadExternalDefinition();
327
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000328 if (data().AllReferencedProtocols.empty() &&
329 data().ReferencedProtocols.empty()) {
330 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000331 return;
332 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000333
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000334 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000335 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000336 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000337 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000338 for (unsigned i = 0; i < ExtNum; i++) {
339 bool protocolExists = false;
340 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000341 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000342 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
343 protocolExists = true;
344 break;
345 }
346 }
347 // Do we want to warn on a protocol in extension class which
348 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000349 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000350 ProtocolRefs.push_back(ProtoInExtension);
351 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000352
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000353 if (ProtocolRefs.empty())
354 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000355
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000356 // Merge ProtocolRefs into class's protocol list;
Benjamin Kramerf9890422015-02-17 16:48:30 +0000357 ProtocolRefs.append(all_referenced_protocol_begin(),
358 all_referenced_protocol_end());
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000359
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000360 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000361}
362
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000363const ObjCInterfaceDecl *
364ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
365 const ObjCInterfaceDecl *IFace = this;
366 while (IFace) {
367 if (IFace->hasDesignatedInitializers())
368 return IFace;
369 if (!IFace->inheritsDesignatedInitializers())
370 break;
371 IFace = IFace->getSuperClass();
372 }
Craig Topper36250ad2014-05-12 05:36:57 +0000373 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000374}
375
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000376static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
377 for (const auto *MD : D->instance_methods()) {
378 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
379 return true;
380 }
381 for (const auto *Ext : D->visible_extensions()) {
382 for (const auto *MD : Ext->instance_methods()) {
383 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
384 return true;
385 }
386 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000387 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000388 for (const auto *MD : ImplD->instance_methods()) {
389 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
390 return true;
391 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000392 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000393 return false;
394}
395
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000396bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
397 switch (data().InheritedDesignatedInitializers) {
398 case DefinitionData::IDI_Inherited:
399 return true;
400 case DefinitionData::IDI_NotInherited:
401 return false;
402 case DefinitionData::IDI_Unknown: {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000403 // If the class introduced initializers we conservatively assume that we
404 // don't know if any of them is a designated initializer to avoid possible
405 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000406 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000407 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000408 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000409 if (auto SuperD = getSuperClass()) {
410 data().InheritedDesignatedInitializers =
411 SuperD->declaresOrInheritsDesignatedInitializers() ?
412 DefinitionData::IDI_Inherited :
413 DefinitionData::IDI_NotInherited;
414 } else {
415 data().InheritedDesignatedInitializers =
416 DefinitionData::IDI_NotInherited;
417 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000418 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000419 assert(data().InheritedDesignatedInitializers
420 != DefinitionData::IDI_Unknown);
421 return data().InheritedDesignatedInitializers ==
422 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000423 }
424 }
425
426 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
427}
428
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000429void ObjCInterfaceDecl::getDesignatedInitializers(
430 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000431 // Check for a complete definition and recover if not so.
432 if (!isThisDeclarationADefinition())
433 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000434 if (data().ExternallyCompleted)
435 LoadExternalDefinition();
436
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000437 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000438 if (!IFace)
439 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000440
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000441 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000442 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000443 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000444 for (const auto *Ext : IFace->visible_extensions()) {
445 for (const auto *MD : Ext->instance_methods())
446 if (MD->isThisDeclarationADesignatedInitializer())
447 Methods.push_back(MD);
448 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000449}
450
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000451bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
452 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000453 // Check for a complete definition and recover if not so.
454 if (!isThisDeclarationADefinition())
455 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000456 if (data().ExternallyCompleted)
457 LoadExternalDefinition();
458
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000459 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000460 if (!IFace)
461 return false;
462
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000463 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000464 if (MD->isThisDeclarationADesignatedInitializer()) {
465 if (InitMethod)
466 *InitMethod = MD;
467 return true;
468 }
469 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000470 for (const auto *Ext : IFace->visible_extensions()) {
471 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
472 if (MD->isThisDeclarationADesignatedInitializer()) {
473 if (InitMethod)
474 *InitMethod = MD;
475 return true;
476 }
477 }
478 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000479 return false;
480}
481
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000482void ObjCInterfaceDecl::allocateDefinitionData() {
483 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000484 Data.setPointer(new (getASTContext()) DefinitionData());
485 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000486
487 // Make the type point at the definition, now that we have one.
488 if (TypeForDecl)
489 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000490}
491
492void ObjCInterfaceDecl::startDefinition() {
493 allocateDefinitionData();
494
Douglas Gregor66b310c2011-12-15 18:03:09 +0000495 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000496 for (auto RD : redecls()) {
497 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000498 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000499 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000500}
501
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000502ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
503 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000504 // FIXME: Should make sure no callers ever do this.
505 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000506 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000507
508 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000509 LoadExternalDefinition();
510
Chris Lattner89375192008-03-16 00:19:01 +0000511 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000512 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000513 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000514 clsDeclared = ClassDecl;
515 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000516 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000517
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000518 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000519 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000520 clsDeclared = ClassDecl;
521 return I;
522 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000523 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000524
Chris Lattner89375192008-03-16 00:19:01 +0000525 ClassDecl = ClassDecl->getSuperClass();
526 }
Craig Topper36250ad2014-05-12 05:36:57 +0000527 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000528}
529
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000530/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
531/// class whose name is passed as argument. If it is not one of the super classes
532/// the it returns NULL.
533ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
534 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000535 // FIXME: Should make sure no callers ever do this.
536 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000537 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000538
539 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000540 LoadExternalDefinition();
541
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000542 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000543 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000544 if (ClassDecl->getIdentifier() == ICName)
545 return ClassDecl;
546 ClassDecl = ClassDecl->getSuperClass();
547 }
Craig Topper36250ad2014-05-12 05:36:57 +0000548 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000549}
550
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000551ObjCProtocolDecl *
552ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000553 for (auto *P : all_referenced_protocols())
554 if (P->lookupProtocolNamed(Name))
555 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000556 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000557 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000558}
559
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000560/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000561/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000562/// When argument category "C" is specified, any implicit method found
563/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000564ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000565 bool isInstance,
566 bool shallowCategoryLookup,
567 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000568 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000569{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000570 // FIXME: Should make sure no callers ever do this.
571 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000572 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000573
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000574 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000575 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000576
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000577 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000578 LoadExternalDefinition();
579
Ted Kremenek00781502013-11-23 01:01:29 +0000580 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000581 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000582 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000583 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000584
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000585 // 2. Didn't find one yet - now look through categories.
586 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000587 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000588 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000589 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000590
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000591 // 3. Didn't find one yet - look through primary class's protocols.
592 for (const auto *I : ClassDecl->protocols())
593 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
594 return MethodDecl;
595
596 // 4. Didn't find one yet - now look through categories' protocols
597 if (!shallowCategoryLookup)
598 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000599 // Didn't find one yet - look through protocols.
600 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000601 Cat->getReferencedProtocols();
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000602 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
603 E = Protocols.end(); I != E; ++I)
604 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000605 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000606 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000607 }
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000608
609
Ted Kremenek00781502013-11-23 01:01:29 +0000610 if (!followSuper)
Craig Topper36250ad2014-05-12 05:36:57 +0000611 return nullptr;
Ted Kremenek00781502013-11-23 01:01:29 +0000612
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000613 // 5. Get to the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000614 ClassDecl = ClassDecl->getSuperClass();
615 }
Craig Topper36250ad2014-05-12 05:36:57 +0000616 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000617}
618
Anna Zaksc77a3b12012-07-27 19:07:44 +0000619// Will search "local" class/category implementations for a method decl.
620// If failed, then we search in class's root for an instance method.
621// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000622ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
623 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000624 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000625 // FIXME: Should make sure no callers ever do this.
626 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000627 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000628
629 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000630 LoadExternalDefinition();
631
Craig Topper36250ad2014-05-12 05:36:57 +0000632 ObjCMethodDecl *Method = nullptr;
Steve Naroffbb69c942009-10-01 23:46:04 +0000633 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000634 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
635 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000636
637 // Look through local category implementations associated with the class.
638 if (!Method)
Nico Weberf6098392015-03-02 01:12:28 +0000639 Method = getCategoryMethod(Sel, Instance);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000640
641 // Before we give up, check if the selector is an instance method.
642 // But only in the root. This matches gcc's behavior and what the
643 // runtime expects.
644 if (!Instance && !Method && !getSuperClass()) {
645 Method = lookupInstanceMethod(Sel);
646 // Look through local category implementations associated
647 // with the root class.
648 if (!Method)
649 Method = lookupPrivateMethod(Sel, true);
650 }
651
Steve Naroffbb69c942009-10-01 23:46:04 +0000652 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000653 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000654 return Method;
655}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000656
657//===----------------------------------------------------------------------===//
658// ObjCMethodDecl
659//===----------------------------------------------------------------------===//
660
Alp Toker314cc812014-01-25 16:55:45 +0000661ObjCMethodDecl *ObjCMethodDecl::Create(
662 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
663 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
664 DeclContext *contextDecl, bool isInstance, bool isVariadic,
665 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
666 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000667 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000668 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000669 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
670 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000671}
672
Douglas Gregor72172e92012-01-05 21:55:30 +0000673ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000674 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000675 Selector(), QualType(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000676}
677
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000678bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
679 return getMethodFamily() == OMF_init &&
680 hasAttr<ObjCDesignatedInitializerAttr>();
681}
682
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000683bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
684 const ObjCMethodDecl **InitMethod) const {
685 if (getMethodFamily() != OMF_init)
686 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000687 const DeclContext *DC = getDeclContext();
688 if (isa<ObjCProtocolDecl>(DC))
689 return false;
690 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000691 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000692 return false;
693}
694
Douglas Gregora6017bb2012-10-09 17:21:28 +0000695Stmt *ObjCMethodDecl::getBody() const {
696 return Body.get(getASTContext().getExternalSource());
697}
698
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000699void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
700 assert(PrevMethod);
701 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
702 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000703 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000704}
705
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000706void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
707 ArrayRef<ParmVarDecl*> Params,
708 ArrayRef<SourceLocation> SelLocs) {
Craig Topper36250ad2014-05-12 05:36:57 +0000709 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000710 NumParams = Params.size();
711 if (Params.empty() && SelLocs.empty())
712 return;
713
714 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
715 sizeof(SourceLocation) * SelLocs.size();
716 ParamsAndSelLocs = C.Allocate(Size);
717 std::copy(Params.begin(), Params.end(), getParams());
718 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
719}
720
721void ObjCMethodDecl::getSelectorLocs(
722 SmallVectorImpl<SourceLocation> &SelLocs) const {
723 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
724 SelLocs.push_back(getSelectorLoc(i));
725}
726
727void ObjCMethodDecl::setMethodParams(ASTContext &C,
728 ArrayRef<ParmVarDecl*> Params,
729 ArrayRef<SourceLocation> SelLocs) {
730 assert((!SelLocs.empty() || isImplicit()) &&
731 "No selector locs for non-implicit method");
732 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000733 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000734
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000735 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
736 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000737 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000738 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000739
740 setParamsAndSelLocs(C, Params, SelLocs);
741}
742
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000743/// \brief A definition will return its interface declaration.
744/// An interface declaration will return its definition.
745/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000746ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000747 ASTContext &Ctx = getASTContext();
Craig Topper36250ad2014-05-12 05:36:57 +0000748 ObjCMethodDecl *Redecl = nullptr;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000749 if (HasRedeclaration)
750 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000751 if (Redecl)
752 return Redecl;
753
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000754 Decl *CtxD = cast<Decl>(getDeclContext());
755
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000756 if (!CtxD->isInvalidDecl()) {
757 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
758 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
759 if (!ImplD->isInvalidDecl())
760 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000761
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000762 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
763 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
764 if (!ImplD->isInvalidDecl())
765 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000766
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000767 } else if (ObjCImplementationDecl *ImplD =
768 dyn_cast<ObjCImplementationDecl>(CtxD)) {
769 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
770 if (!IFD->isInvalidDecl())
771 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000772
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000773 } else if (ObjCCategoryImplDecl *CImplD =
774 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
775 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
776 if (!CatD->isInvalidDecl())
777 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
778 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000779 }
780
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000781 if (!Redecl && isRedeclaration()) {
782 // This is the last redeclaration, go back to the first method.
783 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
784 isInstanceMethod());
785 }
786
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000787 return Redecl ? Redecl : this;
788}
789
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000790ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
791 Decl *CtxD = cast<Decl>(getDeclContext());
792
793 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
794 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
795 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
796 isInstanceMethod()))
797 return MD;
798
799 } else if (ObjCCategoryImplDecl *CImplD =
800 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000801 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000802 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
803 isInstanceMethod()))
804 return MD;
805 }
806
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000807 if (isRedeclaration())
808 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
809 isInstanceMethod());
810
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000811 return this;
812}
813
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000814SourceLocation ObjCMethodDecl::getLocEnd() const {
815 if (Stmt *Body = getBody())
816 return Body->getLocEnd();
817 return DeclEndLoc;
818}
819
John McCallb4526252011-03-02 01:50:55 +0000820ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
821 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000822 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000823 return family;
824
John McCall86bc21f2011-03-02 11:33:24 +0000825 // Check for an explicit attribute.
826 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
827 // The unfortunate necessity of mapping between enums here is due
828 // to the attributes framework.
829 switch (attr->getFamily()) {
830 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
831 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
832 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
833 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
834 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
835 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
836 }
837 Family = static_cast<unsigned>(family);
838 return family;
839 }
840
John McCallb4526252011-03-02 01:50:55 +0000841 family = getSelector().getMethodFamily();
842 switch (family) {
843 case OMF_None: break;
844
845 // init only has a conventional meaning for an instance method, and
846 // it has to return an object.
847 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000848 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000849 family = OMF_None;
850 break;
851
852 // alloc/copy/new have a conventional meaning for both class and
853 // instance methods, but they require an object return.
854 case OMF_alloc:
855 case OMF_copy:
856 case OMF_mutableCopy:
857 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000858 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000859 family = OMF_None;
860 break;
861
862 // These selectors have a conventional meaning only for instance methods.
863 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000864 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000865 case OMF_retain:
866 case OMF_release:
867 case OMF_autorelease:
868 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000869 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000870 if (!isInstanceMethod())
871 family = OMF_None;
872 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000873
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000874 case OMF_initialize:
875 if (isInstanceMethod() || !getReturnType()->isVoidType())
876 family = OMF_None;
877 break;
878
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000879 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000880 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000881 family = OMF_None;
882 else {
883 unsigned noParams = param_size();
884 if (noParams < 1 || noParams > 3)
885 family = OMF_None;
886 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000887 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000888 QualType ArgT = (*it);
889 if (!ArgT->isObjCSelType()) {
890 family = OMF_None;
891 break;
892 }
893 while (--noParams) {
894 it++;
895 ArgT = (*it);
896 if (!ArgT->isObjCIdType()) {
897 family = OMF_None;
898 break;
899 }
900 }
901 }
902 }
903 break;
904
John McCallb4526252011-03-02 01:50:55 +0000905 }
906
907 // Cache the result.
908 Family = static_cast<unsigned>(family);
909 return family;
910}
911
Mike Stump11289f42009-09-09 15:08:12 +0000912void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000913 const ObjCInterfaceDecl *OID) {
914 QualType selfTy;
915 if (isInstanceMethod()) {
916 // There may be no interface context due to error in declaration
917 // of the interface (which has been reported). Recover gracefully.
918 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000919 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000920 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000921 } else {
922 selfTy = Context.getObjCIdType();
923 }
924 } else // we have a factory method.
925 selfTy = Context.getObjCClassType();
926
John McCalld4631322011-06-17 06:42:21 +0000927 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000928 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000929
David Blaikiebbafb8a2012-03-11 07:00:24 +0000930 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000931 if (isInstanceMethod()) {
932 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000933
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000934 // 'self' is always __strong. It's actually pseudo-strong except
935 // in init methods (or methods labeled ns_consumes_self), though.
936 Qualifiers qs;
937 qs.setObjCLifetime(Qualifiers::OCL_Strong);
938 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000939
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000940 // In addition, 'self' is const unless this is an init method.
941 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
942 selfTy = selfTy.withConst();
943 selfIsPseudoStrong = true;
944 }
945 }
946 else {
947 assert(isClassMethod());
948 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000949 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000950 selfIsPseudoStrong = true;
951 }
John McCall31168b02011-06-15 23:02:42 +0000952 }
953
954 ImplicitParamDecl *self
955 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
956 &Context.Idents.get("self"), selfTy);
957 setSelfDecl(self);
958
959 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000960 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000961
John McCalld4631322011-06-17 06:42:21 +0000962 if (selfIsPseudoStrong)
963 self->setARCPseudoStrong(true);
964
Mike Stump11289f42009-09-09 15:08:12 +0000965 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
966 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000967 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000968}
969
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000970ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
971 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
972 return ID;
973 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
974 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000975 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000976 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000977 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +0000978 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +0000979 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000980}
981
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000982SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
983 const auto *TSI = getReturnTypeSourceInfo();
984 if (TSI)
985 return TSI->getTypeLoc().getSourceRange();
986 return SourceRange();
987}
988
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000989static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
990 const ObjCMethodDecl *Method,
991 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
992 bool MovedToSuper) {
993 if (!Container)
994 return;
995
996 // In categories look for overriden methods from protocols. A method from
997 // category is not "overriden" since it is considered as the "same" method
998 // (same USR) as the one from the interface.
999 if (const ObjCCategoryDecl *
1000 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1001 // Check whether we have a matching method at this category but only if we
1002 // are at the super class level.
1003 if (MovedToSuper)
1004 if (ObjCMethodDecl *
1005 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001006 Method->isInstanceMethod(),
1007 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001008 if (Method != Overridden) {
1009 // We found an override at this category; there is no need to look
1010 // into its protocols.
1011 Methods.push_back(Overridden);
1012 return;
1013 }
1014
Aaron Ballman19a41762014-03-14 12:55:57 +00001015 for (const auto *P : Category->protocols())
1016 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001017 return;
1018 }
1019
1020 // Check whether we have a matching method at this level.
1021 if (const ObjCMethodDecl *
1022 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001023 Method->isInstanceMethod(),
1024 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001025 if (Method != Overridden) {
1026 // We found an override at this level; there is no need to look
1027 // into other protocols or categories.
1028 Methods.push_back(Overridden);
1029 return;
1030 }
1031
1032 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001033 for (const auto *P : Protocol->protocols())
1034 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001035 }
1036
1037 if (const ObjCInterfaceDecl *
1038 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001039 for (const auto *P : Interface->protocols())
1040 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001041
Aaron Ballman15063e12014-03-13 21:35:02 +00001042 for (const auto *Cat : Interface->known_categories())
1043 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001044
1045 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1046 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1047 /*MovedToSuper=*/true);
1048 }
1049}
1050
1051static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1052 const ObjCMethodDecl *Method,
1053 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1054 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1055 /*MovedToSuper=*/false);
1056}
1057
1058static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1059 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1060 assert(Method->isOverriding());
1061
1062 if (const ObjCProtocolDecl *
1063 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1064 CollectOverriddenMethods(ProtD, Method, overridden);
1065
1066 } else if (const ObjCImplDecl *
1067 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1068 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1069 if (!ID)
1070 return;
1071 // Start searching for overridden methods using the method from the
1072 // interface as starting point.
1073 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001074 Method->isInstanceMethod(),
1075 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001076 Method = IFaceMeth;
1077 CollectOverriddenMethods(ID, Method, overridden);
1078
1079 } else if (const ObjCCategoryDecl *
1080 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1081 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1082 if (!ID)
1083 return;
1084 // Start searching for overridden methods using the method from the
1085 // interface as starting point.
1086 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001087 Method->isInstanceMethod(),
1088 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001089 Method = IFaceMeth;
1090 CollectOverriddenMethods(ID, Method, overridden);
1091
1092 } else {
1093 CollectOverriddenMethods(
1094 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1095 Method, overridden);
1096 }
1097}
1098
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001099void ObjCMethodDecl::getOverriddenMethods(
1100 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1101 const ObjCMethodDecl *Method = this;
1102
1103 if (Method->isRedeclaration()) {
1104 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1105 getMethod(Method->getSelector(), Method->isInstanceMethod());
1106 }
1107
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001108 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001109 collectOverriddenMethodsSlow(Method, Overridden);
1110 assert(!Overridden.empty() &&
1111 "ObjCMethodDecl's overriding bit is not as expected");
1112 }
1113}
1114
Jordan Rose2bd991a2012-10-10 16:42:54 +00001115const ObjCPropertyDecl *
1116ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1117 Selector Sel = getSelector();
1118 unsigned NumArgs = Sel.getNumArgs();
1119 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001120 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001121
Jordan Rose16ba5532015-01-16 23:04:26 +00001122 if (!isInstanceMethod())
Craig Topper36250ad2014-05-12 05:36:57 +00001123 return nullptr;
1124
Jordan Rose2bd991a2012-10-10 16:42:54 +00001125 if (isPropertyAccessor()) {
1126 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001127 // If container is class extension, find its primary class.
1128 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1129 if (CatDecl->IsClassExtension())
1130 Container = CatDecl->getClassInterface();
1131
Jordan Rose2bd991a2012-10-10 16:42:54 +00001132 bool IsGetter = (NumArgs == 0);
1133
Aaron Ballmand174edf2014-03-13 19:11:50 +00001134 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001135 Selector NextSel = IsGetter ? I->getGetterName()
1136 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001137 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001138 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001139 }
1140
1141 llvm_unreachable("Marked as a property accessor but no property found!");
1142 }
1143
1144 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001145 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001146
1147 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1148 OverridesTy Overrides;
1149 getOverriddenMethods(Overrides);
1150 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1151 I != E; ++I) {
1152 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1153 return Prop;
1154 }
1155
Craig Topper36250ad2014-05-12 05:36:57 +00001156 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001157}
1158
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001159//===----------------------------------------------------------------------===//
Douglas Gregor85f3f952015-07-07 03:57:15 +00001160// ObjCTypeParamDecl
1161//===----------------------------------------------------------------------===//
1162
1163void ObjCTypeParamDecl::anchor() { }
1164
1165ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
1166 SourceLocation nameLoc,
1167 IdentifierInfo *name,
1168 SourceLocation colonLoc,
1169 TypeSourceInfo *boundInfo) {
1170 return new (ctx, dc) ObjCTypeParamDecl(ctx, dc, nameLoc, name, colonLoc,
1171 boundInfo);
1172}
1173
1174ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1175 unsigned ID) {
1176 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr, SourceLocation(),
1177 nullptr, SourceLocation(), nullptr);
1178}
1179
1180SourceRange ObjCTypeParamDecl::getSourceRange() const {
1181 if (hasExplicitBound()) {
1182 return SourceRange(getLocation(),
1183 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1184 }
1185
1186 return SourceRange(getLocation());
1187}
1188
1189//===----------------------------------------------------------------------===//
1190// ObjCTypeParamList
1191//===----------------------------------------------------------------------===//
1192ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1193 ArrayRef<ObjCTypeParamDecl *> typeParams,
1194 SourceLocation rAngleLoc)
1195 : Brackets(lAngleLoc, rAngleLoc), NumParams(typeParams.size())
1196{
1197 std::copy(typeParams.begin(), typeParams.end(), begin());
1198}
1199
1200
1201ObjCTypeParamList *ObjCTypeParamList::create(
1202 ASTContext &ctx,
1203 SourceLocation lAngleLoc,
1204 ArrayRef<ObjCTypeParamDecl *> typeParams,
1205 SourceLocation rAngleLoc) {
1206 unsigned size = sizeof(ObjCTypeParamList)
1207 + sizeof(ObjCTypeParamDecl *) * typeParams.size();
1208 static_assert(alignof(ObjCTypeParamList) >= alignof(ObjCTypeParamDecl*),
1209 "type parameter list needs greater alignment");
1210 unsigned align = llvm::alignOf<ObjCTypeParamList>();
1211 void *mem = ctx.Allocate(size, align);
1212 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1213}
1214
1215//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001216// ObjCInterfaceDecl
1217//===----------------------------------------------------------------------===//
1218
Douglas Gregord53ae832012-01-17 18:09:05 +00001219ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001220 DeclContext *DC,
1221 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001222 IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001223 ObjCTypeParamList *typeParamList,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001224 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001225 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001226 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001227 ObjCInterfaceDecl *Result = new (C, DC)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001228 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1229 isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001230 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001231 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001232 return Result;
1233}
1234
Richard Smith053f6c62014-05-16 23:01:30 +00001235ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001236 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001237 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001238 SourceLocation(),
1239 nullptr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001240 nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001241 SourceLocation(),
1242 nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001243 Result->Data.setInt(!C.getLangOpts().Modules);
1244 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001245}
1246
Richard Smith053f6c62014-05-16 23:01:30 +00001247ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1248 SourceLocation AtLoc, IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001249 ObjCTypeParamList *typeParamList,
Richard Smith053f6c62014-05-16 23:01:30 +00001250 SourceLocation CLoc,
1251 ObjCInterfaceDecl *PrevDecl,
1252 bool IsInternal)
1253 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001254 redeclarable_base(C), TypeForDecl(nullptr), TypeParamList(typeParamList),
1255 Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001256 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001257
1258 // Copy the 'data' pointer over.
1259 if (PrevDecl)
1260 Data = PrevDecl->Data;
1261
Richard Smith053f6c62014-05-16 23:01:30 +00001262 setImplicit(IsInternal);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001263
1264 // Update the declaration context of the type parameters.
1265 if (typeParamList) {
1266 for (auto typeParam : *typeParamList)
1267 typeParam->setDeclContext(this);
1268 }
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001269}
1270
Douglas Gregor73693022010-12-01 23:49:52 +00001271void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001272 assert(data().ExternallyCompleted && "Class is not externally completed");
1273 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001274 getASTContext().getExternalSource()->CompleteType(
1275 const_cast<ObjCInterfaceDecl *>(this));
1276}
1277
1278void ObjCInterfaceDecl::setExternallyCompleted() {
1279 assert(getASTContext().getExternalSource() &&
1280 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001281 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001282 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001283 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001284}
1285
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001286void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001287 // Check for a complete definition and recover if not so.
1288 if (!isThisDeclarationADefinition())
1289 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001290 data().HasDesignatedInitializers = true;
1291}
1292
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001293bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001294 // Check for a complete definition and recover if not so.
1295 if (!isThisDeclarationADefinition())
1296 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001297 if (data().ExternallyCompleted)
1298 LoadExternalDefinition();
1299
1300 return data().HasDesignatedInitializers;
1301}
1302
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001303StringRef
1304ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001305 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1306 return ObjCRTName->getMetadataName();
1307
1308 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001309}
1310
1311StringRef
1312ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001313 if (ObjCInterfaceDecl *ID =
1314 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1315 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001316
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001317 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001318}
1319
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001320ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001321 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1322 if (data().ExternallyCompleted)
1323 LoadExternalDefinition();
1324
1325 return getASTContext().getObjCImplementation(
1326 const_cast<ObjCInterfaceDecl*>(Def));
1327 }
1328
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001329 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001330 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001331}
1332
1333void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001334 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001335}
1336
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001337namespace {
1338 struct SynthesizeIvarChunk {
1339 uint64_t Size;
1340 ObjCIvarDecl *Ivar;
1341 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1342 : Size(size), Ivar(ivar) {}
1343 };
1344
1345 bool operator<(const SynthesizeIvarChunk & LHS,
1346 const SynthesizeIvarChunk &RHS) {
1347 return LHS.Size < RHS.Size;
1348 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001349}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001350
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001351/// all_declared_ivar_begin - return first ivar declared in this class,
1352/// its extensions and its implementation. Lazily build the list on first
1353/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001354///
1355/// Caveat: The list returned by this method reflects the current
1356/// state of the parser. The cache will be updated for every ivar
1357/// added by an extension or the implementation when they are
1358/// encountered.
1359/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001360ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001361 // FIXME: Should make sure no callers ever do this.
1362 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001363 return nullptr;
1364
1365 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001366 if (!data().IvarList) {
1367 if (!ivar_empty()) {
1368 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1369 data().IvarList = *I; ++I;
1370 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001371 curIvar->setNextIvar(*I);
1372 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001373
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001374 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001375 if (!Ext->ivar_empty()) {
1376 ObjCCategoryDecl::ivar_iterator
1377 I = Ext->ivar_begin(),
1378 E = Ext->ivar_end();
1379 if (!data().IvarList) {
1380 data().IvarList = *I; ++I;
1381 curIvar = data().IvarList;
1382 }
1383 for ( ;I != E; curIvar = *I, ++I)
1384 curIvar->setNextIvar(*I);
1385 }
1386 }
1387 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001388 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001389
1390 // cached and complete!
1391 if (!data().IvarListMissingImplementation)
1392 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001393
1394 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001395 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001396 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001397 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001398 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001399 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1400 layout.push_back(SynthesizeIvarChunk(
1401 IV->getASTContext().getTypeSize(IV->getType()), IV));
1402 continue;
1403 }
1404 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001405 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001406 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001407 curIvar->setNextIvar(IV);
1408 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001409 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001410
1411 if (!layout.empty()) {
1412 // Order synthesized ivars by their size.
1413 std::stable_sort(layout.begin(), layout.end());
1414 unsigned Ix = 0, EIx = layout.size();
1415 if (!data().IvarList) {
1416 data().IvarList = layout[0].Ivar; Ix++;
1417 curIvar = data().IvarList;
1418 }
1419 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1420 curIvar->setNextIvar(layout[Ix].Ivar);
1421 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001422 }
1423 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001424 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001425}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001426
1427/// FindCategoryDeclaration - Finds category declaration in the list of
1428/// categories for this class and returns it. Name of the category is passed
1429/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001430///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001431ObjCCategoryDecl *
1432ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001433 // FIXME: Should make sure no callers ever do this.
1434 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001435 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001436
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001437 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001438 LoadExternalDefinition();
1439
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001440 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001441 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001442 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001443
1444 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001445}
1446
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001447ObjCMethodDecl *
1448ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001449 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001450 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001451 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1452 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001453 }
1454
Craig Topper36250ad2014-05-12 05:36:57 +00001455 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001456}
1457
1458ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001459 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001460 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001461 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1462 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001463 }
Craig Topper36250ad2014-05-12 05:36:57 +00001464
1465 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001466}
1467
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001468/// ClassImplementsProtocol - Checks that 'lProto' protocol
1469/// has been implemented in IDecl class, its super class or categories (if
1470/// lookupCategory is true).
1471bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1472 bool lookupCategory,
1473 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001474 if (!hasDefinition())
1475 return false;
1476
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001477 ObjCInterfaceDecl *IDecl = this;
1478 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001479 for (auto *PI : IDecl->protocols()){
1480 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001481 return true;
1482 // This is dubious and is added to be compatible with gcc. In gcc, it is
1483 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1484 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1485 // object. This IMO, should be a bug.
1486 // FIXME: Treat this as an extension, and flag this as an error when GCC
1487 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001488 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001489 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001490 return true;
1491 }
Mike Stump11289f42009-09-09 15:08:12 +00001492
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001493 // 2nd, look up the category.
1494 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001495 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001496 for (auto *PI : Cat->protocols())
1497 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001498 return true;
1499 }
Mike Stump11289f42009-09-09 15:08:12 +00001500
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001501 // 3rd, look up the super class(s)
1502 if (IDecl->getSuperClass())
1503 return
1504 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1505 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001506
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001507 return false;
1508}
1509
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001510//===----------------------------------------------------------------------===//
1511// ObjCIvarDecl
1512//===----------------------------------------------------------------------===//
1513
David Blaikie68e081d2011-12-20 02:48:34 +00001514void ObjCIvarDecl::anchor() { }
1515
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001516ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001517 SourceLocation StartLoc,
1518 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001519 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001520 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001521 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001522 if (DC) {
1523 // Ivar's can only appear in interfaces, implementations (via synthesized
1524 // properties), and class extensions (via direct declaration, or synthesized
1525 // properties).
1526 //
1527 // FIXME: This should really be asserting this:
1528 // (isa<ObjCCategoryDecl>(DC) &&
1529 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1530 // but unfortunately we sometimes place ivars into non-class extension
1531 // categories on error. This breaks an AST invariant, and should not be
1532 // fixed.
1533 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1534 isa<ObjCCategoryDecl>(DC)) &&
1535 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001536 // Once a new ivar is created in any of class/class-extension/implementation
1537 // decl contexts, the previously built IvarList must be rebuilt.
1538 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1539 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001540 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001541 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001542 else
1543 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001544 }
Craig Topper36250ad2014-05-12 05:36:57 +00001545 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001546 }
1547
Richard Smithf7981722013-11-22 09:01:48 +00001548 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001549 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001550}
1551
Douglas Gregor72172e92012-01-05 21:55:30 +00001552ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001553 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1554 nullptr, QualType(), nullptr,
1555 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001556}
1557
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001558const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1559 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001560
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001561 switch (DC->getKind()) {
1562 default:
1563 case ObjCCategoryImpl:
1564 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001565 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001566
1567 // Ivars can only appear in class extension categories.
1568 case ObjCCategory: {
1569 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1570 assert(CD->IsClassExtension() && "invalid container for ivar!");
1571 return CD->getClassInterface();
1572 }
1573
1574 case ObjCImplementation:
1575 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1576
1577 case ObjCInterface:
1578 return cast<ObjCInterfaceDecl>(DC);
1579 }
1580}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001581
1582//===----------------------------------------------------------------------===//
1583// ObjCAtDefsFieldDecl
1584//===----------------------------------------------------------------------===//
1585
David Blaikie68e081d2011-12-20 02:48:34 +00001586void ObjCAtDefsFieldDecl::anchor() { }
1587
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001588ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001589*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1590 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001591 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001592 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001593}
1594
Richard Smithf7981722013-11-22 09:01:48 +00001595ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001596 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001597 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1598 SourceLocation(), nullptr, QualType(),
1599 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001600}
1601
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001602//===----------------------------------------------------------------------===//
1603// ObjCProtocolDecl
1604//===----------------------------------------------------------------------===//
1605
David Blaikie68e081d2011-12-20 02:48:34 +00001606void ObjCProtocolDecl::anchor() { }
1607
Richard Smith053f6c62014-05-16 23:01:30 +00001608ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1609 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001610 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001611 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001612 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1613 redeclarable_base(C), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001614 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001615 if (PrevDecl)
1616 Data = PrevDecl->Data;
1617}
1618
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001619ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001620 IdentifierInfo *Id,
1621 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001622 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001623 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001624 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001625 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001626 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001627 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001628}
1629
Richard Smithf7981722013-11-22 09:01:48 +00001630ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001631 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001632 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001633 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001634 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001635 Result->Data.setInt(!C.getLangOpts().Modules);
1636 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001637}
1638
Steve Naroff114aecb2009-03-01 16:12:44 +00001639ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1640 ObjCProtocolDecl *PDecl = this;
1641
1642 if (Name == getIdentifier())
1643 return PDecl;
1644
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001645 for (auto *I : protocols())
1646 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001647 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001648
Craig Topper36250ad2014-05-12 05:36:57 +00001649 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001650}
1651
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001652// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001653// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001654ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1655 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001656 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001657
Douglas Gregoreed49792013-01-17 00:38:46 +00001658 // If there is no definition or the definition is hidden, we don't find
1659 // anything.
1660 const ObjCProtocolDecl *Def = getDefinition();
1661 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001662 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001663
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001664 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001665 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001666
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001667 for (const auto *I : protocols())
1668 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001669 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001670 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001671}
1672
Douglas Gregore6e48b12012-01-01 19:29:29 +00001673void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001674 assert(!Data.getPointer() && "Protocol already has a definition!");
1675 Data.setPointer(new (getASTContext()) DefinitionData);
1676 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001677}
1678
1679void ObjCProtocolDecl::startDefinition() {
1680 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001681
1682 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001683 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001684 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001685}
1686
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001687void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1688 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001689
1690 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001691 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001692 // Insert into PM if not there already.
1693 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001694 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001695 }
1696 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001697 for (const auto *PI : PDecl->protocols())
1698 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001699 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001700}
1701
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001702
1703void ObjCProtocolDecl::collectInheritedProtocolProperties(
1704 const ObjCPropertyDecl *Property,
1705 ProtocolPropertyMap &PM) const {
1706 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1707 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001708 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001709 if (Prop == Property)
1710 continue;
1711 if (Prop->getIdentifier() == Property->getIdentifier()) {
1712 PM[PDecl] = Prop;
1713 MatchFound = true;
1714 break;
1715 }
1716 }
1717 // Scan through protocol's protocols which did not have a matching property.
1718 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001719 for (const auto *PI : PDecl->protocols())
1720 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001721 }
1722}
Anna Zaks673d76b2012-10-18 19:17:53 +00001723
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001724StringRef
1725ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001726 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1727 return ObjCRTName->getMetadataName();
1728
1729 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001730}
1731
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001732//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001733// ObjCCategoryDecl
1734//===----------------------------------------------------------------------===//
1735
David Blaikie68e081d2011-12-20 02:48:34 +00001736void ObjCCategoryDecl::anchor() { }
1737
Douglas Gregor85f3f952015-07-07 03:57:15 +00001738ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1739 SourceLocation ClassNameLoc,
1740 SourceLocation CategoryNameLoc,
1741 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1742 ObjCTypeParamList *typeParamList,
1743 SourceLocation IvarLBraceLoc,
1744 SourceLocation IvarRBraceLoc)
1745 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1746 ClassInterface(IDecl), TypeParamList(typeParamList),
1747 NextClassCategory(nullptr), CategoryNameLoc(CategoryNameLoc),
1748 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc)
1749{
1750 // Set the declaration context of each of the type parameters.
1751 if (typeParamList) {
1752 for (auto typeParam : *typeParamList) {
1753 typeParam->setDeclContext(this);
1754 }
1755 }
1756}
1757
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001758ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001759 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001760 SourceLocation ClassNameLoc,
1761 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001762 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001763 ObjCInterfaceDecl *IDecl,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001764 ObjCTypeParamList *typeParamList,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001765 SourceLocation IvarLBraceLoc,
1766 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001767 ObjCCategoryDecl *CatDecl =
1768 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001769 IDecl, typeParamList, IvarLBraceLoc,
1770 IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001771 if (IDecl) {
1772 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001773 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001774 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001775 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001776 if (ASTMutationListener *L = C.getASTMutationListener())
1777 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1778 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001779 }
1780
1781 return CatDecl;
1782}
1783
Richard Smithf7981722013-11-22 09:01:48 +00001784ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001785 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001786 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1787 SourceLocation(), SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001788 nullptr, nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001789}
1790
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001791ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1792 return getASTContext().getObjCImplementation(
1793 const_cast<ObjCCategoryDecl*>(this));
1794}
1795
1796void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1797 getASTContext().setObjCImplementation(this, ImplD);
1798}
1799
1800
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001801//===----------------------------------------------------------------------===//
1802// ObjCCategoryImplDecl
1803//===----------------------------------------------------------------------===//
1804
David Blaikie68e081d2011-12-20 02:48:34 +00001805void ObjCCategoryImplDecl::anchor() { }
1806
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001807ObjCCategoryImplDecl *
1808ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001809 IdentifierInfo *Id,
1810 ObjCInterfaceDecl *ClassInterface,
1811 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001812 SourceLocation atStartLoc,
1813 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001814 if (ClassInterface && ClassInterface->hasDefinition())
1815 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001816 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1817 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001818}
1819
Douglas Gregor72172e92012-01-05 21:55:30 +00001820ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1821 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001822 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1823 SourceLocation(), SourceLocation(),
1824 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001825}
1826
Steve Narofff406f4d2009-10-29 21:11:04 +00001827ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001828 // The class interface might be NULL if we are working with invalid code.
1829 if (const ObjCInterfaceDecl *ID = getClassInterface())
1830 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00001831 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001832}
1833
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001834
David Blaikie68e081d2011-12-20 02:48:34 +00001835void ObjCImplDecl::anchor() { }
1836
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001837void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001838 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001839 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001840 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001841}
1842
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001843void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1844 ASTContext &Ctx = getASTContext();
1845
1846 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001847 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001848 if (IFace)
1849 Ctx.setObjCImplementation(IFace, ImplD);
1850
Duncan Sands49c29ee2009-07-21 07:56:29 +00001851 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001852 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1853 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1854 Ctx.setObjCImplementation(CD, ImplD);
1855 }
1856
1857 ClassInterface = IFace;
1858}
1859
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001860/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001861/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001862/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001863///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001864ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001865FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001866 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001867 if (PID->getPropertyIvarDecl() &&
1868 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1869 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001870 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001871}
1872
1873/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001874/// added to the list of those properties \@synthesized/\@dynamic in this
1875/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001876///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001877ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001878FindPropertyImplDecl(IdentifierInfo *Id) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001879 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001880 if (PID->getPropertyDecl()->getIdentifier() == Id)
1881 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001882 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001883}
1884
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001885raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001886 const ObjCCategoryImplDecl &CID) {
1887 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001888 return OS;
1889}
1890
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001891//===----------------------------------------------------------------------===//
1892// ObjCImplementationDecl
1893//===----------------------------------------------------------------------===//
1894
David Blaikie68e081d2011-12-20 02:48:34 +00001895void ObjCImplementationDecl::anchor() { }
1896
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001897ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001898ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001899 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001900 ObjCInterfaceDecl *SuperDecl,
1901 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001902 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001903 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001904 SourceLocation IvarLBraceLoc,
1905 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001906 if (ClassInterface && ClassInterface->hasDefinition())
1907 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001908 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1909 nameLoc, atStartLoc, superLoc,
1910 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001911}
1912
Douglas Gregor72172e92012-01-05 21:55:30 +00001913ObjCImplementationDecl *
1914ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001915 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
1916 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001917}
1918
John McCall0410e572011-07-22 04:15:06 +00001919void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1920 CXXCtorInitializer ** initializers,
1921 unsigned numInitializers) {
1922 if (numInitializers > 0) {
1923 NumIvarInitializers = numInitializers;
1924 CXXCtorInitializer **ivarInitializers =
1925 new (C) CXXCtorInitializer*[NumIvarInitializers];
1926 memcpy(ivarInitializers, initializers,
1927 numInitializers * sizeof(CXXCtorInitializer*));
1928 IvarInitializers = ivarInitializers;
1929 }
1930}
1931
Richard Smithc2bb8182015-03-24 06:36:48 +00001932ObjCImplementationDecl::init_const_iterator
1933ObjCImplementationDecl::init_begin() const {
1934 return IvarInitializers.get(getASTContext().getExternalSource());
1935}
1936
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001937raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001938 const ObjCImplementationDecl &ID) {
1939 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001940 return OS;
1941}
1942
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001943//===----------------------------------------------------------------------===//
1944// ObjCCompatibleAliasDecl
1945//===----------------------------------------------------------------------===//
1946
David Blaikie68e081d2011-12-20 02:48:34 +00001947void ObjCCompatibleAliasDecl::anchor() { }
1948
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001949ObjCCompatibleAliasDecl *
1950ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1951 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001952 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001953 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001954 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001955}
1956
Douglas Gregor72172e92012-01-05 21:55:30 +00001957ObjCCompatibleAliasDecl *
1958ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001959 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
1960 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001961}
1962
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001963//===----------------------------------------------------------------------===//
1964// ObjCPropertyDecl
1965//===----------------------------------------------------------------------===//
1966
David Blaikie68e081d2011-12-20 02:48:34 +00001967void ObjCPropertyDecl::anchor() { }
1968
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001969ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1970 SourceLocation L,
1971 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001972 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001973 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00001974 QualType T,
1975 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001976 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001977 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
1978 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001979}
1980
Richard Smithf7981722013-11-22 09:01:48 +00001981ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001982 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001983 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
1984 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00001985 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00001986}
1987
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001988//===----------------------------------------------------------------------===//
1989// ObjCPropertyImplDecl
1990//===----------------------------------------------------------------------===//
1991
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001992ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001993 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001994 SourceLocation atLoc,
1995 SourceLocation L,
1996 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001997 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001998 ObjCIvarDecl *ivar,
1999 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002000 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2001 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002002}
Chris Lattnered0e1642008-03-17 01:19:02 +00002003
Richard Smithf7981722013-11-22 09:01:48 +00002004ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002005 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002006 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2007 SourceLocation(), nullptr, Dynamic,
2008 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002009}
2010
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002011SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2012 SourceLocation EndLoc = getLocation();
2013 if (IvarLoc.isValid())
2014 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002015
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002016 return SourceRange(AtLoc, EndLoc);
2017}