blob: 0844a222f8cb40bf830604473bcc334aefc7066f [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.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000115 for (const auto *Cat : ID->visible_categories()) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000116 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
117 if (!MD->isImplicit())
118 return true;
119 if (Cat->IsClassExtension())
120 continue;
121 // Also search through the categories looking for a 'readwrite' declaration
122 // of this property. If one found, presumably a setter will be provided
123 // (properties declared in categories will not get auto-synthesized).
Aaron Ballmand174edf2014-03-13 19:11:50 +0000124 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000125 if (P->getIdentifier() == Property->getIdentifier()) {
126 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
127 return true;
128 break;
129 }
130 }
131
132 // Also look into protocols, for a user declared instance method.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000133 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000134 if (Proto->HasUserDeclaredSetterMethod(Property))
135 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000136
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000137 // And in its super class.
138 ObjCInterfaceDecl *OSC = ID->getSuperClass();
139 while (OSC) {
140 if (OSC->HasUserDeclaredSetterMethod(Property))
141 return true;
142 OSC = OSC->getSuperClass();
143 }
144 }
145 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
146 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
147 E = PD->protocol_end(); PI != E; ++PI) {
148 if ((*PI)->HasUserDeclaredSetterMethod(Property))
149 return true;
150 }
151 return false;
152}
153
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000154ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000155ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000156 IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000157 // If this context is a hidden protocol definition, don't find any
158 // property.
159 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
160 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
161 if (Def->isHidden())
162 return 0;
163 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000164
David Blaikieff7d47a2012-12-19 00:45:41 +0000165 DeclContext::lookup_const_result R = DC->lookup(propertyID);
166 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
167 ++I)
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000168 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
169 return PD;
170
171 return 0;
172}
173
Anna Zaks454477c2012-09-27 19:45:11 +0000174IdentifierInfo *
175ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
176 SmallString<128> ivarName;
177 {
178 llvm::raw_svector_ostream os(ivarName);
179 os << '_' << getIdentifier()->getName();
180 }
181 return &Ctx.Idents.get(ivarName.str());
182}
183
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000184/// FindPropertyDeclaration - Finds declaration of the property given its name
185/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000186ObjCPropertyDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000187ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000188 // Don't find properties within hidden protocol definitions.
189 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
190 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
191 if (Def->isHidden())
192 return 0;
193 }
Mike Stump11289f42009-09-09 15:08:12 +0000194
Ted Kremenekddcd1092010-03-15 20:11:53 +0000195 if (ObjCPropertyDecl *PD =
196 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
197 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000198
Ted Kremenekddcd1092010-03-15 20:11:53 +0000199 switch (getKind()) {
200 default:
201 break;
202 case Decl::ObjCProtocol: {
203 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
204 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
205 E = PID->protocol_end(); I != E; ++I)
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000206 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
207 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000208 break;
209 }
210 case Decl::ObjCInterface: {
211 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000212 // Look through categories (but not extensions).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000213 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000214 if (!Cat->IsClassExtension())
215 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
216 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000217 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000218
219 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000220 for (const auto *I : OID->all_referenced_protocols())
221 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000222 return P;
223
224 // Finally, check the super class.
225 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
226 return superClass->FindPropertyDeclaration(PropertyId);
227 break;
228 }
229 case Decl::ObjCCategory: {
230 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
231 // Look through protocols.
232 if (!OCD->IsClassExtension())
233 for (ObjCCategoryDecl::protocol_iterator
234 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
235 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
236 return P;
237
238 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000239 }
240 }
Steve Narofff9c65242008-06-05 13:55:23 +0000241 return 0;
242}
243
David Blaikie68e081d2011-12-20 02:48:34 +0000244void ObjCInterfaceDecl::anchor() { }
245
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000246/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
247/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000248/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000249///
250ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000251ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000252 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000253 // FIXME: Should make sure no callers ever do this.
254 if (!hasDefinition())
255 return 0;
256
257 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000258 LoadExternalDefinition();
259
Ted Kremenekd133a862010-03-15 20:30:07 +0000260 if (ObjCPropertyDecl *PD =
261 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
262 return PD;
263
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000264 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000265 for (const auto *I : all_referenced_protocols())
266 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000267 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000268
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000269 return 0;
270}
271
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000272void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
273 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000274 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000275 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000276 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000277 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000278 for (const auto *PI : all_referenced_protocols())
279 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000280 // Note, the properties declared only in class extensions are still copied
281 // into the main @interface's property list, and therefore we don't
282 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000283}
284
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000285bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
286 const ObjCInterfaceDecl *Class = this;
287 while (Class) {
288 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
289 return true;
290 Class = Class->getSuperClass();
291 }
292 return false;
293}
294
295const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
296 const ObjCInterfaceDecl *Class = this;
297 while (Class) {
298 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
299 return Class;
300 Class = Class->getSuperClass();
301 }
302 return 0;
303}
304
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000305void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
306 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
307 ASTContext &C)
308{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000309 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000310 LoadExternalDefinition();
311
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000312 if (data().AllReferencedProtocols.empty() &&
313 data().ReferencedProtocols.empty()) {
314 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000315 return;
316 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000317
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000318 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000319 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000320 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000321 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000322 for (unsigned i = 0; i < ExtNum; i++) {
323 bool protocolExists = false;
324 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000325 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000326 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
327 protocolExists = true;
328 break;
329 }
330 }
331 // Do we want to warn on a protocol in extension class which
332 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000333 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000334 ProtocolRefs.push_back(ProtoInExtension);
335 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000336
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000337 if (ProtocolRefs.empty())
338 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000339
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000340 // Merge ProtocolRefs into class's protocol list;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000341 for (auto *P : all_referenced_protocols()) {
342 ProtocolRefs.push_back(P);
Douglas Gregor002b6712010-01-16 15:02:53 +0000343 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000344
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000345 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000346}
347
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000348const ObjCInterfaceDecl *
349ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
350 const ObjCInterfaceDecl *IFace = this;
351 while (IFace) {
352 if (IFace->hasDesignatedInitializers())
353 return IFace;
354 if (!IFace->inheritsDesignatedInitializers())
355 break;
356 IFace = IFace->getSuperClass();
357 }
358 return 0;
359}
360
361bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
362 switch (data().InheritedDesignatedInitializers) {
363 case DefinitionData::IDI_Inherited:
364 return true;
365 case DefinitionData::IDI_NotInherited:
366 return false;
367 case DefinitionData::IDI_Unknown: {
368 bool isIntroducingInitializers = false;
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000369 for (const auto *MD : instance_methods()) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000370 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding()) {
371 isIntroducingInitializers = true;
372 break;
373 }
374 }
375 // If the class introduced initializers we conservatively assume that we
376 // don't know if any of them is a designated initializer to avoid possible
377 // misleading warnings.
378 if (isIntroducingInitializers) {
379 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
380 return false;
381 } else {
382 data().InheritedDesignatedInitializers = DefinitionData::IDI_Inherited;
383 return true;
384 }
385 }
386 }
387
388 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
389}
390
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000391void ObjCInterfaceDecl::getDesignatedInitializers(
392 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000393 // Check for a complete definition and recover if not so.
394 if (!isThisDeclarationADefinition())
395 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000396 if (data().ExternallyCompleted)
397 LoadExternalDefinition();
398
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000399 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000400 if (!IFace)
401 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000402
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000403 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000404 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000405 Methods.push_back(MD);
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000406}
407
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000408bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
409 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000410 // Check for a complete definition and recover if not so.
411 if (!isThisDeclarationADefinition())
412 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000413 if (data().ExternallyCompleted)
414 LoadExternalDefinition();
415
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000416 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000417 if (!IFace)
418 return false;
419
Argyrios Kyrtzidise919fc22013-12-10 18:36:43 +0000420 if (const ObjCMethodDecl *MD = IFace->getMethod(Sel, /*isInstance=*/true)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000421 if (MD->isThisDeclarationADesignatedInitializer()) {
422 if (InitMethod)
423 *InitMethod = MD;
424 return true;
425 }
426 }
427 return false;
428}
429
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000430void ObjCInterfaceDecl::allocateDefinitionData() {
431 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000432 Data.setPointer(new (getASTContext()) DefinitionData());
433 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000434
435 // Make the type point at the definition, now that we have one.
436 if (TypeForDecl)
437 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000438}
439
440void ObjCInterfaceDecl::startDefinition() {
441 allocateDefinitionData();
442
Douglas Gregor66b310c2011-12-15 18:03:09 +0000443 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000444 for (auto RD : redecls()) {
445 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000446 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000447 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000448}
449
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000450ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
451 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000452 // FIXME: Should make sure no callers ever do this.
453 if (!hasDefinition())
454 return 0;
455
456 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000457 LoadExternalDefinition();
458
Chris Lattner89375192008-03-16 00:19:01 +0000459 ObjCInterfaceDecl* ClassDecl = this;
460 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000461 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000462 clsDeclared = ClassDecl;
463 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000464 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000465
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000466 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000467 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000468 clsDeclared = ClassDecl;
469 return I;
470 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000471 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000472
Chris Lattner89375192008-03-16 00:19:01 +0000473 ClassDecl = ClassDecl->getSuperClass();
474 }
475 return NULL;
476}
477
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000478/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
479/// class whose name is passed as argument. If it is not one of the super classes
480/// the it returns NULL.
481ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
482 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000483 // FIXME: Should make sure no callers ever do this.
484 if (!hasDefinition())
485 return 0;
486
487 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000488 LoadExternalDefinition();
489
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000490 ObjCInterfaceDecl* ClassDecl = this;
491 while (ClassDecl != NULL) {
492 if (ClassDecl->getIdentifier() == ICName)
493 return ClassDecl;
494 ClassDecl = ClassDecl->getSuperClass();
495 }
496 return NULL;
497}
498
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000499ObjCProtocolDecl *
500ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000501 for (auto *P : all_referenced_protocols())
502 if (P->lookupProtocolNamed(Name))
503 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000504 ObjCInterfaceDecl *SuperClass = getSuperClass();
505 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
506}
507
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000508/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000509/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000510/// When argument category "C" is specified, any implicit method found
511/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000512ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000513 bool isInstance,
514 bool shallowCategoryLookup,
515 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000516 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000517{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000518 // FIXME: Should make sure no callers ever do this.
519 if (!hasDefinition())
520 return 0;
521
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000522 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000523 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000524
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000525 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000526 LoadExternalDefinition();
527
Ted Kremenek00781502013-11-23 01:01:29 +0000528 while (ClassDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000529 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000530 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000531
Chris Lattner89375192008-03-16 00:19:01 +0000532 // Didn't find one yet - look through protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000533 for (const auto *I : ClassDecl->protocols())
534 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000535 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000536
537 // Didn't find one yet - now look through categories.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000538 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000539 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000540 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000541 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000542
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000543 if (!shallowCategoryLookup) {
544 // Didn't find one yet - look through protocols.
545 const ObjCList<ObjCProtocolDecl> &Protocols =
546 Cat->getReferencedProtocols();
547 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
548 E = Protocols.end(); I != E; ++I)
549 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000550 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000551 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000552 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000553 }
Ted Kremenek00781502013-11-23 01:01:29 +0000554
555 if (!followSuper)
556 return NULL;
557
558 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000559 ClassDecl = ClassDecl->getSuperClass();
560 }
561 return NULL;
562}
563
Anna Zaksc77a3b12012-07-27 19:07:44 +0000564// Will search "local" class/category implementations for a method decl.
565// If failed, then we search in class's root for an instance method.
566// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000567ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
568 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000569 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000570 // FIXME: Should make sure no callers ever do this.
571 if (!hasDefinition())
572 return 0;
573
574 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000575 LoadExternalDefinition();
576
Steve Naroffbb69c942009-10-01 23:46:04 +0000577 ObjCMethodDecl *Method = 0;
578 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000579 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
580 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000581
582 // Look through local category implementations associated with the class.
583 if (!Method)
584 Method = Instance ? getCategoryInstanceMethod(Sel)
585 : getCategoryClassMethod(Sel);
586
587 // Before we give up, check if the selector is an instance method.
588 // But only in the root. This matches gcc's behavior and what the
589 // runtime expects.
590 if (!Instance && !Method && !getSuperClass()) {
591 Method = lookupInstanceMethod(Sel);
592 // Look through local category implementations associated
593 // with the root class.
594 if (!Method)
595 Method = lookupPrivateMethod(Sel, true);
596 }
597
Steve Naroffbb69c942009-10-01 23:46:04 +0000598 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000599 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000600 return Method;
601}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000602
603//===----------------------------------------------------------------------===//
604// ObjCMethodDecl
605//===----------------------------------------------------------------------===//
606
Alp Toker314cc812014-01-25 16:55:45 +0000607ObjCMethodDecl *ObjCMethodDecl::Create(
608 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
609 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
610 DeclContext *contextDecl, bool isInstance, bool isVariadic,
611 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
612 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000613 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000614 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000615 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
616 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000617}
618
Douglas Gregor72172e92012-01-05 21:55:30 +0000619ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000620 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
621 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000622}
623
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000624bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
625 return getMethodFamily() == OMF_init &&
626 hasAttr<ObjCDesignatedInitializerAttr>();
627}
628
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000629bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
630 const ObjCMethodDecl **InitMethod) const {
631 if (getMethodFamily() != OMF_init)
632 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000633 const DeclContext *DC = getDeclContext();
634 if (isa<ObjCProtocolDecl>(DC))
635 return false;
636 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000637 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000638 return false;
639}
640
Douglas Gregora6017bb2012-10-09 17:21:28 +0000641Stmt *ObjCMethodDecl::getBody() const {
642 return Body.get(getASTContext().getExternalSource());
643}
644
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000645void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
646 assert(PrevMethod);
647 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
648 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000649 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000650}
651
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000652void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
653 ArrayRef<ParmVarDecl*> Params,
654 ArrayRef<SourceLocation> SelLocs) {
655 ParamsAndSelLocs = 0;
656 NumParams = Params.size();
657 if (Params.empty() && SelLocs.empty())
658 return;
659
660 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
661 sizeof(SourceLocation) * SelLocs.size();
662 ParamsAndSelLocs = C.Allocate(Size);
663 std::copy(Params.begin(), Params.end(), getParams());
664 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
665}
666
667void ObjCMethodDecl::getSelectorLocs(
668 SmallVectorImpl<SourceLocation> &SelLocs) const {
669 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
670 SelLocs.push_back(getSelectorLoc(i));
671}
672
673void ObjCMethodDecl::setMethodParams(ASTContext &C,
674 ArrayRef<ParmVarDecl*> Params,
675 ArrayRef<SourceLocation> SelLocs) {
676 assert((!SelLocs.empty() || isImplicit()) &&
677 "No selector locs for non-implicit method");
678 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000679 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000680
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000681 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
682 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000683 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000684 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000685
686 setParamsAndSelLocs(C, Params, SelLocs);
687}
688
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000689/// \brief A definition will return its interface declaration.
690/// An interface declaration will return its definition.
691/// Otherwise it will return itself.
692ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
693 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000694 ObjCMethodDecl *Redecl = 0;
695 if (HasRedeclaration)
696 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000697 if (Redecl)
698 return Redecl;
699
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000700 Decl *CtxD = cast<Decl>(getDeclContext());
701
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000702 if (!CtxD->isInvalidDecl()) {
703 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
704 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
705 if (!ImplD->isInvalidDecl())
706 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000707
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000708 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
709 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
710 if (!ImplD->isInvalidDecl())
711 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000712
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000713 } else if (ObjCImplementationDecl *ImplD =
714 dyn_cast<ObjCImplementationDecl>(CtxD)) {
715 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
716 if (!IFD->isInvalidDecl())
717 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000718
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000719 } else if (ObjCCategoryImplDecl *CImplD =
720 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
721 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
722 if (!CatD->isInvalidDecl())
723 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
724 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000725 }
726
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000727 if (!Redecl && isRedeclaration()) {
728 // This is the last redeclaration, go back to the first method.
729 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
730 isInstanceMethod());
731 }
732
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000733 return Redecl ? Redecl : this;
734}
735
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000736ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
737 Decl *CtxD = cast<Decl>(getDeclContext());
738
739 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
740 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
741 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
742 isInstanceMethod()))
743 return MD;
744
745 } else if (ObjCCategoryImplDecl *CImplD =
746 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000747 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000748 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
749 isInstanceMethod()))
750 return MD;
751 }
752
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000753 if (isRedeclaration())
754 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
755 isInstanceMethod());
756
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000757 return this;
758}
759
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000760SourceLocation ObjCMethodDecl::getLocEnd() const {
761 if (Stmt *Body = getBody())
762 return Body->getLocEnd();
763 return DeclEndLoc;
764}
765
John McCallb4526252011-03-02 01:50:55 +0000766ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
767 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000768 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000769 return family;
770
John McCall86bc21f2011-03-02 11:33:24 +0000771 // Check for an explicit attribute.
772 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
773 // The unfortunate necessity of mapping between enums here is due
774 // to the attributes framework.
775 switch (attr->getFamily()) {
776 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
777 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
778 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
779 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
780 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
781 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
782 }
783 Family = static_cast<unsigned>(family);
784 return family;
785 }
786
John McCallb4526252011-03-02 01:50:55 +0000787 family = getSelector().getMethodFamily();
788 switch (family) {
789 case OMF_None: break;
790
791 // init only has a conventional meaning for an instance method, and
792 // it has to return an object.
793 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000794 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000795 family = OMF_None;
796 break;
797
798 // alloc/copy/new have a conventional meaning for both class and
799 // instance methods, but they require an object return.
800 case OMF_alloc:
801 case OMF_copy:
802 case OMF_mutableCopy:
803 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000804 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000805 family = OMF_None;
806 break;
807
808 // These selectors have a conventional meaning only for instance methods.
809 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000810 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000811 case OMF_retain:
812 case OMF_release:
813 case OMF_autorelease:
814 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000815 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000816 if (!isInstanceMethod())
817 family = OMF_None;
818 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000819
820 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000821 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000822 family = OMF_None;
823 else {
824 unsigned noParams = param_size();
825 if (noParams < 1 || noParams > 3)
826 family = OMF_None;
827 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000828 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000829 QualType ArgT = (*it);
830 if (!ArgT->isObjCSelType()) {
831 family = OMF_None;
832 break;
833 }
834 while (--noParams) {
835 it++;
836 ArgT = (*it);
837 if (!ArgT->isObjCIdType()) {
838 family = OMF_None;
839 break;
840 }
841 }
842 }
843 }
844 break;
845
John McCallb4526252011-03-02 01:50:55 +0000846 }
847
848 // Cache the result.
849 Family = static_cast<unsigned>(family);
850 return family;
851}
852
Mike Stump11289f42009-09-09 15:08:12 +0000853void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000854 const ObjCInterfaceDecl *OID) {
855 QualType selfTy;
856 if (isInstanceMethod()) {
857 // There may be no interface context due to error in declaration
858 // of the interface (which has been reported). Recover gracefully.
859 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000860 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000861 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000862 } else {
863 selfTy = Context.getObjCIdType();
864 }
865 } else // we have a factory method.
866 selfTy = Context.getObjCClassType();
867
John McCalld4631322011-06-17 06:42:21 +0000868 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000869 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000870
David Blaikiebbafb8a2012-03-11 07:00:24 +0000871 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000872 if (isInstanceMethod()) {
873 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000874
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000875 // 'self' is always __strong. It's actually pseudo-strong except
876 // in init methods (or methods labeled ns_consumes_self), though.
877 Qualifiers qs;
878 qs.setObjCLifetime(Qualifiers::OCL_Strong);
879 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000880
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000881 // In addition, 'self' is const unless this is an init method.
882 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
883 selfTy = selfTy.withConst();
884 selfIsPseudoStrong = true;
885 }
886 }
887 else {
888 assert(isClassMethod());
889 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000890 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000891 selfIsPseudoStrong = true;
892 }
John McCall31168b02011-06-15 23:02:42 +0000893 }
894
895 ImplicitParamDecl *self
896 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
897 &Context.Idents.get("self"), selfTy);
898 setSelfDecl(self);
899
900 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000901 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000902
John McCalld4631322011-06-17 06:42:21 +0000903 if (selfIsPseudoStrong)
904 self->setARCPseudoStrong(true);
905
Mike Stump11289f42009-09-09 15:08:12 +0000906 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
907 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000908 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000909}
910
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000911ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
912 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
913 return ID;
914 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
915 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000916 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000917 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000918 if (isa<ObjCProtocolDecl>(getDeclContext()))
919 return 0;
David Blaikie83d382b2011-09-23 05:06:16 +0000920 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000921}
922
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000923static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
924 const ObjCMethodDecl *Method,
925 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
926 bool MovedToSuper) {
927 if (!Container)
928 return;
929
930 // In categories look for overriden methods from protocols. A method from
931 // category is not "overriden" since it is considered as the "same" method
932 // (same USR) as the one from the interface.
933 if (const ObjCCategoryDecl *
934 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
935 // Check whether we have a matching method at this category but only if we
936 // are at the super class level.
937 if (MovedToSuper)
938 if (ObjCMethodDecl *
939 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000940 Method->isInstanceMethod(),
941 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000942 if (Method != Overridden) {
943 // We found an override at this category; there is no need to look
944 // into its protocols.
945 Methods.push_back(Overridden);
946 return;
947 }
948
949 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
950 PEnd = Category->protocol_end();
951 P != PEnd; ++P)
952 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
953 return;
954 }
955
956 // Check whether we have a matching method at this level.
957 if (const ObjCMethodDecl *
958 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000959 Method->isInstanceMethod(),
960 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000961 if (Method != Overridden) {
962 // We found an override at this level; there is no need to look
963 // into other protocols or categories.
964 Methods.push_back(Overridden);
965 return;
966 }
967
968 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
969 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
970 PEnd = Protocol->protocol_end();
971 P != PEnd; ++P)
972 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
973 }
974
975 if (const ObjCInterfaceDecl *
976 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +0000977 for (const auto *P : Interface->protocols())
978 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000979
Aaron Ballman15063e12014-03-13 21:35:02 +0000980 for (const auto *Cat : Interface->known_categories())
981 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000982
983 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
984 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
985 /*MovedToSuper=*/true);
986 }
987}
988
989static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
990 const ObjCMethodDecl *Method,
991 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
992 CollectOverriddenMethodsRecurse(Container, Method, Methods,
993 /*MovedToSuper=*/false);
994}
995
996static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
997 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
998 assert(Method->isOverriding());
999
1000 if (const ObjCProtocolDecl *
1001 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1002 CollectOverriddenMethods(ProtD, Method, overridden);
1003
1004 } else if (const ObjCImplDecl *
1005 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1006 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1007 if (!ID)
1008 return;
1009 // Start searching for overridden methods using the method from the
1010 // interface as starting point.
1011 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001012 Method->isInstanceMethod(),
1013 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001014 Method = IFaceMeth;
1015 CollectOverriddenMethods(ID, Method, overridden);
1016
1017 } else if (const ObjCCategoryDecl *
1018 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1019 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1020 if (!ID)
1021 return;
1022 // Start searching for overridden methods using the method from the
1023 // interface as starting point.
1024 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001025 Method->isInstanceMethod(),
1026 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001027 Method = IFaceMeth;
1028 CollectOverriddenMethods(ID, Method, overridden);
1029
1030 } else {
1031 CollectOverriddenMethods(
1032 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1033 Method, overridden);
1034 }
1035}
1036
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001037void ObjCMethodDecl::getOverriddenMethods(
1038 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1039 const ObjCMethodDecl *Method = this;
1040
1041 if (Method->isRedeclaration()) {
1042 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1043 getMethod(Method->getSelector(), Method->isInstanceMethod());
1044 }
1045
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001046 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001047 collectOverriddenMethodsSlow(Method, Overridden);
1048 assert(!Overridden.empty() &&
1049 "ObjCMethodDecl's overriding bit is not as expected");
1050 }
1051}
1052
Jordan Rose2bd991a2012-10-10 16:42:54 +00001053const ObjCPropertyDecl *
1054ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1055 Selector Sel = getSelector();
1056 unsigned NumArgs = Sel.getNumArgs();
1057 if (NumArgs > 1)
1058 return 0;
1059
Jordan Rose59e34ec2012-10-11 16:02:02 +00001060 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001061 return 0;
1062
1063 if (isPropertyAccessor()) {
1064 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001065 // If container is class extension, find its primary class.
1066 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1067 if (CatDecl->IsClassExtension())
1068 Container = CatDecl->getClassInterface();
1069
Jordan Rose2bd991a2012-10-10 16:42:54 +00001070 bool IsGetter = (NumArgs == 0);
1071
Aaron Ballmand174edf2014-03-13 19:11:50 +00001072 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001073 Selector NextSel = IsGetter ? I->getGetterName()
1074 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001075 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001076 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001077 }
1078
1079 llvm_unreachable("Marked as a property accessor but no property found!");
1080 }
1081
1082 if (!CheckOverrides)
1083 return 0;
1084
1085 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1086 OverridesTy Overrides;
1087 getOverriddenMethods(Overrides);
1088 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1089 I != E; ++I) {
1090 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1091 return Prop;
1092 }
1093
1094 return 0;
1095
1096}
1097
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001098//===----------------------------------------------------------------------===//
1099// ObjCInterfaceDecl
1100//===----------------------------------------------------------------------===//
1101
Douglas Gregord53ae832012-01-17 18:09:05 +00001102ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001103 DeclContext *DC,
1104 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001105 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001106 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001107 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001108 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001109 ObjCInterfaceDecl *Result = new (C, DC)
1110 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001111 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001112 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001113 return Result;
1114}
1115
Richard Smithf7981722013-11-22 09:01:48 +00001116ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001117 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001118 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1119 0, SourceLocation(),
1120 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001121 Result->Data.setInt(!C.getLangOpts().Modules);
1122 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001123}
1124
1125ObjCInterfaceDecl::
1126ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001127 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1128 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001129 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001130 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001131{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001132 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001133
1134 // Copy the 'data' pointer over.
1135 if (PrevDecl)
1136 Data = PrevDecl->Data;
1137
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001138 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001139}
1140
Douglas Gregor73693022010-12-01 23:49:52 +00001141void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001142 assert(data().ExternallyCompleted && "Class is not externally completed");
1143 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001144 getASTContext().getExternalSource()->CompleteType(
1145 const_cast<ObjCInterfaceDecl *>(this));
1146}
1147
1148void ObjCInterfaceDecl::setExternallyCompleted() {
1149 assert(getASTContext().getExternalSource() &&
1150 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001151 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001152 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001153 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001154}
1155
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001156void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001157 // Check for a complete definition and recover if not so.
1158 if (!isThisDeclarationADefinition())
1159 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001160 data().HasDesignatedInitializers = true;
1161}
1162
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001163bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001164 // Check for a complete definition and recover if not so.
1165 if (!isThisDeclarationADefinition())
1166 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001167 if (data().ExternallyCompleted)
1168 LoadExternalDefinition();
1169
1170 return data().HasDesignatedInitializers;
1171}
1172
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001173ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001174 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1175 if (data().ExternallyCompleted)
1176 LoadExternalDefinition();
1177
1178 return getASTContext().getObjCImplementation(
1179 const_cast<ObjCInterfaceDecl*>(Def));
1180 }
1181
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001182 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001183 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001184}
1185
1186void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001187 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001188}
1189
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001190namespace {
1191 struct SynthesizeIvarChunk {
1192 uint64_t Size;
1193 ObjCIvarDecl *Ivar;
1194 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1195 : Size(size), Ivar(ivar) {}
1196 };
1197
1198 bool operator<(const SynthesizeIvarChunk & LHS,
1199 const SynthesizeIvarChunk &RHS) {
1200 return LHS.Size < RHS.Size;
1201 }
1202}
1203
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001204/// all_declared_ivar_begin - return first ivar declared in this class,
1205/// its extensions and its implementation. Lazily build the list on first
1206/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001207///
1208/// Caveat: The list returned by this method reflects the current
1209/// state of the parser. The cache will be updated for every ivar
1210/// added by an extension or the implementation when they are
1211/// encountered.
1212/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001213ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001214 // FIXME: Should make sure no callers ever do this.
1215 if (!hasDefinition())
1216 return 0;
1217
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001218 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001219 if (!data().IvarList) {
1220 if (!ivar_empty()) {
1221 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1222 data().IvarList = *I; ++I;
1223 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001224 curIvar->setNextIvar(*I);
1225 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001226
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001227 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001228 if (!Ext->ivar_empty()) {
1229 ObjCCategoryDecl::ivar_iterator
1230 I = Ext->ivar_begin(),
1231 E = Ext->ivar_end();
1232 if (!data().IvarList) {
1233 data().IvarList = *I; ++I;
1234 curIvar = data().IvarList;
1235 }
1236 for ( ;I != E; curIvar = *I, ++I)
1237 curIvar->setNextIvar(*I);
1238 }
1239 }
1240 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001241 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001242
1243 // cached and complete!
1244 if (!data().IvarListMissingImplementation)
1245 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001246
1247 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001248 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001249 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001250 SmallVector<SynthesizeIvarChunk, 16> layout;
1251 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1252 E = ImplDecl->ivar_end(); I != E; ++I) {
1253 ObjCIvarDecl *IV = *I;
1254 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1255 layout.push_back(SynthesizeIvarChunk(
1256 IV->getASTContext().getTypeSize(IV->getType()), IV));
1257 continue;
1258 }
1259 if (!data().IvarList)
1260 data().IvarList = *I;
1261 else
1262 curIvar->setNextIvar(*I);
1263 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001264 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001265
1266 if (!layout.empty()) {
1267 // Order synthesized ivars by their size.
1268 std::stable_sort(layout.begin(), layout.end());
1269 unsigned Ix = 0, EIx = layout.size();
1270 if (!data().IvarList) {
1271 data().IvarList = layout[0].Ivar; Ix++;
1272 curIvar = data().IvarList;
1273 }
1274 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1275 curIvar->setNextIvar(layout[Ix].Ivar);
1276 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001277 }
1278 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001279 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001280}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001281
1282/// FindCategoryDeclaration - Finds category declaration in the list of
1283/// categories for this class and returns it. Name of the category is passed
1284/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001285///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001286ObjCCategoryDecl *
1287ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001288 // FIXME: Should make sure no callers ever do this.
1289 if (!hasDefinition())
1290 return 0;
1291
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001292 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001293 LoadExternalDefinition();
1294
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001295 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001296 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001297 return Cat;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001298
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001299 return 0;
1300}
1301
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001302ObjCMethodDecl *
1303ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001304 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001305 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001306 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1307 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001308 }
1309
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001310 return 0;
1311}
1312
1313ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001314 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001315 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001316 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1317 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001318 }
1319
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001320 return 0;
1321}
1322
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001323/// ClassImplementsProtocol - Checks that 'lProto' protocol
1324/// has been implemented in IDecl class, its super class or categories (if
1325/// lookupCategory is true).
1326bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1327 bool lookupCategory,
1328 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001329 if (!hasDefinition())
1330 return false;
1331
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001332 ObjCInterfaceDecl *IDecl = this;
1333 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001334 for (auto *PI : IDecl->protocols()){
1335 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001336 return true;
1337 // This is dubious and is added to be compatible with gcc. In gcc, it is
1338 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1339 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1340 // object. This IMO, should be a bug.
1341 // FIXME: Treat this as an extension, and flag this as an error when GCC
1342 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001343 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001344 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001345 return true;
1346 }
Mike Stump11289f42009-09-09 15:08:12 +00001347
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001348 // 2nd, look up the category.
1349 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001350 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001351 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1352 E = Cat->protocol_end();
1353 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001354 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1355 return true;
1356 }
Mike Stump11289f42009-09-09 15:08:12 +00001357
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001358 // 3rd, look up the super class(s)
1359 if (IDecl->getSuperClass())
1360 return
1361 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1362 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001363
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001364 return false;
1365}
1366
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001367//===----------------------------------------------------------------------===//
1368// ObjCIvarDecl
1369//===----------------------------------------------------------------------===//
1370
David Blaikie68e081d2011-12-20 02:48:34 +00001371void ObjCIvarDecl::anchor() { }
1372
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001373ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001374 SourceLocation StartLoc,
1375 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001376 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001377 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001378 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001379 if (DC) {
1380 // Ivar's can only appear in interfaces, implementations (via synthesized
1381 // properties), and class extensions (via direct declaration, or synthesized
1382 // properties).
1383 //
1384 // FIXME: This should really be asserting this:
1385 // (isa<ObjCCategoryDecl>(DC) &&
1386 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1387 // but unfortunately we sometimes place ivars into non-class extension
1388 // categories on error. This breaks an AST invariant, and should not be
1389 // fixed.
1390 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1391 isa<ObjCCategoryDecl>(DC)) &&
1392 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001393 // Once a new ivar is created in any of class/class-extension/implementation
1394 // decl contexts, the previously built IvarList must be rebuilt.
1395 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1396 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001397 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001398 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001399 else
1400 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001401 }
1402 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001403 }
1404
Richard Smithf7981722013-11-22 09:01:48 +00001405 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001406 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001407}
1408
Douglas Gregor72172e92012-01-05 21:55:30 +00001409ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001410 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001411 QualType(), 0, ObjCIvarDecl::None, 0, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001412}
1413
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001414const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1415 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001416
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001417 switch (DC->getKind()) {
1418 default:
1419 case ObjCCategoryImpl:
1420 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001421 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001422
1423 // Ivars can only appear in class extension categories.
1424 case ObjCCategory: {
1425 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1426 assert(CD->IsClassExtension() && "invalid container for ivar!");
1427 return CD->getClassInterface();
1428 }
1429
1430 case ObjCImplementation:
1431 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1432
1433 case ObjCInterface:
1434 return cast<ObjCInterfaceDecl>(DC);
1435 }
1436}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001437
1438//===----------------------------------------------------------------------===//
1439// ObjCAtDefsFieldDecl
1440//===----------------------------------------------------------------------===//
1441
David Blaikie68e081d2011-12-20 02:48:34 +00001442void ObjCAtDefsFieldDecl::anchor() { }
1443
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001444ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001445*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1446 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001447 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001448 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001449}
1450
Richard Smithf7981722013-11-22 09:01:48 +00001451ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001452 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001453 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1454 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001455}
1456
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001457//===----------------------------------------------------------------------===//
1458// ObjCProtocolDecl
1459//===----------------------------------------------------------------------===//
1460
David Blaikie68e081d2011-12-20 02:48:34 +00001461void ObjCProtocolDecl::anchor() { }
1462
Douglas Gregor32c17572012-01-01 20:30:41 +00001463ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1464 SourceLocation nameLoc,
1465 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001466 ObjCProtocolDecl *PrevDecl)
1467 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001468{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001469 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001470 if (PrevDecl)
1471 Data = PrevDecl->Data;
1472}
1473
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001474ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001475 IdentifierInfo *Id,
1476 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001477 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001478 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001479 ObjCProtocolDecl *Result =
1480 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001481 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001482 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001483}
1484
Richard Smithf7981722013-11-22 09:01:48 +00001485ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001486 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001487 ObjCProtocolDecl *Result =
1488 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001489 Result->Data.setInt(!C.getLangOpts().Modules);
1490 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001491}
1492
Steve Naroff114aecb2009-03-01 16:12:44 +00001493ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1494 ObjCProtocolDecl *PDecl = this;
1495
1496 if (Name == getIdentifier())
1497 return PDecl;
1498
1499 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1500 if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1501 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001502
Steve Naroff114aecb2009-03-01 16:12:44 +00001503 return NULL;
1504}
1505
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001506// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001507// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001508ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1509 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001510 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001511
Douglas Gregoreed49792013-01-17 00:38:46 +00001512 // If there is no definition or the definition is hidden, we don't find
1513 // anything.
1514 const ObjCProtocolDecl *Def = getDefinition();
1515 if (!Def || Def->isHidden())
1516 return NULL;
1517
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001518 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001519 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001520
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001521 for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001522 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001523 return MethodDecl;
1524 return NULL;
1525}
1526
Douglas Gregore6e48b12012-01-01 19:29:29 +00001527void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001528 assert(!Data.getPointer() && "Protocol already has a definition!");
1529 Data.setPointer(new (getASTContext()) DefinitionData);
1530 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001531}
1532
1533void ObjCProtocolDecl::startDefinition() {
1534 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001535
1536 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001537 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001538 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001539}
1540
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001541void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1542 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001543
1544 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001545 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001546 // Insert into PM if not there already.
1547 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001548 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001549 }
1550 // Scan through protocol's protocols.
1551 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1552 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001553 (*PI)->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001554 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001555}
1556
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001557
1558void ObjCProtocolDecl::collectInheritedProtocolProperties(
1559 const ObjCPropertyDecl *Property,
1560 ProtocolPropertyMap &PM) const {
1561 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1562 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001563 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001564 if (Prop == Property)
1565 continue;
1566 if (Prop->getIdentifier() == Property->getIdentifier()) {
1567 PM[PDecl] = Prop;
1568 MatchFound = true;
1569 break;
1570 }
1571 }
1572 // Scan through protocol's protocols which did not have a matching property.
1573 if (!MatchFound)
1574 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1575 E = PDecl->protocol_end(); PI != E; ++PI)
1576 (*PI)->collectInheritedProtocolProperties(Property, PM);
1577 }
1578}
Anna Zaks673d76b2012-10-18 19:17:53 +00001579
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001580//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001581// ObjCCategoryDecl
1582//===----------------------------------------------------------------------===//
1583
David Blaikie68e081d2011-12-20 02:48:34 +00001584void ObjCCategoryDecl::anchor() { }
1585
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001586ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001587 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001588 SourceLocation ClassNameLoc,
1589 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001590 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001591 ObjCInterfaceDecl *IDecl,
1592 SourceLocation IvarLBraceLoc,
1593 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001594 ObjCCategoryDecl *CatDecl =
1595 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1596 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001597 if (IDecl) {
1598 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001599 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001600 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001601 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001602 if (ASTMutationListener *L = C.getASTMutationListener())
1603 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1604 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001605 }
1606
1607 return CatDecl;
1608}
1609
Richard Smithf7981722013-11-22 09:01:48 +00001610ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001611 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001612 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1613 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001614}
1615
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001616ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1617 return getASTContext().getObjCImplementation(
1618 const_cast<ObjCCategoryDecl*>(this));
1619}
1620
1621void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1622 getASTContext().setObjCImplementation(this, ImplD);
1623}
1624
1625
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001626//===----------------------------------------------------------------------===//
1627// ObjCCategoryImplDecl
1628//===----------------------------------------------------------------------===//
1629
David Blaikie68e081d2011-12-20 02:48:34 +00001630void ObjCCategoryImplDecl::anchor() { }
1631
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001632ObjCCategoryImplDecl *
1633ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001634 IdentifierInfo *Id,
1635 ObjCInterfaceDecl *ClassInterface,
1636 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001637 SourceLocation atStartLoc,
1638 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001639 if (ClassInterface && ClassInterface->hasDefinition())
1640 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001641 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1642 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001643}
1644
Douglas Gregor72172e92012-01-05 21:55:30 +00001645ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1646 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001647 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1648 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001649}
1650
Steve Narofff406f4d2009-10-29 21:11:04 +00001651ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001652 // The class interface might be NULL if we are working with invalid code.
1653 if (const ObjCInterfaceDecl *ID = getClassInterface())
1654 return ID->FindCategoryDeclaration(getIdentifier());
1655 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001656}
1657
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001658
David Blaikie68e081d2011-12-20 02:48:34 +00001659void ObjCImplDecl::anchor() { }
1660
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001661void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001662 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001663 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001664 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001665}
1666
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001667void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1668 ASTContext &Ctx = getASTContext();
1669
1670 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001671 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001672 if (IFace)
1673 Ctx.setObjCImplementation(IFace, ImplD);
1674
Duncan Sands49c29ee2009-07-21 07:56:29 +00001675 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001676 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1677 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1678 Ctx.setObjCImplementation(CD, ImplD);
1679 }
1680
1681 ClassInterface = IFace;
1682}
1683
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001684/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001685/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001686/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001687///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001688ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001689FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1690 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001691 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001692 if (PID->getPropertyIvarDecl() &&
1693 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1694 return PID;
1695 }
1696 return 0;
1697}
1698
1699/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001700/// added to the list of those properties \@synthesized/\@dynamic in this
1701/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001702///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001703ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001704FindPropertyImplDecl(IdentifierInfo *Id) const {
1705 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001706 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001707 if (PID->getPropertyDecl()->getIdentifier() == Id)
1708 return PID;
1709 }
1710 return 0;
1711}
1712
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001713raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001714 const ObjCCategoryImplDecl &CID) {
1715 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001716 return OS;
1717}
1718
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001719//===----------------------------------------------------------------------===//
1720// ObjCImplementationDecl
1721//===----------------------------------------------------------------------===//
1722
David Blaikie68e081d2011-12-20 02:48:34 +00001723void ObjCImplementationDecl::anchor() { }
1724
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001725ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001726ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001727 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001728 ObjCInterfaceDecl *SuperDecl,
1729 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001730 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001731 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001732 SourceLocation IvarLBraceLoc,
1733 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001734 if (ClassInterface && ClassInterface->hasDefinition())
1735 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001736 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1737 nameLoc, atStartLoc, superLoc,
1738 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001739}
1740
Douglas Gregor72172e92012-01-05 21:55:30 +00001741ObjCImplementationDecl *
1742ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001743 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1744 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001745}
1746
John McCall0410e572011-07-22 04:15:06 +00001747void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1748 CXXCtorInitializer ** initializers,
1749 unsigned numInitializers) {
1750 if (numInitializers > 0) {
1751 NumIvarInitializers = numInitializers;
1752 CXXCtorInitializer **ivarInitializers =
1753 new (C) CXXCtorInitializer*[NumIvarInitializers];
1754 memcpy(ivarInitializers, initializers,
1755 numInitializers * sizeof(CXXCtorInitializer*));
1756 IvarInitializers = ivarInitializers;
1757 }
1758}
1759
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001760raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001761 const ObjCImplementationDecl &ID) {
1762 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001763 return OS;
1764}
1765
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001766//===----------------------------------------------------------------------===//
1767// ObjCCompatibleAliasDecl
1768//===----------------------------------------------------------------------===//
1769
David Blaikie68e081d2011-12-20 02:48:34 +00001770void ObjCCompatibleAliasDecl::anchor() { }
1771
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001772ObjCCompatibleAliasDecl *
1773ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1774 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001775 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001776 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001777 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001778}
1779
Douglas Gregor72172e92012-01-05 21:55:30 +00001780ObjCCompatibleAliasDecl *
1781ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001782 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001783}
1784
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001785//===----------------------------------------------------------------------===//
1786// ObjCPropertyDecl
1787//===----------------------------------------------------------------------===//
1788
David Blaikie68e081d2011-12-20 02:48:34 +00001789void ObjCPropertyDecl::anchor() { }
1790
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001791ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1792 SourceLocation L,
1793 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001794 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001795 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001796 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001797 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001798 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001799}
1800
Richard Smithf7981722013-11-22 09:01:48 +00001801ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001802 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001803 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1804 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001805}
1806
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001807//===----------------------------------------------------------------------===//
1808// ObjCPropertyImplDecl
1809//===----------------------------------------------------------------------===//
1810
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001811ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001812 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001813 SourceLocation atLoc,
1814 SourceLocation L,
1815 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001816 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001817 ObjCIvarDecl *ivar,
1818 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001819 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1820 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001821}
Chris Lattnered0e1642008-03-17 01:19:02 +00001822
Richard Smithf7981722013-11-22 09:01:48 +00001823ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001824 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001825 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1826 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001827}
1828
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001829SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1830 SourceLocation EndLoc = getLocation();
1831 if (IvarLoc.isValid())
1832 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001833
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001834 return SourceRange(AtLoc, EndLoc);
1835}