blob: da4bc549cf4275db0374d4d96c660e834c42f0ec [file] [log] [blame]
Chris Lattner89375192008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000016#include "clang/AST/ASTMutationListener.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/Stmt.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000019#include "llvm/ADT/STLExtras.h"
Anna Zaks454477c2012-09-27 19:45:11 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner89375192008-03-16 00:19:01 +000021using namespace clang;
22
Chris Lattner8d8829e2008-03-16 00:49:28 +000023//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000024// ObjCListBase
25//===----------------------------------------------------------------------===//
26
Chris Lattner22298722009-02-20 21:35:13 +000027void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Craig Topper36250ad2014-05-12 05:36:57 +000028 List = nullptr;
Chris Lattner4d1eb762009-02-20 21:16:26 +000029 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000030
31
Chris Lattner7c981a72009-02-20 21:44:01 +000032 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000033 NumElts = Elts;
34 memcpy(List, InList, sizeof(void*)*Elts);
35}
36
Douglas Gregor002b6712010-01-16 15:02:53 +000037void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
38 const SourceLocation *Locs, ASTContext &Ctx) {
39 if (Elts == 0)
40 return;
41
42 Locations = new (Ctx) SourceLocation[Elts];
43 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
44 set(InList, Elts, Ctx);
45}
46
Chris Lattner4d1eb762009-02-20 21:16:26 +000047//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000048// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000049//===----------------------------------------------------------------------===//
50
David Blaikie68e081d2011-12-20 02:48:34 +000051void ObjCContainerDecl::anchor() { }
52
Fariborz Jahanian68453832009-06-05 18:16:35 +000053/// getIvarDecl - This method looks up an ivar in this ContextDecl.
54///
55ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000056ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Richard Smithcf4bdde2015-02-21 02:45:19 +000057 lookup_result R = lookup(Id);
58 for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000059 Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian68453832009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
Craig Topper36250ad2014-05-12 05:36:57 +000063 return nullptr;
Fariborz Jahanian68453832009-06-05 18:16:35 +000064}
65
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000066// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000067ObjCMethodDecl *
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69 bool AllowHidden) const {
Douglas Gregoreed49792013-01-17 00:38:46 +000070 // If this context is a hidden protocol definition, don't find any
71 // methods there.
72 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
73 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000074 if (Def->isHidden() && !AllowHidden)
Craig Topper36250ad2014-05-12 05:36:57 +000075 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +000076 }
77
Steve Naroffc4173fa2009-02-22 19:35:57 +000078 // Since instance & class methods can have the same name, the loop below
79 // ensures we get the correct method.
80 //
81 // @interface Whatever
82 // - (int) class_method;
83 // + (float) class_method;
84 // @end
85 //
Richard Smithcf4bdde2015-02-21 02:45:19 +000086 lookup_result R = lookup(Sel);
87 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000088 Meth != MethEnd; ++Meth) {
Steve Naroffc4173fa2009-02-22 19:35:57 +000089 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000090 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +000091 return MD;
92 }
Craig Topper36250ad2014-05-12 05:36:57 +000093 return nullptr;
Steve Naroff35c62ae2009-01-08 17:28:14 +000094}
95
Nico Weber4701ffd2014-12-22 05:21:03 +000096/// \brief This routine returns 'true' if a user declared setter method was
97/// found in the class, its protocols, its super classes or categories.
98/// It also returns 'true' if one of its categories has declared a 'readwrite'
99/// property. This is because, user must provide a setter method for the
100/// category's 'readwrite' property.
101bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
102 const ObjCPropertyDecl *Property) const {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000103 Selector Sel = Property->getSetterName();
Richard Smithcf4bdde2015-02-21 02:45:19 +0000104 lookup_result R = lookup(Sel);
105 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000106 Meth != MethEnd; ++Meth) {
107 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
108 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
109 return true;
110 }
111
112 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
113 // Also look into categories, including class extensions, looking
114 // for a user declared instance method.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000115 for (const auto *Cat : ID->visible_categories()) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000116 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
117 if (!MD->isImplicit())
118 return true;
119 if (Cat->IsClassExtension())
120 continue;
Nico Weber4701ffd2014-12-22 05:21:03 +0000121 // Also search through the categories looking for a 'readwrite'
122 // declaration of this property. If one found, presumably a setter will
123 // be provided (properties declared in categories will not get
124 // auto-synthesized).
Aaron Ballmand174edf2014-03-13 19:11:50 +0000125 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000126 if (P->getIdentifier() == Property->getIdentifier()) {
127 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
128 return true;
129 break;
130 }
131 }
132
133 // Also look into protocols, for a user declared instance method.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000134 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000135 if (Proto->HasUserDeclaredSetterMethod(Property))
136 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000137
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000138 // And in its super class.
139 ObjCInterfaceDecl *OSC = ID->getSuperClass();
140 while (OSC) {
141 if (OSC->HasUserDeclaredSetterMethod(Property))
142 return true;
143 OSC = OSC->getSuperClass();
144 }
145 }
146 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000147 for (const auto *PI : PD->protocols())
148 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000149 return true;
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000150 return false;
151}
152
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000153ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000154ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Jordan Rose210bfe92014-11-19 22:03:46 +0000155 const IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000156 // If this context is a hidden protocol definition, don't find any
157 // property.
158 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
159 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
160 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000161 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000162 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000163
Richard Smithcf4bdde2015-02-21 02:45:19 +0000164 DeclContext::lookup_result R = DC->lookup(propertyID);
165 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
David Blaikieff7d47a2012-12-19 00:45:41 +0000166 ++I)
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000167 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
168 return PD;
169
Craig Topper36250ad2014-05-12 05:36:57 +0000170 return nullptr;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000171}
172
Anna Zaks454477c2012-09-27 19:45:11 +0000173IdentifierInfo *
174ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
175 SmallString<128> ivarName;
176 {
177 llvm::raw_svector_ostream os(ivarName);
178 os << '_' << getIdentifier()->getName();
179 }
180 return &Ctx.Idents.get(ivarName.str());
181}
182
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000183/// FindPropertyDeclaration - Finds declaration of the property given its name
184/// in 'PropertyId' and returns it. It returns 0, if not found.
Jordan Rose210bfe92014-11-19 22:03:46 +0000185ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
186 const IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000187 // Don't find properties within hidden protocol definitions.
188 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
189 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
190 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000191 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Ted Kremenekddcd1092010-03-15 20:11:53 +0000194 if (ObjCPropertyDecl *PD =
195 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
196 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Ted Kremenekddcd1092010-03-15 20:11:53 +0000198 switch (getKind()) {
199 default:
200 break;
201 case Decl::ObjCProtocol: {
202 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000203 for (const auto *I : PID->protocols())
204 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000205 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000206 break;
207 }
208 case Decl::ObjCInterface: {
209 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000210 // Look through categories (but not extensions).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000211 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000212 if (!Cat->IsClassExtension())
213 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
214 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000215 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000216
217 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000218 for (const auto *I : OID->all_referenced_protocols())
219 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000220 return P;
221
222 // Finally, check the super class.
223 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
224 return superClass->FindPropertyDeclaration(PropertyId);
225 break;
226 }
227 case Decl::ObjCCategory: {
228 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
229 // Look through protocols.
230 if (!OCD->IsClassExtension())
Aaron Ballman19a41762014-03-14 12:55:57 +0000231 for (const auto *I : OCD->protocols())
232 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
233 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000234 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000235 }
236 }
Craig Topper36250ad2014-05-12 05:36:57 +0000237 return nullptr;
Steve Narofff9c65242008-06-05 13:55:23 +0000238}
239
David Blaikie68e081d2011-12-20 02:48:34 +0000240void ObjCInterfaceDecl::anchor() { }
241
Douglas Gregor85f3f952015-07-07 03:57:15 +0000242ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
243 // If this particular declaration has a type parameter list, return it.
244 if (ObjCTypeParamList *written = getTypeParamListAsWritten())
245 return written;
246
247 // If there is a definition, return its type parameter list.
248 if (const ObjCInterfaceDecl *def = getDefinition())
249 return def->getTypeParamListAsWritten();
250
251 // Otherwise, look at previous declarations to determine whether any
252 // of them has a type parameter list, skipping over those
253 // declarations that do not.
Douglas Gregorc5e07f52015-07-07 03:58:01 +0000254 for (auto decl = getMostRecentDecl(); decl; decl = decl->getPreviousDecl()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000255 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
256 return written;
257 }
258
259 return nullptr;
260}
261
Douglas 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 Gregor1ac1b632015-07-07 03:58:54 +00001201 ObjCTypeParamVariance variance,
1202 SourceLocation varianceLoc,
Douglas Gregore83b9562015-07-07 03:57:53 +00001203 unsigned index,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001204 SourceLocation nameLoc,
1205 IdentifierInfo *name,
1206 SourceLocation colonLoc,
1207 TypeSourceInfo *boundInfo) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001208 return new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1209 nameLoc, name, colonLoc, boundInfo);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001210}
1211
1212ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1213 unsigned ID) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001214 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1215 ObjCTypeParamVariance::Invariant,
1216 SourceLocation(), 0, SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001217 nullptr, SourceLocation(), nullptr);
1218}
1219
1220SourceRange ObjCTypeParamDecl::getSourceRange() const {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001221 SourceLocation startLoc = VarianceLoc;
1222 if (startLoc.isInvalid())
1223 startLoc = getLocation();
1224
Douglas Gregor85f3f952015-07-07 03:57:15 +00001225 if (hasExplicitBound()) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001226 return SourceRange(startLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001227 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1228 }
1229
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001230 return SourceRange(startLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001231}
1232
1233//===----------------------------------------------------------------------===//
1234// ObjCTypeParamList
1235//===----------------------------------------------------------------------===//
1236ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1237 ArrayRef<ObjCTypeParamDecl *> typeParams,
1238 SourceLocation rAngleLoc)
1239 : Brackets(lAngleLoc, rAngleLoc), NumParams(typeParams.size())
1240{
1241 std::copy(typeParams.begin(), typeParams.end(), begin());
1242}
1243
1244
1245ObjCTypeParamList *ObjCTypeParamList::create(
1246 ASTContext &ctx,
1247 SourceLocation lAngleLoc,
1248 ArrayRef<ObjCTypeParamDecl *> typeParams,
1249 SourceLocation rAngleLoc) {
1250 unsigned size = sizeof(ObjCTypeParamList)
1251 + sizeof(ObjCTypeParamDecl *) * typeParams.size();
1252 static_assert(alignof(ObjCTypeParamList) >= alignof(ObjCTypeParamDecl*),
1253 "type parameter list needs greater alignment");
1254 unsigned align = llvm::alignOf<ObjCTypeParamList>();
1255 void *mem = ctx.Allocate(size, align);
1256 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1257}
1258
Douglas Gregore83b9562015-07-07 03:57:53 +00001259void ObjCTypeParamList::gatherDefaultTypeArgs(
1260 SmallVectorImpl<QualType> &typeArgs) const {
1261 typeArgs.reserve(size());
1262 for (auto typeParam : *this)
1263 typeArgs.push_back(typeParam->getUnderlyingType());
1264}
1265
Douglas Gregor85f3f952015-07-07 03:57:15 +00001266//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001267// ObjCInterfaceDecl
1268//===----------------------------------------------------------------------===//
1269
Douglas Gregord53ae832012-01-17 18:09:05 +00001270ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001271 DeclContext *DC,
1272 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001273 IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001274 ObjCTypeParamList *typeParamList,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001275 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001276 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001277 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001278 ObjCInterfaceDecl *Result = new (C, DC)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001279 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1280 isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001281 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001282 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001283 return Result;
1284}
1285
Richard Smith053f6c62014-05-16 23:01:30 +00001286ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001287 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001288 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001289 SourceLocation(),
1290 nullptr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001291 nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001292 SourceLocation(),
1293 nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001294 Result->Data.setInt(!C.getLangOpts().Modules);
1295 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001296}
1297
Richard Smith053f6c62014-05-16 23:01:30 +00001298ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1299 SourceLocation AtLoc, IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001300 ObjCTypeParamList *typeParamList,
Richard Smith053f6c62014-05-16 23:01:30 +00001301 SourceLocation CLoc,
1302 ObjCInterfaceDecl *PrevDecl,
1303 bool IsInternal)
1304 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001305 redeclarable_base(C), TypeForDecl(nullptr), TypeParamList(typeParamList),
1306 Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001307 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001308
1309 // Copy the 'data' pointer over.
1310 if (PrevDecl)
1311 Data = PrevDecl->Data;
1312
Richard Smith053f6c62014-05-16 23:01:30 +00001313 setImplicit(IsInternal);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001314
1315 // Update the declaration context of the type parameters.
1316 if (typeParamList) {
1317 for (auto typeParam : *typeParamList)
1318 typeParam->setDeclContext(this);
1319 }
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001320}
1321
Douglas Gregor73693022010-12-01 23:49:52 +00001322void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001323 assert(data().ExternallyCompleted && "Class is not externally completed");
1324 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001325 getASTContext().getExternalSource()->CompleteType(
1326 const_cast<ObjCInterfaceDecl *>(this));
1327}
1328
1329void ObjCInterfaceDecl::setExternallyCompleted() {
1330 assert(getASTContext().getExternalSource() &&
1331 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001332 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001333 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001334 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001335}
1336
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001337void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001338 // Check for a complete definition and recover if not so.
1339 if (!isThisDeclarationADefinition())
1340 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001341 data().HasDesignatedInitializers = true;
1342}
1343
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001344bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001345 // Check for a complete definition and recover if not so.
1346 if (!isThisDeclarationADefinition())
1347 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001348 if (data().ExternallyCompleted)
1349 LoadExternalDefinition();
1350
1351 return data().HasDesignatedInitializers;
1352}
1353
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001354StringRef
1355ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001356 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1357 return ObjCRTName->getMetadataName();
1358
1359 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001360}
1361
1362StringRef
1363ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001364 if (ObjCInterfaceDecl *ID =
1365 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1366 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001367
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001368 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001369}
1370
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001371ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001372 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1373 if (data().ExternallyCompleted)
1374 LoadExternalDefinition();
1375
1376 return getASTContext().getObjCImplementation(
1377 const_cast<ObjCInterfaceDecl*>(Def));
1378 }
1379
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001380 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001381 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001382}
1383
1384void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001385 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001386}
1387
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001388namespace {
1389 struct SynthesizeIvarChunk {
1390 uint64_t Size;
1391 ObjCIvarDecl *Ivar;
1392 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1393 : Size(size), Ivar(ivar) {}
1394 };
1395
1396 bool operator<(const SynthesizeIvarChunk & LHS,
1397 const SynthesizeIvarChunk &RHS) {
1398 return LHS.Size < RHS.Size;
1399 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001400}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001401
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001402/// all_declared_ivar_begin - return first ivar declared in this class,
1403/// its extensions and its implementation. Lazily build the list on first
1404/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001405///
1406/// Caveat: The list returned by this method reflects the current
1407/// state of the parser. The cache will be updated for every ivar
1408/// added by an extension or the implementation when they are
1409/// encountered.
1410/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001411ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001412 // FIXME: Should make sure no callers ever do this.
1413 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001414 return nullptr;
1415
1416 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001417 if (!data().IvarList) {
1418 if (!ivar_empty()) {
1419 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1420 data().IvarList = *I; ++I;
1421 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001422 curIvar->setNextIvar(*I);
1423 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001424
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001425 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001426 if (!Ext->ivar_empty()) {
1427 ObjCCategoryDecl::ivar_iterator
1428 I = Ext->ivar_begin(),
1429 E = Ext->ivar_end();
1430 if (!data().IvarList) {
1431 data().IvarList = *I; ++I;
1432 curIvar = data().IvarList;
1433 }
1434 for ( ;I != E; curIvar = *I, ++I)
1435 curIvar->setNextIvar(*I);
1436 }
1437 }
1438 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001439 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001440
1441 // cached and complete!
1442 if (!data().IvarListMissingImplementation)
1443 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001444
1445 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001446 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001447 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001448 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001449 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001450 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1451 layout.push_back(SynthesizeIvarChunk(
1452 IV->getASTContext().getTypeSize(IV->getType()), IV));
1453 continue;
1454 }
1455 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001456 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001457 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001458 curIvar->setNextIvar(IV);
1459 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001460 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001461
1462 if (!layout.empty()) {
1463 // Order synthesized ivars by their size.
1464 std::stable_sort(layout.begin(), layout.end());
1465 unsigned Ix = 0, EIx = layout.size();
1466 if (!data().IvarList) {
1467 data().IvarList = layout[0].Ivar; Ix++;
1468 curIvar = data().IvarList;
1469 }
1470 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1471 curIvar->setNextIvar(layout[Ix].Ivar);
1472 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001473 }
1474 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001475 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001476}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001477
1478/// FindCategoryDeclaration - Finds category declaration in the list of
1479/// categories for this class and returns it. Name of the category is passed
1480/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001481///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001482ObjCCategoryDecl *
1483ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001484 // FIXME: Should make sure no callers ever do this.
1485 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001486 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001487
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001488 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001489 LoadExternalDefinition();
1490
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001491 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001492 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001493 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001494
1495 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001496}
1497
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001498ObjCMethodDecl *
1499ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001500 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001501 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001502 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1503 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001504 }
1505
Craig Topper36250ad2014-05-12 05:36:57 +00001506 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001507}
1508
1509ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001510 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001511 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001512 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1513 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001514 }
Craig Topper36250ad2014-05-12 05:36:57 +00001515
1516 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001517}
1518
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001519/// ClassImplementsProtocol - Checks that 'lProto' protocol
1520/// has been implemented in IDecl class, its super class or categories (if
1521/// lookupCategory is true).
1522bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1523 bool lookupCategory,
1524 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001525 if (!hasDefinition())
1526 return false;
1527
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001528 ObjCInterfaceDecl *IDecl = this;
1529 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001530 for (auto *PI : IDecl->protocols()){
1531 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001532 return true;
1533 // This is dubious and is added to be compatible with gcc. In gcc, it is
1534 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1535 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1536 // object. This IMO, should be a bug.
1537 // FIXME: Treat this as an extension, and flag this as an error when GCC
1538 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001539 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001540 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
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 // 2nd, look up the category.
1545 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001546 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001547 for (auto *PI : Cat->protocols())
1548 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001549 return true;
1550 }
Mike Stump11289f42009-09-09 15:08:12 +00001551
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001552 // 3rd, look up the super class(s)
1553 if (IDecl->getSuperClass())
1554 return
1555 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1556 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001557
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001558 return false;
1559}
1560
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001561//===----------------------------------------------------------------------===//
1562// ObjCIvarDecl
1563//===----------------------------------------------------------------------===//
1564
David Blaikie68e081d2011-12-20 02:48:34 +00001565void ObjCIvarDecl::anchor() { }
1566
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001567ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001568 SourceLocation StartLoc,
1569 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001570 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001571 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001572 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001573 if (DC) {
1574 // Ivar's can only appear in interfaces, implementations (via synthesized
1575 // properties), and class extensions (via direct declaration, or synthesized
1576 // properties).
1577 //
1578 // FIXME: This should really be asserting this:
1579 // (isa<ObjCCategoryDecl>(DC) &&
1580 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1581 // but unfortunately we sometimes place ivars into non-class extension
1582 // categories on error. This breaks an AST invariant, and should not be
1583 // fixed.
1584 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1585 isa<ObjCCategoryDecl>(DC)) &&
1586 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001587 // Once a new ivar is created in any of class/class-extension/implementation
1588 // decl contexts, the previously built IvarList must be rebuilt.
1589 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1590 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001591 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001592 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001593 else
1594 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001595 }
Craig Topper36250ad2014-05-12 05:36:57 +00001596 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001597 }
1598
Richard Smithf7981722013-11-22 09:01:48 +00001599 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001600 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001601}
1602
Douglas Gregor72172e92012-01-05 21:55:30 +00001603ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001604 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1605 nullptr, QualType(), nullptr,
1606 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001607}
1608
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001609const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1610 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001611
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001612 switch (DC->getKind()) {
1613 default:
1614 case ObjCCategoryImpl:
1615 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001616 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001617
1618 // Ivars can only appear in class extension categories.
1619 case ObjCCategory: {
1620 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1621 assert(CD->IsClassExtension() && "invalid container for ivar!");
1622 return CD->getClassInterface();
1623 }
1624
1625 case ObjCImplementation:
1626 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1627
1628 case ObjCInterface:
1629 return cast<ObjCInterfaceDecl>(DC);
1630 }
1631}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001632
Douglas Gregore83b9562015-07-07 03:57:53 +00001633QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1634 return getType().substObjCMemberType(objectType, getDeclContext(),
1635 ObjCSubstitutionContext::Property);
1636}
1637
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001638//===----------------------------------------------------------------------===//
1639// ObjCAtDefsFieldDecl
1640//===----------------------------------------------------------------------===//
1641
David Blaikie68e081d2011-12-20 02:48:34 +00001642void ObjCAtDefsFieldDecl::anchor() { }
1643
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001644ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001645*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1646 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001647 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001648 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001649}
1650
Richard Smithf7981722013-11-22 09:01:48 +00001651ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001652 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001653 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1654 SourceLocation(), nullptr, QualType(),
1655 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001656}
1657
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001658//===----------------------------------------------------------------------===//
1659// ObjCProtocolDecl
1660//===----------------------------------------------------------------------===//
1661
David Blaikie68e081d2011-12-20 02:48:34 +00001662void ObjCProtocolDecl::anchor() { }
1663
Richard Smith053f6c62014-05-16 23:01:30 +00001664ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1665 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001666 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001667 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001668 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1669 redeclarable_base(C), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001670 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001671 if (PrevDecl)
1672 Data = PrevDecl->Data;
1673}
1674
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001675ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001676 IdentifierInfo *Id,
1677 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001678 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001679 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001680 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001681 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001682 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001683 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001684}
1685
Richard Smithf7981722013-11-22 09:01:48 +00001686ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001687 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001688 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001689 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001690 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001691 Result->Data.setInt(!C.getLangOpts().Modules);
1692 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001693}
1694
Steve Naroff114aecb2009-03-01 16:12:44 +00001695ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1696 ObjCProtocolDecl *PDecl = this;
1697
1698 if (Name == getIdentifier())
1699 return PDecl;
1700
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001701 for (auto *I : protocols())
1702 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001703 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001704
Craig Topper36250ad2014-05-12 05:36:57 +00001705 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001706}
1707
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001708// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001709// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001710ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1711 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001712 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001713
Douglas Gregoreed49792013-01-17 00:38:46 +00001714 // If there is no definition or the definition is hidden, we don't find
1715 // anything.
1716 const ObjCProtocolDecl *Def = getDefinition();
1717 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001718 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001719
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001720 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001721 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001722
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001723 for (const auto *I : protocols())
1724 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001725 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001726 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001727}
1728
Douglas Gregore6e48b12012-01-01 19:29:29 +00001729void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001730 assert(!Data.getPointer() && "Protocol already has a definition!");
1731 Data.setPointer(new (getASTContext()) DefinitionData);
1732 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001733}
1734
1735void ObjCProtocolDecl::startDefinition() {
1736 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001737
1738 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001739 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001740 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001741}
1742
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001743void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1744 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001745
1746 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001747 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001748 // Insert into PM if not there already.
1749 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001750 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001751 }
1752 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001753 for (const auto *PI : PDecl->protocols())
1754 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001755 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001756}
1757
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001758
1759void ObjCProtocolDecl::collectInheritedProtocolProperties(
1760 const ObjCPropertyDecl *Property,
1761 ProtocolPropertyMap &PM) const {
1762 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1763 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001764 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001765 if (Prop == Property)
1766 continue;
1767 if (Prop->getIdentifier() == Property->getIdentifier()) {
1768 PM[PDecl] = Prop;
1769 MatchFound = true;
1770 break;
1771 }
1772 }
1773 // Scan through protocol's protocols which did not have a matching property.
1774 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001775 for (const auto *PI : PDecl->protocols())
1776 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001777 }
1778}
Anna Zaks673d76b2012-10-18 19:17:53 +00001779
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001780StringRef
1781ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001782 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1783 return ObjCRTName->getMetadataName();
1784
1785 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001786}
1787
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001788//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001789// ObjCCategoryDecl
1790//===----------------------------------------------------------------------===//
1791
David Blaikie68e081d2011-12-20 02:48:34 +00001792void ObjCCategoryDecl::anchor() { }
1793
Douglas Gregor85f3f952015-07-07 03:57:15 +00001794ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1795 SourceLocation ClassNameLoc,
1796 SourceLocation CategoryNameLoc,
1797 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1798 ObjCTypeParamList *typeParamList,
1799 SourceLocation IvarLBraceLoc,
1800 SourceLocation IvarRBraceLoc)
1801 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1802 ClassInterface(IDecl), TypeParamList(typeParamList),
1803 NextClassCategory(nullptr), CategoryNameLoc(CategoryNameLoc),
1804 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc)
1805{
1806 // Set the declaration context of each of the type parameters.
1807 if (typeParamList) {
1808 for (auto typeParam : *typeParamList) {
1809 typeParam->setDeclContext(this);
1810 }
1811 }
1812}
1813
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001814ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001815 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001816 SourceLocation ClassNameLoc,
1817 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001818 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001819 ObjCInterfaceDecl *IDecl,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001820 ObjCTypeParamList *typeParamList,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001821 SourceLocation IvarLBraceLoc,
1822 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001823 ObjCCategoryDecl *CatDecl =
1824 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001825 IDecl, typeParamList, IvarLBraceLoc,
1826 IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001827 if (IDecl) {
1828 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001829 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001830 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001831 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001832 if (ASTMutationListener *L = C.getASTMutationListener())
1833 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1834 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001835 }
1836
1837 return CatDecl;
1838}
1839
Richard Smithf7981722013-11-22 09:01:48 +00001840ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001841 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001842 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1843 SourceLocation(), SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001844 nullptr, nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001845}
1846
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001847ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1848 return getASTContext().getObjCImplementation(
1849 const_cast<ObjCCategoryDecl*>(this));
1850}
1851
1852void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1853 getASTContext().setObjCImplementation(this, ImplD);
1854}
1855
1856
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001857//===----------------------------------------------------------------------===//
1858// ObjCCategoryImplDecl
1859//===----------------------------------------------------------------------===//
1860
David Blaikie68e081d2011-12-20 02:48:34 +00001861void ObjCCategoryImplDecl::anchor() { }
1862
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001863ObjCCategoryImplDecl *
1864ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001865 IdentifierInfo *Id,
1866 ObjCInterfaceDecl *ClassInterface,
1867 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001868 SourceLocation atStartLoc,
1869 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001870 if (ClassInterface && ClassInterface->hasDefinition())
1871 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001872 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1873 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001874}
1875
Douglas Gregor72172e92012-01-05 21:55:30 +00001876ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1877 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001878 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1879 SourceLocation(), SourceLocation(),
1880 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001881}
1882
Steve Narofff406f4d2009-10-29 21:11:04 +00001883ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001884 // The class interface might be NULL if we are working with invalid code.
1885 if (const ObjCInterfaceDecl *ID = getClassInterface())
1886 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00001887 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001888}
1889
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001890
David Blaikie68e081d2011-12-20 02:48:34 +00001891void ObjCImplDecl::anchor() { }
1892
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001893void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001894 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001895 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001896 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001897}
1898
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001899void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1900 ASTContext &Ctx = getASTContext();
1901
1902 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001903 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001904 if (IFace)
1905 Ctx.setObjCImplementation(IFace, ImplD);
1906
Duncan Sands49c29ee2009-07-21 07:56:29 +00001907 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001908 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1909 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1910 Ctx.setObjCImplementation(CD, ImplD);
1911 }
1912
1913 ClassInterface = IFace;
1914}
1915
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001916/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001917/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001918/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001919///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001920ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001921FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001922 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001923 if (PID->getPropertyIvarDecl() &&
1924 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1925 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001926 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001927}
1928
1929/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001930/// added to the list of those properties \@synthesized/\@dynamic in this
1931/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001932///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001933ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001934FindPropertyImplDecl(IdentifierInfo *Id) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001935 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001936 if (PID->getPropertyDecl()->getIdentifier() == Id)
1937 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001938 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001939}
1940
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001941raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001942 const ObjCCategoryImplDecl &CID) {
1943 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001944 return OS;
1945}
1946
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001947//===----------------------------------------------------------------------===//
1948// ObjCImplementationDecl
1949//===----------------------------------------------------------------------===//
1950
David Blaikie68e081d2011-12-20 02:48:34 +00001951void ObjCImplementationDecl::anchor() { }
1952
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001953ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001954ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001955 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001956 ObjCInterfaceDecl *SuperDecl,
1957 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001958 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001959 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001960 SourceLocation IvarLBraceLoc,
1961 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001962 if (ClassInterface && ClassInterface->hasDefinition())
1963 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001964 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1965 nameLoc, atStartLoc, superLoc,
1966 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001967}
1968
Douglas Gregor72172e92012-01-05 21:55:30 +00001969ObjCImplementationDecl *
1970ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001971 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
1972 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001973}
1974
John McCall0410e572011-07-22 04:15:06 +00001975void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1976 CXXCtorInitializer ** initializers,
1977 unsigned numInitializers) {
1978 if (numInitializers > 0) {
1979 NumIvarInitializers = numInitializers;
1980 CXXCtorInitializer **ivarInitializers =
1981 new (C) CXXCtorInitializer*[NumIvarInitializers];
1982 memcpy(ivarInitializers, initializers,
1983 numInitializers * sizeof(CXXCtorInitializer*));
1984 IvarInitializers = ivarInitializers;
1985 }
1986}
1987
Richard Smithc2bb8182015-03-24 06:36:48 +00001988ObjCImplementationDecl::init_const_iterator
1989ObjCImplementationDecl::init_begin() const {
1990 return IvarInitializers.get(getASTContext().getExternalSource());
1991}
1992
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001993raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001994 const ObjCImplementationDecl &ID) {
1995 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001996 return OS;
1997}
1998
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001999//===----------------------------------------------------------------------===//
2000// ObjCCompatibleAliasDecl
2001//===----------------------------------------------------------------------===//
2002
David Blaikie68e081d2011-12-20 02:48:34 +00002003void ObjCCompatibleAliasDecl::anchor() { }
2004
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002005ObjCCompatibleAliasDecl *
2006ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2007 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00002008 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002009 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00002010 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002011}
2012
Douglas Gregor72172e92012-01-05 21:55:30 +00002013ObjCCompatibleAliasDecl *
2014ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002015 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2016 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002017}
2018
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002019//===----------------------------------------------------------------------===//
2020// ObjCPropertyDecl
2021//===----------------------------------------------------------------------===//
2022
David Blaikie68e081d2011-12-20 02:48:34 +00002023void ObjCPropertyDecl::anchor() { }
2024
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002025ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2026 SourceLocation L,
2027 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00002028 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002029 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00002030 QualType T,
2031 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002032 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002033 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2034 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002035}
2036
Richard Smithf7981722013-11-22 09:01:48 +00002037ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002038 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002039 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2040 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00002041 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00002042}
2043
Douglas Gregore83b9562015-07-07 03:57:53 +00002044QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2045 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2046 ObjCSubstitutionContext::Property);
2047}
2048
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002049//===----------------------------------------------------------------------===//
2050// ObjCPropertyImplDecl
2051//===----------------------------------------------------------------------===//
2052
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002053ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002054 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002055 SourceLocation atLoc,
2056 SourceLocation L,
2057 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002058 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002059 ObjCIvarDecl *ivar,
2060 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002061 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2062 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002063}
Chris Lattnered0e1642008-03-17 01:19:02 +00002064
Richard Smithf7981722013-11-22 09:01:48 +00002065ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002066 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002067 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2068 SourceLocation(), nullptr, Dynamic,
2069 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002070}
2071
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002072SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2073 SourceLocation EndLoc = getLocation();
2074 if (IvarLoc.isValid())
2075 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002076
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002077 return SourceRange(AtLoc, EndLoc);
2078}