blob: 280c412ae8ff3431e428ccc8fc9d972bf7d1c084 [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
Adrian Prantl2ecf89e2015-07-08 22:15:59 +0000948QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
949 const ObjCInterfaceDecl *OID,
950 bool &selfIsPseudoStrong,
951 bool &selfIsConsumed) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000952 QualType selfTy;
Adrian Prantl2ecf89e2015-07-08 22:15:59 +0000953 selfIsPseudoStrong = false;
954 selfIsConsumed = false;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000955 if (isInstanceMethod()) {
956 // There may be no interface context due to error in declaration
957 // of the interface (which has been reported). Recover gracefully.
958 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000959 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000960 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000961 } else {
962 selfTy = Context.getObjCIdType();
963 }
964 } else // we have a factory method.
965 selfTy = Context.getObjCClassType();
966
David Blaikiebbafb8a2012-03-11 07:00:24 +0000967 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000968 if (isInstanceMethod()) {
969 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000970
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000971 // 'self' is always __strong. It's actually pseudo-strong except
972 // in init methods (or methods labeled ns_consumes_self), though.
973 Qualifiers qs;
974 qs.setObjCLifetime(Qualifiers::OCL_Strong);
975 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000976
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000977 // In addition, 'self' is const unless this is an init method.
978 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
979 selfTy = selfTy.withConst();
980 selfIsPseudoStrong = true;
981 }
982 }
983 else {
984 assert(isClassMethod());
985 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000986 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000987 selfIsPseudoStrong = true;
988 }
John McCall31168b02011-06-15 23:02:42 +0000989 }
Adrian Prantl2ecf89e2015-07-08 22:15:59 +0000990 return selfTy;
991}
John McCall31168b02011-06-15 23:02:42 +0000992
Adrian Prantl2ecf89e2015-07-08 22:15:59 +0000993void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
994 const ObjCInterfaceDecl *OID) {
995 bool selfIsPseudoStrong, selfIsConsumed;
996 QualType selfTy =
997 getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
John McCall31168b02011-06-15 23:02:42 +0000998 ImplicitParamDecl *self
999 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1000 &Context.Idents.get("self"), selfTy);
1001 setSelfDecl(self);
1002
1003 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +00001004 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001005
John McCalld4631322011-06-17 06:42:21 +00001006 if (selfIsPseudoStrong)
1007 self->setARCPseudoStrong(true);
1008
Mike Stump11289f42009-09-09 15:08:12 +00001009 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
1010 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +00001011 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001012}
1013
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001014ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
1015 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
1016 return ID;
1017 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1018 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +00001019 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001020 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +00001021 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +00001022 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +00001023 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001024}
1025
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001026SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1027 const auto *TSI = getReturnTypeSourceInfo();
1028 if (TSI)
1029 return TSI->getTypeLoc().getSourceRange();
1030 return SourceRange();
1031}
1032
Douglas Gregor9b7b3e92015-07-07 06:20:27 +00001033QualType ObjCMethodDecl::getSendResultType() const {
1034 ASTContext &Ctx = getASTContext();
1035 return getReturnType().getNonLValueExprType(Ctx)
1036 .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1037}
1038
Douglas Gregore83b9562015-07-07 03:57:53 +00001039QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1040 // FIXME: Handle related result types here.
1041
1042 return getReturnType().getNonLValueExprType(getASTContext())
1043 .substObjCMemberType(receiverType, getDeclContext(),
1044 ObjCSubstitutionContext::Result);
1045}
1046
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001047static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1048 const ObjCMethodDecl *Method,
1049 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1050 bool MovedToSuper) {
1051 if (!Container)
1052 return;
1053
1054 // In categories look for overriden methods from protocols. A method from
1055 // category is not "overriden" since it is considered as the "same" method
1056 // (same USR) as the one from the interface.
1057 if (const ObjCCategoryDecl *
1058 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1059 // Check whether we have a matching method at this category but only if we
1060 // are at the super class level.
1061 if (MovedToSuper)
1062 if (ObjCMethodDecl *
1063 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001064 Method->isInstanceMethod(),
1065 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001066 if (Method != Overridden) {
1067 // We found an override at this category; there is no need to look
1068 // into its protocols.
1069 Methods.push_back(Overridden);
1070 return;
1071 }
1072
Aaron Ballman19a41762014-03-14 12:55:57 +00001073 for (const auto *P : Category->protocols())
1074 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001075 return;
1076 }
1077
1078 // Check whether we have a matching method at this level.
1079 if (const ObjCMethodDecl *
1080 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001081 Method->isInstanceMethod(),
1082 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001083 if (Method != Overridden) {
1084 // We found an override at this level; there is no need to look
1085 // into other protocols or categories.
1086 Methods.push_back(Overridden);
1087 return;
1088 }
1089
1090 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001091 for (const auto *P : Protocol->protocols())
1092 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001093 }
1094
1095 if (const ObjCInterfaceDecl *
1096 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001097 for (const auto *P : Interface->protocols())
1098 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001099
Aaron Ballman15063e12014-03-13 21:35:02 +00001100 for (const auto *Cat : Interface->known_categories())
1101 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001102
1103 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1104 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1105 /*MovedToSuper=*/true);
1106 }
1107}
1108
1109static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1110 const ObjCMethodDecl *Method,
1111 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1112 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1113 /*MovedToSuper=*/false);
1114}
1115
1116static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1117 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1118 assert(Method->isOverriding());
1119
1120 if (const ObjCProtocolDecl *
1121 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1122 CollectOverriddenMethods(ProtD, Method, overridden);
1123
1124 } else if (const ObjCImplDecl *
1125 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1126 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1127 if (!ID)
1128 return;
1129 // Start searching for overridden methods using the method from the
1130 // interface as starting point.
1131 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001132 Method->isInstanceMethod(),
1133 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001134 Method = IFaceMeth;
1135 CollectOverriddenMethods(ID, Method, overridden);
1136
1137 } else if (const ObjCCategoryDecl *
1138 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1139 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1140 if (!ID)
1141 return;
1142 // Start searching for overridden methods using the method from the
1143 // interface as starting point.
1144 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001145 Method->isInstanceMethod(),
1146 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001147 Method = IFaceMeth;
1148 CollectOverriddenMethods(ID, Method, overridden);
1149
1150 } else {
1151 CollectOverriddenMethods(
1152 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1153 Method, overridden);
1154 }
1155}
1156
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001157void ObjCMethodDecl::getOverriddenMethods(
1158 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1159 const ObjCMethodDecl *Method = this;
1160
1161 if (Method->isRedeclaration()) {
1162 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1163 getMethod(Method->getSelector(), Method->isInstanceMethod());
1164 }
1165
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001166 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001167 collectOverriddenMethodsSlow(Method, Overridden);
1168 assert(!Overridden.empty() &&
1169 "ObjCMethodDecl's overriding bit is not as expected");
1170 }
1171}
1172
Jordan Rose2bd991a2012-10-10 16:42:54 +00001173const ObjCPropertyDecl *
1174ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1175 Selector Sel = getSelector();
1176 unsigned NumArgs = Sel.getNumArgs();
1177 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001178 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001179
Jordan Rose16ba5532015-01-16 23:04:26 +00001180 if (!isInstanceMethod())
Craig Topper36250ad2014-05-12 05:36:57 +00001181 return nullptr;
1182
Jordan Rose2bd991a2012-10-10 16:42:54 +00001183 if (isPropertyAccessor()) {
1184 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001185 // If container is class extension, find its primary class.
1186 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1187 if (CatDecl->IsClassExtension())
1188 Container = CatDecl->getClassInterface();
1189
Jordan Rose2bd991a2012-10-10 16:42:54 +00001190 bool IsGetter = (NumArgs == 0);
1191
Aaron Ballmand174edf2014-03-13 19:11:50 +00001192 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001193 Selector NextSel = IsGetter ? I->getGetterName()
1194 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001195 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001196 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001197 }
1198
1199 llvm_unreachable("Marked as a property accessor but no property found!");
1200 }
1201
1202 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001203 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001204
1205 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1206 OverridesTy Overrides;
1207 getOverriddenMethods(Overrides);
1208 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1209 I != E; ++I) {
1210 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1211 return Prop;
1212 }
1213
Craig Topper36250ad2014-05-12 05:36:57 +00001214 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001215}
1216
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001217//===----------------------------------------------------------------------===//
Douglas Gregor85f3f952015-07-07 03:57:15 +00001218// ObjCTypeParamDecl
1219//===----------------------------------------------------------------------===//
1220
1221void ObjCTypeParamDecl::anchor() { }
1222
1223ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001224 ObjCTypeParamVariance variance,
1225 SourceLocation varianceLoc,
Douglas Gregore83b9562015-07-07 03:57:53 +00001226 unsigned index,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001227 SourceLocation nameLoc,
1228 IdentifierInfo *name,
1229 SourceLocation colonLoc,
1230 TypeSourceInfo *boundInfo) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001231 return new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1232 nameLoc, name, colonLoc, boundInfo);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001233}
1234
1235ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1236 unsigned ID) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001237 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1238 ObjCTypeParamVariance::Invariant,
1239 SourceLocation(), 0, SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001240 nullptr, SourceLocation(), nullptr);
1241}
1242
1243SourceRange ObjCTypeParamDecl::getSourceRange() const {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001244 SourceLocation startLoc = VarianceLoc;
1245 if (startLoc.isInvalid())
1246 startLoc = getLocation();
1247
Douglas Gregor85f3f952015-07-07 03:57:15 +00001248 if (hasExplicitBound()) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001249 return SourceRange(startLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001250 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1251 }
1252
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001253 return SourceRange(startLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001254}
1255
1256//===----------------------------------------------------------------------===//
1257// ObjCTypeParamList
1258//===----------------------------------------------------------------------===//
1259ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1260 ArrayRef<ObjCTypeParamDecl *> typeParams,
1261 SourceLocation rAngleLoc)
Douglas Gregor0a8890ff2015-07-07 06:20:46 +00001262 : NumParams(typeParams.size())
Douglas Gregor85f3f952015-07-07 03:57:15 +00001263{
Douglas Gregor0a8890ff2015-07-07 06:20:46 +00001264 Brackets.Begin = lAngleLoc.getRawEncoding();
1265 Brackets.End = rAngleLoc.getRawEncoding();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001266 std::copy(typeParams.begin(), typeParams.end(), begin());
1267}
1268
1269
1270ObjCTypeParamList *ObjCTypeParamList::create(
1271 ASTContext &ctx,
1272 SourceLocation lAngleLoc,
1273 ArrayRef<ObjCTypeParamDecl *> typeParams,
1274 SourceLocation rAngleLoc) {
1275 unsigned size = sizeof(ObjCTypeParamList)
1276 + sizeof(ObjCTypeParamDecl *) * typeParams.size();
Aaron Ballman76413ec2015-07-07 17:12:14 +00001277 static_assert(llvm::AlignOf<ObjCTypeParamList>::Alignment >=
1278 llvm::AlignOf<ObjCTypeParamDecl *>::Alignment,
1279 "type parameter list needs greater alignment");
Douglas Gregor85f3f952015-07-07 03:57:15 +00001280 unsigned align = llvm::alignOf<ObjCTypeParamList>();
1281 void *mem = ctx.Allocate(size, align);
1282 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1283}
1284
Douglas Gregore83b9562015-07-07 03:57:53 +00001285void ObjCTypeParamList::gatherDefaultTypeArgs(
1286 SmallVectorImpl<QualType> &typeArgs) const {
1287 typeArgs.reserve(size());
1288 for (auto typeParam : *this)
1289 typeArgs.push_back(typeParam->getUnderlyingType());
1290}
1291
Douglas Gregor85f3f952015-07-07 03:57:15 +00001292//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001293// ObjCInterfaceDecl
1294//===----------------------------------------------------------------------===//
1295
Douglas Gregord53ae832012-01-17 18:09:05 +00001296ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001297 DeclContext *DC,
1298 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001299 IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001300 ObjCTypeParamList *typeParamList,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001301 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001302 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001303 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001304 ObjCInterfaceDecl *Result = new (C, DC)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001305 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1306 isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001307 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001308 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001309 return Result;
1310}
1311
Richard Smith053f6c62014-05-16 23:01:30 +00001312ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001313 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001314 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001315 SourceLocation(),
1316 nullptr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001317 nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001318 SourceLocation(),
1319 nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001320 Result->Data.setInt(!C.getLangOpts().Modules);
1321 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001322}
1323
Richard Smith053f6c62014-05-16 23:01:30 +00001324ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1325 SourceLocation AtLoc, IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001326 ObjCTypeParamList *typeParamList,
Richard Smith053f6c62014-05-16 23:01:30 +00001327 SourceLocation CLoc,
1328 ObjCInterfaceDecl *PrevDecl,
1329 bool IsInternal)
1330 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001331 redeclarable_base(C), TypeForDecl(nullptr), TypeParamList(nullptr),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001332 Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001333 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001334
1335 // Copy the 'data' pointer over.
1336 if (PrevDecl)
1337 Data = PrevDecl->Data;
1338
Richard Smith053f6c62014-05-16 23:01:30 +00001339 setImplicit(IsInternal);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001340
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001341 setTypeParamList(typeParamList);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001342}
1343
Douglas Gregor73693022010-12-01 23:49:52 +00001344void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001345 assert(data().ExternallyCompleted && "Class is not externally completed");
1346 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001347 getASTContext().getExternalSource()->CompleteType(
1348 const_cast<ObjCInterfaceDecl *>(this));
1349}
1350
1351void ObjCInterfaceDecl::setExternallyCompleted() {
1352 assert(getASTContext().getExternalSource() &&
1353 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001354 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001355 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001356 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001357}
1358
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001359void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001360 // Check for a complete definition and recover if not so.
1361 if (!isThisDeclarationADefinition())
1362 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001363 data().HasDesignatedInitializers = true;
1364}
1365
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001366bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001367 // Check for a complete definition and recover if not so.
1368 if (!isThisDeclarationADefinition())
1369 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001370 if (data().ExternallyCompleted)
1371 LoadExternalDefinition();
1372
1373 return data().HasDesignatedInitializers;
1374}
1375
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001376StringRef
1377ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001378 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1379 return ObjCRTName->getMetadataName();
1380
1381 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001382}
1383
1384StringRef
1385ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001386 if (ObjCInterfaceDecl *ID =
1387 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1388 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001389
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001390 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001391}
1392
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001393ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001394 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1395 if (data().ExternallyCompleted)
1396 LoadExternalDefinition();
1397
1398 return getASTContext().getObjCImplementation(
1399 const_cast<ObjCInterfaceDecl*>(Def));
1400 }
1401
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001402 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001403 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001404}
1405
1406void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001407 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001408}
1409
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001410namespace {
1411 struct SynthesizeIvarChunk {
1412 uint64_t Size;
1413 ObjCIvarDecl *Ivar;
1414 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1415 : Size(size), Ivar(ivar) {}
1416 };
1417
1418 bool operator<(const SynthesizeIvarChunk & LHS,
1419 const SynthesizeIvarChunk &RHS) {
1420 return LHS.Size < RHS.Size;
1421 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001422}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001423
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001424/// all_declared_ivar_begin - return first ivar declared in this class,
1425/// its extensions and its implementation. Lazily build the list on first
1426/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001427///
1428/// Caveat: The list returned by this method reflects the current
1429/// state of the parser. The cache will be updated for every ivar
1430/// added by an extension or the implementation when they are
1431/// encountered.
1432/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001433ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001434 // FIXME: Should make sure no callers ever do this.
1435 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001436 return nullptr;
1437
1438 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001439 if (!data().IvarList) {
1440 if (!ivar_empty()) {
1441 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1442 data().IvarList = *I; ++I;
1443 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001444 curIvar->setNextIvar(*I);
1445 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001446
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001447 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001448 if (!Ext->ivar_empty()) {
1449 ObjCCategoryDecl::ivar_iterator
1450 I = Ext->ivar_begin(),
1451 E = Ext->ivar_end();
1452 if (!data().IvarList) {
1453 data().IvarList = *I; ++I;
1454 curIvar = data().IvarList;
1455 }
1456 for ( ;I != E; curIvar = *I, ++I)
1457 curIvar->setNextIvar(*I);
1458 }
1459 }
1460 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001461 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001462
1463 // cached and complete!
1464 if (!data().IvarListMissingImplementation)
1465 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001466
1467 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001468 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001469 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001470 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001471 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001472 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1473 layout.push_back(SynthesizeIvarChunk(
1474 IV->getASTContext().getTypeSize(IV->getType()), IV));
1475 continue;
1476 }
1477 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001478 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001479 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001480 curIvar->setNextIvar(IV);
1481 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001482 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001483
1484 if (!layout.empty()) {
1485 // Order synthesized ivars by their size.
1486 std::stable_sort(layout.begin(), layout.end());
1487 unsigned Ix = 0, EIx = layout.size();
1488 if (!data().IvarList) {
1489 data().IvarList = layout[0].Ivar; Ix++;
1490 curIvar = data().IvarList;
1491 }
1492 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1493 curIvar->setNextIvar(layout[Ix].Ivar);
1494 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001495 }
1496 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001497 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001498}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001499
1500/// FindCategoryDeclaration - Finds category declaration in the list of
1501/// categories for this class and returns it. Name of the category is passed
1502/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001503///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001504ObjCCategoryDecl *
1505ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001506 // FIXME: Should make sure no callers ever do this.
1507 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001508 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001509
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001510 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001511 LoadExternalDefinition();
1512
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001513 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001514 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001515 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001516
1517 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001518}
1519
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001520ObjCMethodDecl *
1521ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001522 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001523 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001524 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1525 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001526 }
1527
Craig Topper36250ad2014-05-12 05:36:57 +00001528 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001529}
1530
1531ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001532 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001533 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001534 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1535 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001536 }
Craig Topper36250ad2014-05-12 05:36:57 +00001537
1538 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001539}
1540
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001541/// ClassImplementsProtocol - Checks that 'lProto' protocol
1542/// has been implemented in IDecl class, its super class or categories (if
1543/// lookupCategory is true).
1544bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1545 bool lookupCategory,
1546 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001547 if (!hasDefinition())
1548 return false;
1549
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001550 ObjCInterfaceDecl *IDecl = this;
1551 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001552 for (auto *PI : IDecl->protocols()){
1553 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001554 return true;
1555 // This is dubious and is added to be compatible with gcc. In gcc, it is
1556 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1557 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1558 // object. This IMO, should be a bug.
1559 // FIXME: Treat this as an extension, and flag this as an error when GCC
1560 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001561 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001562 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
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 // 2nd, look up the category.
1567 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001568 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001569 for (auto *PI : Cat->protocols())
1570 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001571 return true;
1572 }
Mike Stump11289f42009-09-09 15:08:12 +00001573
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001574 // 3rd, look up the super class(s)
1575 if (IDecl->getSuperClass())
1576 return
1577 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1578 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001579
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001580 return false;
1581}
1582
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001583//===----------------------------------------------------------------------===//
1584// ObjCIvarDecl
1585//===----------------------------------------------------------------------===//
1586
David Blaikie68e081d2011-12-20 02:48:34 +00001587void ObjCIvarDecl::anchor() { }
1588
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001589ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001590 SourceLocation StartLoc,
1591 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001592 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001593 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001594 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001595 if (DC) {
1596 // Ivar's can only appear in interfaces, implementations (via synthesized
1597 // properties), and class extensions (via direct declaration, or synthesized
1598 // properties).
1599 //
1600 // FIXME: This should really be asserting this:
1601 // (isa<ObjCCategoryDecl>(DC) &&
1602 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1603 // but unfortunately we sometimes place ivars into non-class extension
1604 // categories on error. This breaks an AST invariant, and should not be
1605 // fixed.
1606 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1607 isa<ObjCCategoryDecl>(DC)) &&
1608 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001609 // Once a new ivar is created in any of class/class-extension/implementation
1610 // decl contexts, the previously built IvarList must be rebuilt.
1611 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1612 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001613 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001614 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001615 else
1616 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001617 }
Craig Topper36250ad2014-05-12 05:36:57 +00001618 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001619 }
1620
Richard Smithf7981722013-11-22 09:01:48 +00001621 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001622 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001623}
1624
Douglas Gregor72172e92012-01-05 21:55:30 +00001625ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001626 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1627 nullptr, QualType(), nullptr,
1628 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001629}
1630
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001631const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1632 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001633
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001634 switch (DC->getKind()) {
1635 default:
1636 case ObjCCategoryImpl:
1637 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001638 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001639
1640 // Ivars can only appear in class extension categories.
1641 case ObjCCategory: {
1642 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1643 assert(CD->IsClassExtension() && "invalid container for ivar!");
1644 return CD->getClassInterface();
1645 }
1646
1647 case ObjCImplementation:
1648 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1649
1650 case ObjCInterface:
1651 return cast<ObjCInterfaceDecl>(DC);
1652 }
1653}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001654
Douglas Gregore83b9562015-07-07 03:57:53 +00001655QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1656 return getType().substObjCMemberType(objectType, getDeclContext(),
1657 ObjCSubstitutionContext::Property);
1658}
1659
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001660//===----------------------------------------------------------------------===//
1661// ObjCAtDefsFieldDecl
1662//===----------------------------------------------------------------------===//
1663
David Blaikie68e081d2011-12-20 02:48:34 +00001664void ObjCAtDefsFieldDecl::anchor() { }
1665
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001666ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001667*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1668 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001669 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001670 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001671}
1672
Richard Smithf7981722013-11-22 09:01:48 +00001673ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001674 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001675 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1676 SourceLocation(), nullptr, QualType(),
1677 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001678}
1679
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001680//===----------------------------------------------------------------------===//
1681// ObjCProtocolDecl
1682//===----------------------------------------------------------------------===//
1683
David Blaikie68e081d2011-12-20 02:48:34 +00001684void ObjCProtocolDecl::anchor() { }
1685
Richard Smith053f6c62014-05-16 23:01:30 +00001686ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1687 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001688 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001689 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001690 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1691 redeclarable_base(C), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001692 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001693 if (PrevDecl)
1694 Data = PrevDecl->Data;
1695}
1696
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001697ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001698 IdentifierInfo *Id,
1699 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001700 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001701 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001702 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001703 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001704 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001705 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001706}
1707
Richard Smithf7981722013-11-22 09:01:48 +00001708ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001709 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001710 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001711 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001712 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001713 Result->Data.setInt(!C.getLangOpts().Modules);
1714 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001715}
1716
Steve Naroff114aecb2009-03-01 16:12:44 +00001717ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1718 ObjCProtocolDecl *PDecl = this;
1719
1720 if (Name == getIdentifier())
1721 return PDecl;
1722
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001723 for (auto *I : protocols())
1724 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001725 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001726
Craig Topper36250ad2014-05-12 05:36:57 +00001727 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001728}
1729
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001730// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001731// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001732ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1733 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001734 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001735
Douglas Gregoreed49792013-01-17 00:38:46 +00001736 // If there is no definition or the definition is hidden, we don't find
1737 // anything.
1738 const ObjCProtocolDecl *Def = getDefinition();
1739 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001740 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001741
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001742 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001743 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001744
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001745 for (const auto *I : protocols())
1746 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001747 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001748 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001749}
1750
Douglas Gregore6e48b12012-01-01 19:29:29 +00001751void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001752 assert(!Data.getPointer() && "Protocol already has a definition!");
1753 Data.setPointer(new (getASTContext()) DefinitionData);
1754 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001755}
1756
1757void ObjCProtocolDecl::startDefinition() {
1758 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001759
1760 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001761 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001762 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001763}
1764
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001765void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1766 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001767
1768 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001769 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001770 // Insert into PM if not there already.
1771 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001772 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001773 }
1774 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001775 for (const auto *PI : PDecl->protocols())
1776 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001777 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001778}
1779
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001780
1781void ObjCProtocolDecl::collectInheritedProtocolProperties(
1782 const ObjCPropertyDecl *Property,
1783 ProtocolPropertyMap &PM) const {
1784 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1785 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001786 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001787 if (Prop == Property)
1788 continue;
1789 if (Prop->getIdentifier() == Property->getIdentifier()) {
1790 PM[PDecl] = Prop;
1791 MatchFound = true;
1792 break;
1793 }
1794 }
1795 // Scan through protocol's protocols which did not have a matching property.
1796 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001797 for (const auto *PI : PDecl->protocols())
1798 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001799 }
1800}
Anna Zaks673d76b2012-10-18 19:17:53 +00001801
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001802StringRef
1803ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001804 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1805 return ObjCRTName->getMetadataName();
1806
1807 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001808}
1809
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001810//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001811// ObjCCategoryDecl
1812//===----------------------------------------------------------------------===//
1813
David Blaikie68e081d2011-12-20 02:48:34 +00001814void ObjCCategoryDecl::anchor() { }
1815
Douglas Gregor85f3f952015-07-07 03:57:15 +00001816ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1817 SourceLocation ClassNameLoc,
1818 SourceLocation CategoryNameLoc,
1819 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1820 ObjCTypeParamList *typeParamList,
1821 SourceLocation IvarLBraceLoc,
1822 SourceLocation IvarRBraceLoc)
1823 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001824 ClassInterface(IDecl), TypeParamList(nullptr),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001825 NextClassCategory(nullptr), CategoryNameLoc(CategoryNameLoc),
1826 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc)
1827{
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001828 setTypeParamList(typeParamList);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001829}
1830
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001831ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001832 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001833 SourceLocation ClassNameLoc,
1834 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001835 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001836 ObjCInterfaceDecl *IDecl,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001837 ObjCTypeParamList *typeParamList,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001838 SourceLocation IvarLBraceLoc,
1839 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001840 ObjCCategoryDecl *CatDecl =
1841 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001842 IDecl, typeParamList, IvarLBraceLoc,
1843 IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001844 if (IDecl) {
1845 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001846 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001847 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001848 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001849 if (ASTMutationListener *L = C.getASTMutationListener())
1850 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1851 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001852 }
1853
1854 return CatDecl;
1855}
1856
Richard Smithf7981722013-11-22 09:01:48 +00001857ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001858 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001859 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1860 SourceLocation(), SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001861 nullptr, nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001862}
1863
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001864ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1865 return getASTContext().getObjCImplementation(
1866 const_cast<ObjCCategoryDecl*>(this));
1867}
1868
1869void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1870 getASTContext().setObjCImplementation(this, ImplD);
1871}
1872
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001873void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
1874 TypeParamList = TPL;
1875 if (!TPL)
1876 return;
1877 // Set the declaration context of each of the type parameters.
1878 for (auto typeParam : *TypeParamList)
1879 typeParam->setDeclContext(this);
1880}
1881
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001882
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001883//===----------------------------------------------------------------------===//
1884// ObjCCategoryImplDecl
1885//===----------------------------------------------------------------------===//
1886
David Blaikie68e081d2011-12-20 02:48:34 +00001887void ObjCCategoryImplDecl::anchor() { }
1888
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001889ObjCCategoryImplDecl *
1890ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001891 IdentifierInfo *Id,
1892 ObjCInterfaceDecl *ClassInterface,
1893 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001894 SourceLocation atStartLoc,
1895 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001896 if (ClassInterface && ClassInterface->hasDefinition())
1897 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001898 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1899 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001900}
1901
Douglas Gregor72172e92012-01-05 21:55:30 +00001902ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1903 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001904 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1905 SourceLocation(), SourceLocation(),
1906 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001907}
1908
Steve Narofff406f4d2009-10-29 21:11:04 +00001909ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001910 // The class interface might be NULL if we are working with invalid code.
1911 if (const ObjCInterfaceDecl *ID = getClassInterface())
1912 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00001913 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001914}
1915
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001916
David Blaikie68e081d2011-12-20 02:48:34 +00001917void ObjCImplDecl::anchor() { }
1918
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001919void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001920 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001921 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001922 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001923}
1924
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001925void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1926 ASTContext &Ctx = getASTContext();
1927
1928 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001929 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001930 if (IFace)
1931 Ctx.setObjCImplementation(IFace, ImplD);
1932
Duncan Sands49c29ee2009-07-21 07:56:29 +00001933 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001934 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1935 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1936 Ctx.setObjCImplementation(CD, ImplD);
1937 }
1938
1939 ClassInterface = IFace;
1940}
1941
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001942/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001943/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001944/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001945///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001946ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001947FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001948 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001949 if (PID->getPropertyIvarDecl() &&
1950 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1951 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001952 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001953}
1954
1955/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001956/// added to the list of those properties \@synthesized/\@dynamic in this
1957/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001958///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001959ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001960FindPropertyImplDecl(IdentifierInfo *Id) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001961 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001962 if (PID->getPropertyDecl()->getIdentifier() == Id)
1963 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001964 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001965}
1966
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001967raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001968 const ObjCCategoryImplDecl &CID) {
1969 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001970 return OS;
1971}
1972
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001973//===----------------------------------------------------------------------===//
1974// ObjCImplementationDecl
1975//===----------------------------------------------------------------------===//
1976
David Blaikie68e081d2011-12-20 02:48:34 +00001977void ObjCImplementationDecl::anchor() { }
1978
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001979ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001980ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001981 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001982 ObjCInterfaceDecl *SuperDecl,
1983 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001984 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001985 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001986 SourceLocation IvarLBraceLoc,
1987 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001988 if (ClassInterface && ClassInterface->hasDefinition())
1989 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001990 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1991 nameLoc, atStartLoc, superLoc,
1992 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001993}
1994
Douglas Gregor72172e92012-01-05 21:55:30 +00001995ObjCImplementationDecl *
1996ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001997 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
1998 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001999}
2000
John McCall0410e572011-07-22 04:15:06 +00002001void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2002 CXXCtorInitializer ** initializers,
2003 unsigned numInitializers) {
2004 if (numInitializers > 0) {
2005 NumIvarInitializers = numInitializers;
2006 CXXCtorInitializer **ivarInitializers =
2007 new (C) CXXCtorInitializer*[NumIvarInitializers];
2008 memcpy(ivarInitializers, initializers,
2009 numInitializers * sizeof(CXXCtorInitializer*));
2010 IvarInitializers = ivarInitializers;
2011 }
2012}
2013
Richard Smithc2bb8182015-03-24 06:36:48 +00002014ObjCImplementationDecl::init_const_iterator
2015ObjCImplementationDecl::init_begin() const {
2016 return IvarInitializers.get(getASTContext().getExternalSource());
2017}
2018
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002019raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002020 const ObjCImplementationDecl &ID) {
2021 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002022 return OS;
2023}
2024
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002025//===----------------------------------------------------------------------===//
2026// ObjCCompatibleAliasDecl
2027//===----------------------------------------------------------------------===//
2028
David Blaikie68e081d2011-12-20 02:48:34 +00002029void ObjCCompatibleAliasDecl::anchor() { }
2030
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002031ObjCCompatibleAliasDecl *
2032ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2033 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00002034 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002035 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00002036 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002037}
2038
Douglas Gregor72172e92012-01-05 21:55:30 +00002039ObjCCompatibleAliasDecl *
2040ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002041 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2042 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002043}
2044
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002045//===----------------------------------------------------------------------===//
2046// ObjCPropertyDecl
2047//===----------------------------------------------------------------------===//
2048
David Blaikie68e081d2011-12-20 02:48:34 +00002049void ObjCPropertyDecl::anchor() { }
2050
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002051ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2052 SourceLocation L,
2053 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00002054 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002055 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00002056 QualType T,
2057 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002058 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002059 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2060 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002061}
2062
Richard Smithf7981722013-11-22 09:01:48 +00002063ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002064 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002065 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2066 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00002067 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00002068}
2069
Douglas Gregore83b9562015-07-07 03:57:53 +00002070QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2071 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2072 ObjCSubstitutionContext::Property);
2073}
2074
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002075//===----------------------------------------------------------------------===//
2076// ObjCPropertyImplDecl
2077//===----------------------------------------------------------------------===//
2078
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002079ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002080 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002081 SourceLocation atLoc,
2082 SourceLocation L,
2083 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002084 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002085 ObjCIvarDecl *ivar,
2086 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002087 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2088 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002089}
Chris Lattnered0e1642008-03-17 01:19:02 +00002090
Richard Smithf7981722013-11-22 09:01:48 +00002091ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002092 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002093 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2094 SourceLocation(), nullptr, Dynamic,
2095 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002096}
2097
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002098SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2099 SourceLocation EndLoc = getLocation();
2100 if (IvarLoc.isValid())
2101 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002102
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002103 return SourceRange(AtLoc, EndLoc);
2104}