blob: a93c8f613368bc0f0a7a4c811a7fe4243778029f [file] [log] [blame]
Chris Lattner89375192008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000016#include "clang/AST/ASTMutationListener.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/Stmt.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000019#include "llvm/ADT/STLExtras.h"
Anna Zaks454477c2012-09-27 19:45:11 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner89375192008-03-16 00:19:01 +000021using namespace clang;
22
Chris Lattner8d8829e2008-03-16 00:49:28 +000023//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000024// ObjCListBase
25//===----------------------------------------------------------------------===//
26
Chris Lattner22298722009-02-20 21:35:13 +000027void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Craig Topper36250ad2014-05-12 05:36:57 +000028 List = nullptr;
Chris Lattner4d1eb762009-02-20 21:16:26 +000029 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000030
31
Chris Lattner7c981a72009-02-20 21:44:01 +000032 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000033 NumElts = Elts;
34 memcpy(List, InList, sizeof(void*)*Elts);
35}
36
Douglas Gregor002b6712010-01-16 15:02:53 +000037void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
38 const SourceLocation *Locs, ASTContext &Ctx) {
39 if (Elts == 0)
40 return;
41
42 Locations = new (Ctx) SourceLocation[Elts];
43 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
44 set(InList, Elts, Ctx);
45}
46
Chris Lattner4d1eb762009-02-20 21:16:26 +000047//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000048// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000049//===----------------------------------------------------------------------===//
50
David Blaikie68e081d2011-12-20 02:48:34 +000051void ObjCContainerDecl::anchor() { }
52
Fariborz Jahanian68453832009-06-05 18:16:35 +000053/// getIvarDecl - This method looks up an ivar in this ContextDecl.
54///
55ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000056ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Richard Smithcf4bdde2015-02-21 02:45:19 +000057 lookup_result R = lookup(Id);
58 for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000059 Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian68453832009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
Craig Topper36250ad2014-05-12 05:36:57 +000063 return nullptr;
Fariborz Jahanian68453832009-06-05 18:16:35 +000064}
65
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000066// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000067ObjCMethodDecl *
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69 bool AllowHidden) const {
Douglas Gregoreed49792013-01-17 00:38:46 +000070 // If this context is a hidden protocol definition, don't find any
71 // methods there.
72 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
73 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000074 if (Def->isHidden() && !AllowHidden)
Craig Topper36250ad2014-05-12 05:36:57 +000075 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +000076 }
77
Steve Naroffc4173fa2009-02-22 19:35:57 +000078 // Since instance & class methods can have the same name, the loop below
79 // ensures we get the correct method.
80 //
81 // @interface Whatever
82 // - (int) class_method;
83 // + (float) class_method;
84 // @end
85 //
Richard Smithcf4bdde2015-02-21 02:45:19 +000086 lookup_result R = lookup(Sel);
87 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000088 Meth != MethEnd; ++Meth) {
Steve Naroffc4173fa2009-02-22 19:35:57 +000089 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000090 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +000091 return MD;
92 }
Craig Topper36250ad2014-05-12 05:36:57 +000093 return nullptr;
Steve Naroff35c62ae2009-01-08 17:28:14 +000094}
95
Nico Weber4701ffd2014-12-22 05:21:03 +000096/// \brief This routine returns 'true' if a user declared setter method was
97/// found in the class, its protocols, its super classes or categories.
98/// It also returns 'true' if one of its categories has declared a 'readwrite'
99/// property. This is because, user must provide a setter method for the
100/// category's 'readwrite' property.
101bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
102 const ObjCPropertyDecl *Property) const {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000103 Selector Sel = Property->getSetterName();
Richard Smithcf4bdde2015-02-21 02:45:19 +0000104 lookup_result R = lookup(Sel);
105 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000106 Meth != MethEnd; ++Meth) {
107 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
108 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
109 return true;
110 }
111
112 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
113 // Also look into categories, including class extensions, looking
114 // for a user declared instance method.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000115 for (const auto *Cat : ID->visible_categories()) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000116 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
117 if (!MD->isImplicit())
118 return true;
119 if (Cat->IsClassExtension())
120 continue;
Nico Weber4701ffd2014-12-22 05:21:03 +0000121 // Also search through the categories looking for a 'readwrite'
122 // declaration of this property. If one found, presumably a setter will
123 // be provided (properties declared in categories will not get
124 // auto-synthesized).
Aaron Ballmand174edf2014-03-13 19:11:50 +0000125 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000126 if (P->getIdentifier() == Property->getIdentifier()) {
127 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
128 return true;
129 break;
130 }
131 }
132
133 // Also look into protocols, for a user declared instance method.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000134 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000135 if (Proto->HasUserDeclaredSetterMethod(Property))
136 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000137
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000138 // And in its super class.
139 ObjCInterfaceDecl *OSC = ID->getSuperClass();
140 while (OSC) {
141 if (OSC->HasUserDeclaredSetterMethod(Property))
142 return true;
143 OSC = OSC->getSuperClass();
144 }
145 }
146 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000147 for (const auto *PI : PD->protocols())
148 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000149 return true;
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000150 return false;
151}
152
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000153ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000154ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Jordan Rose210bfe92014-11-19 22:03:46 +0000155 const IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000156 // If this context is a hidden protocol definition, don't find any
157 // property.
158 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
159 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
160 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000161 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000162 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000163
Richard Smithcf4bdde2015-02-21 02:45:19 +0000164 DeclContext::lookup_result R = DC->lookup(propertyID);
165 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
David Blaikieff7d47a2012-12-19 00:45:41 +0000166 ++I)
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000167 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
168 return PD;
169
Craig Topper36250ad2014-05-12 05:36:57 +0000170 return nullptr;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000171}
172
Anna Zaks454477c2012-09-27 19:45:11 +0000173IdentifierInfo *
174ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
175 SmallString<128> ivarName;
176 {
177 llvm::raw_svector_ostream os(ivarName);
178 os << '_' << getIdentifier()->getName();
179 }
180 return &Ctx.Idents.get(ivarName.str());
181}
182
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000183/// FindPropertyDeclaration - Finds declaration of the property given its name
184/// in 'PropertyId' and returns it. It returns 0, if not found.
Jordan Rose210bfe92014-11-19 22:03:46 +0000185ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
186 const IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000187 // Don't find properties within hidden protocol definitions.
188 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
189 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
190 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000191 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Ted Kremenekddcd1092010-03-15 20:11:53 +0000194 if (ObjCPropertyDecl *PD =
195 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
196 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Ted Kremenekddcd1092010-03-15 20:11:53 +0000198 switch (getKind()) {
199 default:
200 break;
201 case Decl::ObjCProtocol: {
202 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000203 for (const auto *I : PID->protocols())
204 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000205 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000206 break;
207 }
208 case Decl::ObjCInterface: {
209 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000210 // Look through categories (but not extensions).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000211 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000212 if (!Cat->IsClassExtension())
213 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
214 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000215 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000216
217 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000218 for (const auto *I : OID->all_referenced_protocols())
219 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000220 return P;
221
222 // Finally, check the super class.
223 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
224 return superClass->FindPropertyDeclaration(PropertyId);
225 break;
226 }
227 case Decl::ObjCCategory: {
228 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
229 // Look through protocols.
230 if (!OCD->IsClassExtension())
Aaron Ballman19a41762014-03-14 12:55:57 +0000231 for (const auto *I : OCD->protocols())
232 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
233 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000234 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000235 }
236 }
Craig Topper36250ad2014-05-12 05:36:57 +0000237 return nullptr;
Steve Narofff9c65242008-06-05 13:55:23 +0000238}
239
David Blaikie68e081d2011-12-20 02:48:34 +0000240void ObjCInterfaceDecl::anchor() { }
241
Douglas Gregor85f3f952015-07-07 03:57:15 +0000242ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
243 // If this particular declaration has a type parameter list, return it.
244 if (ObjCTypeParamList *written = getTypeParamListAsWritten())
245 return written;
246
247 // If there is a definition, return its type parameter list.
248 if (const ObjCInterfaceDecl *def = getDefinition())
249 return def->getTypeParamListAsWritten();
250
251 // Otherwise, look at previous declarations to determine whether any
252 // of them has a type parameter list, skipping over those
253 // declarations that do not.
254 for (auto decl = getPreviousDecl(); decl; decl = decl->getPreviousDecl()) {
255 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
256 return written;
257 }
258
259 return nullptr;
260}
261
Douglas Gregore9d95f12015-07-07 03:57:35 +0000262ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
263 // FIXME: Should make sure no callers ever do this.
264 if (!hasDefinition())
265 return nullptr;
266
267 if (data().ExternallyCompleted)
268 LoadExternalDefinition();
269
270 if (const ObjCObjectType *superType = getSuperClassType()) {
271 if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
272 if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
273 return superDef;
274
275 return superDecl;
276 }
277 }
278
279 return nullptr;
280}
281
282SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
283 if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
284 return superTInfo->getTypeLoc().getLocStart();
285
286 return SourceLocation();
287}
288
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000289/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
290/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000291/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000292///
293ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000294ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000295 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000296 // FIXME: Should make sure no callers ever do this.
297 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000298 return nullptr;
299
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000300 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000301 LoadExternalDefinition();
302
Ted Kremenekd133a862010-03-15 20:30:07 +0000303 if (ObjCPropertyDecl *PD =
304 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
305 return PD;
306
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000307 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000308 for (const auto *I : all_referenced_protocols())
309 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000310 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000311
Craig Topper36250ad2014-05-12 05:36:57 +0000312 return nullptr;
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000313}
314
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000315void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
316 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000317 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000318 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000319 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000320 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000321 for (const auto *PI : all_referenced_protocols())
322 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000323 // Note, the properties declared only in class extensions are still copied
324 // into the main @interface's property list, and therefore we don't
325 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000326}
327
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000328bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
329 const ObjCInterfaceDecl *Class = this;
330 while (Class) {
331 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
332 return true;
333 Class = Class->getSuperClass();
334 }
335 return false;
336}
337
338const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
339 const ObjCInterfaceDecl *Class = this;
340 while (Class) {
341 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
342 return Class;
343 Class = Class->getSuperClass();
344 }
Craig Topper36250ad2014-05-12 05:36:57 +0000345 return nullptr;
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000346}
347
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000348void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
349 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
350 ASTContext &C)
351{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000352 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000353 LoadExternalDefinition();
354
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000355 if (data().AllReferencedProtocols.empty() &&
356 data().ReferencedProtocols.empty()) {
357 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000358 return;
359 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000360
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000361 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000362 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000363 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000364 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000365 for (unsigned i = 0; i < ExtNum; i++) {
366 bool protocolExists = false;
367 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000368 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000369 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
370 protocolExists = true;
371 break;
372 }
373 }
374 // Do we want to warn on a protocol in extension class which
375 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000376 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000377 ProtocolRefs.push_back(ProtoInExtension);
378 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000379
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000380 if (ProtocolRefs.empty())
381 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000382
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000383 // Merge ProtocolRefs into class's protocol list;
Benjamin Kramerf9890422015-02-17 16:48:30 +0000384 ProtocolRefs.append(all_referenced_protocol_begin(),
385 all_referenced_protocol_end());
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000386
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000387 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000388}
389
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000390const ObjCInterfaceDecl *
391ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
392 const ObjCInterfaceDecl *IFace = this;
393 while (IFace) {
394 if (IFace->hasDesignatedInitializers())
395 return IFace;
396 if (!IFace->inheritsDesignatedInitializers())
397 break;
398 IFace = IFace->getSuperClass();
399 }
Craig Topper36250ad2014-05-12 05:36:57 +0000400 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000401}
402
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000403static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
404 for (const auto *MD : D->instance_methods()) {
405 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
406 return true;
407 }
408 for (const auto *Ext : D->visible_extensions()) {
409 for (const auto *MD : Ext->instance_methods()) {
410 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
411 return true;
412 }
413 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000414 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000415 for (const auto *MD : ImplD->instance_methods()) {
416 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
417 return true;
418 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000419 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000420 return false;
421}
422
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000423bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
424 switch (data().InheritedDesignatedInitializers) {
425 case DefinitionData::IDI_Inherited:
426 return true;
427 case DefinitionData::IDI_NotInherited:
428 return false;
429 case DefinitionData::IDI_Unknown: {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000430 // If the class introduced initializers we conservatively assume that we
431 // don't know if any of them is a designated initializer to avoid possible
432 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000433 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000434 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000435 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000436 if (auto SuperD = getSuperClass()) {
437 data().InheritedDesignatedInitializers =
438 SuperD->declaresOrInheritsDesignatedInitializers() ?
439 DefinitionData::IDI_Inherited :
440 DefinitionData::IDI_NotInherited;
441 } else {
442 data().InheritedDesignatedInitializers =
443 DefinitionData::IDI_NotInherited;
444 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000445 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000446 assert(data().InheritedDesignatedInitializers
447 != DefinitionData::IDI_Unknown);
448 return data().InheritedDesignatedInitializers ==
449 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000450 }
451 }
452
453 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
454}
455
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000456void ObjCInterfaceDecl::getDesignatedInitializers(
457 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000458 // Check for a complete definition and recover if not so.
459 if (!isThisDeclarationADefinition())
460 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000461 if (data().ExternallyCompleted)
462 LoadExternalDefinition();
463
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000464 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000465 if (!IFace)
466 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000467
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000468 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000469 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000470 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000471 for (const auto *Ext : IFace->visible_extensions()) {
472 for (const auto *MD : Ext->instance_methods())
473 if (MD->isThisDeclarationADesignatedInitializer())
474 Methods.push_back(MD);
475 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000476}
477
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000478bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
479 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000480 // Check for a complete definition and recover if not so.
481 if (!isThisDeclarationADefinition())
482 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000483 if (data().ExternallyCompleted)
484 LoadExternalDefinition();
485
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000486 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000487 if (!IFace)
488 return false;
489
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000490 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000491 if (MD->isThisDeclarationADesignatedInitializer()) {
492 if (InitMethod)
493 *InitMethod = MD;
494 return true;
495 }
496 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000497 for (const auto *Ext : IFace->visible_extensions()) {
498 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
499 if (MD->isThisDeclarationADesignatedInitializer()) {
500 if (InitMethod)
501 *InitMethod = MD;
502 return true;
503 }
504 }
505 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000506 return false;
507}
508
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000509void ObjCInterfaceDecl::allocateDefinitionData() {
510 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000511 Data.setPointer(new (getASTContext()) DefinitionData());
512 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000513
514 // Make the type point at the definition, now that we have one.
515 if (TypeForDecl)
516 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000517}
518
519void ObjCInterfaceDecl::startDefinition() {
520 allocateDefinitionData();
521
Douglas Gregor66b310c2011-12-15 18:03:09 +0000522 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000523 for (auto RD : redecls()) {
524 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000525 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000526 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000527}
528
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000529ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
530 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000531 // FIXME: Should make sure no callers ever do this.
532 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000533 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000534
535 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000536 LoadExternalDefinition();
537
Chris Lattner89375192008-03-16 00:19:01 +0000538 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000539 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000540 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000541 clsDeclared = ClassDecl;
542 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000543 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000544
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000545 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000546 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000547 clsDeclared = ClassDecl;
548 return I;
549 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000550 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000551
Chris Lattner89375192008-03-16 00:19:01 +0000552 ClassDecl = ClassDecl->getSuperClass();
553 }
Craig Topper36250ad2014-05-12 05:36:57 +0000554 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000555}
556
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000557/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
558/// class whose name is passed as argument. If it is not one of the super classes
559/// the it returns NULL.
560ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
561 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000562 // FIXME: Should make sure no callers ever do this.
563 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000564 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000565
566 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000567 LoadExternalDefinition();
568
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000569 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000570 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000571 if (ClassDecl->getIdentifier() == ICName)
572 return ClassDecl;
573 ClassDecl = ClassDecl->getSuperClass();
574 }
Craig Topper36250ad2014-05-12 05:36:57 +0000575 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000576}
577
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000578ObjCProtocolDecl *
579ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000580 for (auto *P : all_referenced_protocols())
581 if (P->lookupProtocolNamed(Name))
582 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000583 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000584 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000585}
586
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000587/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000588/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000589/// When argument category "C" is specified, any implicit method found
590/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000591ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000592 bool isInstance,
593 bool shallowCategoryLookup,
594 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000595 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000596{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000597 // FIXME: Should make sure no callers ever do this.
598 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000599 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000600
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000601 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000602 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000603
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000604 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000605 LoadExternalDefinition();
606
Ted Kremenek00781502013-11-23 01:01:29 +0000607 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000608 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000609 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000610 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000611
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000612 // 2. Didn't find one yet - now look through categories.
613 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000614 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000615 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000616 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000617
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000618 // 3. Didn't find one yet - look through primary class's protocols.
619 for (const auto *I : ClassDecl->protocols())
620 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
621 return MethodDecl;
622
623 // 4. Didn't find one yet - now look through categories' protocols
624 if (!shallowCategoryLookup)
625 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000626 // Didn't find one yet - look through protocols.
627 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000628 Cat->getReferencedProtocols();
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000629 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
630 E = Protocols.end(); I != E; ++I)
631 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000632 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000633 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000634 }
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000635
636
Ted Kremenek00781502013-11-23 01:01:29 +0000637 if (!followSuper)
Craig Topper36250ad2014-05-12 05:36:57 +0000638 return nullptr;
Ted Kremenek00781502013-11-23 01:01:29 +0000639
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000640 // 5. Get to the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000641 ClassDecl = ClassDecl->getSuperClass();
642 }
Craig Topper36250ad2014-05-12 05:36:57 +0000643 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000644}
645
Anna Zaksc77a3b12012-07-27 19:07:44 +0000646// Will search "local" class/category implementations for a method decl.
647// If failed, then we search in class's root for an instance method.
648// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000649ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
650 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000651 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000652 // FIXME: Should make sure no callers ever do this.
653 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000654 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000655
656 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000657 LoadExternalDefinition();
658
Craig Topper36250ad2014-05-12 05:36:57 +0000659 ObjCMethodDecl *Method = nullptr;
Steve Naroffbb69c942009-10-01 23:46:04 +0000660 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000661 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
662 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000663
664 // Look through local category implementations associated with the class.
665 if (!Method)
Nico Weberf6098392015-03-02 01:12:28 +0000666 Method = getCategoryMethod(Sel, Instance);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000667
668 // Before we give up, check if the selector is an instance method.
669 // But only in the root. This matches gcc's behavior and what the
670 // runtime expects.
671 if (!Instance && !Method && !getSuperClass()) {
672 Method = lookupInstanceMethod(Sel);
673 // Look through local category implementations associated
674 // with the root class.
675 if (!Method)
676 Method = lookupPrivateMethod(Sel, true);
677 }
678
Steve Naroffbb69c942009-10-01 23:46:04 +0000679 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000680 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000681 return Method;
682}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000683
684//===----------------------------------------------------------------------===//
685// ObjCMethodDecl
686//===----------------------------------------------------------------------===//
687
Alp Toker314cc812014-01-25 16:55:45 +0000688ObjCMethodDecl *ObjCMethodDecl::Create(
689 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
690 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
691 DeclContext *contextDecl, bool isInstance, bool isVariadic,
692 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
693 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000694 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000695 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000696 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
697 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000698}
699
Douglas Gregor72172e92012-01-05 21:55:30 +0000700ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000701 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000702 Selector(), QualType(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000703}
704
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000705bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
706 return getMethodFamily() == OMF_init &&
707 hasAttr<ObjCDesignatedInitializerAttr>();
708}
709
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000710bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
711 const ObjCMethodDecl **InitMethod) const {
712 if (getMethodFamily() != OMF_init)
713 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000714 const DeclContext *DC = getDeclContext();
715 if (isa<ObjCProtocolDecl>(DC))
716 return false;
717 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000718 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000719 return false;
720}
721
Douglas Gregora6017bb2012-10-09 17:21:28 +0000722Stmt *ObjCMethodDecl::getBody() const {
723 return Body.get(getASTContext().getExternalSource());
724}
725
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000726void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
727 assert(PrevMethod);
728 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
729 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000730 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000731}
732
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000733void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
734 ArrayRef<ParmVarDecl*> Params,
735 ArrayRef<SourceLocation> SelLocs) {
Craig Topper36250ad2014-05-12 05:36:57 +0000736 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000737 NumParams = Params.size();
738 if (Params.empty() && SelLocs.empty())
739 return;
740
741 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
742 sizeof(SourceLocation) * SelLocs.size();
743 ParamsAndSelLocs = C.Allocate(Size);
744 std::copy(Params.begin(), Params.end(), getParams());
745 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
746}
747
748void ObjCMethodDecl::getSelectorLocs(
749 SmallVectorImpl<SourceLocation> &SelLocs) const {
750 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
751 SelLocs.push_back(getSelectorLoc(i));
752}
753
754void ObjCMethodDecl::setMethodParams(ASTContext &C,
755 ArrayRef<ParmVarDecl*> Params,
756 ArrayRef<SourceLocation> SelLocs) {
757 assert((!SelLocs.empty() || isImplicit()) &&
758 "No selector locs for non-implicit method");
759 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000760 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000761
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000762 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
763 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000764 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000765 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000766
767 setParamsAndSelLocs(C, Params, SelLocs);
768}
769
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000770/// \brief A definition will return its interface declaration.
771/// An interface declaration will return its definition.
772/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000773ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000774 ASTContext &Ctx = getASTContext();
Craig Topper36250ad2014-05-12 05:36:57 +0000775 ObjCMethodDecl *Redecl = nullptr;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000776 if (HasRedeclaration)
777 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000778 if (Redecl)
779 return Redecl;
780
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000781 Decl *CtxD = cast<Decl>(getDeclContext());
782
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000783 if (!CtxD->isInvalidDecl()) {
784 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
785 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
786 if (!ImplD->isInvalidDecl())
787 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000788
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000789 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
790 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
791 if (!ImplD->isInvalidDecl())
792 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000793
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000794 } else if (ObjCImplementationDecl *ImplD =
795 dyn_cast<ObjCImplementationDecl>(CtxD)) {
796 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
797 if (!IFD->isInvalidDecl())
798 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000799
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000800 } else if (ObjCCategoryImplDecl *CImplD =
801 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
802 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
803 if (!CatD->isInvalidDecl())
804 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
805 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000806 }
807
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000808 if (!Redecl && isRedeclaration()) {
809 // This is the last redeclaration, go back to the first method.
810 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
811 isInstanceMethod());
812 }
813
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000814 return Redecl ? Redecl : this;
815}
816
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000817ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
818 Decl *CtxD = cast<Decl>(getDeclContext());
819
820 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
821 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
822 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
823 isInstanceMethod()))
824 return MD;
825
826 } else if (ObjCCategoryImplDecl *CImplD =
827 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000828 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000829 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
830 isInstanceMethod()))
831 return MD;
832 }
833
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000834 if (isRedeclaration())
835 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
836 isInstanceMethod());
837
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000838 return this;
839}
840
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000841SourceLocation ObjCMethodDecl::getLocEnd() const {
842 if (Stmt *Body = getBody())
843 return Body->getLocEnd();
844 return DeclEndLoc;
845}
846
John McCallb4526252011-03-02 01:50:55 +0000847ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
848 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000849 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000850 return family;
851
John McCall86bc21f2011-03-02 11:33:24 +0000852 // Check for an explicit attribute.
853 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
854 // The unfortunate necessity of mapping between enums here is due
855 // to the attributes framework.
856 switch (attr->getFamily()) {
857 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
858 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
859 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
860 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
861 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
862 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
863 }
864 Family = static_cast<unsigned>(family);
865 return family;
866 }
867
John McCallb4526252011-03-02 01:50:55 +0000868 family = getSelector().getMethodFamily();
869 switch (family) {
870 case OMF_None: break;
871
872 // init only has a conventional meaning for an instance method, and
873 // it has to return an object.
874 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000875 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000876 family = OMF_None;
877 break;
878
879 // alloc/copy/new have a conventional meaning for both class and
880 // instance methods, but they require an object return.
881 case OMF_alloc:
882 case OMF_copy:
883 case OMF_mutableCopy:
884 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000885 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000886 family = OMF_None;
887 break;
888
889 // These selectors have a conventional meaning only for instance methods.
890 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000891 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000892 case OMF_retain:
893 case OMF_release:
894 case OMF_autorelease:
895 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000896 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000897 if (!isInstanceMethod())
898 family = OMF_None;
899 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000900
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000901 case OMF_initialize:
902 if (isInstanceMethod() || !getReturnType()->isVoidType())
903 family = OMF_None;
904 break;
905
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000906 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000907 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000908 family = OMF_None;
909 else {
910 unsigned noParams = param_size();
911 if (noParams < 1 || noParams > 3)
912 family = OMF_None;
913 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000914 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000915 QualType ArgT = (*it);
916 if (!ArgT->isObjCSelType()) {
917 family = OMF_None;
918 break;
919 }
920 while (--noParams) {
921 it++;
922 ArgT = (*it);
923 if (!ArgT->isObjCIdType()) {
924 family = OMF_None;
925 break;
926 }
927 }
928 }
929 }
930 break;
931
John McCallb4526252011-03-02 01:50:55 +0000932 }
933
934 // Cache the result.
935 Family = static_cast<unsigned>(family);
936 return family;
937}
938
Mike Stump11289f42009-09-09 15:08:12 +0000939void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000940 const ObjCInterfaceDecl *OID) {
941 QualType selfTy;
942 if (isInstanceMethod()) {
943 // There may be no interface context due to error in declaration
944 // of the interface (which has been reported). Recover gracefully.
945 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000946 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000947 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000948 } else {
949 selfTy = Context.getObjCIdType();
950 }
951 } else // we have a factory method.
952 selfTy = Context.getObjCClassType();
953
John McCalld4631322011-06-17 06:42:21 +0000954 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000955 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000956
David Blaikiebbafb8a2012-03-11 07:00:24 +0000957 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000958 if (isInstanceMethod()) {
959 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000960
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000961 // 'self' is always __strong. It's actually pseudo-strong except
962 // in init methods (or methods labeled ns_consumes_self), though.
963 Qualifiers qs;
964 qs.setObjCLifetime(Qualifiers::OCL_Strong);
965 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000966
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000967 // In addition, 'self' is const unless this is an init method.
968 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
969 selfTy = selfTy.withConst();
970 selfIsPseudoStrong = true;
971 }
972 }
973 else {
974 assert(isClassMethod());
975 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000976 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000977 selfIsPseudoStrong = true;
978 }
John McCall31168b02011-06-15 23:02:42 +0000979 }
980
981 ImplicitParamDecl *self
982 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
983 &Context.Idents.get("self"), selfTy);
984 setSelfDecl(self);
985
986 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000987 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000988
John McCalld4631322011-06-17 06:42:21 +0000989 if (selfIsPseudoStrong)
990 self->setARCPseudoStrong(true);
991
Mike Stump11289f42009-09-09 15:08:12 +0000992 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
993 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000994 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000995}
996
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000997ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
998 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
999 return ID;
1000 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1001 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +00001002 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001003 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +00001004 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +00001005 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +00001006 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001007}
1008
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001009SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1010 const auto *TSI = getReturnTypeSourceInfo();
1011 if (TSI)
1012 return TSI->getTypeLoc().getSourceRange();
1013 return SourceRange();
1014}
1015
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001016static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1017 const ObjCMethodDecl *Method,
1018 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1019 bool MovedToSuper) {
1020 if (!Container)
1021 return;
1022
1023 // In categories look for overriden methods from protocols. A method from
1024 // category is not "overriden" since it is considered as the "same" method
1025 // (same USR) as the one from the interface.
1026 if (const ObjCCategoryDecl *
1027 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1028 // Check whether we have a matching method at this category but only if we
1029 // are at the super class level.
1030 if (MovedToSuper)
1031 if (ObjCMethodDecl *
1032 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001033 Method->isInstanceMethod(),
1034 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001035 if (Method != Overridden) {
1036 // We found an override at this category; there is no need to look
1037 // into its protocols.
1038 Methods.push_back(Overridden);
1039 return;
1040 }
1041
Aaron Ballman19a41762014-03-14 12:55:57 +00001042 for (const auto *P : Category->protocols())
1043 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001044 return;
1045 }
1046
1047 // Check whether we have a matching method at this level.
1048 if (const ObjCMethodDecl *
1049 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001050 Method->isInstanceMethod(),
1051 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001052 if (Method != Overridden) {
1053 // We found an override at this level; there is no need to look
1054 // into other protocols or categories.
1055 Methods.push_back(Overridden);
1056 return;
1057 }
1058
1059 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001060 for (const auto *P : Protocol->protocols())
1061 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001062 }
1063
1064 if (const ObjCInterfaceDecl *
1065 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001066 for (const auto *P : Interface->protocols())
1067 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001068
Aaron Ballman15063e12014-03-13 21:35:02 +00001069 for (const auto *Cat : Interface->known_categories())
1070 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001071
1072 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1073 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1074 /*MovedToSuper=*/true);
1075 }
1076}
1077
1078static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1079 const ObjCMethodDecl *Method,
1080 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1081 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1082 /*MovedToSuper=*/false);
1083}
1084
1085static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1086 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1087 assert(Method->isOverriding());
1088
1089 if (const ObjCProtocolDecl *
1090 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1091 CollectOverriddenMethods(ProtD, Method, overridden);
1092
1093 } else if (const ObjCImplDecl *
1094 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1095 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1096 if (!ID)
1097 return;
1098 // Start searching for overridden methods using the method from the
1099 // interface as starting point.
1100 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001101 Method->isInstanceMethod(),
1102 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001103 Method = IFaceMeth;
1104 CollectOverriddenMethods(ID, Method, overridden);
1105
1106 } else if (const ObjCCategoryDecl *
1107 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1108 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1109 if (!ID)
1110 return;
1111 // Start searching for overridden methods using the method from the
1112 // interface as starting point.
1113 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001114 Method->isInstanceMethod(),
1115 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001116 Method = IFaceMeth;
1117 CollectOverriddenMethods(ID, Method, overridden);
1118
1119 } else {
1120 CollectOverriddenMethods(
1121 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1122 Method, overridden);
1123 }
1124}
1125
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001126void ObjCMethodDecl::getOverriddenMethods(
1127 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1128 const ObjCMethodDecl *Method = this;
1129
1130 if (Method->isRedeclaration()) {
1131 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1132 getMethod(Method->getSelector(), Method->isInstanceMethod());
1133 }
1134
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001135 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001136 collectOverriddenMethodsSlow(Method, Overridden);
1137 assert(!Overridden.empty() &&
1138 "ObjCMethodDecl's overriding bit is not as expected");
1139 }
1140}
1141
Jordan Rose2bd991a2012-10-10 16:42:54 +00001142const ObjCPropertyDecl *
1143ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1144 Selector Sel = getSelector();
1145 unsigned NumArgs = Sel.getNumArgs();
1146 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001147 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001148
Jordan Rose16ba5532015-01-16 23:04:26 +00001149 if (!isInstanceMethod())
Craig Topper36250ad2014-05-12 05:36:57 +00001150 return nullptr;
1151
Jordan Rose2bd991a2012-10-10 16:42:54 +00001152 if (isPropertyAccessor()) {
1153 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001154 // If container is class extension, find its primary class.
1155 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1156 if (CatDecl->IsClassExtension())
1157 Container = CatDecl->getClassInterface();
1158
Jordan Rose2bd991a2012-10-10 16:42:54 +00001159 bool IsGetter = (NumArgs == 0);
1160
Aaron Ballmand174edf2014-03-13 19:11:50 +00001161 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001162 Selector NextSel = IsGetter ? I->getGetterName()
1163 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001164 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001165 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001166 }
1167
1168 llvm_unreachable("Marked as a property accessor but no property found!");
1169 }
1170
1171 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001172 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001173
1174 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1175 OverridesTy Overrides;
1176 getOverriddenMethods(Overrides);
1177 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1178 I != E; ++I) {
1179 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1180 return Prop;
1181 }
1182
Craig Topper36250ad2014-05-12 05:36:57 +00001183 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001184}
1185
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001186//===----------------------------------------------------------------------===//
Douglas Gregor85f3f952015-07-07 03:57:15 +00001187// ObjCTypeParamDecl
1188//===----------------------------------------------------------------------===//
1189
1190void ObjCTypeParamDecl::anchor() { }
1191
1192ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
1193 SourceLocation nameLoc,
1194 IdentifierInfo *name,
1195 SourceLocation colonLoc,
1196 TypeSourceInfo *boundInfo) {
1197 return new (ctx, dc) ObjCTypeParamDecl(ctx, dc, nameLoc, name, colonLoc,
1198 boundInfo);
1199}
1200
1201ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1202 unsigned ID) {
1203 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr, SourceLocation(),
1204 nullptr, SourceLocation(), nullptr);
1205}
1206
1207SourceRange ObjCTypeParamDecl::getSourceRange() const {
1208 if (hasExplicitBound()) {
1209 return SourceRange(getLocation(),
1210 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1211 }
1212
1213 return SourceRange(getLocation());
1214}
1215
1216//===----------------------------------------------------------------------===//
1217// ObjCTypeParamList
1218//===----------------------------------------------------------------------===//
1219ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1220 ArrayRef<ObjCTypeParamDecl *> typeParams,
1221 SourceLocation rAngleLoc)
1222 : Brackets(lAngleLoc, rAngleLoc), NumParams(typeParams.size())
1223{
1224 std::copy(typeParams.begin(), typeParams.end(), begin());
1225}
1226
1227
1228ObjCTypeParamList *ObjCTypeParamList::create(
1229 ASTContext &ctx,
1230 SourceLocation lAngleLoc,
1231 ArrayRef<ObjCTypeParamDecl *> typeParams,
1232 SourceLocation rAngleLoc) {
1233 unsigned size = sizeof(ObjCTypeParamList)
1234 + sizeof(ObjCTypeParamDecl *) * typeParams.size();
1235 static_assert(alignof(ObjCTypeParamList) >= alignof(ObjCTypeParamDecl*),
1236 "type parameter list needs greater alignment");
1237 unsigned align = llvm::alignOf<ObjCTypeParamList>();
1238 void *mem = ctx.Allocate(size, align);
1239 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1240}
1241
1242//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001243// ObjCInterfaceDecl
1244//===----------------------------------------------------------------------===//
1245
Douglas Gregord53ae832012-01-17 18:09:05 +00001246ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001247 DeclContext *DC,
1248 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001249 IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001250 ObjCTypeParamList *typeParamList,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001251 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001252 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001253 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001254 ObjCInterfaceDecl *Result = new (C, DC)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001255 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1256 isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001257 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001258 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001259 return Result;
1260}
1261
Richard Smith053f6c62014-05-16 23:01:30 +00001262ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001263 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001264 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001265 SourceLocation(),
1266 nullptr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001267 nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001268 SourceLocation(),
1269 nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001270 Result->Data.setInt(!C.getLangOpts().Modules);
1271 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001272}
1273
Richard Smith053f6c62014-05-16 23:01:30 +00001274ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1275 SourceLocation AtLoc, IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001276 ObjCTypeParamList *typeParamList,
Richard Smith053f6c62014-05-16 23:01:30 +00001277 SourceLocation CLoc,
1278 ObjCInterfaceDecl *PrevDecl,
1279 bool IsInternal)
1280 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001281 redeclarable_base(C), TypeForDecl(nullptr), TypeParamList(typeParamList),
1282 Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001283 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001284
1285 // Copy the 'data' pointer over.
1286 if (PrevDecl)
1287 Data = PrevDecl->Data;
1288
Richard Smith053f6c62014-05-16 23:01:30 +00001289 setImplicit(IsInternal);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001290
1291 // Update the declaration context of the type parameters.
1292 if (typeParamList) {
1293 for (auto typeParam : *typeParamList)
1294 typeParam->setDeclContext(this);
1295 }
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001296}
1297
Douglas Gregor73693022010-12-01 23:49:52 +00001298void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001299 assert(data().ExternallyCompleted && "Class is not externally completed");
1300 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001301 getASTContext().getExternalSource()->CompleteType(
1302 const_cast<ObjCInterfaceDecl *>(this));
1303}
1304
1305void ObjCInterfaceDecl::setExternallyCompleted() {
1306 assert(getASTContext().getExternalSource() &&
1307 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001308 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001309 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001310 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001311}
1312
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001313void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001314 // Check for a complete definition and recover if not so.
1315 if (!isThisDeclarationADefinition())
1316 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001317 data().HasDesignatedInitializers = true;
1318}
1319
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001320bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001321 // Check for a complete definition and recover if not so.
1322 if (!isThisDeclarationADefinition())
1323 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001324 if (data().ExternallyCompleted)
1325 LoadExternalDefinition();
1326
1327 return data().HasDesignatedInitializers;
1328}
1329
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001330StringRef
1331ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001332 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1333 return ObjCRTName->getMetadataName();
1334
1335 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001336}
1337
1338StringRef
1339ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001340 if (ObjCInterfaceDecl *ID =
1341 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1342 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001343
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001344 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001345}
1346
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001347ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001348 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1349 if (data().ExternallyCompleted)
1350 LoadExternalDefinition();
1351
1352 return getASTContext().getObjCImplementation(
1353 const_cast<ObjCInterfaceDecl*>(Def));
1354 }
1355
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001356 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001357 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001358}
1359
1360void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001361 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001362}
1363
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001364namespace {
1365 struct SynthesizeIvarChunk {
1366 uint64_t Size;
1367 ObjCIvarDecl *Ivar;
1368 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1369 : Size(size), Ivar(ivar) {}
1370 };
1371
1372 bool operator<(const SynthesizeIvarChunk & LHS,
1373 const SynthesizeIvarChunk &RHS) {
1374 return LHS.Size < RHS.Size;
1375 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001376}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001377
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001378/// all_declared_ivar_begin - return first ivar declared in this class,
1379/// its extensions and its implementation. Lazily build the list on first
1380/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001381///
1382/// Caveat: The list returned by this method reflects the current
1383/// state of the parser. The cache will be updated for every ivar
1384/// added by an extension or the implementation when they are
1385/// encountered.
1386/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001387ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001388 // FIXME: Should make sure no callers ever do this.
1389 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001390 return nullptr;
1391
1392 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001393 if (!data().IvarList) {
1394 if (!ivar_empty()) {
1395 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1396 data().IvarList = *I; ++I;
1397 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001398 curIvar->setNextIvar(*I);
1399 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001400
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001401 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001402 if (!Ext->ivar_empty()) {
1403 ObjCCategoryDecl::ivar_iterator
1404 I = Ext->ivar_begin(),
1405 E = Ext->ivar_end();
1406 if (!data().IvarList) {
1407 data().IvarList = *I; ++I;
1408 curIvar = data().IvarList;
1409 }
1410 for ( ;I != E; curIvar = *I, ++I)
1411 curIvar->setNextIvar(*I);
1412 }
1413 }
1414 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001415 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001416
1417 // cached and complete!
1418 if (!data().IvarListMissingImplementation)
1419 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001420
1421 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001422 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001423 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001424 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001425 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001426 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1427 layout.push_back(SynthesizeIvarChunk(
1428 IV->getASTContext().getTypeSize(IV->getType()), IV));
1429 continue;
1430 }
1431 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001432 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001433 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001434 curIvar->setNextIvar(IV);
1435 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001436 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001437
1438 if (!layout.empty()) {
1439 // Order synthesized ivars by their size.
1440 std::stable_sort(layout.begin(), layout.end());
1441 unsigned Ix = 0, EIx = layout.size();
1442 if (!data().IvarList) {
1443 data().IvarList = layout[0].Ivar; Ix++;
1444 curIvar = data().IvarList;
1445 }
1446 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1447 curIvar->setNextIvar(layout[Ix].Ivar);
1448 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001449 }
1450 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001451 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001452}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001453
1454/// FindCategoryDeclaration - Finds category declaration in the list of
1455/// categories for this class and returns it. Name of the category is passed
1456/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001457///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001458ObjCCategoryDecl *
1459ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001460 // FIXME: Should make sure no callers ever do this.
1461 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001462 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001463
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001464 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001465 LoadExternalDefinition();
1466
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001467 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001468 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001469 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001470
1471 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001472}
1473
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001474ObjCMethodDecl *
1475ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001476 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001477 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001478 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1479 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001480 }
1481
Craig Topper36250ad2014-05-12 05:36:57 +00001482 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001483}
1484
1485ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001486 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001487 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001488 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1489 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001490 }
Craig Topper36250ad2014-05-12 05:36:57 +00001491
1492 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001493}
1494
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001495/// ClassImplementsProtocol - Checks that 'lProto' protocol
1496/// has been implemented in IDecl class, its super class or categories (if
1497/// lookupCategory is true).
1498bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1499 bool lookupCategory,
1500 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001501 if (!hasDefinition())
1502 return false;
1503
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001504 ObjCInterfaceDecl *IDecl = this;
1505 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001506 for (auto *PI : IDecl->protocols()){
1507 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001508 return true;
1509 // This is dubious and is added to be compatible with gcc. In gcc, it is
1510 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1511 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1512 // object. This IMO, should be a bug.
1513 // FIXME: Treat this as an extension, and flag this as an error when GCC
1514 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001515 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001516 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001517 return true;
1518 }
Mike Stump11289f42009-09-09 15:08:12 +00001519
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001520 // 2nd, look up the category.
1521 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001522 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001523 for (auto *PI : Cat->protocols())
1524 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001525 return true;
1526 }
Mike Stump11289f42009-09-09 15:08:12 +00001527
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001528 // 3rd, look up the super class(s)
1529 if (IDecl->getSuperClass())
1530 return
1531 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1532 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001533
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001534 return false;
1535}
1536
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001537//===----------------------------------------------------------------------===//
1538// ObjCIvarDecl
1539//===----------------------------------------------------------------------===//
1540
David Blaikie68e081d2011-12-20 02:48:34 +00001541void ObjCIvarDecl::anchor() { }
1542
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001543ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001544 SourceLocation StartLoc,
1545 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001546 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001547 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001548 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001549 if (DC) {
1550 // Ivar's can only appear in interfaces, implementations (via synthesized
1551 // properties), and class extensions (via direct declaration, or synthesized
1552 // properties).
1553 //
1554 // FIXME: This should really be asserting this:
1555 // (isa<ObjCCategoryDecl>(DC) &&
1556 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1557 // but unfortunately we sometimes place ivars into non-class extension
1558 // categories on error. This breaks an AST invariant, and should not be
1559 // fixed.
1560 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1561 isa<ObjCCategoryDecl>(DC)) &&
1562 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001563 // Once a new ivar is created in any of class/class-extension/implementation
1564 // decl contexts, the previously built IvarList must be rebuilt.
1565 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1566 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001567 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001568 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001569 else
1570 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001571 }
Craig Topper36250ad2014-05-12 05:36:57 +00001572 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001573 }
1574
Richard Smithf7981722013-11-22 09:01:48 +00001575 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001576 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001577}
1578
Douglas Gregor72172e92012-01-05 21:55:30 +00001579ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001580 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1581 nullptr, QualType(), nullptr,
1582 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001583}
1584
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001585const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1586 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001587
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001588 switch (DC->getKind()) {
1589 default:
1590 case ObjCCategoryImpl:
1591 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001592 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001593
1594 // Ivars can only appear in class extension categories.
1595 case ObjCCategory: {
1596 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1597 assert(CD->IsClassExtension() && "invalid container for ivar!");
1598 return CD->getClassInterface();
1599 }
1600
1601 case ObjCImplementation:
1602 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1603
1604 case ObjCInterface:
1605 return cast<ObjCInterfaceDecl>(DC);
1606 }
1607}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001608
1609//===----------------------------------------------------------------------===//
1610// ObjCAtDefsFieldDecl
1611//===----------------------------------------------------------------------===//
1612
David Blaikie68e081d2011-12-20 02:48:34 +00001613void ObjCAtDefsFieldDecl::anchor() { }
1614
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001615ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001616*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1617 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001618 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001619 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001620}
1621
Richard Smithf7981722013-11-22 09:01:48 +00001622ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001623 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001624 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1625 SourceLocation(), nullptr, QualType(),
1626 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001627}
1628
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001629//===----------------------------------------------------------------------===//
1630// ObjCProtocolDecl
1631//===----------------------------------------------------------------------===//
1632
David Blaikie68e081d2011-12-20 02:48:34 +00001633void ObjCProtocolDecl::anchor() { }
1634
Richard Smith053f6c62014-05-16 23:01:30 +00001635ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1636 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001637 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001638 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001639 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1640 redeclarable_base(C), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001641 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001642 if (PrevDecl)
1643 Data = PrevDecl->Data;
1644}
1645
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001646ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001647 IdentifierInfo *Id,
1648 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001649 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001650 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001651 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001652 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001653 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001654 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001655}
1656
Richard Smithf7981722013-11-22 09:01:48 +00001657ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001658 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001659 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001660 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001661 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001662 Result->Data.setInt(!C.getLangOpts().Modules);
1663 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001664}
1665
Steve Naroff114aecb2009-03-01 16:12:44 +00001666ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1667 ObjCProtocolDecl *PDecl = this;
1668
1669 if (Name == getIdentifier())
1670 return PDecl;
1671
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001672 for (auto *I : protocols())
1673 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001674 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001675
Craig Topper36250ad2014-05-12 05:36:57 +00001676 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001677}
1678
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001679// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001680// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001681ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1682 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001683 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001684
Douglas Gregoreed49792013-01-17 00:38:46 +00001685 // If there is no definition or the definition is hidden, we don't find
1686 // anything.
1687 const ObjCProtocolDecl *Def = getDefinition();
1688 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001689 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001690
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001691 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001692 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001693
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001694 for (const auto *I : protocols())
1695 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001696 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001697 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001698}
1699
Douglas Gregore6e48b12012-01-01 19:29:29 +00001700void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001701 assert(!Data.getPointer() && "Protocol already has a definition!");
1702 Data.setPointer(new (getASTContext()) DefinitionData);
1703 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001704}
1705
1706void ObjCProtocolDecl::startDefinition() {
1707 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001708
1709 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001710 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001711 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001712}
1713
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001714void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1715 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001716
1717 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001718 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001719 // Insert into PM if not there already.
1720 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001721 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001722 }
1723 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001724 for (const auto *PI : PDecl->protocols())
1725 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001726 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001727}
1728
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001729
1730void ObjCProtocolDecl::collectInheritedProtocolProperties(
1731 const ObjCPropertyDecl *Property,
1732 ProtocolPropertyMap &PM) const {
1733 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1734 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001735 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001736 if (Prop == Property)
1737 continue;
1738 if (Prop->getIdentifier() == Property->getIdentifier()) {
1739 PM[PDecl] = Prop;
1740 MatchFound = true;
1741 break;
1742 }
1743 }
1744 // Scan through protocol's protocols which did not have a matching property.
1745 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001746 for (const auto *PI : PDecl->protocols())
1747 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001748 }
1749}
Anna Zaks673d76b2012-10-18 19:17:53 +00001750
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001751StringRef
1752ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001753 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1754 return ObjCRTName->getMetadataName();
1755
1756 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001757}
1758
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001759//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001760// ObjCCategoryDecl
1761//===----------------------------------------------------------------------===//
1762
David Blaikie68e081d2011-12-20 02:48:34 +00001763void ObjCCategoryDecl::anchor() { }
1764
Douglas Gregor85f3f952015-07-07 03:57:15 +00001765ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1766 SourceLocation ClassNameLoc,
1767 SourceLocation CategoryNameLoc,
1768 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1769 ObjCTypeParamList *typeParamList,
1770 SourceLocation IvarLBraceLoc,
1771 SourceLocation IvarRBraceLoc)
1772 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1773 ClassInterface(IDecl), TypeParamList(typeParamList),
1774 NextClassCategory(nullptr), CategoryNameLoc(CategoryNameLoc),
1775 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc)
1776{
1777 // Set the declaration context of each of the type parameters.
1778 if (typeParamList) {
1779 for (auto typeParam : *typeParamList) {
1780 typeParam->setDeclContext(this);
1781 }
1782 }
1783}
1784
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001785ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001786 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001787 SourceLocation ClassNameLoc,
1788 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001789 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001790 ObjCInterfaceDecl *IDecl,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001791 ObjCTypeParamList *typeParamList,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001792 SourceLocation IvarLBraceLoc,
1793 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001794 ObjCCategoryDecl *CatDecl =
1795 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001796 IDecl, typeParamList, IvarLBraceLoc,
1797 IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001798 if (IDecl) {
1799 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001800 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001801 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001802 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001803 if (ASTMutationListener *L = C.getASTMutationListener())
1804 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1805 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001806 }
1807
1808 return CatDecl;
1809}
1810
Richard Smithf7981722013-11-22 09:01:48 +00001811ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001812 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001813 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1814 SourceLocation(), SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001815 nullptr, nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001816}
1817
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001818ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1819 return getASTContext().getObjCImplementation(
1820 const_cast<ObjCCategoryDecl*>(this));
1821}
1822
1823void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1824 getASTContext().setObjCImplementation(this, ImplD);
1825}
1826
1827
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001828//===----------------------------------------------------------------------===//
1829// ObjCCategoryImplDecl
1830//===----------------------------------------------------------------------===//
1831
David Blaikie68e081d2011-12-20 02:48:34 +00001832void ObjCCategoryImplDecl::anchor() { }
1833
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001834ObjCCategoryImplDecl *
1835ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001836 IdentifierInfo *Id,
1837 ObjCInterfaceDecl *ClassInterface,
1838 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001839 SourceLocation atStartLoc,
1840 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001841 if (ClassInterface && ClassInterface->hasDefinition())
1842 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001843 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1844 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001845}
1846
Douglas Gregor72172e92012-01-05 21:55:30 +00001847ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1848 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001849 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1850 SourceLocation(), SourceLocation(),
1851 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001852}
1853
Steve Narofff406f4d2009-10-29 21:11:04 +00001854ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001855 // The class interface might be NULL if we are working with invalid code.
1856 if (const ObjCInterfaceDecl *ID = getClassInterface())
1857 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00001858 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001859}
1860
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001861
David Blaikie68e081d2011-12-20 02:48:34 +00001862void ObjCImplDecl::anchor() { }
1863
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001864void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001865 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001866 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001867 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001868}
1869
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001870void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1871 ASTContext &Ctx = getASTContext();
1872
1873 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001874 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001875 if (IFace)
1876 Ctx.setObjCImplementation(IFace, ImplD);
1877
Duncan Sands49c29ee2009-07-21 07:56:29 +00001878 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001879 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1880 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1881 Ctx.setObjCImplementation(CD, ImplD);
1882 }
1883
1884 ClassInterface = IFace;
1885}
1886
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001887/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001888/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001889/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001890///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001891ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001892FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001893 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001894 if (PID->getPropertyIvarDecl() &&
1895 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1896 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001897 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001898}
1899
1900/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001901/// added to the list of those properties \@synthesized/\@dynamic in this
1902/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001903///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001904ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001905FindPropertyImplDecl(IdentifierInfo *Id) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001906 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001907 if (PID->getPropertyDecl()->getIdentifier() == Id)
1908 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001909 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001910}
1911
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001912raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001913 const ObjCCategoryImplDecl &CID) {
1914 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001915 return OS;
1916}
1917
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001918//===----------------------------------------------------------------------===//
1919// ObjCImplementationDecl
1920//===----------------------------------------------------------------------===//
1921
David Blaikie68e081d2011-12-20 02:48:34 +00001922void ObjCImplementationDecl::anchor() { }
1923
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001924ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001925ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001926 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001927 ObjCInterfaceDecl *SuperDecl,
1928 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001929 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001930 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001931 SourceLocation IvarLBraceLoc,
1932 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001933 if (ClassInterface && ClassInterface->hasDefinition())
1934 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001935 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1936 nameLoc, atStartLoc, superLoc,
1937 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001938}
1939
Douglas Gregor72172e92012-01-05 21:55:30 +00001940ObjCImplementationDecl *
1941ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001942 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
1943 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001944}
1945
John McCall0410e572011-07-22 04:15:06 +00001946void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1947 CXXCtorInitializer ** initializers,
1948 unsigned numInitializers) {
1949 if (numInitializers > 0) {
1950 NumIvarInitializers = numInitializers;
1951 CXXCtorInitializer **ivarInitializers =
1952 new (C) CXXCtorInitializer*[NumIvarInitializers];
1953 memcpy(ivarInitializers, initializers,
1954 numInitializers * sizeof(CXXCtorInitializer*));
1955 IvarInitializers = ivarInitializers;
1956 }
1957}
1958
Richard Smithc2bb8182015-03-24 06:36:48 +00001959ObjCImplementationDecl::init_const_iterator
1960ObjCImplementationDecl::init_begin() const {
1961 return IvarInitializers.get(getASTContext().getExternalSource());
1962}
1963
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001964raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001965 const ObjCImplementationDecl &ID) {
1966 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001967 return OS;
1968}
1969
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001970//===----------------------------------------------------------------------===//
1971// ObjCCompatibleAliasDecl
1972//===----------------------------------------------------------------------===//
1973
David Blaikie68e081d2011-12-20 02:48:34 +00001974void ObjCCompatibleAliasDecl::anchor() { }
1975
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001976ObjCCompatibleAliasDecl *
1977ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1978 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001979 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001980 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001981 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001982}
1983
Douglas Gregor72172e92012-01-05 21:55:30 +00001984ObjCCompatibleAliasDecl *
1985ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001986 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
1987 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001988}
1989
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001990//===----------------------------------------------------------------------===//
1991// ObjCPropertyDecl
1992//===----------------------------------------------------------------------===//
1993
David Blaikie68e081d2011-12-20 02:48:34 +00001994void ObjCPropertyDecl::anchor() { }
1995
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001996ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1997 SourceLocation L,
1998 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001999 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002000 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00002001 QualType T,
2002 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002003 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002004 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2005 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002006}
2007
Richard Smithf7981722013-11-22 09:01:48 +00002008ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002009 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002010 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2011 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00002012 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00002013}
2014
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002015//===----------------------------------------------------------------------===//
2016// ObjCPropertyImplDecl
2017//===----------------------------------------------------------------------===//
2018
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002019ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002020 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002021 SourceLocation atLoc,
2022 SourceLocation L,
2023 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002024 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002025 ObjCIvarDecl *ivar,
2026 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002027 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2028 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002029}
Chris Lattnered0e1642008-03-17 01:19:02 +00002030
Richard Smithf7981722013-11-22 09:01:48 +00002031ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002032 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002033 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2034 SourceLocation(), nullptr, Dynamic,
2035 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002036}
2037
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002038SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2039 SourceLocation EndLoc = getLocation();
2040 if (IvarLoc.isValid())
2041 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002042
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002043 return SourceRange(AtLoc, EndLoc);
2044}