blob: 4b60a9eff1054d8a138f570b74fc50ca91606ed7 [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) {
Douglas Gregorb412e172010-07-25 18:17:45 +000028 List = 0;
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 {
David Blaikieff7d47a2012-12-19 00:45:41 +000057 lookup_const_result R = lookup(Id);
58 for (lookup_const_iterator Ivar = R.begin(), IvarEnd = R.end();
59 Ivar != IvarEnd; ++Ivar) {
Fariborz Jahanian68453832009-06-05 18:16:35 +000060 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61 return ivar;
62 }
63 return 0;
64}
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)
Douglas Gregoreed49792013-01-17 00:38:46 +000075 return 0;
76 }
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 //
David Blaikieff7d47a2012-12-19 00:45:41 +000086 lookup_const_result R = lookup(Sel);
87 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
88 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 }
Steve Naroff35c62ae2009-01-08 17:28:14 +000093 return 0;
94}
95
Fariborz Jahanian1446b342013-03-21 20:50:53 +000096/// HasUserDeclaredSetterMethod - This routine returns 'true' if a user declared setter
97/// method was 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' property.
99/// This is because, user must provide a setter method for the category's 'readwrite'
100/// property.
101bool
102ObjCContainerDecl::HasUserDeclaredSetterMethod(const ObjCPropertyDecl *Property) const {
103 Selector Sel = Property->getSetterName();
104 lookup_const_result R = lookup(Sel);
105 for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
106 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.
115 for (ObjCInterfaceDecl::visible_categories_iterator
116 Cat = ID->visible_categories_begin(),
117 CatEnd = ID->visible_categories_end();
118 Cat != CatEnd;
119 ++Cat) {
120 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
121 if (!MD->isImplicit())
122 return true;
123 if (Cat->IsClassExtension())
124 continue;
125 // Also search through the categories looking for a 'readwrite' declaration
126 // of this property. If one found, presumably a setter will be provided
127 // (properties declared in categories will not get auto-synthesized).
Aaron Ballmand174edf2014-03-13 19:11:50 +0000128 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000129 if (P->getIdentifier() == Property->getIdentifier()) {
130 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
131 return true;
132 break;
133 }
134 }
135
136 // Also look into protocols, for a user declared instance method.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000137 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000138 if (Proto->HasUserDeclaredSetterMethod(Property))
139 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000140
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000141 // And in its super class.
142 ObjCInterfaceDecl *OSC = ID->getSuperClass();
143 while (OSC) {
144 if (OSC->HasUserDeclaredSetterMethod(Property))
145 return true;
146 OSC = OSC->getSuperClass();
147 }
148 }
149 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
150 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
151 E = PD->protocol_end(); PI != E; ++PI) {
152 if ((*PI)->HasUserDeclaredSetterMethod(Property))
153 return true;
154 }
155 return false;
156}
157
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000158ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000159ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000160 IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000161 // If this context is a hidden protocol definition, don't find any
162 // property.
163 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
164 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
165 if (Def->isHidden())
166 return 0;
167 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000168
David Blaikieff7d47a2012-12-19 00:45:41 +0000169 DeclContext::lookup_const_result R = DC->lookup(propertyID);
170 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
171 ++I)
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000172 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
173 return PD;
174
175 return 0;
176}
177
Anna Zaks454477c2012-09-27 19:45:11 +0000178IdentifierInfo *
179ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
180 SmallString<128> ivarName;
181 {
182 llvm::raw_svector_ostream os(ivarName);
183 os << '_' << getIdentifier()->getName();
184 }
185 return &Ctx.Idents.get(ivarName.str());
186}
187
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000188/// FindPropertyDeclaration - Finds declaration of the property given its name
189/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000190ObjCPropertyDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000191ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000192 // Don't find properties within hidden protocol definitions.
193 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
194 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
195 if (Def->isHidden())
196 return 0;
197 }
Mike Stump11289f42009-09-09 15:08:12 +0000198
Ted Kremenekddcd1092010-03-15 20:11:53 +0000199 if (ObjCPropertyDecl *PD =
200 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
201 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenekddcd1092010-03-15 20:11:53 +0000203 switch (getKind()) {
204 default:
205 break;
206 case Decl::ObjCProtocol: {
207 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
208 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
209 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000210 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
211 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000212 break;
213 }
214 case Decl::ObjCInterface: {
215 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000216 // Look through categories (but not extensions).
217 for (ObjCInterfaceDecl::visible_categories_iterator
218 Cat = OID->visible_categories_begin(),
219 CatEnd = OID->visible_categories_end();
220 Cat != CatEnd; ++Cat) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000221 if (!Cat->IsClassExtension())
222 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
223 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000224 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000225
226 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000227 for (const auto *I : OID->all_referenced_protocols())
228 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000229 return P;
230
231 // Finally, check the super class.
232 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
233 return superClass->FindPropertyDeclaration(PropertyId);
234 break;
235 }
236 case Decl::ObjCCategory: {
237 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
238 // Look through protocols.
239 if (!OCD->IsClassExtension())
240 for (ObjCCategoryDecl::protocol_iterator
241 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
242 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
243 return P;
244
245 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000246 }
247 }
Steve Narofff9c65242008-06-05 13:55:23 +0000248 return 0;
249}
250
David Blaikie68e081d2011-12-20 02:48:34 +0000251void ObjCInterfaceDecl::anchor() { }
252
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000253/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
254/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000255/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000256///
257ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000258ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000259 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000260 // FIXME: Should make sure no callers ever do this.
261 if (!hasDefinition())
262 return 0;
263
264 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000265 LoadExternalDefinition();
266
Ted Kremenekd133a862010-03-15 20:30:07 +0000267 if (ObjCPropertyDecl *PD =
268 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
269 return PD;
270
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000271 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000272 for (const auto *I : all_referenced_protocols())
273 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000274 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000275
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000276 return 0;
277}
278
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000279void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
280 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000281 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000282 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000283 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000284 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000285 for (const auto *PI : all_referenced_protocols())
286 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000287 // Note, the properties declared only in class extensions are still copied
288 // into the main @interface's property list, and therefore we don't
289 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000290}
291
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000292bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
293 const ObjCInterfaceDecl *Class = this;
294 while (Class) {
295 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
296 return true;
297 Class = Class->getSuperClass();
298 }
299 return false;
300}
301
302const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
303 const ObjCInterfaceDecl *Class = this;
304 while (Class) {
305 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
306 return Class;
307 Class = Class->getSuperClass();
308 }
309 return 0;
310}
311
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000312void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
313 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
314 ASTContext &C)
315{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000316 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000317 LoadExternalDefinition();
318
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000319 if (data().AllReferencedProtocols.empty() &&
320 data().ReferencedProtocols.empty()) {
321 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000322 return;
323 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000324
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000325 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000326 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000327 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000328 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000329 for (unsigned i = 0; i < ExtNum; i++) {
330 bool protocolExists = false;
331 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000332 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000333 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
334 protocolExists = true;
335 break;
336 }
337 }
338 // Do we want to warn on a protocol in extension class which
339 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000340 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000341 ProtocolRefs.push_back(ProtoInExtension);
342 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000343
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000344 if (ProtocolRefs.empty())
345 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000346
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000347 // Merge ProtocolRefs into class's protocol list;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000348 for (auto *P : all_referenced_protocols()) {
349 ProtocolRefs.push_back(P);
Douglas Gregor002b6712010-01-16 15:02:53 +0000350 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000351
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000352 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000353}
354
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000355const ObjCInterfaceDecl *
356ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
357 const ObjCInterfaceDecl *IFace = this;
358 while (IFace) {
359 if (IFace->hasDesignatedInitializers())
360 return IFace;
361 if (!IFace->inheritsDesignatedInitializers())
362 break;
363 IFace = IFace->getSuperClass();
364 }
365 return 0;
366}
367
368bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
369 switch (data().InheritedDesignatedInitializers) {
370 case DefinitionData::IDI_Inherited:
371 return true;
372 case DefinitionData::IDI_NotInherited:
373 return false;
374 case DefinitionData::IDI_Unknown: {
375 bool isIntroducingInitializers = false;
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000376 for (const auto *MD : instance_methods()) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000377 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding()) {
378 isIntroducingInitializers = true;
379 break;
380 }
381 }
382 // If the class introduced initializers we conservatively assume that we
383 // don't know if any of them is a designated initializer to avoid possible
384 // misleading warnings.
385 if (isIntroducingInitializers) {
386 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
387 return false;
388 } else {
389 data().InheritedDesignatedInitializers = DefinitionData::IDI_Inherited;
390 return true;
391 }
392 }
393 }
394
395 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
396}
397
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000398void ObjCInterfaceDecl::getDesignatedInitializers(
399 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000400 // Check for a complete definition and recover if not so.
401 if (!isThisDeclarationADefinition())
402 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000403 if (data().ExternallyCompleted)
404 LoadExternalDefinition();
405
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000406 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000407 if (!IFace)
408 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000409
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000410 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000411 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000412 Methods.push_back(MD);
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000413}
414
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000415bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
416 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000417 // Check for a complete definition and recover if not so.
418 if (!isThisDeclarationADefinition())
419 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000420 if (data().ExternallyCompleted)
421 LoadExternalDefinition();
422
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000423 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000424 if (!IFace)
425 return false;
426
Argyrios Kyrtzidise919fc22013-12-10 18:36:43 +0000427 if (const ObjCMethodDecl *MD = IFace->getMethod(Sel, /*isInstance=*/true)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000428 if (MD->isThisDeclarationADesignatedInitializer()) {
429 if (InitMethod)
430 *InitMethod = MD;
431 return true;
432 }
433 }
434 return false;
435}
436
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000437void ObjCInterfaceDecl::allocateDefinitionData() {
438 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000439 Data.setPointer(new (getASTContext()) DefinitionData());
440 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000441
442 // Make the type point at the definition, now that we have one.
443 if (TypeForDecl)
444 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000445}
446
447void ObjCInterfaceDecl::startDefinition() {
448 allocateDefinitionData();
449
Douglas Gregor66b310c2011-12-15 18:03:09 +0000450 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000451 for (auto RD : redecls()) {
452 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000453 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000454 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000455}
456
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000457ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
458 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000459 // FIXME: Should make sure no callers ever do this.
460 if (!hasDefinition())
461 return 0;
462
463 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000464 LoadExternalDefinition();
465
Chris Lattner89375192008-03-16 00:19:01 +0000466 ObjCInterfaceDecl* ClassDecl = this;
467 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000468 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000469 clsDeclared = ClassDecl;
470 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000471 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000472
473 for (ObjCInterfaceDecl::visible_extensions_iterator
474 Ext = ClassDecl->visible_extensions_begin(),
475 ExtEnd = ClassDecl->visible_extensions_end();
476 Ext != ExtEnd; ++Ext) {
477 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000478 clsDeclared = ClassDecl;
479 return I;
480 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000481 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000482
Chris Lattner89375192008-03-16 00:19:01 +0000483 ClassDecl = ClassDecl->getSuperClass();
484 }
485 return NULL;
486}
487
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000488/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
489/// class whose name is passed as argument. If it is not one of the super classes
490/// the it returns NULL.
491ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
492 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000493 // FIXME: Should make sure no callers ever do this.
494 if (!hasDefinition())
495 return 0;
496
497 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000498 LoadExternalDefinition();
499
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000500 ObjCInterfaceDecl* ClassDecl = this;
501 while (ClassDecl != NULL) {
502 if (ClassDecl->getIdentifier() == ICName)
503 return ClassDecl;
504 ClassDecl = ClassDecl->getSuperClass();
505 }
506 return NULL;
507}
508
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000509ObjCProtocolDecl *
510ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000511 for (auto *P : all_referenced_protocols())
512 if (P->lookupProtocolNamed(Name))
513 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000514 ObjCInterfaceDecl *SuperClass = getSuperClass();
515 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
516}
517
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000518/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000519/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000520/// When argument category "C" is specified, any implicit method found
521/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000522ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000523 bool isInstance,
524 bool shallowCategoryLookup,
525 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000526 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000527{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000528 // FIXME: Should make sure no callers ever do this.
529 if (!hasDefinition())
530 return 0;
531
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000532 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000533 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000534
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000535 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000536 LoadExternalDefinition();
537
Ted Kremenek00781502013-11-23 01:01:29 +0000538 while (ClassDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000539 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000540 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000541
Chris Lattner89375192008-03-16 00:19:01 +0000542 // Didn't find one yet - look through protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000543 for (const auto *I : ClassDecl->protocols())
544 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000545 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000546
547 // Didn't find one yet - now look through categories.
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000548 for (ObjCInterfaceDecl::visible_categories_iterator
549 Cat = ClassDecl->visible_categories_begin(),
550 CatEnd = ClassDecl->visible_categories_end();
551 Cat != CatEnd; ++Cat) {
552 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
553 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000554 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000555
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000556 if (!shallowCategoryLookup) {
557 // Didn't find one yet - look through protocols.
558 const ObjCList<ObjCProtocolDecl> &Protocols =
559 Cat->getReferencedProtocols();
560 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
561 E = Protocols.end(); I != E; ++I)
562 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
563 if (C != (*Cat) || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000564 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000565 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000566 }
Ted Kremenek00781502013-11-23 01:01:29 +0000567
568 if (!followSuper)
569 return NULL;
570
571 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000572 ClassDecl = ClassDecl->getSuperClass();
573 }
574 return NULL;
575}
576
Anna Zaksc77a3b12012-07-27 19:07:44 +0000577// Will search "local" class/category implementations for a method decl.
578// If failed, then we search in class's root for an instance method.
579// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000580ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
581 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000582 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000583 // FIXME: Should make sure no callers ever do this.
584 if (!hasDefinition())
585 return 0;
586
587 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000588 LoadExternalDefinition();
589
Steve Naroffbb69c942009-10-01 23:46:04 +0000590 ObjCMethodDecl *Method = 0;
591 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000592 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
593 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000594
595 // Look through local category implementations associated with the class.
596 if (!Method)
597 Method = Instance ? getCategoryInstanceMethod(Sel)
598 : getCategoryClassMethod(Sel);
599
600 // Before we give up, check if the selector is an instance method.
601 // But only in the root. This matches gcc's behavior and what the
602 // runtime expects.
603 if (!Instance && !Method && !getSuperClass()) {
604 Method = lookupInstanceMethod(Sel);
605 // Look through local category implementations associated
606 // with the root class.
607 if (!Method)
608 Method = lookupPrivateMethod(Sel, true);
609 }
610
Steve Naroffbb69c942009-10-01 23:46:04 +0000611 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000612 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000613 return Method;
614}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000615
616//===----------------------------------------------------------------------===//
617// ObjCMethodDecl
618//===----------------------------------------------------------------------===//
619
Alp Toker314cc812014-01-25 16:55:45 +0000620ObjCMethodDecl *ObjCMethodDecl::Create(
621 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
622 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
623 DeclContext *contextDecl, bool isInstance, bool isVariadic,
624 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
625 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000626 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000627 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000628 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
629 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000630}
631
Douglas Gregor72172e92012-01-05 21:55:30 +0000632ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000633 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
634 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000635}
636
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000637bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
638 return getMethodFamily() == OMF_init &&
639 hasAttr<ObjCDesignatedInitializerAttr>();
640}
641
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000642bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
643 const ObjCMethodDecl **InitMethod) const {
644 if (getMethodFamily() != OMF_init)
645 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000646 const DeclContext *DC = getDeclContext();
647 if (isa<ObjCProtocolDecl>(DC))
648 return false;
649 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000650 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000651 return false;
652}
653
Douglas Gregora6017bb2012-10-09 17:21:28 +0000654Stmt *ObjCMethodDecl::getBody() const {
655 return Body.get(getASTContext().getExternalSource());
656}
657
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000658void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
659 assert(PrevMethod);
660 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
661 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000662 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000663}
664
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000665void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
666 ArrayRef<ParmVarDecl*> Params,
667 ArrayRef<SourceLocation> SelLocs) {
668 ParamsAndSelLocs = 0;
669 NumParams = Params.size();
670 if (Params.empty() && SelLocs.empty())
671 return;
672
673 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
674 sizeof(SourceLocation) * SelLocs.size();
675 ParamsAndSelLocs = C.Allocate(Size);
676 std::copy(Params.begin(), Params.end(), getParams());
677 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
678}
679
680void ObjCMethodDecl::getSelectorLocs(
681 SmallVectorImpl<SourceLocation> &SelLocs) const {
682 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
683 SelLocs.push_back(getSelectorLoc(i));
684}
685
686void ObjCMethodDecl::setMethodParams(ASTContext &C,
687 ArrayRef<ParmVarDecl*> Params,
688 ArrayRef<SourceLocation> SelLocs) {
689 assert((!SelLocs.empty() || isImplicit()) &&
690 "No selector locs for non-implicit method");
691 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000692 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000693
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000694 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
695 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000696 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000697 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000698
699 setParamsAndSelLocs(C, Params, SelLocs);
700}
701
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000702/// \brief A definition will return its interface declaration.
703/// An interface declaration will return its definition.
704/// Otherwise it will return itself.
705ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
706 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000707 ObjCMethodDecl *Redecl = 0;
708 if (HasRedeclaration)
709 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000710 if (Redecl)
711 return Redecl;
712
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000713 Decl *CtxD = cast<Decl>(getDeclContext());
714
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000715 if (!CtxD->isInvalidDecl()) {
716 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
717 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
718 if (!ImplD->isInvalidDecl())
719 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000720
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000721 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
722 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
723 if (!ImplD->isInvalidDecl())
724 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000725
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000726 } else if (ObjCImplementationDecl *ImplD =
727 dyn_cast<ObjCImplementationDecl>(CtxD)) {
728 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
729 if (!IFD->isInvalidDecl())
730 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000731
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000732 } else if (ObjCCategoryImplDecl *CImplD =
733 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
734 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
735 if (!CatD->isInvalidDecl())
736 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
737 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000738 }
739
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000740 if (!Redecl && isRedeclaration()) {
741 // This is the last redeclaration, go back to the first method.
742 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
743 isInstanceMethod());
744 }
745
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000746 return Redecl ? Redecl : this;
747}
748
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000749ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
750 Decl *CtxD = cast<Decl>(getDeclContext());
751
752 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
753 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
754 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
755 isInstanceMethod()))
756 return MD;
757
758 } else if (ObjCCategoryImplDecl *CImplD =
759 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000760 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000761 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
762 isInstanceMethod()))
763 return MD;
764 }
765
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000766 if (isRedeclaration())
767 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
768 isInstanceMethod());
769
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000770 return this;
771}
772
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000773SourceLocation ObjCMethodDecl::getLocEnd() const {
774 if (Stmt *Body = getBody())
775 return Body->getLocEnd();
776 return DeclEndLoc;
777}
778
John McCallb4526252011-03-02 01:50:55 +0000779ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
780 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000781 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000782 return family;
783
John McCall86bc21f2011-03-02 11:33:24 +0000784 // Check for an explicit attribute.
785 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
786 // The unfortunate necessity of mapping between enums here is due
787 // to the attributes framework.
788 switch (attr->getFamily()) {
789 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
790 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
791 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
792 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
793 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
794 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
795 }
796 Family = static_cast<unsigned>(family);
797 return family;
798 }
799
John McCallb4526252011-03-02 01:50:55 +0000800 family = getSelector().getMethodFamily();
801 switch (family) {
802 case OMF_None: break;
803
804 // init only has a conventional meaning for an instance method, and
805 // it has to return an object.
806 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000807 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000808 family = OMF_None;
809 break;
810
811 // alloc/copy/new have a conventional meaning for both class and
812 // instance methods, but they require an object return.
813 case OMF_alloc:
814 case OMF_copy:
815 case OMF_mutableCopy:
816 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000817 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000818 family = OMF_None;
819 break;
820
821 // These selectors have a conventional meaning only for instance methods.
822 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000823 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000824 case OMF_retain:
825 case OMF_release:
826 case OMF_autorelease:
827 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000828 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000829 if (!isInstanceMethod())
830 family = OMF_None;
831 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000832
833 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000834 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000835 family = OMF_None;
836 else {
837 unsigned noParams = param_size();
838 if (noParams < 1 || noParams > 3)
839 family = OMF_None;
840 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000841 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000842 QualType ArgT = (*it);
843 if (!ArgT->isObjCSelType()) {
844 family = OMF_None;
845 break;
846 }
847 while (--noParams) {
848 it++;
849 ArgT = (*it);
850 if (!ArgT->isObjCIdType()) {
851 family = OMF_None;
852 break;
853 }
854 }
855 }
856 }
857 break;
858
John McCallb4526252011-03-02 01:50:55 +0000859 }
860
861 // Cache the result.
862 Family = static_cast<unsigned>(family);
863 return family;
864}
865
Mike Stump11289f42009-09-09 15:08:12 +0000866void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000867 const ObjCInterfaceDecl *OID) {
868 QualType selfTy;
869 if (isInstanceMethod()) {
870 // There may be no interface context due to error in declaration
871 // of the interface (which has been reported). Recover gracefully.
872 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000873 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000874 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000875 } else {
876 selfTy = Context.getObjCIdType();
877 }
878 } else // we have a factory method.
879 selfTy = Context.getObjCClassType();
880
John McCalld4631322011-06-17 06:42:21 +0000881 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000882 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000883
David Blaikiebbafb8a2012-03-11 07:00:24 +0000884 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000885 if (isInstanceMethod()) {
886 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000887
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000888 // 'self' is always __strong. It's actually pseudo-strong except
889 // in init methods (or methods labeled ns_consumes_self), though.
890 Qualifiers qs;
891 qs.setObjCLifetime(Qualifiers::OCL_Strong);
892 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000893
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000894 // In addition, 'self' is const unless this is an init method.
895 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
896 selfTy = selfTy.withConst();
897 selfIsPseudoStrong = true;
898 }
899 }
900 else {
901 assert(isClassMethod());
902 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000903 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000904 selfIsPseudoStrong = true;
905 }
John McCall31168b02011-06-15 23:02:42 +0000906 }
907
908 ImplicitParamDecl *self
909 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
910 &Context.Idents.get("self"), selfTy);
911 setSelfDecl(self);
912
913 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000914 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000915
John McCalld4631322011-06-17 06:42:21 +0000916 if (selfIsPseudoStrong)
917 self->setARCPseudoStrong(true);
918
Mike Stump11289f42009-09-09 15:08:12 +0000919 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
920 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000921 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000922}
923
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000924ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
925 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
926 return ID;
927 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
928 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000929 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000930 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000931 if (isa<ObjCProtocolDecl>(getDeclContext()))
932 return 0;
David Blaikie83d382b2011-09-23 05:06:16 +0000933 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000934}
935
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000936static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
937 const ObjCMethodDecl *Method,
938 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
939 bool MovedToSuper) {
940 if (!Container)
941 return;
942
943 // In categories look for overriden methods from protocols. A method from
944 // category is not "overriden" since it is considered as the "same" method
945 // (same USR) as the one from the interface.
946 if (const ObjCCategoryDecl *
947 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
948 // Check whether we have a matching method at this category but only if we
949 // are at the super class level.
950 if (MovedToSuper)
951 if (ObjCMethodDecl *
952 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000953 Method->isInstanceMethod(),
954 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000955 if (Method != Overridden) {
956 // We found an override at this category; there is no need to look
957 // into its protocols.
958 Methods.push_back(Overridden);
959 return;
960 }
961
962 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
963 PEnd = Category->protocol_end();
964 P != PEnd; ++P)
965 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
966 return;
967 }
968
969 // Check whether we have a matching method at this level.
970 if (const ObjCMethodDecl *
971 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000972 Method->isInstanceMethod(),
973 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000974 if (Method != Overridden) {
975 // We found an override at this level; there is no need to look
976 // into other protocols or categories.
977 Methods.push_back(Overridden);
978 return;
979 }
980
981 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
982 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
983 PEnd = Protocol->protocol_end();
984 P != PEnd; ++P)
985 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
986 }
987
988 if (const ObjCInterfaceDecl *
989 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +0000990 for (const auto *P : Interface->protocols())
991 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000992
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000993 for (ObjCInterfaceDecl::known_categories_iterator
994 Cat = Interface->known_categories_begin(),
995 CatEnd = Interface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000996 Cat != CatEnd; ++Cat) {
997 CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000998 MovedToSuper);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000999 }
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001000
1001 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1002 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1003 /*MovedToSuper=*/true);
1004 }
1005}
1006
1007static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1008 const ObjCMethodDecl *Method,
1009 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1010 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1011 /*MovedToSuper=*/false);
1012}
1013
1014static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1015 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1016 assert(Method->isOverriding());
1017
1018 if (const ObjCProtocolDecl *
1019 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1020 CollectOverriddenMethods(ProtD, Method, overridden);
1021
1022 } else if (const ObjCImplDecl *
1023 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1024 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1025 if (!ID)
1026 return;
1027 // Start searching for overridden methods using the method from the
1028 // interface as starting point.
1029 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001030 Method->isInstanceMethod(),
1031 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001032 Method = IFaceMeth;
1033 CollectOverriddenMethods(ID, Method, overridden);
1034
1035 } else if (const ObjCCategoryDecl *
1036 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1037 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1038 if (!ID)
1039 return;
1040 // Start searching for overridden methods using the method from the
1041 // interface as starting point.
1042 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001043 Method->isInstanceMethod(),
1044 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001045 Method = IFaceMeth;
1046 CollectOverriddenMethods(ID, Method, overridden);
1047
1048 } else {
1049 CollectOverriddenMethods(
1050 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1051 Method, overridden);
1052 }
1053}
1054
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001055void ObjCMethodDecl::getOverriddenMethods(
1056 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1057 const ObjCMethodDecl *Method = this;
1058
1059 if (Method->isRedeclaration()) {
1060 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1061 getMethod(Method->getSelector(), Method->isInstanceMethod());
1062 }
1063
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001064 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001065 collectOverriddenMethodsSlow(Method, Overridden);
1066 assert(!Overridden.empty() &&
1067 "ObjCMethodDecl's overriding bit is not as expected");
1068 }
1069}
1070
Jordan Rose2bd991a2012-10-10 16:42:54 +00001071const ObjCPropertyDecl *
1072ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1073 Selector Sel = getSelector();
1074 unsigned NumArgs = Sel.getNumArgs();
1075 if (NumArgs > 1)
1076 return 0;
1077
Jordan Rose59e34ec2012-10-11 16:02:02 +00001078 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001079 return 0;
1080
1081 if (isPropertyAccessor()) {
1082 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001083 // If container is class extension, find its primary class.
1084 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1085 if (CatDecl->IsClassExtension())
1086 Container = CatDecl->getClassInterface();
1087
Jordan Rose2bd991a2012-10-10 16:42:54 +00001088 bool IsGetter = (NumArgs == 0);
1089
Aaron Ballmand174edf2014-03-13 19:11:50 +00001090 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001091 Selector NextSel = IsGetter ? I->getGetterName()
1092 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001093 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001094 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001095 }
1096
1097 llvm_unreachable("Marked as a property accessor but no property found!");
1098 }
1099
1100 if (!CheckOverrides)
1101 return 0;
1102
1103 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1104 OverridesTy Overrides;
1105 getOverriddenMethods(Overrides);
1106 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1107 I != E; ++I) {
1108 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1109 return Prop;
1110 }
1111
1112 return 0;
1113
1114}
1115
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001116//===----------------------------------------------------------------------===//
1117// ObjCInterfaceDecl
1118//===----------------------------------------------------------------------===//
1119
Douglas Gregord53ae832012-01-17 18:09:05 +00001120ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001121 DeclContext *DC,
1122 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001123 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001124 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001125 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001126 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001127 ObjCInterfaceDecl *Result = new (C, DC)
1128 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001129 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001130 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001131 return Result;
1132}
1133
Richard Smithf7981722013-11-22 09:01:48 +00001134ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001135 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001136 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1137 0, SourceLocation(),
1138 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001139 Result->Data.setInt(!C.getLangOpts().Modules);
1140 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001141}
1142
1143ObjCInterfaceDecl::
1144ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001145 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1146 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001147 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001148 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001149{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001150 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001151
1152 // Copy the 'data' pointer over.
1153 if (PrevDecl)
1154 Data = PrevDecl->Data;
1155
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001156 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001157}
1158
Douglas Gregor73693022010-12-01 23:49:52 +00001159void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001160 assert(data().ExternallyCompleted && "Class is not externally completed");
1161 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001162 getASTContext().getExternalSource()->CompleteType(
1163 const_cast<ObjCInterfaceDecl *>(this));
1164}
1165
1166void ObjCInterfaceDecl::setExternallyCompleted() {
1167 assert(getASTContext().getExternalSource() &&
1168 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001169 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001170 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001171 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001172}
1173
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001174void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001175 // Check for a complete definition and recover if not so.
1176 if (!isThisDeclarationADefinition())
1177 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001178 data().HasDesignatedInitializers = true;
1179}
1180
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001181bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001182 // Check for a complete definition and recover if not so.
1183 if (!isThisDeclarationADefinition())
1184 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001185 if (data().ExternallyCompleted)
1186 LoadExternalDefinition();
1187
1188 return data().HasDesignatedInitializers;
1189}
1190
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001191ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001192 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1193 if (data().ExternallyCompleted)
1194 LoadExternalDefinition();
1195
1196 return getASTContext().getObjCImplementation(
1197 const_cast<ObjCInterfaceDecl*>(Def));
1198 }
1199
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001200 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001201 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001202}
1203
1204void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001205 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001206}
1207
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001208namespace {
1209 struct SynthesizeIvarChunk {
1210 uint64_t Size;
1211 ObjCIvarDecl *Ivar;
1212 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1213 : Size(size), Ivar(ivar) {}
1214 };
1215
1216 bool operator<(const SynthesizeIvarChunk & LHS,
1217 const SynthesizeIvarChunk &RHS) {
1218 return LHS.Size < RHS.Size;
1219 }
1220}
1221
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001222/// all_declared_ivar_begin - return first ivar declared in this class,
1223/// its extensions and its implementation. Lazily build the list on first
1224/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001225///
1226/// Caveat: The list returned by this method reflects the current
1227/// state of the parser. The cache will be updated for every ivar
1228/// added by an extension or the implementation when they are
1229/// encountered.
1230/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001231ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001232 // FIXME: Should make sure no callers ever do this.
1233 if (!hasDefinition())
1234 return 0;
1235
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001236 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001237 if (!data().IvarList) {
1238 if (!ivar_empty()) {
1239 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1240 data().IvarList = *I; ++I;
1241 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001242 curIvar->setNextIvar(*I);
1243 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001244
1245 for (ObjCInterfaceDecl::known_extensions_iterator
1246 Ext = known_extensions_begin(),
1247 ExtEnd = known_extensions_end();
1248 Ext != ExtEnd; ++Ext) {
1249 if (!Ext->ivar_empty()) {
1250 ObjCCategoryDecl::ivar_iterator
1251 I = Ext->ivar_begin(),
1252 E = Ext->ivar_end();
1253 if (!data().IvarList) {
1254 data().IvarList = *I; ++I;
1255 curIvar = data().IvarList;
1256 }
1257 for ( ;I != E; curIvar = *I, ++I)
1258 curIvar->setNextIvar(*I);
1259 }
1260 }
1261 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001262 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001263
1264 // cached and complete!
1265 if (!data().IvarListMissingImplementation)
1266 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001267
1268 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001269 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001270 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001271 SmallVector<SynthesizeIvarChunk, 16> layout;
1272 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1273 E = ImplDecl->ivar_end(); I != E; ++I) {
1274 ObjCIvarDecl *IV = *I;
1275 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1276 layout.push_back(SynthesizeIvarChunk(
1277 IV->getASTContext().getTypeSize(IV->getType()), IV));
1278 continue;
1279 }
1280 if (!data().IvarList)
1281 data().IvarList = *I;
1282 else
1283 curIvar->setNextIvar(*I);
1284 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001285 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001286
1287 if (!layout.empty()) {
1288 // Order synthesized ivars by their size.
1289 std::stable_sort(layout.begin(), layout.end());
1290 unsigned Ix = 0, EIx = layout.size();
1291 if (!data().IvarList) {
1292 data().IvarList = layout[0].Ivar; Ix++;
1293 curIvar = data().IvarList;
1294 }
1295 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1296 curIvar->setNextIvar(layout[Ix].Ivar);
1297 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001298 }
1299 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001300 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001301}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001302
1303/// FindCategoryDeclaration - Finds category declaration in the list of
1304/// categories for this class and returns it. Name of the category is passed
1305/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001306///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001307ObjCCategoryDecl *
1308ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001309 // FIXME: Should make sure no callers ever do this.
1310 if (!hasDefinition())
1311 return 0;
1312
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001313 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001314 LoadExternalDefinition();
1315
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001316 for (visible_categories_iterator Cat = visible_categories_begin(),
1317 CatEnd = visible_categories_end();
1318 Cat != CatEnd;
1319 ++Cat) {
1320 if (Cat->getIdentifier() == CategoryId)
1321 return *Cat;
1322 }
1323
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001324 return 0;
1325}
1326
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001327ObjCMethodDecl *
1328ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001329 for (visible_categories_iterator Cat = visible_categories_begin(),
1330 CatEnd = visible_categories_end();
1331 Cat != CatEnd;
1332 ++Cat) {
1333 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001334 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1335 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001336 }
1337
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001338 return 0;
1339}
1340
1341ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001342 for (visible_categories_iterator Cat = visible_categories_begin(),
1343 CatEnd = visible_categories_end();
1344 Cat != CatEnd;
1345 ++Cat) {
1346 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001347 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1348 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001349 }
1350
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001351 return 0;
1352}
1353
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001354/// ClassImplementsProtocol - Checks that 'lProto' protocol
1355/// has been implemented in IDecl class, its super class or categories (if
1356/// lookupCategory is true).
1357bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1358 bool lookupCategory,
1359 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001360 if (!hasDefinition())
1361 return false;
1362
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001363 ObjCInterfaceDecl *IDecl = this;
1364 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001365 for (auto *PI : IDecl->protocols()){
1366 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001367 return true;
1368 // This is dubious and is added to be compatible with gcc. In gcc, it is
1369 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1370 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1371 // object. This IMO, should be a bug.
1372 // FIXME: Treat this as an extension, and flag this as an error when GCC
1373 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001374 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001375 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001376 return true;
1377 }
Mike Stump11289f42009-09-09 15:08:12 +00001378
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001379 // 2nd, look up the category.
1380 if (lookupCategory)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001381 for (visible_categories_iterator Cat = visible_categories_begin(),
1382 CatEnd = visible_categories_end();
1383 Cat != CatEnd;
1384 ++Cat) {
1385 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1386 E = Cat->protocol_end();
1387 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001388 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1389 return true;
1390 }
Mike Stump11289f42009-09-09 15:08:12 +00001391
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001392 // 3rd, look up the super class(s)
1393 if (IDecl->getSuperClass())
1394 return
1395 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1396 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001397
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001398 return false;
1399}
1400
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001401//===----------------------------------------------------------------------===//
1402// ObjCIvarDecl
1403//===----------------------------------------------------------------------===//
1404
David Blaikie68e081d2011-12-20 02:48:34 +00001405void ObjCIvarDecl::anchor() { }
1406
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001407ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001408 SourceLocation StartLoc,
1409 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001410 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001411 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001412 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001413 if (DC) {
1414 // Ivar's can only appear in interfaces, implementations (via synthesized
1415 // properties), and class extensions (via direct declaration, or synthesized
1416 // properties).
1417 //
1418 // FIXME: This should really be asserting this:
1419 // (isa<ObjCCategoryDecl>(DC) &&
1420 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1421 // but unfortunately we sometimes place ivars into non-class extension
1422 // categories on error. This breaks an AST invariant, and should not be
1423 // fixed.
1424 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1425 isa<ObjCCategoryDecl>(DC)) &&
1426 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001427 // Once a new ivar is created in any of class/class-extension/implementation
1428 // decl contexts, the previously built IvarList must be rebuilt.
1429 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1430 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001431 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001432 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001433 else
1434 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001435 }
1436 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001437 }
1438
Richard Smithf7981722013-11-22 09:01:48 +00001439 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001440 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001441}
1442
Douglas Gregor72172e92012-01-05 21:55:30 +00001443ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001444 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001445 QualType(), 0, ObjCIvarDecl::None, 0, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001446}
1447
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001448const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1449 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001450
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001451 switch (DC->getKind()) {
1452 default:
1453 case ObjCCategoryImpl:
1454 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001455 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001456
1457 // Ivars can only appear in class extension categories.
1458 case ObjCCategory: {
1459 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1460 assert(CD->IsClassExtension() && "invalid container for ivar!");
1461 return CD->getClassInterface();
1462 }
1463
1464 case ObjCImplementation:
1465 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1466
1467 case ObjCInterface:
1468 return cast<ObjCInterfaceDecl>(DC);
1469 }
1470}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001471
1472//===----------------------------------------------------------------------===//
1473// ObjCAtDefsFieldDecl
1474//===----------------------------------------------------------------------===//
1475
David Blaikie68e081d2011-12-20 02:48:34 +00001476void ObjCAtDefsFieldDecl::anchor() { }
1477
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001478ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001479*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1480 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001481 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001482 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001483}
1484
Richard Smithf7981722013-11-22 09:01:48 +00001485ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001486 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001487 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1488 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001489}
1490
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001491//===----------------------------------------------------------------------===//
1492// ObjCProtocolDecl
1493//===----------------------------------------------------------------------===//
1494
David Blaikie68e081d2011-12-20 02:48:34 +00001495void ObjCProtocolDecl::anchor() { }
1496
Douglas Gregor32c17572012-01-01 20:30:41 +00001497ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1498 SourceLocation nameLoc,
1499 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001500 ObjCProtocolDecl *PrevDecl)
1501 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001502{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001503 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001504 if (PrevDecl)
1505 Data = PrevDecl->Data;
1506}
1507
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001508ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001509 IdentifierInfo *Id,
1510 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001511 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001512 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001513 ObjCProtocolDecl *Result =
1514 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001515 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001516 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001517}
1518
Richard Smithf7981722013-11-22 09:01:48 +00001519ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001520 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001521 ObjCProtocolDecl *Result =
1522 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001523 Result->Data.setInt(!C.getLangOpts().Modules);
1524 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001525}
1526
Steve Naroff114aecb2009-03-01 16:12:44 +00001527ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1528 ObjCProtocolDecl *PDecl = this;
1529
1530 if (Name == getIdentifier())
1531 return PDecl;
1532
1533 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1534 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1535 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001536
Steve Naroff114aecb2009-03-01 16:12:44 +00001537 return NULL;
1538}
1539
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001540// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001541// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001542ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1543 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001544 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001545
Douglas Gregoreed49792013-01-17 00:38:46 +00001546 // If there is no definition or the definition is hidden, we don't find
1547 // anything.
1548 const ObjCProtocolDecl *Def = getDefinition();
1549 if (!Def || Def->isHidden())
1550 return NULL;
1551
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001552 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001553 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001554
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001555 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001556 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001557 return MethodDecl;
1558 return NULL;
1559}
1560
Douglas Gregore6e48b12012-01-01 19:29:29 +00001561void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001562 assert(!Data.getPointer() && "Protocol already has a definition!");
1563 Data.setPointer(new (getASTContext()) DefinitionData);
1564 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001565}
1566
1567void ObjCProtocolDecl::startDefinition() {
1568 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001569
1570 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001571 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001572 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001573}
1574
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001575void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1576 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001577
1578 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001579 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001580 // Insert into PM if not there already.
1581 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001582 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001583 }
1584 // Scan through protocol's protocols.
1585 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1586 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001587 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001588 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001589}
1590
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001591
1592void ObjCProtocolDecl::collectInheritedProtocolProperties(
1593 const ObjCPropertyDecl *Property,
1594 ProtocolPropertyMap &PM) const {
1595 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1596 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001597 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001598 if (Prop == Property)
1599 continue;
1600 if (Prop->getIdentifier() == Property->getIdentifier()) {
1601 PM[PDecl] = Prop;
1602 MatchFound = true;
1603 break;
1604 }
1605 }
1606 // Scan through protocol's protocols which did not have a matching property.
1607 if (!MatchFound)
1608 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1609 E = PDecl->protocol_end(); PI != E; ++PI)
1610 (*PI)->collectInheritedProtocolProperties(Property, PM);
1611 }
1612}
Anna Zaks673d76b2012-10-18 19:17:53 +00001613
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001614//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001615// ObjCCategoryDecl
1616//===----------------------------------------------------------------------===//
1617
David Blaikie68e081d2011-12-20 02:48:34 +00001618void ObjCCategoryDecl::anchor() { }
1619
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001620ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001621 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001622 SourceLocation ClassNameLoc,
1623 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001624 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001625 ObjCInterfaceDecl *IDecl,
1626 SourceLocation IvarLBraceLoc,
1627 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001628 ObjCCategoryDecl *CatDecl =
1629 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1630 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001631 if (IDecl) {
1632 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001633 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001634 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001635 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001636 if (ASTMutationListener *L = C.getASTMutationListener())
1637 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1638 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001639 }
1640
1641 return CatDecl;
1642}
1643
Richard Smithf7981722013-11-22 09:01:48 +00001644ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001645 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001646 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1647 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001648}
1649
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001650ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1651 return getASTContext().getObjCImplementation(
1652 const_cast<ObjCCategoryDecl*>(this));
1653}
1654
1655void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1656 getASTContext().setObjCImplementation(this, ImplD);
1657}
1658
1659
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001660//===----------------------------------------------------------------------===//
1661// ObjCCategoryImplDecl
1662//===----------------------------------------------------------------------===//
1663
David Blaikie68e081d2011-12-20 02:48:34 +00001664void ObjCCategoryImplDecl::anchor() { }
1665
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001666ObjCCategoryImplDecl *
1667ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001668 IdentifierInfo *Id,
1669 ObjCInterfaceDecl *ClassInterface,
1670 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001671 SourceLocation atStartLoc,
1672 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001673 if (ClassInterface && ClassInterface->hasDefinition())
1674 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001675 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1676 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001677}
1678
Douglas Gregor72172e92012-01-05 21:55:30 +00001679ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1680 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001681 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1682 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001683}
1684
Steve Narofff406f4d2009-10-29 21:11:04 +00001685ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001686 // The class interface might be NULL if we are working with invalid code.
1687 if (const ObjCInterfaceDecl *ID = getClassInterface())
1688 return ID->FindCategoryDeclaration(getIdentifier());
1689 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001690}
1691
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001692
David Blaikie68e081d2011-12-20 02:48:34 +00001693void ObjCImplDecl::anchor() { }
1694
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001695void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001696 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001697 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001698 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001699}
1700
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001701void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1702 ASTContext &Ctx = getASTContext();
1703
1704 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001705 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001706 if (IFace)
1707 Ctx.setObjCImplementation(IFace, ImplD);
1708
Duncan Sands49c29ee2009-07-21 07:56:29 +00001709 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001710 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1711 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1712 Ctx.setObjCImplementation(CD, ImplD);
1713 }
1714
1715 ClassInterface = IFace;
1716}
1717
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001718/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001719/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001720/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001721///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001722ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001723FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1724 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001725 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001726 if (PID->getPropertyIvarDecl() &&
1727 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1728 return PID;
1729 }
1730 return 0;
1731}
1732
1733/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001734/// added to the list of those properties \@synthesized/\@dynamic in this
1735/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001736///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001737ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001738FindPropertyImplDecl(IdentifierInfo *Id) const {
1739 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001740 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001741 if (PID->getPropertyDecl()->getIdentifier() == Id)
1742 return PID;
1743 }
1744 return 0;
1745}
1746
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001747raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001748 const ObjCCategoryImplDecl &CID) {
1749 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001750 return OS;
1751}
1752
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001753//===----------------------------------------------------------------------===//
1754// ObjCImplementationDecl
1755//===----------------------------------------------------------------------===//
1756
David Blaikie68e081d2011-12-20 02:48:34 +00001757void ObjCImplementationDecl::anchor() { }
1758
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001759ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001760ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001761 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001762 ObjCInterfaceDecl *SuperDecl,
1763 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001764 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001765 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001766 SourceLocation IvarLBraceLoc,
1767 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001768 if (ClassInterface && ClassInterface->hasDefinition())
1769 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001770 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1771 nameLoc, atStartLoc, superLoc,
1772 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001773}
1774
Douglas Gregor72172e92012-01-05 21:55:30 +00001775ObjCImplementationDecl *
1776ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001777 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1778 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001779}
1780
John McCall0410e572011-07-22 04:15:06 +00001781void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1782 CXXCtorInitializer ** initializers,
1783 unsigned numInitializers) {
1784 if (numInitializers > 0) {
1785 NumIvarInitializers = numInitializers;
1786 CXXCtorInitializer **ivarInitializers =
1787 new (C) CXXCtorInitializer*[NumIvarInitializers];
1788 memcpy(ivarInitializers, initializers,
1789 numInitializers * sizeof(CXXCtorInitializer*));
1790 IvarInitializers = ivarInitializers;
1791 }
1792}
1793
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001794raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001795 const ObjCImplementationDecl &ID) {
1796 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001797 return OS;
1798}
1799
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001800//===----------------------------------------------------------------------===//
1801// ObjCCompatibleAliasDecl
1802//===----------------------------------------------------------------------===//
1803
David Blaikie68e081d2011-12-20 02:48:34 +00001804void ObjCCompatibleAliasDecl::anchor() { }
1805
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001806ObjCCompatibleAliasDecl *
1807ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1808 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001809 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001810 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001811 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001812}
1813
Douglas Gregor72172e92012-01-05 21:55:30 +00001814ObjCCompatibleAliasDecl *
1815ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001816 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001817}
1818
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001819//===----------------------------------------------------------------------===//
1820// ObjCPropertyDecl
1821//===----------------------------------------------------------------------===//
1822
David Blaikie68e081d2011-12-20 02:48:34 +00001823void ObjCPropertyDecl::anchor() { }
1824
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001825ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1826 SourceLocation L,
1827 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001828 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001829 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001830 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001831 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001832 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001833}
1834
Richard Smithf7981722013-11-22 09:01:48 +00001835ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001836 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001837 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1838 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001839}
1840
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001841//===----------------------------------------------------------------------===//
1842// ObjCPropertyImplDecl
1843//===----------------------------------------------------------------------===//
1844
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001845ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001846 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001847 SourceLocation atLoc,
1848 SourceLocation L,
1849 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001850 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001851 ObjCIvarDecl *ivar,
1852 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001853 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1854 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001855}
Chris Lattnered0e1642008-03-17 01:19:02 +00001856
Richard Smithf7981722013-11-22 09:01:48 +00001857ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001858 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001859 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1860 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001861}
1862
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001863SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1864 SourceLocation EndLoc = getLocation();
1865 if (IvarLoc.isValid())
1866 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001867
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001868 return SourceRange(AtLoc, EndLoc);
1869}