blob: 927988f960adfa4fb2c35b953d83e2b1bf387af9 [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.
Douglas Gregorc5e07f52015-07-07 03:58:01 +0000254 for (auto decl = getMostRecentDecl(); decl; decl = decl->getPreviousDecl()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000255 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
256 return written;
257 }
258
259 return nullptr;
260}
261
Douglas Gregorab7f0b32015-07-07 06:20:12 +0000262void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
263 TypeParamList = TPL;
264 if (!TPL)
265 return;
266 // Set the declaration context of each of the type parameters.
267 for (auto typeParam : *TypeParamList)
268 typeParam->setDeclContext(this);
269}
270
Douglas Gregore9d95f12015-07-07 03:57:35 +0000271ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
272 // FIXME: Should make sure no callers ever do this.
273 if (!hasDefinition())
274 return nullptr;
275
276 if (data().ExternallyCompleted)
277 LoadExternalDefinition();
278
279 if (const ObjCObjectType *superType = getSuperClassType()) {
280 if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
281 if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
282 return superDef;
283
284 return superDecl;
285 }
286 }
287
288 return nullptr;
289}
290
291SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
292 if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
293 return superTInfo->getTypeLoc().getLocStart();
294
295 return SourceLocation();
296}
297
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000298/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
299/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000300/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000301///
302ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000303ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000304 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000305 // FIXME: Should make sure no callers ever do this.
306 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000307 return nullptr;
308
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000309 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000310 LoadExternalDefinition();
311
Ted Kremenekd133a862010-03-15 20:30:07 +0000312 if (ObjCPropertyDecl *PD =
313 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
314 return PD;
315
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000316 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000317 for (const auto *I : all_referenced_protocols())
318 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000319 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000320
Craig Topper36250ad2014-05-12 05:36:57 +0000321 return nullptr;
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000322}
323
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000324void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
325 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000326 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000327 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000328 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000329 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000330 for (const auto *PI : all_referenced_protocols())
331 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000332 // Note, the properties declared only in class extensions are still copied
333 // into the main @interface's property list, and therefore we don't
334 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000335}
336
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000337bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
338 const ObjCInterfaceDecl *Class = this;
339 while (Class) {
340 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
341 return true;
342 Class = Class->getSuperClass();
343 }
344 return false;
345}
346
347const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
348 const ObjCInterfaceDecl *Class = this;
349 while (Class) {
350 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
351 return Class;
352 Class = Class->getSuperClass();
353 }
Craig Topper36250ad2014-05-12 05:36:57 +0000354 return nullptr;
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000355}
356
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000357void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
358 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
359 ASTContext &C)
360{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000361 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000362 LoadExternalDefinition();
363
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000364 if (data().AllReferencedProtocols.empty() &&
365 data().ReferencedProtocols.empty()) {
366 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000367 return;
368 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000369
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000370 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000371 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000372 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000373 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000374 for (unsigned i = 0; i < ExtNum; i++) {
375 bool protocolExists = false;
376 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000377 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000378 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
379 protocolExists = true;
380 break;
381 }
382 }
383 // Do we want to warn on a protocol in extension class which
384 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000385 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000386 ProtocolRefs.push_back(ProtoInExtension);
387 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000388
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000389 if (ProtocolRefs.empty())
390 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000391
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000392 // Merge ProtocolRefs into class's protocol list;
Benjamin Kramerf9890422015-02-17 16:48:30 +0000393 ProtocolRefs.append(all_referenced_protocol_begin(),
394 all_referenced_protocol_end());
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000395
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000396 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000397}
398
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000399const ObjCInterfaceDecl *
400ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
401 const ObjCInterfaceDecl *IFace = this;
402 while (IFace) {
403 if (IFace->hasDesignatedInitializers())
404 return IFace;
405 if (!IFace->inheritsDesignatedInitializers())
406 break;
407 IFace = IFace->getSuperClass();
408 }
Craig Topper36250ad2014-05-12 05:36:57 +0000409 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000410}
411
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000412static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
413 for (const auto *MD : D->instance_methods()) {
414 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
415 return true;
416 }
417 for (const auto *Ext : D->visible_extensions()) {
418 for (const auto *MD : Ext->instance_methods()) {
419 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
420 return true;
421 }
422 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000423 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000424 for (const auto *MD : ImplD->instance_methods()) {
425 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
426 return true;
427 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000428 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000429 return false;
430}
431
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000432bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
433 switch (data().InheritedDesignatedInitializers) {
434 case DefinitionData::IDI_Inherited:
435 return true;
436 case DefinitionData::IDI_NotInherited:
437 return false;
438 case DefinitionData::IDI_Unknown: {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000439 // If the class introduced initializers we conservatively assume that we
440 // don't know if any of them is a designated initializer to avoid possible
441 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000442 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000443 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000444 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000445 if (auto SuperD = getSuperClass()) {
446 data().InheritedDesignatedInitializers =
447 SuperD->declaresOrInheritsDesignatedInitializers() ?
448 DefinitionData::IDI_Inherited :
449 DefinitionData::IDI_NotInherited;
450 } else {
451 data().InheritedDesignatedInitializers =
452 DefinitionData::IDI_NotInherited;
453 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000454 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000455 assert(data().InheritedDesignatedInitializers
456 != DefinitionData::IDI_Unknown);
457 return data().InheritedDesignatedInitializers ==
458 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000459 }
460 }
461
462 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
463}
464
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000465void ObjCInterfaceDecl::getDesignatedInitializers(
466 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000467 // Check for a complete definition and recover if not so.
468 if (!isThisDeclarationADefinition())
469 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000470 if (data().ExternallyCompleted)
471 LoadExternalDefinition();
472
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000473 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000474 if (!IFace)
475 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000476
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000477 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000478 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000479 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000480 for (const auto *Ext : IFace->visible_extensions()) {
481 for (const auto *MD : Ext->instance_methods())
482 if (MD->isThisDeclarationADesignatedInitializer())
483 Methods.push_back(MD);
484 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000485}
486
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000487bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
488 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000489 // Check for a complete definition and recover if not so.
490 if (!isThisDeclarationADefinition())
491 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000492 if (data().ExternallyCompleted)
493 LoadExternalDefinition();
494
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000495 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000496 if (!IFace)
497 return false;
498
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000499 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000500 if (MD->isThisDeclarationADesignatedInitializer()) {
501 if (InitMethod)
502 *InitMethod = MD;
503 return true;
504 }
505 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000506 for (const auto *Ext : IFace->visible_extensions()) {
507 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
508 if (MD->isThisDeclarationADesignatedInitializer()) {
509 if (InitMethod)
510 *InitMethod = MD;
511 return true;
512 }
513 }
514 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000515 return false;
516}
517
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000518void ObjCInterfaceDecl::allocateDefinitionData() {
519 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000520 Data.setPointer(new (getASTContext()) DefinitionData());
521 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000522
523 // Make the type point at the definition, now that we have one.
524 if (TypeForDecl)
525 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000526}
527
528void ObjCInterfaceDecl::startDefinition() {
529 allocateDefinitionData();
530
Douglas Gregor66b310c2011-12-15 18:03:09 +0000531 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000532 for (auto RD : redecls()) {
533 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000534 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000535 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000536}
537
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000538ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
539 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000540 // FIXME: Should make sure no callers ever do this.
541 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000542 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000543
544 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000545 LoadExternalDefinition();
546
Chris Lattner89375192008-03-16 00:19:01 +0000547 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000548 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000549 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000550 clsDeclared = ClassDecl;
551 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000552 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000553
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000554 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000555 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000556 clsDeclared = ClassDecl;
557 return I;
558 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000559 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000560
Chris Lattner89375192008-03-16 00:19:01 +0000561 ClassDecl = ClassDecl->getSuperClass();
562 }
Craig Topper36250ad2014-05-12 05:36:57 +0000563 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000564}
565
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000566/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
567/// class whose name is passed as argument. If it is not one of the super classes
568/// the it returns NULL.
569ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
570 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000571 // FIXME: Should make sure no callers ever do this.
572 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000573 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000574
575 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000576 LoadExternalDefinition();
577
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000578 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000579 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000580 if (ClassDecl->getIdentifier() == ICName)
581 return ClassDecl;
582 ClassDecl = ClassDecl->getSuperClass();
583 }
Craig Topper36250ad2014-05-12 05:36:57 +0000584 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000585}
586
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000587ObjCProtocolDecl *
588ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000589 for (auto *P : all_referenced_protocols())
590 if (P->lookupProtocolNamed(Name))
591 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000592 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000593 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000594}
595
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000596/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000597/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000598/// When argument category "C" is specified, any implicit method found
599/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000600ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000601 bool isInstance,
602 bool shallowCategoryLookup,
603 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000604 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000605{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000606 // FIXME: Should make sure no callers ever do this.
607 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000608 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000609
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000610 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000611 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000612
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000613 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000614 LoadExternalDefinition();
615
Ted Kremenek00781502013-11-23 01:01:29 +0000616 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000617 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000618 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000619 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000620
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000621 // 2. Didn't find one yet - now look through categories.
622 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000623 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000624 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000625 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000626
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000627 // 3. Didn't find one yet - look through primary class's protocols.
628 for (const auto *I : ClassDecl->protocols())
629 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
630 return MethodDecl;
631
632 // 4. Didn't find one yet - now look through categories' protocols
633 if (!shallowCategoryLookup)
634 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000635 // Didn't find one yet - look through protocols.
636 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000637 Cat->getReferencedProtocols();
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000638 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
639 E = Protocols.end(); I != E; ++I)
640 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000641 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000642 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000643 }
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000644
645
Ted Kremenek00781502013-11-23 01:01:29 +0000646 if (!followSuper)
Craig Topper36250ad2014-05-12 05:36:57 +0000647 return nullptr;
Ted Kremenek00781502013-11-23 01:01:29 +0000648
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000649 // 5. Get to the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000650 ClassDecl = ClassDecl->getSuperClass();
651 }
Craig Topper36250ad2014-05-12 05:36:57 +0000652 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000653}
654
Anna Zaksc77a3b12012-07-27 19:07:44 +0000655// Will search "local" class/category implementations for a method decl.
656// If failed, then we search in class's root for an instance method.
657// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000658ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
659 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000660 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000661 // FIXME: Should make sure no callers ever do this.
662 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000663 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000664
665 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000666 LoadExternalDefinition();
667
Craig Topper36250ad2014-05-12 05:36:57 +0000668 ObjCMethodDecl *Method = nullptr;
Steve Naroffbb69c942009-10-01 23:46:04 +0000669 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000670 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
671 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000672
673 // Look through local category implementations associated with the class.
674 if (!Method)
Nico Weberf6098392015-03-02 01:12:28 +0000675 Method = getCategoryMethod(Sel, Instance);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000676
677 // Before we give up, check if the selector is an instance method.
678 // But only in the root. This matches gcc's behavior and what the
679 // runtime expects.
680 if (!Instance && !Method && !getSuperClass()) {
681 Method = lookupInstanceMethod(Sel);
682 // Look through local category implementations associated
683 // with the root class.
684 if (!Method)
685 Method = lookupPrivateMethod(Sel, true);
686 }
687
Steve Naroffbb69c942009-10-01 23:46:04 +0000688 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000689 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000690 return Method;
691}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000692
693//===----------------------------------------------------------------------===//
694// ObjCMethodDecl
695//===----------------------------------------------------------------------===//
696
Alp Toker314cc812014-01-25 16:55:45 +0000697ObjCMethodDecl *ObjCMethodDecl::Create(
698 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
699 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
700 DeclContext *contextDecl, bool isInstance, bool isVariadic,
701 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
702 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000703 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000704 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000705 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
706 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000707}
708
Douglas Gregor72172e92012-01-05 21:55:30 +0000709ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000710 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000711 Selector(), QualType(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000712}
713
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000714bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
715 return getMethodFamily() == OMF_init &&
716 hasAttr<ObjCDesignatedInitializerAttr>();
717}
718
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000719bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
720 const ObjCMethodDecl **InitMethod) const {
721 if (getMethodFamily() != OMF_init)
722 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000723 const DeclContext *DC = getDeclContext();
724 if (isa<ObjCProtocolDecl>(DC))
725 return false;
726 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000727 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000728 return false;
729}
730
Douglas Gregora6017bb2012-10-09 17:21:28 +0000731Stmt *ObjCMethodDecl::getBody() const {
732 return Body.get(getASTContext().getExternalSource());
733}
734
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000735void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
736 assert(PrevMethod);
737 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
738 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000739 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000740}
741
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000742void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
743 ArrayRef<ParmVarDecl*> Params,
744 ArrayRef<SourceLocation> SelLocs) {
Craig Topper36250ad2014-05-12 05:36:57 +0000745 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000746 NumParams = Params.size();
747 if (Params.empty() && SelLocs.empty())
748 return;
749
750 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
751 sizeof(SourceLocation) * SelLocs.size();
752 ParamsAndSelLocs = C.Allocate(Size);
753 std::copy(Params.begin(), Params.end(), getParams());
754 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
755}
756
757void ObjCMethodDecl::getSelectorLocs(
758 SmallVectorImpl<SourceLocation> &SelLocs) const {
759 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
760 SelLocs.push_back(getSelectorLoc(i));
761}
762
763void ObjCMethodDecl::setMethodParams(ASTContext &C,
764 ArrayRef<ParmVarDecl*> Params,
765 ArrayRef<SourceLocation> SelLocs) {
766 assert((!SelLocs.empty() || isImplicit()) &&
767 "No selector locs for non-implicit method");
768 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000769 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000770
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000771 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
772 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000773 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000774 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000775
776 setParamsAndSelLocs(C, Params, SelLocs);
777}
778
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000779/// \brief A definition will return its interface declaration.
780/// An interface declaration will return its definition.
781/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000782ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000783 ASTContext &Ctx = getASTContext();
Craig Topper36250ad2014-05-12 05:36:57 +0000784 ObjCMethodDecl *Redecl = nullptr;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000785 if (HasRedeclaration)
786 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000787 if (Redecl)
788 return Redecl;
789
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000790 Decl *CtxD = cast<Decl>(getDeclContext());
791
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000792 if (!CtxD->isInvalidDecl()) {
793 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
794 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
795 if (!ImplD->isInvalidDecl())
796 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000797
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000798 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
799 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
800 if (!ImplD->isInvalidDecl())
801 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000802
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000803 } else if (ObjCImplementationDecl *ImplD =
804 dyn_cast<ObjCImplementationDecl>(CtxD)) {
805 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
806 if (!IFD->isInvalidDecl())
807 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000808
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000809 } else if (ObjCCategoryImplDecl *CImplD =
810 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
811 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
812 if (!CatD->isInvalidDecl())
813 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
814 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000815 }
816
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000817 if (!Redecl && isRedeclaration()) {
818 // This is the last redeclaration, go back to the first method.
819 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
820 isInstanceMethod());
821 }
822
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000823 return Redecl ? Redecl : this;
824}
825
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000826ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
827 Decl *CtxD = cast<Decl>(getDeclContext());
828
829 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
830 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
831 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
832 isInstanceMethod()))
833 return MD;
834
835 } else if (ObjCCategoryImplDecl *CImplD =
836 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000837 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000838 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
839 isInstanceMethod()))
840 return MD;
841 }
842
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000843 if (isRedeclaration())
844 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
845 isInstanceMethod());
846
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000847 return this;
848}
849
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000850SourceLocation ObjCMethodDecl::getLocEnd() const {
851 if (Stmt *Body = getBody())
852 return Body->getLocEnd();
853 return DeclEndLoc;
854}
855
John McCallb4526252011-03-02 01:50:55 +0000856ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
857 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000858 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000859 return family;
860
John McCall86bc21f2011-03-02 11:33:24 +0000861 // Check for an explicit attribute.
862 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
863 // The unfortunate necessity of mapping between enums here is due
864 // to the attributes framework.
865 switch (attr->getFamily()) {
866 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
867 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
868 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
869 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
870 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
871 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
872 }
873 Family = static_cast<unsigned>(family);
874 return family;
875 }
876
John McCallb4526252011-03-02 01:50:55 +0000877 family = getSelector().getMethodFamily();
878 switch (family) {
879 case OMF_None: break;
880
881 // init only has a conventional meaning for an instance method, and
882 // it has to return an object.
883 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000884 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000885 family = OMF_None;
886 break;
887
888 // alloc/copy/new have a conventional meaning for both class and
889 // instance methods, but they require an object return.
890 case OMF_alloc:
891 case OMF_copy:
892 case OMF_mutableCopy:
893 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000894 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000895 family = OMF_None;
896 break;
897
898 // These selectors have a conventional meaning only for instance methods.
899 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000900 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000901 case OMF_retain:
902 case OMF_release:
903 case OMF_autorelease:
904 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000905 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000906 if (!isInstanceMethod())
907 family = OMF_None;
908 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000909
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000910 case OMF_initialize:
911 if (isInstanceMethod() || !getReturnType()->isVoidType())
912 family = OMF_None;
913 break;
914
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000915 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000916 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000917 family = OMF_None;
918 else {
919 unsigned noParams = param_size();
920 if (noParams < 1 || noParams > 3)
921 family = OMF_None;
922 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000923 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000924 QualType ArgT = (*it);
925 if (!ArgT->isObjCSelType()) {
926 family = OMF_None;
927 break;
928 }
929 while (--noParams) {
930 it++;
931 ArgT = (*it);
932 if (!ArgT->isObjCIdType()) {
933 family = OMF_None;
934 break;
935 }
936 }
937 }
938 }
939 break;
940
John McCallb4526252011-03-02 01:50:55 +0000941 }
942
943 // Cache the result.
944 Family = static_cast<unsigned>(family);
945 return family;
946}
947
Mike Stump11289f42009-09-09 15:08:12 +0000948void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000949 const ObjCInterfaceDecl *OID) {
950 QualType selfTy;
951 if (isInstanceMethod()) {
952 // There may be no interface context due to error in declaration
953 // of the interface (which has been reported). Recover gracefully.
954 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000955 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000956 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000957 } else {
958 selfTy = Context.getObjCIdType();
959 }
960 } else // we have a factory method.
961 selfTy = Context.getObjCClassType();
962
John McCalld4631322011-06-17 06:42:21 +0000963 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000964 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000965
David Blaikiebbafb8a2012-03-11 07:00:24 +0000966 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000967 if (isInstanceMethod()) {
968 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000969
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000970 // 'self' is always __strong. It's actually pseudo-strong except
971 // in init methods (or methods labeled ns_consumes_self), though.
972 Qualifiers qs;
973 qs.setObjCLifetime(Qualifiers::OCL_Strong);
974 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000975
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000976 // In addition, 'self' is const unless this is an init method.
977 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
978 selfTy = selfTy.withConst();
979 selfIsPseudoStrong = true;
980 }
981 }
982 else {
983 assert(isClassMethod());
984 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000985 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000986 selfIsPseudoStrong = true;
987 }
John McCall31168b02011-06-15 23:02:42 +0000988 }
989
990 ImplicitParamDecl *self
991 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
992 &Context.Idents.get("self"), selfTy);
993 setSelfDecl(self);
994
995 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000996 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000997
John McCalld4631322011-06-17 06:42:21 +0000998 if (selfIsPseudoStrong)
999 self->setARCPseudoStrong(true);
1000
Mike Stump11289f42009-09-09 15:08:12 +00001001 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
1002 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +00001003 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001004}
1005
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001006ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
1007 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
1008 return ID;
1009 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1010 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +00001011 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001012 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +00001013 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +00001014 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +00001015 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001016}
1017
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001018SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1019 const auto *TSI = getReturnTypeSourceInfo();
1020 if (TSI)
1021 return TSI->getTypeLoc().getSourceRange();
1022 return SourceRange();
1023}
1024
Douglas Gregor9b7b3e92015-07-07 06:20:27 +00001025QualType ObjCMethodDecl::getSendResultType() const {
1026 ASTContext &Ctx = getASTContext();
1027 return getReturnType().getNonLValueExprType(Ctx)
1028 .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1029}
1030
Douglas Gregore83b9562015-07-07 03:57:53 +00001031QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1032 // FIXME: Handle related result types here.
1033
1034 return getReturnType().getNonLValueExprType(getASTContext())
1035 .substObjCMemberType(receiverType, getDeclContext(),
1036 ObjCSubstitutionContext::Result);
1037}
1038
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001039static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1040 const ObjCMethodDecl *Method,
1041 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1042 bool MovedToSuper) {
1043 if (!Container)
1044 return;
1045
1046 // In categories look for overriden methods from protocols. A method from
1047 // category is not "overriden" since it is considered as the "same" method
1048 // (same USR) as the one from the interface.
1049 if (const ObjCCategoryDecl *
1050 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1051 // Check whether we have a matching method at this category but only if we
1052 // are at the super class level.
1053 if (MovedToSuper)
1054 if (ObjCMethodDecl *
1055 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001056 Method->isInstanceMethod(),
1057 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001058 if (Method != Overridden) {
1059 // We found an override at this category; there is no need to look
1060 // into its protocols.
1061 Methods.push_back(Overridden);
1062 return;
1063 }
1064
Aaron Ballman19a41762014-03-14 12:55:57 +00001065 for (const auto *P : Category->protocols())
1066 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001067 return;
1068 }
1069
1070 // Check whether we have a matching method at this level.
1071 if (const ObjCMethodDecl *
1072 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001073 Method->isInstanceMethod(),
1074 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001075 if (Method != Overridden) {
1076 // We found an override at this level; there is no need to look
1077 // into other protocols or categories.
1078 Methods.push_back(Overridden);
1079 return;
1080 }
1081
1082 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001083 for (const auto *P : Protocol->protocols())
1084 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001085 }
1086
1087 if (const ObjCInterfaceDecl *
1088 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001089 for (const auto *P : Interface->protocols())
1090 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001091
Aaron Ballman15063e12014-03-13 21:35:02 +00001092 for (const auto *Cat : Interface->known_categories())
1093 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001094
1095 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1096 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1097 /*MovedToSuper=*/true);
1098 }
1099}
1100
1101static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1102 const ObjCMethodDecl *Method,
1103 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1104 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1105 /*MovedToSuper=*/false);
1106}
1107
1108static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1109 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1110 assert(Method->isOverriding());
1111
1112 if (const ObjCProtocolDecl *
1113 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1114 CollectOverriddenMethods(ProtD, Method, overridden);
1115
1116 } else if (const ObjCImplDecl *
1117 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1118 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1119 if (!ID)
1120 return;
1121 // Start searching for overridden methods using the method from the
1122 // interface as starting point.
1123 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001124 Method->isInstanceMethod(),
1125 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001126 Method = IFaceMeth;
1127 CollectOverriddenMethods(ID, Method, overridden);
1128
1129 } else if (const ObjCCategoryDecl *
1130 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1131 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1132 if (!ID)
1133 return;
1134 // Start searching for overridden methods using the method from the
1135 // interface as starting point.
1136 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001137 Method->isInstanceMethod(),
1138 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001139 Method = IFaceMeth;
1140 CollectOverriddenMethods(ID, Method, overridden);
1141
1142 } else {
1143 CollectOverriddenMethods(
1144 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1145 Method, overridden);
1146 }
1147}
1148
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001149void ObjCMethodDecl::getOverriddenMethods(
1150 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1151 const ObjCMethodDecl *Method = this;
1152
1153 if (Method->isRedeclaration()) {
1154 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1155 getMethod(Method->getSelector(), Method->isInstanceMethod());
1156 }
1157
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001158 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001159 collectOverriddenMethodsSlow(Method, Overridden);
1160 assert(!Overridden.empty() &&
1161 "ObjCMethodDecl's overriding bit is not as expected");
1162 }
1163}
1164
Jordan Rose2bd991a2012-10-10 16:42:54 +00001165const ObjCPropertyDecl *
1166ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1167 Selector Sel = getSelector();
1168 unsigned NumArgs = Sel.getNumArgs();
1169 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001170 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001171
Jordan Rose16ba5532015-01-16 23:04:26 +00001172 if (!isInstanceMethod())
Craig Topper36250ad2014-05-12 05:36:57 +00001173 return nullptr;
1174
Jordan Rose2bd991a2012-10-10 16:42:54 +00001175 if (isPropertyAccessor()) {
1176 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001177 // If container is class extension, find its primary class.
1178 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1179 if (CatDecl->IsClassExtension())
1180 Container = CatDecl->getClassInterface();
1181
Jordan Rose2bd991a2012-10-10 16:42:54 +00001182 bool IsGetter = (NumArgs == 0);
1183
Aaron Ballmand174edf2014-03-13 19:11:50 +00001184 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001185 Selector NextSel = IsGetter ? I->getGetterName()
1186 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001187 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001188 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001189 }
1190
1191 llvm_unreachable("Marked as a property accessor but no property found!");
1192 }
1193
1194 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001195 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001196
1197 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1198 OverridesTy Overrides;
1199 getOverriddenMethods(Overrides);
1200 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1201 I != E; ++I) {
1202 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1203 return Prop;
1204 }
1205
Craig Topper36250ad2014-05-12 05:36:57 +00001206 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001207}
1208
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001209//===----------------------------------------------------------------------===//
Douglas Gregor85f3f952015-07-07 03:57:15 +00001210// ObjCTypeParamDecl
1211//===----------------------------------------------------------------------===//
1212
1213void ObjCTypeParamDecl::anchor() { }
1214
1215ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001216 ObjCTypeParamVariance variance,
1217 SourceLocation varianceLoc,
Douglas Gregore83b9562015-07-07 03:57:53 +00001218 unsigned index,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001219 SourceLocation nameLoc,
1220 IdentifierInfo *name,
1221 SourceLocation colonLoc,
1222 TypeSourceInfo *boundInfo) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001223 return new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1224 nameLoc, name, colonLoc, boundInfo);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001225}
1226
1227ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1228 unsigned ID) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001229 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1230 ObjCTypeParamVariance::Invariant,
1231 SourceLocation(), 0, SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001232 nullptr, SourceLocation(), nullptr);
1233}
1234
1235SourceRange ObjCTypeParamDecl::getSourceRange() const {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001236 SourceLocation startLoc = VarianceLoc;
1237 if (startLoc.isInvalid())
1238 startLoc = getLocation();
1239
Douglas Gregor85f3f952015-07-07 03:57:15 +00001240 if (hasExplicitBound()) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001241 return SourceRange(startLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001242 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1243 }
1244
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001245 return SourceRange(startLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001246}
1247
1248//===----------------------------------------------------------------------===//
1249// ObjCTypeParamList
1250//===----------------------------------------------------------------------===//
1251ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1252 ArrayRef<ObjCTypeParamDecl *> typeParams,
1253 SourceLocation rAngleLoc)
Douglas Gregor0a8890ff2015-07-07 06:20:46 +00001254 : NumParams(typeParams.size())
Douglas Gregor85f3f952015-07-07 03:57:15 +00001255{
Douglas Gregor0a8890ff2015-07-07 06:20:46 +00001256 Brackets.Begin = lAngleLoc.getRawEncoding();
1257 Brackets.End = rAngleLoc.getRawEncoding();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001258 std::copy(typeParams.begin(), typeParams.end(), begin());
1259}
1260
1261
1262ObjCTypeParamList *ObjCTypeParamList::create(
1263 ASTContext &ctx,
1264 SourceLocation lAngleLoc,
1265 ArrayRef<ObjCTypeParamDecl *> typeParams,
1266 SourceLocation rAngleLoc) {
1267 unsigned size = sizeof(ObjCTypeParamList)
1268 + sizeof(ObjCTypeParamDecl *) * typeParams.size();
Aaron Ballman76413ec2015-07-07 17:12:14 +00001269 static_assert(llvm::AlignOf<ObjCTypeParamList>::Alignment >=
1270 llvm::AlignOf<ObjCTypeParamDecl *>::Alignment,
1271 "type parameter list needs greater alignment");
Douglas Gregor85f3f952015-07-07 03:57:15 +00001272 unsigned align = llvm::alignOf<ObjCTypeParamList>();
1273 void *mem = ctx.Allocate(size, align);
1274 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1275}
1276
Douglas Gregore83b9562015-07-07 03:57:53 +00001277void ObjCTypeParamList::gatherDefaultTypeArgs(
1278 SmallVectorImpl<QualType> &typeArgs) const {
1279 typeArgs.reserve(size());
1280 for (auto typeParam : *this)
1281 typeArgs.push_back(typeParam->getUnderlyingType());
1282}
1283
Douglas Gregor85f3f952015-07-07 03:57:15 +00001284//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001285// ObjCInterfaceDecl
1286//===----------------------------------------------------------------------===//
1287
Douglas Gregord53ae832012-01-17 18:09:05 +00001288ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001289 DeclContext *DC,
1290 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001291 IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001292 ObjCTypeParamList *typeParamList,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001293 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001294 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001295 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001296 ObjCInterfaceDecl *Result = new (C, DC)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001297 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1298 isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001299 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001300 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001301 return Result;
1302}
1303
Richard Smith053f6c62014-05-16 23:01:30 +00001304ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001305 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001306 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001307 SourceLocation(),
1308 nullptr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001309 nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001310 SourceLocation(),
1311 nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001312 Result->Data.setInt(!C.getLangOpts().Modules);
1313 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001314}
1315
Richard Smith053f6c62014-05-16 23:01:30 +00001316ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1317 SourceLocation AtLoc, IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001318 ObjCTypeParamList *typeParamList,
Richard Smith053f6c62014-05-16 23:01:30 +00001319 SourceLocation CLoc,
1320 ObjCInterfaceDecl *PrevDecl,
1321 bool IsInternal)
1322 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001323 redeclarable_base(C), TypeForDecl(nullptr), TypeParamList(nullptr),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001324 Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001325 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001326
1327 // Copy the 'data' pointer over.
1328 if (PrevDecl)
1329 Data = PrevDecl->Data;
1330
Richard Smith053f6c62014-05-16 23:01:30 +00001331 setImplicit(IsInternal);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001332
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001333 setTypeParamList(typeParamList);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001334}
1335
Douglas Gregor73693022010-12-01 23:49:52 +00001336void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001337 assert(data().ExternallyCompleted && "Class is not externally completed");
1338 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001339 getASTContext().getExternalSource()->CompleteType(
1340 const_cast<ObjCInterfaceDecl *>(this));
1341}
1342
1343void ObjCInterfaceDecl::setExternallyCompleted() {
1344 assert(getASTContext().getExternalSource() &&
1345 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001346 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001347 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001348 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001349}
1350
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001351void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001352 // Check for a complete definition and recover if not so.
1353 if (!isThisDeclarationADefinition())
1354 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001355 data().HasDesignatedInitializers = true;
1356}
1357
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001358bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001359 // Check for a complete definition and recover if not so.
1360 if (!isThisDeclarationADefinition())
1361 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001362 if (data().ExternallyCompleted)
1363 LoadExternalDefinition();
1364
1365 return data().HasDesignatedInitializers;
1366}
1367
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001368StringRef
1369ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001370 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1371 return ObjCRTName->getMetadataName();
1372
1373 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001374}
1375
1376StringRef
1377ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001378 if (ObjCInterfaceDecl *ID =
1379 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1380 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001381
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001382 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001383}
1384
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001385ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001386 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1387 if (data().ExternallyCompleted)
1388 LoadExternalDefinition();
1389
1390 return getASTContext().getObjCImplementation(
1391 const_cast<ObjCInterfaceDecl*>(Def));
1392 }
1393
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001394 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001395 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001396}
1397
1398void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001399 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001400}
1401
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001402namespace {
1403 struct SynthesizeIvarChunk {
1404 uint64_t Size;
1405 ObjCIvarDecl *Ivar;
1406 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1407 : Size(size), Ivar(ivar) {}
1408 };
1409
1410 bool operator<(const SynthesizeIvarChunk & LHS,
1411 const SynthesizeIvarChunk &RHS) {
1412 return LHS.Size < RHS.Size;
1413 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001414}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001415
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001416/// all_declared_ivar_begin - return first ivar declared in this class,
1417/// its extensions and its implementation. Lazily build the list on first
1418/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001419///
1420/// Caveat: The list returned by this method reflects the current
1421/// state of the parser. The cache will be updated for every ivar
1422/// added by an extension or the implementation when they are
1423/// encountered.
1424/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001425ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001426 // FIXME: Should make sure no callers ever do this.
1427 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001428 return nullptr;
1429
1430 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001431 if (!data().IvarList) {
1432 if (!ivar_empty()) {
1433 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1434 data().IvarList = *I; ++I;
1435 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001436 curIvar->setNextIvar(*I);
1437 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001438
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001439 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001440 if (!Ext->ivar_empty()) {
1441 ObjCCategoryDecl::ivar_iterator
1442 I = Ext->ivar_begin(),
1443 E = Ext->ivar_end();
1444 if (!data().IvarList) {
1445 data().IvarList = *I; ++I;
1446 curIvar = data().IvarList;
1447 }
1448 for ( ;I != E; curIvar = *I, ++I)
1449 curIvar->setNextIvar(*I);
1450 }
1451 }
1452 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001453 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001454
1455 // cached and complete!
1456 if (!data().IvarListMissingImplementation)
1457 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001458
1459 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001460 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001461 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001462 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001463 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001464 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1465 layout.push_back(SynthesizeIvarChunk(
1466 IV->getASTContext().getTypeSize(IV->getType()), IV));
1467 continue;
1468 }
1469 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001470 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001471 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001472 curIvar->setNextIvar(IV);
1473 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001474 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001475
1476 if (!layout.empty()) {
1477 // Order synthesized ivars by their size.
1478 std::stable_sort(layout.begin(), layout.end());
1479 unsigned Ix = 0, EIx = layout.size();
1480 if (!data().IvarList) {
1481 data().IvarList = layout[0].Ivar; Ix++;
1482 curIvar = data().IvarList;
1483 }
1484 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1485 curIvar->setNextIvar(layout[Ix].Ivar);
1486 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001487 }
1488 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001489 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001490}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001491
1492/// FindCategoryDeclaration - Finds category declaration in the list of
1493/// categories for this class and returns it. Name of the category is passed
1494/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001495///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001496ObjCCategoryDecl *
1497ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001498 // FIXME: Should make sure no callers ever do this.
1499 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001500 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001501
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001502 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001503 LoadExternalDefinition();
1504
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001505 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001506 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001507 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001508
1509 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001510}
1511
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001512ObjCMethodDecl *
1513ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001514 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001515 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001516 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1517 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001518 }
1519
Craig Topper36250ad2014-05-12 05:36:57 +00001520 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001521}
1522
1523ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001524 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001525 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001526 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1527 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001528 }
Craig Topper36250ad2014-05-12 05:36:57 +00001529
1530 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001531}
1532
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001533/// ClassImplementsProtocol - Checks that 'lProto' protocol
1534/// has been implemented in IDecl class, its super class or categories (if
1535/// lookupCategory is true).
1536bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1537 bool lookupCategory,
1538 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001539 if (!hasDefinition())
1540 return false;
1541
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001542 ObjCInterfaceDecl *IDecl = this;
1543 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001544 for (auto *PI : IDecl->protocols()){
1545 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001546 return true;
1547 // This is dubious and is added to be compatible with gcc. In gcc, it is
1548 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1549 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1550 // object. This IMO, should be a bug.
1551 // FIXME: Treat this as an extension, and flag this as an error when GCC
1552 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001553 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001554 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001555 return true;
1556 }
Mike Stump11289f42009-09-09 15:08:12 +00001557
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001558 // 2nd, look up the category.
1559 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001560 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001561 for (auto *PI : Cat->protocols())
1562 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001563 return true;
1564 }
Mike Stump11289f42009-09-09 15:08:12 +00001565
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001566 // 3rd, look up the super class(s)
1567 if (IDecl->getSuperClass())
1568 return
1569 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1570 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001571
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001572 return false;
1573}
1574
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001575//===----------------------------------------------------------------------===//
1576// ObjCIvarDecl
1577//===----------------------------------------------------------------------===//
1578
David Blaikie68e081d2011-12-20 02:48:34 +00001579void ObjCIvarDecl::anchor() { }
1580
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001581ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001582 SourceLocation StartLoc,
1583 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001584 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001585 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001586 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001587 if (DC) {
1588 // Ivar's can only appear in interfaces, implementations (via synthesized
1589 // properties), and class extensions (via direct declaration, or synthesized
1590 // properties).
1591 //
1592 // FIXME: This should really be asserting this:
1593 // (isa<ObjCCategoryDecl>(DC) &&
1594 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1595 // but unfortunately we sometimes place ivars into non-class extension
1596 // categories on error. This breaks an AST invariant, and should not be
1597 // fixed.
1598 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1599 isa<ObjCCategoryDecl>(DC)) &&
1600 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001601 // Once a new ivar is created in any of class/class-extension/implementation
1602 // decl contexts, the previously built IvarList must be rebuilt.
1603 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1604 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001605 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001606 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001607 else
1608 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001609 }
Craig Topper36250ad2014-05-12 05:36:57 +00001610 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001611 }
1612
Richard Smithf7981722013-11-22 09:01:48 +00001613 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001614 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001615}
1616
Douglas Gregor72172e92012-01-05 21:55:30 +00001617ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001618 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1619 nullptr, QualType(), nullptr,
1620 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001621}
1622
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001623const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1624 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001625
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001626 switch (DC->getKind()) {
1627 default:
1628 case ObjCCategoryImpl:
1629 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001630 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001631
1632 // Ivars can only appear in class extension categories.
1633 case ObjCCategory: {
1634 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1635 assert(CD->IsClassExtension() && "invalid container for ivar!");
1636 return CD->getClassInterface();
1637 }
1638
1639 case ObjCImplementation:
1640 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1641
1642 case ObjCInterface:
1643 return cast<ObjCInterfaceDecl>(DC);
1644 }
1645}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001646
Douglas Gregore83b9562015-07-07 03:57:53 +00001647QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1648 return getType().substObjCMemberType(objectType, getDeclContext(),
1649 ObjCSubstitutionContext::Property);
1650}
1651
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001652//===----------------------------------------------------------------------===//
1653// ObjCAtDefsFieldDecl
1654//===----------------------------------------------------------------------===//
1655
David Blaikie68e081d2011-12-20 02:48:34 +00001656void ObjCAtDefsFieldDecl::anchor() { }
1657
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001658ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001659*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1660 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001661 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001662 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001663}
1664
Richard Smithf7981722013-11-22 09:01:48 +00001665ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001666 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001667 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1668 SourceLocation(), nullptr, QualType(),
1669 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001670}
1671
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001672//===----------------------------------------------------------------------===//
1673// ObjCProtocolDecl
1674//===----------------------------------------------------------------------===//
1675
David Blaikie68e081d2011-12-20 02:48:34 +00001676void ObjCProtocolDecl::anchor() { }
1677
Richard Smith053f6c62014-05-16 23:01:30 +00001678ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1679 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001680 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001681 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001682 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1683 redeclarable_base(C), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001684 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001685 if (PrevDecl)
1686 Data = PrevDecl->Data;
1687}
1688
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001689ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001690 IdentifierInfo *Id,
1691 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001692 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001693 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001694 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001695 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001696 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001697 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001698}
1699
Richard Smithf7981722013-11-22 09:01:48 +00001700ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001701 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001702 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001703 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001704 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001705 Result->Data.setInt(!C.getLangOpts().Modules);
1706 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001707}
1708
Steve Naroff114aecb2009-03-01 16:12:44 +00001709ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1710 ObjCProtocolDecl *PDecl = this;
1711
1712 if (Name == getIdentifier())
1713 return PDecl;
1714
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001715 for (auto *I : protocols())
1716 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001717 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001718
Craig Topper36250ad2014-05-12 05:36:57 +00001719 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001720}
1721
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001722// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001723// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001724ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1725 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001726 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001727
Douglas Gregoreed49792013-01-17 00:38:46 +00001728 // If there is no definition or the definition is hidden, we don't find
1729 // anything.
1730 const ObjCProtocolDecl *Def = getDefinition();
1731 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001732 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001733
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001734 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001735 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001736
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001737 for (const auto *I : protocols())
1738 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001739 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001740 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001741}
1742
Douglas Gregore6e48b12012-01-01 19:29:29 +00001743void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001744 assert(!Data.getPointer() && "Protocol already has a definition!");
1745 Data.setPointer(new (getASTContext()) DefinitionData);
1746 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001747}
1748
1749void ObjCProtocolDecl::startDefinition() {
1750 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001751
1752 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001753 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001754 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001755}
1756
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001757void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1758 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001759
1760 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001761 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001762 // Insert into PM if not there already.
1763 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001764 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001765 }
1766 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001767 for (const auto *PI : PDecl->protocols())
1768 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001769 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001770}
1771
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001772
1773void ObjCProtocolDecl::collectInheritedProtocolProperties(
1774 const ObjCPropertyDecl *Property,
1775 ProtocolPropertyMap &PM) const {
1776 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1777 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001778 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001779 if (Prop == Property)
1780 continue;
1781 if (Prop->getIdentifier() == Property->getIdentifier()) {
1782 PM[PDecl] = Prop;
1783 MatchFound = true;
1784 break;
1785 }
1786 }
1787 // Scan through protocol's protocols which did not have a matching property.
1788 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001789 for (const auto *PI : PDecl->protocols())
1790 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001791 }
1792}
Anna Zaks673d76b2012-10-18 19:17:53 +00001793
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001794StringRef
1795ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001796 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1797 return ObjCRTName->getMetadataName();
1798
1799 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001800}
1801
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001802//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001803// ObjCCategoryDecl
1804//===----------------------------------------------------------------------===//
1805
David Blaikie68e081d2011-12-20 02:48:34 +00001806void ObjCCategoryDecl::anchor() { }
1807
Douglas Gregor85f3f952015-07-07 03:57:15 +00001808ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1809 SourceLocation ClassNameLoc,
1810 SourceLocation CategoryNameLoc,
1811 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1812 ObjCTypeParamList *typeParamList,
1813 SourceLocation IvarLBraceLoc,
1814 SourceLocation IvarRBraceLoc)
1815 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001816 ClassInterface(IDecl), TypeParamList(nullptr),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001817 NextClassCategory(nullptr), CategoryNameLoc(CategoryNameLoc),
1818 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc)
1819{
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001820 setTypeParamList(typeParamList);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001821}
1822
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001823ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001824 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001825 SourceLocation ClassNameLoc,
1826 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001827 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001828 ObjCInterfaceDecl *IDecl,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001829 ObjCTypeParamList *typeParamList,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001830 SourceLocation IvarLBraceLoc,
1831 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001832 ObjCCategoryDecl *CatDecl =
1833 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001834 IDecl, typeParamList, IvarLBraceLoc,
1835 IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001836 if (IDecl) {
1837 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001838 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001839 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001840 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001841 if (ASTMutationListener *L = C.getASTMutationListener())
1842 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1843 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001844 }
1845
1846 return CatDecl;
1847}
1848
Richard Smithf7981722013-11-22 09:01:48 +00001849ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001850 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001851 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1852 SourceLocation(), SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001853 nullptr, nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001854}
1855
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001856ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1857 return getASTContext().getObjCImplementation(
1858 const_cast<ObjCCategoryDecl*>(this));
1859}
1860
1861void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1862 getASTContext().setObjCImplementation(this, ImplD);
1863}
1864
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001865void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
1866 TypeParamList = TPL;
1867 if (!TPL)
1868 return;
1869 // Set the declaration context of each of the type parameters.
1870 for (auto typeParam : *TypeParamList)
1871 typeParam->setDeclContext(this);
1872}
1873
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001874
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001875//===----------------------------------------------------------------------===//
1876// ObjCCategoryImplDecl
1877//===----------------------------------------------------------------------===//
1878
David Blaikie68e081d2011-12-20 02:48:34 +00001879void ObjCCategoryImplDecl::anchor() { }
1880
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001881ObjCCategoryImplDecl *
1882ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001883 IdentifierInfo *Id,
1884 ObjCInterfaceDecl *ClassInterface,
1885 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001886 SourceLocation atStartLoc,
1887 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001888 if (ClassInterface && ClassInterface->hasDefinition())
1889 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001890 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1891 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001892}
1893
Douglas Gregor72172e92012-01-05 21:55:30 +00001894ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1895 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001896 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1897 SourceLocation(), SourceLocation(),
1898 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001899}
1900
Steve Narofff406f4d2009-10-29 21:11:04 +00001901ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001902 // The class interface might be NULL if we are working with invalid code.
1903 if (const ObjCInterfaceDecl *ID = getClassInterface())
1904 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00001905 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001906}
1907
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001908
David Blaikie68e081d2011-12-20 02:48:34 +00001909void ObjCImplDecl::anchor() { }
1910
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001911void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001912 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001913 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001914 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001915}
1916
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001917void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1918 ASTContext &Ctx = getASTContext();
1919
1920 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001921 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001922 if (IFace)
1923 Ctx.setObjCImplementation(IFace, ImplD);
1924
Duncan Sands49c29ee2009-07-21 07:56:29 +00001925 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001926 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1927 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1928 Ctx.setObjCImplementation(CD, ImplD);
1929 }
1930
1931 ClassInterface = IFace;
1932}
1933
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001934/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001935/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001936/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001937///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001938ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001939FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001940 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001941 if (PID->getPropertyIvarDecl() &&
1942 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1943 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001944 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001945}
1946
1947/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001948/// added to the list of those properties \@synthesized/\@dynamic in this
1949/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001950///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001951ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001952FindPropertyImplDecl(IdentifierInfo *Id) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001953 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001954 if (PID->getPropertyDecl()->getIdentifier() == Id)
1955 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001956 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001957}
1958
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001959raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001960 const ObjCCategoryImplDecl &CID) {
1961 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001962 return OS;
1963}
1964
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001965//===----------------------------------------------------------------------===//
1966// ObjCImplementationDecl
1967//===----------------------------------------------------------------------===//
1968
David Blaikie68e081d2011-12-20 02:48:34 +00001969void ObjCImplementationDecl::anchor() { }
1970
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001971ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001972ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001973 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001974 ObjCInterfaceDecl *SuperDecl,
1975 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001976 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001977 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001978 SourceLocation IvarLBraceLoc,
1979 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001980 if (ClassInterface && ClassInterface->hasDefinition())
1981 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001982 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1983 nameLoc, atStartLoc, superLoc,
1984 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001985}
1986
Douglas Gregor72172e92012-01-05 21:55:30 +00001987ObjCImplementationDecl *
1988ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001989 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
1990 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001991}
1992
John McCall0410e572011-07-22 04:15:06 +00001993void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1994 CXXCtorInitializer ** initializers,
1995 unsigned numInitializers) {
1996 if (numInitializers > 0) {
1997 NumIvarInitializers = numInitializers;
1998 CXXCtorInitializer **ivarInitializers =
1999 new (C) CXXCtorInitializer*[NumIvarInitializers];
2000 memcpy(ivarInitializers, initializers,
2001 numInitializers * sizeof(CXXCtorInitializer*));
2002 IvarInitializers = ivarInitializers;
2003 }
2004}
2005
Richard Smithc2bb8182015-03-24 06:36:48 +00002006ObjCImplementationDecl::init_const_iterator
2007ObjCImplementationDecl::init_begin() const {
2008 return IvarInitializers.get(getASTContext().getExternalSource());
2009}
2010
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002011raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002012 const ObjCImplementationDecl &ID) {
2013 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002014 return OS;
2015}
2016
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002017//===----------------------------------------------------------------------===//
2018// ObjCCompatibleAliasDecl
2019//===----------------------------------------------------------------------===//
2020
David Blaikie68e081d2011-12-20 02:48:34 +00002021void ObjCCompatibleAliasDecl::anchor() { }
2022
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002023ObjCCompatibleAliasDecl *
2024ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2025 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00002026 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002027 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00002028 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002029}
2030
Douglas Gregor72172e92012-01-05 21:55:30 +00002031ObjCCompatibleAliasDecl *
2032ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002033 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2034 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002035}
2036
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002037//===----------------------------------------------------------------------===//
2038// ObjCPropertyDecl
2039//===----------------------------------------------------------------------===//
2040
David Blaikie68e081d2011-12-20 02:48:34 +00002041void ObjCPropertyDecl::anchor() { }
2042
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002043ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2044 SourceLocation L,
2045 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00002046 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002047 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00002048 QualType T,
2049 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002050 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002051 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2052 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002053}
2054
Richard Smithf7981722013-11-22 09:01:48 +00002055ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002056 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002057 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2058 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00002059 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00002060}
2061
Douglas Gregore83b9562015-07-07 03:57:53 +00002062QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2063 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2064 ObjCSubstitutionContext::Property);
2065}
2066
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002067//===----------------------------------------------------------------------===//
2068// ObjCPropertyImplDecl
2069//===----------------------------------------------------------------------===//
2070
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002071ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002072 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002073 SourceLocation atLoc,
2074 SourceLocation L,
2075 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002076 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002077 ObjCIvarDecl *ivar,
2078 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002079 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2080 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002081}
Chris Lattnered0e1642008-03-17 01:19:02 +00002082
Richard Smithf7981722013-11-22 09:01:48 +00002083ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002084 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002085 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2086 SourceLocation(), nullptr, Dynamic,
2087 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002088}
2089
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002090SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2091 SourceLocation EndLoc = getLocation();
2092 if (IvarLoc.isValid())
2093 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002094
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002095 return SourceRange(AtLoc, EndLoc);
2096}