blob: 69ff33a60aaa951bf469c5bbe066582278125fb6 [file] [log] [blame]
Chris Lattner89375192008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000016#include "clang/AST/ASTMutationListener.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/Stmt.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000019#include "llvm/ADT/STLExtras.h"
Anna Zaks454477c2012-09-27 19:45:11 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner89375192008-03-16 00:19:01 +000021using namespace clang;
22
Chris Lattner8d8829e2008-03-16 00:49:28 +000023//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000024// ObjCListBase
25//===----------------------------------------------------------------------===//
26
Chris Lattner22298722009-02-20 21:35:13 +000027void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Craig Topper36250ad2014-05-12 05:36:57 +000028 List = nullptr;
Chris Lattner4d1eb762009-02-20 21:16:26 +000029 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000030
31
Chris Lattner7c981a72009-02-20 21:44:01 +000032 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000033 NumElts = Elts;
34 memcpy(List, InList, sizeof(void*)*Elts);
35}
36
Douglas Gregor002b6712010-01-16 15:02:53 +000037void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
38 const SourceLocation *Locs, ASTContext &Ctx) {
39 if (Elts == 0)
40 return;
41
42 Locations = new (Ctx) SourceLocation[Elts];
43 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
44 set(InList, Elts, Ctx);
45}
46
Chris Lattner4d1eb762009-02-20 21:16:26 +000047//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000048// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000049//===----------------------------------------------------------------------===//
50
David Blaikie68e081d2011-12-20 02:48:34 +000051void ObjCContainerDecl::anchor() { }
52
Fariborz Jahanian68453832009-06-05 18:16:35 +000053/// getIvarDecl - This method looks up an ivar in this ContextDecl.
54///
55ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000056ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Richard Smithcf4bdde2015-02-21 02:45:19 +000057 lookup_result R = lookup(Id);
58 for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000059 Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian68453832009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
Craig Topper36250ad2014-05-12 05:36:57 +000063 return nullptr;
Fariborz Jahanian68453832009-06-05 18:16:35 +000064}
65
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000066// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000067ObjCMethodDecl *
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69 bool AllowHidden) const {
Douglas Gregoreed49792013-01-17 00:38:46 +000070 // If this context is a hidden protocol definition, don't find any
71 // methods there.
72 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
73 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000074 if (Def->isHidden() && !AllowHidden)
Craig Topper36250ad2014-05-12 05:36:57 +000075 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +000076 }
77
Steve Naroffc4173fa2009-02-22 19:35:57 +000078 // Since instance & class methods can have the same name, the loop below
79 // ensures we get the correct method.
80 //
81 // @interface Whatever
82 // - (int) class_method;
83 // + (float) class_method;
84 // @end
85 //
Richard Smithcf4bdde2015-02-21 02:45:19 +000086 lookup_result R = lookup(Sel);
87 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000088 Meth != MethEnd; ++Meth) {
Steve Naroffc4173fa2009-02-22 19:35:57 +000089 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000090 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +000091 return MD;
92 }
Craig Topper36250ad2014-05-12 05:36:57 +000093 return nullptr;
Steve Naroff35c62ae2009-01-08 17:28:14 +000094}
95
Nico Weber4701ffd2014-12-22 05:21:03 +000096/// \brief This routine returns 'true' if a user declared setter method was
97/// found in the class, its protocols, its super classes or categories.
98/// It also returns 'true' if one of its categories has declared a 'readwrite'
99/// property. This is because, user must provide a setter method for the
100/// category's 'readwrite' property.
101bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
102 const ObjCPropertyDecl *Property) const {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000103 Selector Sel = Property->getSetterName();
Richard Smithcf4bdde2015-02-21 02:45:19 +0000104 lookup_result R = lookup(Sel);
105 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000106 Meth != MethEnd; ++Meth) {
107 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
108 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
109 return true;
110 }
111
112 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
113 // Also look into categories, including class extensions, looking
114 // for a user declared instance method.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000115 for (const auto *Cat : ID->visible_categories()) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000116 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
117 if (!MD->isImplicit())
118 return true;
119 if (Cat->IsClassExtension())
120 continue;
Nico Weber4701ffd2014-12-22 05:21:03 +0000121 // Also search through the categories looking for a 'readwrite'
122 // declaration of this property. If one found, presumably a setter will
123 // be provided (properties declared in categories will not get
124 // auto-synthesized).
Aaron Ballmand174edf2014-03-13 19:11:50 +0000125 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000126 if (P->getIdentifier() == Property->getIdentifier()) {
127 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
128 return true;
129 break;
130 }
131 }
132
133 // Also look into protocols, for a user declared instance method.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000134 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000135 if (Proto->HasUserDeclaredSetterMethod(Property))
136 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000137
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000138 // And in its super class.
139 ObjCInterfaceDecl *OSC = ID->getSuperClass();
140 while (OSC) {
141 if (OSC->HasUserDeclaredSetterMethod(Property))
142 return true;
143 OSC = OSC->getSuperClass();
144 }
145 }
146 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000147 for (const auto *PI : PD->protocols())
148 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000149 return true;
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000150 return false;
151}
152
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000153ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000154ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Jordan Rose210bfe92014-11-19 22:03:46 +0000155 const IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000156 // If this context is a hidden protocol definition, don't find any
157 // property.
158 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
159 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
160 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000161 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000162 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000163
Richard Smithcf4bdde2015-02-21 02:45:19 +0000164 DeclContext::lookup_result R = DC->lookup(propertyID);
165 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
David Blaikieff7d47a2012-12-19 00:45:41 +0000166 ++I)
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000167 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
168 return PD;
169
Craig Topper36250ad2014-05-12 05:36:57 +0000170 return nullptr;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000171}
172
Anna Zaks454477c2012-09-27 19:45:11 +0000173IdentifierInfo *
174ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
175 SmallString<128> ivarName;
176 {
177 llvm::raw_svector_ostream os(ivarName);
178 os << '_' << getIdentifier()->getName();
179 }
180 return &Ctx.Idents.get(ivarName.str());
181}
182
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000183/// FindPropertyDeclaration - Finds declaration of the property given its name
184/// in 'PropertyId' and returns it. It returns 0, if not found.
Jordan Rose210bfe92014-11-19 22:03:46 +0000185ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
186 const IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000187 // Don't find properties within hidden protocol definitions.
188 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
189 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
190 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000191 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Ted Kremenekddcd1092010-03-15 20:11:53 +0000194 if (ObjCPropertyDecl *PD =
195 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
196 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Ted Kremenekddcd1092010-03-15 20:11:53 +0000198 switch (getKind()) {
199 default:
200 break;
201 case Decl::ObjCProtocol: {
202 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000203 for (const auto *I : PID->protocols())
204 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000205 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000206 break;
207 }
208 case Decl::ObjCInterface: {
209 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000210 // Look through categories (but not extensions).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000211 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000212 if (!Cat->IsClassExtension())
213 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
214 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000215 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000216
217 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000218 for (const auto *I : OID->all_referenced_protocols())
219 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000220 return P;
221
222 // Finally, check the super class.
223 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
224 return superClass->FindPropertyDeclaration(PropertyId);
225 break;
226 }
227 case Decl::ObjCCategory: {
228 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
229 // Look through protocols.
230 if (!OCD->IsClassExtension())
Aaron Ballman19a41762014-03-14 12:55:57 +0000231 for (const auto *I : OCD->protocols())
232 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
233 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000234 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000235 }
236 }
Craig Topper36250ad2014-05-12 05:36:57 +0000237 return nullptr;
Steve Narofff9c65242008-06-05 13:55:23 +0000238}
239
David Blaikie68e081d2011-12-20 02:48:34 +0000240void ObjCInterfaceDecl::anchor() { }
241
Douglas Gregor85f3f952015-07-07 03:57:15 +0000242ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
243 // If this particular declaration has a type parameter list, return it.
244 if (ObjCTypeParamList *written = getTypeParamListAsWritten())
245 return written;
246
247 // If there is a definition, return its type parameter list.
248 if (const ObjCInterfaceDecl *def = getDefinition())
249 return def->getTypeParamListAsWritten();
250
251 // Otherwise, look at previous declarations to determine whether any
252 // of them has a type parameter list, skipping over those
253 // declarations that do not.
254 for (auto decl = getPreviousDecl(); decl; decl = decl->getPreviousDecl()) {
255 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
256 return written;
257 }
258
259 return nullptr;
260}
261
Douglas Gregore9d95f12015-07-07 03:57:35 +0000262ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
263 // FIXME: Should make sure no callers ever do this.
264 if (!hasDefinition())
265 return nullptr;
266
267 if (data().ExternallyCompleted)
268 LoadExternalDefinition();
269
270 if (const ObjCObjectType *superType = getSuperClassType()) {
271 if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
272 if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
273 return superDef;
274
275 return superDecl;
276 }
277 }
278
279 return nullptr;
280}
281
282SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
283 if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
284 return superTInfo->getTypeLoc().getLocStart();
285
286 return SourceLocation();
287}
288
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000289/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
290/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000291/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000292///
293ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000294ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000295 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000296 // FIXME: Should make sure no callers ever do this.
297 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000298 return nullptr;
299
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000300 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000301 LoadExternalDefinition();
302
Ted Kremenekd133a862010-03-15 20:30:07 +0000303 if (ObjCPropertyDecl *PD =
304 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
305 return PD;
306
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000307 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000308 for (const auto *I : all_referenced_protocols())
309 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000310 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000311
Craig Topper36250ad2014-05-12 05:36:57 +0000312 return nullptr;
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000313}
314
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000315void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
316 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000317 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000318 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000319 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000320 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000321 for (const auto *PI : all_referenced_protocols())
322 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000323 // Note, the properties declared only in class extensions are still copied
324 // into the main @interface's property list, and therefore we don't
325 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000326}
327
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000328bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
329 const ObjCInterfaceDecl *Class = this;
330 while (Class) {
331 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
332 return true;
333 Class = Class->getSuperClass();
334 }
335 return false;
336}
337
338const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
339 const ObjCInterfaceDecl *Class = this;
340 while (Class) {
341 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
342 return Class;
343 Class = Class->getSuperClass();
344 }
Craig Topper36250ad2014-05-12 05:36:57 +0000345 return nullptr;
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000346}
347
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000348void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
349 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
350 ASTContext &C)
351{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000352 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000353 LoadExternalDefinition();
354
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000355 if (data().AllReferencedProtocols.empty() &&
356 data().ReferencedProtocols.empty()) {
357 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000358 return;
359 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000360
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000361 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000362 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000363 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000364 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000365 for (unsigned i = 0; i < ExtNum; i++) {
366 bool protocolExists = false;
367 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000368 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000369 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
370 protocolExists = true;
371 break;
372 }
373 }
374 // Do we want to warn on a protocol in extension class which
375 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000376 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000377 ProtocolRefs.push_back(ProtoInExtension);
378 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000379
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000380 if (ProtocolRefs.empty())
381 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000382
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000383 // Merge ProtocolRefs into class's protocol list;
Benjamin Kramerf9890422015-02-17 16:48:30 +0000384 ProtocolRefs.append(all_referenced_protocol_begin(),
385 all_referenced_protocol_end());
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000386
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000387 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000388}
389
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000390const ObjCInterfaceDecl *
391ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
392 const ObjCInterfaceDecl *IFace = this;
393 while (IFace) {
394 if (IFace->hasDesignatedInitializers())
395 return IFace;
396 if (!IFace->inheritsDesignatedInitializers())
397 break;
398 IFace = IFace->getSuperClass();
399 }
Craig Topper36250ad2014-05-12 05:36:57 +0000400 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000401}
402
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000403static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
404 for (const auto *MD : D->instance_methods()) {
405 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
406 return true;
407 }
408 for (const auto *Ext : D->visible_extensions()) {
409 for (const auto *MD : Ext->instance_methods()) {
410 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
411 return true;
412 }
413 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000414 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000415 for (const auto *MD : ImplD->instance_methods()) {
416 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
417 return true;
418 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000419 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000420 return false;
421}
422
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000423bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
424 switch (data().InheritedDesignatedInitializers) {
425 case DefinitionData::IDI_Inherited:
426 return true;
427 case DefinitionData::IDI_NotInherited:
428 return false;
429 case DefinitionData::IDI_Unknown: {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000430 // If the class introduced initializers we conservatively assume that we
431 // don't know if any of them is a designated initializer to avoid possible
432 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000433 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000434 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000435 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000436 if (auto SuperD = getSuperClass()) {
437 data().InheritedDesignatedInitializers =
438 SuperD->declaresOrInheritsDesignatedInitializers() ?
439 DefinitionData::IDI_Inherited :
440 DefinitionData::IDI_NotInherited;
441 } else {
442 data().InheritedDesignatedInitializers =
443 DefinitionData::IDI_NotInherited;
444 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000445 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000446 assert(data().InheritedDesignatedInitializers
447 != DefinitionData::IDI_Unknown);
448 return data().InheritedDesignatedInitializers ==
449 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000450 }
451 }
452
453 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
454}
455
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000456void ObjCInterfaceDecl::getDesignatedInitializers(
457 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000458 // Check for a complete definition and recover if not so.
459 if (!isThisDeclarationADefinition())
460 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000461 if (data().ExternallyCompleted)
462 LoadExternalDefinition();
463
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000464 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000465 if (!IFace)
466 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000467
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000468 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000469 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000470 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000471 for (const auto *Ext : IFace->visible_extensions()) {
472 for (const auto *MD : Ext->instance_methods())
473 if (MD->isThisDeclarationADesignatedInitializer())
474 Methods.push_back(MD);
475 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000476}
477
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000478bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
479 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000480 // Check for a complete definition and recover if not so.
481 if (!isThisDeclarationADefinition())
482 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000483 if (data().ExternallyCompleted)
484 LoadExternalDefinition();
485
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000486 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000487 if (!IFace)
488 return false;
489
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000490 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000491 if (MD->isThisDeclarationADesignatedInitializer()) {
492 if (InitMethod)
493 *InitMethod = MD;
494 return true;
495 }
496 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000497 for (const auto *Ext : IFace->visible_extensions()) {
498 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
499 if (MD->isThisDeclarationADesignatedInitializer()) {
500 if (InitMethod)
501 *InitMethod = MD;
502 return true;
503 }
504 }
505 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000506 return false;
507}
508
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000509void ObjCInterfaceDecl::allocateDefinitionData() {
510 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000511 Data.setPointer(new (getASTContext()) DefinitionData());
512 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000513
514 // Make the type point at the definition, now that we have one.
515 if (TypeForDecl)
516 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000517}
518
519void ObjCInterfaceDecl::startDefinition() {
520 allocateDefinitionData();
521
Douglas Gregor66b310c2011-12-15 18:03:09 +0000522 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000523 for (auto RD : redecls()) {
524 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000525 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000526 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000527}
528
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000529ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
530 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000531 // FIXME: Should make sure no callers ever do this.
532 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000533 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000534
535 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000536 LoadExternalDefinition();
537
Chris Lattner89375192008-03-16 00:19:01 +0000538 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000539 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000540 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000541 clsDeclared = ClassDecl;
542 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000543 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000544
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000545 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000546 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000547 clsDeclared = ClassDecl;
548 return I;
549 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000550 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000551
Chris Lattner89375192008-03-16 00:19:01 +0000552 ClassDecl = ClassDecl->getSuperClass();
553 }
Craig Topper36250ad2014-05-12 05:36:57 +0000554 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000555}
556
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000557/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
558/// class whose name is passed as argument. If it is not one of the super classes
559/// the it returns NULL.
560ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
561 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000562 // FIXME: Should make sure no callers ever do this.
563 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000564 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000565
566 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000567 LoadExternalDefinition();
568
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000569 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000570 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000571 if (ClassDecl->getIdentifier() == ICName)
572 return ClassDecl;
573 ClassDecl = ClassDecl->getSuperClass();
574 }
Craig Topper36250ad2014-05-12 05:36:57 +0000575 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000576}
577
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000578ObjCProtocolDecl *
579ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000580 for (auto *P : all_referenced_protocols())
581 if (P->lookupProtocolNamed(Name))
582 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000583 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000584 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000585}
586
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000587/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000588/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000589/// When argument category "C" is specified, any implicit method found
590/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000591ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000592 bool isInstance,
593 bool shallowCategoryLookup,
594 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000595 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000596{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000597 // FIXME: Should make sure no callers ever do this.
598 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000599 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000600
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000601 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000602 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000603
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000604 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000605 LoadExternalDefinition();
606
Ted Kremenek00781502013-11-23 01:01:29 +0000607 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000608 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000609 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000610 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000611
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000612 // 2. Didn't find one yet - now look through categories.
613 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000614 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000615 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000616 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000617
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000618 // 3. Didn't find one yet - look through primary class's protocols.
619 for (const auto *I : ClassDecl->protocols())
620 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
621 return MethodDecl;
622
623 // 4. Didn't find one yet - now look through categories' protocols
624 if (!shallowCategoryLookup)
625 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000626 // Didn't find one yet - look through protocols.
627 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000628 Cat->getReferencedProtocols();
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000629 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
630 E = Protocols.end(); I != E; ++I)
631 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000632 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000633 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000634 }
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000635
636
Ted Kremenek00781502013-11-23 01:01:29 +0000637 if (!followSuper)
Craig Topper36250ad2014-05-12 05:36:57 +0000638 return nullptr;
Ted Kremenek00781502013-11-23 01:01:29 +0000639
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000640 // 5. Get to the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000641 ClassDecl = ClassDecl->getSuperClass();
642 }
Craig Topper36250ad2014-05-12 05:36:57 +0000643 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000644}
645
Anna Zaksc77a3b12012-07-27 19:07:44 +0000646// Will search "local" class/category implementations for a method decl.
647// If failed, then we search in class's root for an instance method.
648// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000649ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
650 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000651 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000652 // FIXME: Should make sure no callers ever do this.
653 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000654 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000655
656 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000657 LoadExternalDefinition();
658
Craig Topper36250ad2014-05-12 05:36:57 +0000659 ObjCMethodDecl *Method = nullptr;
Steve Naroffbb69c942009-10-01 23:46:04 +0000660 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000661 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
662 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000663
664 // Look through local category implementations associated with the class.
665 if (!Method)
Nico Weberf6098392015-03-02 01:12:28 +0000666 Method = getCategoryMethod(Sel, Instance);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000667
668 // Before we give up, check if the selector is an instance method.
669 // But only in the root. This matches gcc's behavior and what the
670 // runtime expects.
671 if (!Instance && !Method && !getSuperClass()) {
672 Method = lookupInstanceMethod(Sel);
673 // Look through local category implementations associated
674 // with the root class.
675 if (!Method)
676 Method = lookupPrivateMethod(Sel, true);
677 }
678
Steve Naroffbb69c942009-10-01 23:46:04 +0000679 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000680 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000681 return Method;
682}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000683
684//===----------------------------------------------------------------------===//
685// ObjCMethodDecl
686//===----------------------------------------------------------------------===//
687
Alp Toker314cc812014-01-25 16:55:45 +0000688ObjCMethodDecl *ObjCMethodDecl::Create(
689 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
690 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
691 DeclContext *contextDecl, bool isInstance, bool isVariadic,
692 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
693 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000694 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000695 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000696 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
697 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000698}
699
Douglas Gregor72172e92012-01-05 21:55:30 +0000700ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000701 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000702 Selector(), QualType(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000703}
704
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000705bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
706 return getMethodFamily() == OMF_init &&
707 hasAttr<ObjCDesignatedInitializerAttr>();
708}
709
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000710bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
711 const ObjCMethodDecl **InitMethod) const {
712 if (getMethodFamily() != OMF_init)
713 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000714 const DeclContext *DC = getDeclContext();
715 if (isa<ObjCProtocolDecl>(DC))
716 return false;
717 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000718 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000719 return false;
720}
721
Douglas Gregora6017bb2012-10-09 17:21:28 +0000722Stmt *ObjCMethodDecl::getBody() const {
723 return Body.get(getASTContext().getExternalSource());
724}
725
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000726void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
727 assert(PrevMethod);
728 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
729 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000730 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000731}
732
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000733void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
734 ArrayRef<ParmVarDecl*> Params,
735 ArrayRef<SourceLocation> SelLocs) {
Craig Topper36250ad2014-05-12 05:36:57 +0000736 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000737 NumParams = Params.size();
738 if (Params.empty() && SelLocs.empty())
739 return;
740
741 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
742 sizeof(SourceLocation) * SelLocs.size();
743 ParamsAndSelLocs = C.Allocate(Size);
744 std::copy(Params.begin(), Params.end(), getParams());
745 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
746}
747
748void ObjCMethodDecl::getSelectorLocs(
749 SmallVectorImpl<SourceLocation> &SelLocs) const {
750 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
751 SelLocs.push_back(getSelectorLoc(i));
752}
753
754void ObjCMethodDecl::setMethodParams(ASTContext &C,
755 ArrayRef<ParmVarDecl*> Params,
756 ArrayRef<SourceLocation> SelLocs) {
757 assert((!SelLocs.empty() || isImplicit()) &&
758 "No selector locs for non-implicit method");
759 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000760 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000761
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000762 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
763 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000764 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000765 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000766
767 setParamsAndSelLocs(C, Params, SelLocs);
768}
769
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000770/// \brief A definition will return its interface declaration.
771/// An interface declaration will return its definition.
772/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000773ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000774 ASTContext &Ctx = getASTContext();
Craig Topper36250ad2014-05-12 05:36:57 +0000775 ObjCMethodDecl *Redecl = nullptr;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000776 if (HasRedeclaration)
777 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000778 if (Redecl)
779 return Redecl;
780
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000781 Decl *CtxD = cast<Decl>(getDeclContext());
782
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000783 if (!CtxD->isInvalidDecl()) {
784 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
785 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
786 if (!ImplD->isInvalidDecl())
787 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000788
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000789 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
790 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
791 if (!ImplD->isInvalidDecl())
792 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000793
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000794 } else if (ObjCImplementationDecl *ImplD =
795 dyn_cast<ObjCImplementationDecl>(CtxD)) {
796 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
797 if (!IFD->isInvalidDecl())
798 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000799
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000800 } else if (ObjCCategoryImplDecl *CImplD =
801 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
802 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
803 if (!CatD->isInvalidDecl())
804 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
805 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000806 }
807
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000808 if (!Redecl && isRedeclaration()) {
809 // This is the last redeclaration, go back to the first method.
810 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
811 isInstanceMethod());
812 }
813
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000814 return Redecl ? Redecl : this;
815}
816
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000817ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
818 Decl *CtxD = cast<Decl>(getDeclContext());
819
820 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
821 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
822 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
823 isInstanceMethod()))
824 return MD;
825
826 } else if (ObjCCategoryImplDecl *CImplD =
827 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000828 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000829 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
830 isInstanceMethod()))
831 return MD;
832 }
833
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000834 if (isRedeclaration())
835 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
836 isInstanceMethod());
837
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000838 return this;
839}
840
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000841SourceLocation ObjCMethodDecl::getLocEnd() const {
842 if (Stmt *Body = getBody())
843 return Body->getLocEnd();
844 return DeclEndLoc;
845}
846
John McCallb4526252011-03-02 01:50:55 +0000847ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
848 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000849 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000850 return family;
851
John McCall86bc21f2011-03-02 11:33:24 +0000852 // Check for an explicit attribute.
853 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
854 // The unfortunate necessity of mapping between enums here is due
855 // to the attributes framework.
856 switch (attr->getFamily()) {
857 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
858 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
859 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
860 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
861 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
862 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
863 }
864 Family = static_cast<unsigned>(family);
865 return family;
866 }
867
John McCallb4526252011-03-02 01:50:55 +0000868 family = getSelector().getMethodFamily();
869 switch (family) {
870 case OMF_None: break;
871
872 // init only has a conventional meaning for an instance method, and
873 // it has to return an object.
874 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000875 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000876 family = OMF_None;
877 break;
878
879 // alloc/copy/new have a conventional meaning for both class and
880 // instance methods, but they require an object return.
881 case OMF_alloc:
882 case OMF_copy:
883 case OMF_mutableCopy:
884 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000885 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000886 family = OMF_None;
887 break;
888
889 // These selectors have a conventional meaning only for instance methods.
890 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000891 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000892 case OMF_retain:
893 case OMF_release:
894 case OMF_autorelease:
895 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000896 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000897 if (!isInstanceMethod())
898 family = OMF_None;
899 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000900
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000901 case OMF_initialize:
902 if (isInstanceMethod() || !getReturnType()->isVoidType())
903 family = OMF_None;
904 break;
905
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000906 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000907 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000908 family = OMF_None;
909 else {
910 unsigned noParams = param_size();
911 if (noParams < 1 || noParams > 3)
912 family = OMF_None;
913 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000914 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000915 QualType ArgT = (*it);
916 if (!ArgT->isObjCSelType()) {
917 family = OMF_None;
918 break;
919 }
920 while (--noParams) {
921 it++;
922 ArgT = (*it);
923 if (!ArgT->isObjCIdType()) {
924 family = OMF_None;
925 break;
926 }
927 }
928 }
929 }
930 break;
931
John McCallb4526252011-03-02 01:50:55 +0000932 }
933
934 // Cache the result.
935 Family = static_cast<unsigned>(family);
936 return family;
937}
938
Mike Stump11289f42009-09-09 15:08:12 +0000939void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000940 const ObjCInterfaceDecl *OID) {
941 QualType selfTy;
942 if (isInstanceMethod()) {
943 // There may be no interface context due to error in declaration
944 // of the interface (which has been reported). Recover gracefully.
945 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000946 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000947 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000948 } else {
949 selfTy = Context.getObjCIdType();
950 }
951 } else // we have a factory method.
952 selfTy = Context.getObjCClassType();
953
John McCalld4631322011-06-17 06:42:21 +0000954 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000955 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000956
David Blaikiebbafb8a2012-03-11 07:00:24 +0000957 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000958 if (isInstanceMethod()) {
959 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000960
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000961 // 'self' is always __strong. It's actually pseudo-strong except
962 // in init methods (or methods labeled ns_consumes_self), though.
963 Qualifiers qs;
964 qs.setObjCLifetime(Qualifiers::OCL_Strong);
965 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000966
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000967 // In addition, 'self' is const unless this is an init method.
968 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
969 selfTy = selfTy.withConst();
970 selfIsPseudoStrong = true;
971 }
972 }
973 else {
974 assert(isClassMethod());
975 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000976 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000977 selfIsPseudoStrong = true;
978 }
John McCall31168b02011-06-15 23:02:42 +0000979 }
980
981 ImplicitParamDecl *self
982 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
983 &Context.Idents.get("self"), selfTy);
984 setSelfDecl(self);
985
986 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000987 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000988
John McCalld4631322011-06-17 06:42:21 +0000989 if (selfIsPseudoStrong)
990 self->setARCPseudoStrong(true);
991
Mike Stump11289f42009-09-09 15:08:12 +0000992 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
993 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000994 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000995}
996
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000997ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
998 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
999 return ID;
1000 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1001 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +00001002 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001003 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +00001004 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +00001005 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +00001006 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001007}
1008
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001009SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1010 const auto *TSI = getReturnTypeSourceInfo();
1011 if (TSI)
1012 return TSI->getTypeLoc().getSourceRange();
1013 return SourceRange();
1014}
1015
Douglas Gregore83b9562015-07-07 03:57:53 +00001016QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1017 // FIXME: Handle related result types here.
1018
1019 return getReturnType().getNonLValueExprType(getASTContext())
1020 .substObjCMemberType(receiverType, getDeclContext(),
1021 ObjCSubstitutionContext::Result);
1022}
1023
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001024static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1025 const ObjCMethodDecl *Method,
1026 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1027 bool MovedToSuper) {
1028 if (!Container)
1029 return;
1030
1031 // In categories look for overriden methods from protocols. A method from
1032 // category is not "overriden" since it is considered as the "same" method
1033 // (same USR) as the one from the interface.
1034 if (const ObjCCategoryDecl *
1035 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1036 // Check whether we have a matching method at this category but only if we
1037 // are at the super class level.
1038 if (MovedToSuper)
1039 if (ObjCMethodDecl *
1040 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001041 Method->isInstanceMethod(),
1042 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001043 if (Method != Overridden) {
1044 // We found an override at this category; there is no need to look
1045 // into its protocols.
1046 Methods.push_back(Overridden);
1047 return;
1048 }
1049
Aaron Ballman19a41762014-03-14 12:55:57 +00001050 for (const auto *P : Category->protocols())
1051 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001052 return;
1053 }
1054
1055 // Check whether we have a matching method at this level.
1056 if (const ObjCMethodDecl *
1057 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001058 Method->isInstanceMethod(),
1059 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001060 if (Method != Overridden) {
1061 // We found an override at this level; there is no need to look
1062 // into other protocols or categories.
1063 Methods.push_back(Overridden);
1064 return;
1065 }
1066
1067 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001068 for (const auto *P : Protocol->protocols())
1069 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001070 }
1071
1072 if (const ObjCInterfaceDecl *
1073 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001074 for (const auto *P : Interface->protocols())
1075 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001076
Aaron Ballman15063e12014-03-13 21:35:02 +00001077 for (const auto *Cat : Interface->known_categories())
1078 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001079
1080 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1081 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1082 /*MovedToSuper=*/true);
1083 }
1084}
1085
1086static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1087 const ObjCMethodDecl *Method,
1088 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1089 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1090 /*MovedToSuper=*/false);
1091}
1092
1093static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1094 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1095 assert(Method->isOverriding());
1096
1097 if (const ObjCProtocolDecl *
1098 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1099 CollectOverriddenMethods(ProtD, Method, overridden);
1100
1101 } else if (const ObjCImplDecl *
1102 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1103 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1104 if (!ID)
1105 return;
1106 // Start searching for overridden methods using the method from the
1107 // interface as starting point.
1108 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001109 Method->isInstanceMethod(),
1110 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001111 Method = IFaceMeth;
1112 CollectOverriddenMethods(ID, Method, overridden);
1113
1114 } else if (const ObjCCategoryDecl *
1115 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1116 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1117 if (!ID)
1118 return;
1119 // Start searching for overridden methods using the method from the
1120 // interface as starting point.
1121 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001122 Method->isInstanceMethod(),
1123 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001124 Method = IFaceMeth;
1125 CollectOverriddenMethods(ID, Method, overridden);
1126
1127 } else {
1128 CollectOverriddenMethods(
1129 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1130 Method, overridden);
1131 }
1132}
1133
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001134void ObjCMethodDecl::getOverriddenMethods(
1135 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1136 const ObjCMethodDecl *Method = this;
1137
1138 if (Method->isRedeclaration()) {
1139 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1140 getMethod(Method->getSelector(), Method->isInstanceMethod());
1141 }
1142
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001143 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001144 collectOverriddenMethodsSlow(Method, Overridden);
1145 assert(!Overridden.empty() &&
1146 "ObjCMethodDecl's overriding bit is not as expected");
1147 }
1148}
1149
Jordan Rose2bd991a2012-10-10 16:42:54 +00001150const ObjCPropertyDecl *
1151ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1152 Selector Sel = getSelector();
1153 unsigned NumArgs = Sel.getNumArgs();
1154 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001155 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001156
Jordan Rose16ba5532015-01-16 23:04:26 +00001157 if (!isInstanceMethod())
Craig Topper36250ad2014-05-12 05:36:57 +00001158 return nullptr;
1159
Jordan Rose2bd991a2012-10-10 16:42:54 +00001160 if (isPropertyAccessor()) {
1161 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001162 // If container is class extension, find its primary class.
1163 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1164 if (CatDecl->IsClassExtension())
1165 Container = CatDecl->getClassInterface();
1166
Jordan Rose2bd991a2012-10-10 16:42:54 +00001167 bool IsGetter = (NumArgs == 0);
1168
Aaron Ballmand174edf2014-03-13 19:11:50 +00001169 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001170 Selector NextSel = IsGetter ? I->getGetterName()
1171 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001172 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001173 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001174 }
1175
1176 llvm_unreachable("Marked as a property accessor but no property found!");
1177 }
1178
1179 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001180 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001181
1182 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1183 OverridesTy Overrides;
1184 getOverriddenMethods(Overrides);
1185 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1186 I != E; ++I) {
1187 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1188 return Prop;
1189 }
1190
Craig Topper36250ad2014-05-12 05:36:57 +00001191 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001192}
1193
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001194//===----------------------------------------------------------------------===//
Douglas Gregor85f3f952015-07-07 03:57:15 +00001195// ObjCTypeParamDecl
1196//===----------------------------------------------------------------------===//
1197
1198void ObjCTypeParamDecl::anchor() { }
1199
1200ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
Douglas Gregore83b9562015-07-07 03:57:53 +00001201 unsigned index,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001202 SourceLocation nameLoc,
1203 IdentifierInfo *name,
1204 SourceLocation colonLoc,
1205 TypeSourceInfo *boundInfo) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001206 return new (ctx, dc) ObjCTypeParamDecl(ctx, dc, index, nameLoc, name, colonLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001207 boundInfo);
1208}
1209
1210ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1211 unsigned ID) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001212 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr, 0, SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001213 nullptr, SourceLocation(), nullptr);
1214}
1215
1216SourceRange ObjCTypeParamDecl::getSourceRange() const {
1217 if (hasExplicitBound()) {
1218 return SourceRange(getLocation(),
1219 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1220 }
1221
1222 return SourceRange(getLocation());
1223}
1224
1225//===----------------------------------------------------------------------===//
1226// ObjCTypeParamList
1227//===----------------------------------------------------------------------===//
1228ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1229 ArrayRef<ObjCTypeParamDecl *> typeParams,
1230 SourceLocation rAngleLoc)
1231 : Brackets(lAngleLoc, rAngleLoc), NumParams(typeParams.size())
1232{
1233 std::copy(typeParams.begin(), typeParams.end(), begin());
1234}
1235
1236
1237ObjCTypeParamList *ObjCTypeParamList::create(
1238 ASTContext &ctx,
1239 SourceLocation lAngleLoc,
1240 ArrayRef<ObjCTypeParamDecl *> typeParams,
1241 SourceLocation rAngleLoc) {
1242 unsigned size = sizeof(ObjCTypeParamList)
1243 + sizeof(ObjCTypeParamDecl *) * typeParams.size();
1244 static_assert(alignof(ObjCTypeParamList) >= alignof(ObjCTypeParamDecl*),
1245 "type parameter list needs greater alignment");
1246 unsigned align = llvm::alignOf<ObjCTypeParamList>();
1247 void *mem = ctx.Allocate(size, align);
1248 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1249}
1250
Douglas Gregore83b9562015-07-07 03:57:53 +00001251void ObjCTypeParamList::gatherDefaultTypeArgs(
1252 SmallVectorImpl<QualType> &typeArgs) const {
1253 typeArgs.reserve(size());
1254 for (auto typeParam : *this)
1255 typeArgs.push_back(typeParam->getUnderlyingType());
1256}
1257
Douglas Gregor85f3f952015-07-07 03:57:15 +00001258//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001259// ObjCInterfaceDecl
1260//===----------------------------------------------------------------------===//
1261
Douglas Gregord53ae832012-01-17 18:09:05 +00001262ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001263 DeclContext *DC,
1264 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001265 IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001266 ObjCTypeParamList *typeParamList,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001267 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001268 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001269 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001270 ObjCInterfaceDecl *Result = new (C, DC)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001271 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1272 isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001273 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001274 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001275 return Result;
1276}
1277
Richard Smith053f6c62014-05-16 23:01:30 +00001278ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001279 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001280 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001281 SourceLocation(),
1282 nullptr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001283 nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001284 SourceLocation(),
1285 nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001286 Result->Data.setInt(!C.getLangOpts().Modules);
1287 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001288}
1289
Richard Smith053f6c62014-05-16 23:01:30 +00001290ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1291 SourceLocation AtLoc, IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001292 ObjCTypeParamList *typeParamList,
Richard Smith053f6c62014-05-16 23:01:30 +00001293 SourceLocation CLoc,
1294 ObjCInterfaceDecl *PrevDecl,
1295 bool IsInternal)
1296 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001297 redeclarable_base(C), TypeForDecl(nullptr), TypeParamList(typeParamList),
1298 Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001299 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001300
1301 // Copy the 'data' pointer over.
1302 if (PrevDecl)
1303 Data = PrevDecl->Data;
1304
Richard Smith053f6c62014-05-16 23:01:30 +00001305 setImplicit(IsInternal);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001306
1307 // Update the declaration context of the type parameters.
1308 if (typeParamList) {
1309 for (auto typeParam : *typeParamList)
1310 typeParam->setDeclContext(this);
1311 }
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001312}
1313
Douglas Gregor73693022010-12-01 23:49:52 +00001314void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001315 assert(data().ExternallyCompleted && "Class is not externally completed");
1316 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001317 getASTContext().getExternalSource()->CompleteType(
1318 const_cast<ObjCInterfaceDecl *>(this));
1319}
1320
1321void ObjCInterfaceDecl::setExternallyCompleted() {
1322 assert(getASTContext().getExternalSource() &&
1323 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001324 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001325 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001326 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001327}
1328
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001329void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001330 // Check for a complete definition and recover if not so.
1331 if (!isThisDeclarationADefinition())
1332 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001333 data().HasDesignatedInitializers = true;
1334}
1335
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001336bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001337 // Check for a complete definition and recover if not so.
1338 if (!isThisDeclarationADefinition())
1339 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001340 if (data().ExternallyCompleted)
1341 LoadExternalDefinition();
1342
1343 return data().HasDesignatedInitializers;
1344}
1345
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001346StringRef
1347ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001348 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1349 return ObjCRTName->getMetadataName();
1350
1351 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001352}
1353
1354StringRef
1355ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001356 if (ObjCInterfaceDecl *ID =
1357 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1358 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001359
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001360 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001361}
1362
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001363ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001364 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1365 if (data().ExternallyCompleted)
1366 LoadExternalDefinition();
1367
1368 return getASTContext().getObjCImplementation(
1369 const_cast<ObjCInterfaceDecl*>(Def));
1370 }
1371
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001372 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001373 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001374}
1375
1376void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001377 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001378}
1379
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001380namespace {
1381 struct SynthesizeIvarChunk {
1382 uint64_t Size;
1383 ObjCIvarDecl *Ivar;
1384 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1385 : Size(size), Ivar(ivar) {}
1386 };
1387
1388 bool operator<(const SynthesizeIvarChunk & LHS,
1389 const SynthesizeIvarChunk &RHS) {
1390 return LHS.Size < RHS.Size;
1391 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001392}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001393
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001394/// all_declared_ivar_begin - return first ivar declared in this class,
1395/// its extensions and its implementation. Lazily build the list on first
1396/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001397///
1398/// Caveat: The list returned by this method reflects the current
1399/// state of the parser. The cache will be updated for every ivar
1400/// added by an extension or the implementation when they are
1401/// encountered.
1402/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001403ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001404 // FIXME: Should make sure no callers ever do this.
1405 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001406 return nullptr;
1407
1408 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001409 if (!data().IvarList) {
1410 if (!ivar_empty()) {
1411 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1412 data().IvarList = *I; ++I;
1413 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001414 curIvar->setNextIvar(*I);
1415 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001416
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001417 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001418 if (!Ext->ivar_empty()) {
1419 ObjCCategoryDecl::ivar_iterator
1420 I = Ext->ivar_begin(),
1421 E = Ext->ivar_end();
1422 if (!data().IvarList) {
1423 data().IvarList = *I; ++I;
1424 curIvar = data().IvarList;
1425 }
1426 for ( ;I != E; curIvar = *I, ++I)
1427 curIvar->setNextIvar(*I);
1428 }
1429 }
1430 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001431 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001432
1433 // cached and complete!
1434 if (!data().IvarListMissingImplementation)
1435 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001436
1437 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001438 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001439 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001440 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001441 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001442 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1443 layout.push_back(SynthesizeIvarChunk(
1444 IV->getASTContext().getTypeSize(IV->getType()), IV));
1445 continue;
1446 }
1447 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001448 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001449 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001450 curIvar->setNextIvar(IV);
1451 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001452 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001453
1454 if (!layout.empty()) {
1455 // Order synthesized ivars by their size.
1456 std::stable_sort(layout.begin(), layout.end());
1457 unsigned Ix = 0, EIx = layout.size();
1458 if (!data().IvarList) {
1459 data().IvarList = layout[0].Ivar; Ix++;
1460 curIvar = data().IvarList;
1461 }
1462 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1463 curIvar->setNextIvar(layout[Ix].Ivar);
1464 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001465 }
1466 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001467 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001468}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001469
1470/// FindCategoryDeclaration - Finds category declaration in the list of
1471/// categories for this class and returns it. Name of the category is passed
1472/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001473///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001474ObjCCategoryDecl *
1475ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001476 // FIXME: Should make sure no callers ever do this.
1477 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001478 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001479
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001480 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001481 LoadExternalDefinition();
1482
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001483 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001484 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001485 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001486
1487 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001488}
1489
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001490ObjCMethodDecl *
1491ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001492 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001493 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001494 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1495 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001496 }
1497
Craig Topper36250ad2014-05-12 05:36:57 +00001498 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001499}
1500
1501ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001502 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001503 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001504 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1505 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001506 }
Craig Topper36250ad2014-05-12 05:36:57 +00001507
1508 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001509}
1510
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001511/// ClassImplementsProtocol - Checks that 'lProto' protocol
1512/// has been implemented in IDecl class, its super class or categories (if
1513/// lookupCategory is true).
1514bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1515 bool lookupCategory,
1516 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001517 if (!hasDefinition())
1518 return false;
1519
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001520 ObjCInterfaceDecl *IDecl = this;
1521 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001522 for (auto *PI : IDecl->protocols()){
1523 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001524 return true;
1525 // This is dubious and is added to be compatible with gcc. In gcc, it is
1526 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1527 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1528 // object. This IMO, should be a bug.
1529 // FIXME: Treat this as an extension, and flag this as an error when GCC
1530 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001531 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001532 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001533 return true;
1534 }
Mike Stump11289f42009-09-09 15:08:12 +00001535
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001536 // 2nd, look up the category.
1537 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001538 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001539 for (auto *PI : Cat->protocols())
1540 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001541 return true;
1542 }
Mike Stump11289f42009-09-09 15:08:12 +00001543
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001544 // 3rd, look up the super class(s)
1545 if (IDecl->getSuperClass())
1546 return
1547 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1548 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001549
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001550 return false;
1551}
1552
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001553//===----------------------------------------------------------------------===//
1554// ObjCIvarDecl
1555//===----------------------------------------------------------------------===//
1556
David Blaikie68e081d2011-12-20 02:48:34 +00001557void ObjCIvarDecl::anchor() { }
1558
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001559ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001560 SourceLocation StartLoc,
1561 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001562 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001563 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001564 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001565 if (DC) {
1566 // Ivar's can only appear in interfaces, implementations (via synthesized
1567 // properties), and class extensions (via direct declaration, or synthesized
1568 // properties).
1569 //
1570 // FIXME: This should really be asserting this:
1571 // (isa<ObjCCategoryDecl>(DC) &&
1572 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1573 // but unfortunately we sometimes place ivars into non-class extension
1574 // categories on error. This breaks an AST invariant, and should not be
1575 // fixed.
1576 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1577 isa<ObjCCategoryDecl>(DC)) &&
1578 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001579 // Once a new ivar is created in any of class/class-extension/implementation
1580 // decl contexts, the previously built IvarList must be rebuilt.
1581 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1582 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001583 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001584 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001585 else
1586 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001587 }
Craig Topper36250ad2014-05-12 05:36:57 +00001588 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001589 }
1590
Richard Smithf7981722013-11-22 09:01:48 +00001591 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001592 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001593}
1594
Douglas Gregor72172e92012-01-05 21:55:30 +00001595ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001596 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1597 nullptr, QualType(), nullptr,
1598 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001599}
1600
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001601const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1602 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001603
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001604 switch (DC->getKind()) {
1605 default:
1606 case ObjCCategoryImpl:
1607 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001608 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001609
1610 // Ivars can only appear in class extension categories.
1611 case ObjCCategory: {
1612 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1613 assert(CD->IsClassExtension() && "invalid container for ivar!");
1614 return CD->getClassInterface();
1615 }
1616
1617 case ObjCImplementation:
1618 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1619
1620 case ObjCInterface:
1621 return cast<ObjCInterfaceDecl>(DC);
1622 }
1623}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001624
Douglas Gregore83b9562015-07-07 03:57:53 +00001625QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1626 return getType().substObjCMemberType(objectType, getDeclContext(),
1627 ObjCSubstitutionContext::Property);
1628}
1629
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001630//===----------------------------------------------------------------------===//
1631// ObjCAtDefsFieldDecl
1632//===----------------------------------------------------------------------===//
1633
David Blaikie68e081d2011-12-20 02:48:34 +00001634void ObjCAtDefsFieldDecl::anchor() { }
1635
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001636ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001637*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1638 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001639 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001640 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001641}
1642
Richard Smithf7981722013-11-22 09:01:48 +00001643ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001644 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001645 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1646 SourceLocation(), nullptr, QualType(),
1647 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001648}
1649
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001650//===----------------------------------------------------------------------===//
1651// ObjCProtocolDecl
1652//===----------------------------------------------------------------------===//
1653
David Blaikie68e081d2011-12-20 02:48:34 +00001654void ObjCProtocolDecl::anchor() { }
1655
Richard Smith053f6c62014-05-16 23:01:30 +00001656ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1657 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001658 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001659 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001660 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1661 redeclarable_base(C), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001662 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001663 if (PrevDecl)
1664 Data = PrevDecl->Data;
1665}
1666
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001667ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001668 IdentifierInfo *Id,
1669 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001670 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001671 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001672 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001673 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001674 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001675 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001676}
1677
Richard Smithf7981722013-11-22 09:01:48 +00001678ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001679 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001680 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001681 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001682 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001683 Result->Data.setInt(!C.getLangOpts().Modules);
1684 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001685}
1686
Steve Naroff114aecb2009-03-01 16:12:44 +00001687ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1688 ObjCProtocolDecl *PDecl = this;
1689
1690 if (Name == getIdentifier())
1691 return PDecl;
1692
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001693 for (auto *I : protocols())
1694 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001695 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001696
Craig Topper36250ad2014-05-12 05:36:57 +00001697 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001698}
1699
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001700// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001701// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001702ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1703 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001704 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001705
Douglas Gregoreed49792013-01-17 00:38:46 +00001706 // If there is no definition or the definition is hidden, we don't find
1707 // anything.
1708 const ObjCProtocolDecl *Def = getDefinition();
1709 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001710 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001711
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001712 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001713 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001714
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001715 for (const auto *I : protocols())
1716 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001717 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001718 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001719}
1720
Douglas Gregore6e48b12012-01-01 19:29:29 +00001721void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001722 assert(!Data.getPointer() && "Protocol already has a definition!");
1723 Data.setPointer(new (getASTContext()) DefinitionData);
1724 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001725}
1726
1727void ObjCProtocolDecl::startDefinition() {
1728 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001729
1730 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001731 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001732 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001733}
1734
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001735void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1736 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001737
1738 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001739 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001740 // Insert into PM if not there already.
1741 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001742 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001743 }
1744 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001745 for (const auto *PI : PDecl->protocols())
1746 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001747 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001748}
1749
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001750
1751void ObjCProtocolDecl::collectInheritedProtocolProperties(
1752 const ObjCPropertyDecl *Property,
1753 ProtocolPropertyMap &PM) const {
1754 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1755 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001756 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001757 if (Prop == Property)
1758 continue;
1759 if (Prop->getIdentifier() == Property->getIdentifier()) {
1760 PM[PDecl] = Prop;
1761 MatchFound = true;
1762 break;
1763 }
1764 }
1765 // Scan through protocol's protocols which did not have a matching property.
1766 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001767 for (const auto *PI : PDecl->protocols())
1768 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001769 }
1770}
Anna Zaks673d76b2012-10-18 19:17:53 +00001771
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001772StringRef
1773ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001774 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1775 return ObjCRTName->getMetadataName();
1776
1777 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001778}
1779
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001780//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001781// ObjCCategoryDecl
1782//===----------------------------------------------------------------------===//
1783
David Blaikie68e081d2011-12-20 02:48:34 +00001784void ObjCCategoryDecl::anchor() { }
1785
Douglas Gregor85f3f952015-07-07 03:57:15 +00001786ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1787 SourceLocation ClassNameLoc,
1788 SourceLocation CategoryNameLoc,
1789 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1790 ObjCTypeParamList *typeParamList,
1791 SourceLocation IvarLBraceLoc,
1792 SourceLocation IvarRBraceLoc)
1793 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1794 ClassInterface(IDecl), TypeParamList(typeParamList),
1795 NextClassCategory(nullptr), CategoryNameLoc(CategoryNameLoc),
1796 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc)
1797{
1798 // Set the declaration context of each of the type parameters.
1799 if (typeParamList) {
1800 for (auto typeParam : *typeParamList) {
1801 typeParam->setDeclContext(this);
1802 }
1803 }
1804}
1805
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001806ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001807 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001808 SourceLocation ClassNameLoc,
1809 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001810 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001811 ObjCInterfaceDecl *IDecl,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001812 ObjCTypeParamList *typeParamList,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001813 SourceLocation IvarLBraceLoc,
1814 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001815 ObjCCategoryDecl *CatDecl =
1816 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001817 IDecl, typeParamList, IvarLBraceLoc,
1818 IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001819 if (IDecl) {
1820 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001821 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001822 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001823 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001824 if (ASTMutationListener *L = C.getASTMutationListener())
1825 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1826 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001827 }
1828
1829 return CatDecl;
1830}
1831
Richard Smithf7981722013-11-22 09:01:48 +00001832ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001833 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001834 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1835 SourceLocation(), SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001836 nullptr, nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001837}
1838
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001839ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1840 return getASTContext().getObjCImplementation(
1841 const_cast<ObjCCategoryDecl*>(this));
1842}
1843
1844void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1845 getASTContext().setObjCImplementation(this, ImplD);
1846}
1847
1848
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001849//===----------------------------------------------------------------------===//
1850// ObjCCategoryImplDecl
1851//===----------------------------------------------------------------------===//
1852
David Blaikie68e081d2011-12-20 02:48:34 +00001853void ObjCCategoryImplDecl::anchor() { }
1854
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001855ObjCCategoryImplDecl *
1856ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001857 IdentifierInfo *Id,
1858 ObjCInterfaceDecl *ClassInterface,
1859 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001860 SourceLocation atStartLoc,
1861 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001862 if (ClassInterface && ClassInterface->hasDefinition())
1863 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001864 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1865 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001866}
1867
Douglas Gregor72172e92012-01-05 21:55:30 +00001868ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1869 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001870 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1871 SourceLocation(), SourceLocation(),
1872 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001873}
1874
Steve Narofff406f4d2009-10-29 21:11:04 +00001875ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001876 // The class interface might be NULL if we are working with invalid code.
1877 if (const ObjCInterfaceDecl *ID = getClassInterface())
1878 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00001879 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001880}
1881
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001882
David Blaikie68e081d2011-12-20 02:48:34 +00001883void ObjCImplDecl::anchor() { }
1884
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001885void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001886 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001887 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001888 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001889}
1890
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001891void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1892 ASTContext &Ctx = getASTContext();
1893
1894 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001895 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001896 if (IFace)
1897 Ctx.setObjCImplementation(IFace, ImplD);
1898
Duncan Sands49c29ee2009-07-21 07:56:29 +00001899 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001900 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1901 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1902 Ctx.setObjCImplementation(CD, ImplD);
1903 }
1904
1905 ClassInterface = IFace;
1906}
1907
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001908/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001909/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001910/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001911///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001912ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001913FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001914 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001915 if (PID->getPropertyIvarDecl() &&
1916 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1917 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001918 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001919}
1920
1921/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001922/// added to the list of those properties \@synthesized/\@dynamic in this
1923/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001924///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001925ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001926FindPropertyImplDecl(IdentifierInfo *Id) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001927 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001928 if (PID->getPropertyDecl()->getIdentifier() == Id)
1929 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001930 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001931}
1932
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001933raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001934 const ObjCCategoryImplDecl &CID) {
1935 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001936 return OS;
1937}
1938
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001939//===----------------------------------------------------------------------===//
1940// ObjCImplementationDecl
1941//===----------------------------------------------------------------------===//
1942
David Blaikie68e081d2011-12-20 02:48:34 +00001943void ObjCImplementationDecl::anchor() { }
1944
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001945ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001946ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001947 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001948 ObjCInterfaceDecl *SuperDecl,
1949 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001950 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001951 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001952 SourceLocation IvarLBraceLoc,
1953 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001954 if (ClassInterface && ClassInterface->hasDefinition())
1955 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001956 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1957 nameLoc, atStartLoc, superLoc,
1958 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001959}
1960
Douglas Gregor72172e92012-01-05 21:55:30 +00001961ObjCImplementationDecl *
1962ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001963 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
1964 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001965}
1966
John McCall0410e572011-07-22 04:15:06 +00001967void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1968 CXXCtorInitializer ** initializers,
1969 unsigned numInitializers) {
1970 if (numInitializers > 0) {
1971 NumIvarInitializers = numInitializers;
1972 CXXCtorInitializer **ivarInitializers =
1973 new (C) CXXCtorInitializer*[NumIvarInitializers];
1974 memcpy(ivarInitializers, initializers,
1975 numInitializers * sizeof(CXXCtorInitializer*));
1976 IvarInitializers = ivarInitializers;
1977 }
1978}
1979
Richard Smithc2bb8182015-03-24 06:36:48 +00001980ObjCImplementationDecl::init_const_iterator
1981ObjCImplementationDecl::init_begin() const {
1982 return IvarInitializers.get(getASTContext().getExternalSource());
1983}
1984
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001985raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001986 const ObjCImplementationDecl &ID) {
1987 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001988 return OS;
1989}
1990
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001991//===----------------------------------------------------------------------===//
1992// ObjCCompatibleAliasDecl
1993//===----------------------------------------------------------------------===//
1994
David Blaikie68e081d2011-12-20 02:48:34 +00001995void ObjCCompatibleAliasDecl::anchor() { }
1996
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001997ObjCCompatibleAliasDecl *
1998ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1999 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00002000 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002001 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00002002 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002003}
2004
Douglas Gregor72172e92012-01-05 21:55:30 +00002005ObjCCompatibleAliasDecl *
2006ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002007 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2008 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002009}
2010
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002011//===----------------------------------------------------------------------===//
2012// ObjCPropertyDecl
2013//===----------------------------------------------------------------------===//
2014
David Blaikie68e081d2011-12-20 02:48:34 +00002015void ObjCPropertyDecl::anchor() { }
2016
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002017ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2018 SourceLocation L,
2019 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00002020 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002021 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00002022 QualType T,
2023 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002024 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002025 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2026 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002027}
2028
Richard Smithf7981722013-11-22 09:01:48 +00002029ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002030 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002031 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2032 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00002033 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00002034}
2035
Douglas Gregore83b9562015-07-07 03:57:53 +00002036QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2037 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2038 ObjCSubstitutionContext::Property);
2039}
2040
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002041//===----------------------------------------------------------------------===//
2042// ObjCPropertyImplDecl
2043//===----------------------------------------------------------------------===//
2044
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002045ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002046 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002047 SourceLocation atLoc,
2048 SourceLocation L,
2049 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002050 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002051 ObjCIvarDecl *ivar,
2052 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002053 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2054 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002055}
Chris Lattnered0e1642008-03-17 01:19:02 +00002056
Richard Smithf7981722013-11-22 09:01:48 +00002057ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002058 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002059 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2060 SourceLocation(), nullptr, Dynamic,
2061 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002062}
2063
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002064SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2065 SourceLocation EndLoc = getLocation();
2066 if (IvarLoc.isValid())
2067 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002068
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002069 return SourceRange(AtLoc, EndLoc);
2070}