blob: 06448f86468c9edadc8a3fcc3880e3a265dcea92 [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))
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000146 for (const auto *PI : PD->protocols())
147 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000148 return true;
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000149 return false;
150}
151
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000152ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000153ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000154 IdentifierInfo *propertyID) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000155 // If this context is a hidden protocol definition, don't find any
156 // property.
157 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
158 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
159 if (Def->isHidden())
160 return 0;
161 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000162
David Blaikieff7d47a2012-12-19 00:45:41 +0000163 DeclContext::lookup_const_result R = DC->lookup(propertyID);
164 for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
165 ++I)
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000166 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
167 return PD;
168
169 return 0;
170}
171
Anna Zaks454477c2012-09-27 19:45:11 +0000172IdentifierInfo *
173ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
174 SmallString<128> ivarName;
175 {
176 llvm::raw_svector_ostream os(ivarName);
177 os << '_' << getIdentifier()->getName();
178 }
179 return &Ctx.Idents.get(ivarName.str());
180}
181
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000182/// FindPropertyDeclaration - Finds declaration of the property given its name
183/// in 'PropertyId' and returns it. It returns 0, if not found.
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000184ObjCPropertyDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000185ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000186 // Don't find properties within hidden protocol definitions.
187 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
188 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
189 if (Def->isHidden())
190 return 0;
191 }
Mike Stump11289f42009-09-09 15:08:12 +0000192
Ted Kremenekddcd1092010-03-15 20:11:53 +0000193 if (ObjCPropertyDecl *PD =
194 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
195 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000196
Ted Kremenekddcd1092010-03-15 20:11:53 +0000197 switch (getKind()) {
198 default:
199 break;
200 case Decl::ObjCProtocol: {
201 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000202 for (const auto *I : PID->protocols())
203 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000204 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000205 break;
206 }
207 case Decl::ObjCInterface: {
208 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000209 // Look through categories (but not extensions).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000210 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000211 if (!Cat->IsClassExtension())
212 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
213 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000214 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000215
216 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000217 for (const auto *I : OID->all_referenced_protocols())
218 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000219 return P;
220
221 // Finally, check the super class.
222 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
223 return superClass->FindPropertyDeclaration(PropertyId);
224 break;
225 }
226 case Decl::ObjCCategory: {
227 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
228 // Look through protocols.
229 if (!OCD->IsClassExtension())
Aaron Ballman19a41762014-03-14 12:55:57 +0000230 for (const auto *I : OCD->protocols())
231 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
232 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000233 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000234 }
235 }
Steve Narofff9c65242008-06-05 13:55:23 +0000236 return 0;
237}
238
David Blaikie68e081d2011-12-20 02:48:34 +0000239void ObjCInterfaceDecl::anchor() { }
240
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000241/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
242/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000243/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000244///
245ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000246ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000247 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000248 // FIXME: Should make sure no callers ever do this.
249 if (!hasDefinition())
250 return 0;
251
252 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000253 LoadExternalDefinition();
254
Ted Kremenekd133a862010-03-15 20:30:07 +0000255 if (ObjCPropertyDecl *PD =
256 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
257 return PD;
258
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000259 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000260 for (const auto *I : all_referenced_protocols())
261 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000262 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000263
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000264 return 0;
265}
266
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000267void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
268 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000269 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000270 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000271 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000272 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000273 for (const auto *PI : all_referenced_protocols())
274 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000275 // Note, the properties declared only in class extensions are still copied
276 // into the main @interface's property list, and therefore we don't
277 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000278}
279
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000280bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
281 const ObjCInterfaceDecl *Class = this;
282 while (Class) {
283 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
284 return true;
285 Class = Class->getSuperClass();
286 }
287 return false;
288}
289
290const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
291 const ObjCInterfaceDecl *Class = this;
292 while (Class) {
293 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
294 return Class;
295 Class = Class->getSuperClass();
296 }
297 return 0;
298}
299
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000300void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
301 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
302 ASTContext &C)
303{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000304 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000305 LoadExternalDefinition();
306
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000307 if (data().AllReferencedProtocols.empty() &&
308 data().ReferencedProtocols.empty()) {
309 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000310 return;
311 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000312
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000313 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000314 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000315 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000316 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000317 for (unsigned i = 0; i < ExtNum; i++) {
318 bool protocolExists = false;
319 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000320 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000321 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
322 protocolExists = true;
323 break;
324 }
325 }
326 // Do we want to warn on a protocol in extension class which
327 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000328 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000329 ProtocolRefs.push_back(ProtoInExtension);
330 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000331
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000332 if (ProtocolRefs.empty())
333 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000334
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000335 // Merge ProtocolRefs into class's protocol list;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000336 for (auto *P : all_referenced_protocols()) {
337 ProtocolRefs.push_back(P);
Douglas Gregor002b6712010-01-16 15:02:53 +0000338 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000339
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000340 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000341}
342
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000343const ObjCInterfaceDecl *
344ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
345 const ObjCInterfaceDecl *IFace = this;
346 while (IFace) {
347 if (IFace->hasDesignatedInitializers())
348 return IFace;
349 if (!IFace->inheritsDesignatedInitializers())
350 break;
351 IFace = IFace->getSuperClass();
352 }
353 return 0;
354}
355
356bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
357 switch (data().InheritedDesignatedInitializers) {
358 case DefinitionData::IDI_Inherited:
359 return true;
360 case DefinitionData::IDI_NotInherited:
361 return false;
362 case DefinitionData::IDI_Unknown: {
363 bool isIntroducingInitializers = false;
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000364 for (const auto *MD : instance_methods()) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000365 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding()) {
366 isIntroducingInitializers = true;
367 break;
368 }
369 }
370 // If the class introduced initializers we conservatively assume that we
371 // don't know if any of them is a designated initializer to avoid possible
372 // misleading warnings.
373 if (isIntroducingInitializers) {
374 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
375 return false;
376 } else {
377 data().InheritedDesignatedInitializers = DefinitionData::IDI_Inherited;
378 return true;
379 }
380 }
381 }
382
383 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
384}
385
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000386void ObjCInterfaceDecl::getDesignatedInitializers(
387 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000388 // Check for a complete definition and recover if not so.
389 if (!isThisDeclarationADefinition())
390 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000391 if (data().ExternallyCompleted)
392 LoadExternalDefinition();
393
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000394 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000395 if (!IFace)
396 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000397
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000398 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000399 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000400 Methods.push_back(MD);
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000401}
402
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000403bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
404 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000405 // Check for a complete definition and recover if not so.
406 if (!isThisDeclarationADefinition())
407 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000408 if (data().ExternallyCompleted)
409 LoadExternalDefinition();
410
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000411 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000412 if (!IFace)
413 return false;
414
Argyrios Kyrtzidise919fc22013-12-10 18:36:43 +0000415 if (const ObjCMethodDecl *MD = IFace->getMethod(Sel, /*isInstance=*/true)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000416 if (MD->isThisDeclarationADesignatedInitializer()) {
417 if (InitMethod)
418 *InitMethod = MD;
419 return true;
420 }
421 }
422 return false;
423}
424
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000425void ObjCInterfaceDecl::allocateDefinitionData() {
426 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000427 Data.setPointer(new (getASTContext()) DefinitionData());
428 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000429
430 // Make the type point at the definition, now that we have one.
431 if (TypeForDecl)
432 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000433}
434
435void ObjCInterfaceDecl::startDefinition() {
436 allocateDefinitionData();
437
Douglas Gregor66b310c2011-12-15 18:03:09 +0000438 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000439 for (auto RD : redecls()) {
440 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000441 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000442 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000443}
444
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000445ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
446 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000447 // FIXME: Should make sure no callers ever do this.
448 if (!hasDefinition())
449 return 0;
450
451 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000452 LoadExternalDefinition();
453
Chris Lattner89375192008-03-16 00:19:01 +0000454 ObjCInterfaceDecl* ClassDecl = this;
455 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000456 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000457 clsDeclared = ClassDecl;
458 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000459 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000460
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000461 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000462 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000463 clsDeclared = ClassDecl;
464 return I;
465 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000466 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000467
Chris Lattner89375192008-03-16 00:19:01 +0000468 ClassDecl = ClassDecl->getSuperClass();
469 }
470 return NULL;
471}
472
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000473/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
474/// class whose name is passed as argument. If it is not one of the super classes
475/// the it returns NULL.
476ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
477 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000478 // FIXME: Should make sure no callers ever do this.
479 if (!hasDefinition())
480 return 0;
481
482 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000483 LoadExternalDefinition();
484
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000485 ObjCInterfaceDecl* ClassDecl = this;
486 while (ClassDecl != NULL) {
487 if (ClassDecl->getIdentifier() == ICName)
488 return ClassDecl;
489 ClassDecl = ClassDecl->getSuperClass();
490 }
491 return NULL;
492}
493
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000494ObjCProtocolDecl *
495ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000496 for (auto *P : all_referenced_protocols())
497 if (P->lookupProtocolNamed(Name))
498 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000499 ObjCInterfaceDecl *SuperClass = getSuperClass();
500 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
501}
502
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000503/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000504/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000505/// When argument category "C" is specified, any implicit method found
506/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000507ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000508 bool isInstance,
509 bool shallowCategoryLookup,
510 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000511 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000512{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000513 // FIXME: Should make sure no callers ever do this.
514 if (!hasDefinition())
515 return 0;
516
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000517 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000518 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000519
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000520 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000521 LoadExternalDefinition();
522
Ted Kremenek00781502013-11-23 01:01:29 +0000523 while (ClassDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000524 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000525 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000526
Chris Lattner89375192008-03-16 00:19:01 +0000527 // Didn't find one yet - look through protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000528 for (const auto *I : ClassDecl->protocols())
529 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000530 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000531
532 // Didn't find one yet - now look through categories.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000533 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000534 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000535 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000536 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000537
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000538 if (!shallowCategoryLookup) {
539 // Didn't find one yet - look through protocols.
540 const ObjCList<ObjCProtocolDecl> &Protocols =
541 Cat->getReferencedProtocols();
542 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
543 E = Protocols.end(); I != E; ++I)
544 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000545 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000546 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000547 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000548 }
Ted Kremenek00781502013-11-23 01:01:29 +0000549
550 if (!followSuper)
551 return NULL;
552
553 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000554 ClassDecl = ClassDecl->getSuperClass();
555 }
556 return NULL;
557}
558
Anna Zaksc77a3b12012-07-27 19:07:44 +0000559// Will search "local" class/category implementations for a method decl.
560// If failed, then we search in class's root for an instance method.
561// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000562ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
563 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000564 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000565 // FIXME: Should make sure no callers ever do this.
566 if (!hasDefinition())
567 return 0;
568
569 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000570 LoadExternalDefinition();
571
Steve Naroffbb69c942009-10-01 23:46:04 +0000572 ObjCMethodDecl *Method = 0;
573 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000574 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
575 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000576
577 // Look through local category implementations associated with the class.
578 if (!Method)
579 Method = Instance ? getCategoryInstanceMethod(Sel)
580 : getCategoryClassMethod(Sel);
581
582 // Before we give up, check if the selector is an instance method.
583 // But only in the root. This matches gcc's behavior and what the
584 // runtime expects.
585 if (!Instance && !Method && !getSuperClass()) {
586 Method = lookupInstanceMethod(Sel);
587 // Look through local category implementations associated
588 // with the root class.
589 if (!Method)
590 Method = lookupPrivateMethod(Sel, true);
591 }
592
Steve Naroffbb69c942009-10-01 23:46:04 +0000593 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000594 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000595 return Method;
596}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000597
598//===----------------------------------------------------------------------===//
599// ObjCMethodDecl
600//===----------------------------------------------------------------------===//
601
Alp Toker314cc812014-01-25 16:55:45 +0000602ObjCMethodDecl *ObjCMethodDecl::Create(
603 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
604 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
605 DeclContext *contextDecl, bool isInstance, bool isVariadic,
606 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
607 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000608 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000609 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000610 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
611 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000612}
613
Douglas Gregor72172e92012-01-05 21:55:30 +0000614ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000615 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
616 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000617}
618
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000619bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
620 return getMethodFamily() == OMF_init &&
621 hasAttr<ObjCDesignatedInitializerAttr>();
622}
623
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000624bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
625 const ObjCMethodDecl **InitMethod) const {
626 if (getMethodFamily() != OMF_init)
627 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000628 const DeclContext *DC = getDeclContext();
629 if (isa<ObjCProtocolDecl>(DC))
630 return false;
631 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000632 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000633 return false;
634}
635
Douglas Gregora6017bb2012-10-09 17:21:28 +0000636Stmt *ObjCMethodDecl::getBody() const {
637 return Body.get(getASTContext().getExternalSource());
638}
639
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000640void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
641 assert(PrevMethod);
642 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
643 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000644 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000645}
646
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000647void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
648 ArrayRef<ParmVarDecl*> Params,
649 ArrayRef<SourceLocation> SelLocs) {
650 ParamsAndSelLocs = 0;
651 NumParams = Params.size();
652 if (Params.empty() && SelLocs.empty())
653 return;
654
655 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
656 sizeof(SourceLocation) * SelLocs.size();
657 ParamsAndSelLocs = C.Allocate(Size);
658 std::copy(Params.begin(), Params.end(), getParams());
659 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
660}
661
662void ObjCMethodDecl::getSelectorLocs(
663 SmallVectorImpl<SourceLocation> &SelLocs) const {
664 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
665 SelLocs.push_back(getSelectorLoc(i));
666}
667
668void ObjCMethodDecl::setMethodParams(ASTContext &C,
669 ArrayRef<ParmVarDecl*> Params,
670 ArrayRef<SourceLocation> SelLocs) {
671 assert((!SelLocs.empty() || isImplicit()) &&
672 "No selector locs for non-implicit method");
673 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000674 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000675
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000676 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
677 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000678 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000679 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000680
681 setParamsAndSelLocs(C, Params, SelLocs);
682}
683
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000684/// \brief A definition will return its interface declaration.
685/// An interface declaration will return its definition.
686/// Otherwise it will return itself.
687ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
688 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000689 ObjCMethodDecl *Redecl = 0;
690 if (HasRedeclaration)
691 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000692 if (Redecl)
693 return Redecl;
694
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000695 Decl *CtxD = cast<Decl>(getDeclContext());
696
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000697 if (!CtxD->isInvalidDecl()) {
698 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
699 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
700 if (!ImplD->isInvalidDecl())
701 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000702
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000703 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
704 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
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 (ObjCImplementationDecl *ImplD =
709 dyn_cast<ObjCImplementationDecl>(CtxD)) {
710 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
711 if (!IFD->isInvalidDecl())
712 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000713
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000714 } else if (ObjCCategoryImplDecl *CImplD =
715 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
716 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
717 if (!CatD->isInvalidDecl())
718 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
719 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000720 }
721
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000722 if (!Redecl && isRedeclaration()) {
723 // This is the last redeclaration, go back to the first method.
724 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
725 isInstanceMethod());
726 }
727
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000728 return Redecl ? Redecl : this;
729}
730
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000731ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
732 Decl *CtxD = cast<Decl>(getDeclContext());
733
734 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
735 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
736 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
737 isInstanceMethod()))
738 return MD;
739
740 } else if (ObjCCategoryImplDecl *CImplD =
741 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000742 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000743 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
744 isInstanceMethod()))
745 return MD;
746 }
747
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000748 if (isRedeclaration())
749 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
750 isInstanceMethod());
751
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000752 return this;
753}
754
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000755SourceLocation ObjCMethodDecl::getLocEnd() const {
756 if (Stmt *Body = getBody())
757 return Body->getLocEnd();
758 return DeclEndLoc;
759}
760
John McCallb4526252011-03-02 01:50:55 +0000761ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
762 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000763 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000764 return family;
765
John McCall86bc21f2011-03-02 11:33:24 +0000766 // Check for an explicit attribute.
767 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
768 // The unfortunate necessity of mapping between enums here is due
769 // to the attributes framework.
770 switch (attr->getFamily()) {
771 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
772 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
773 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
774 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
775 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
776 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
777 }
778 Family = static_cast<unsigned>(family);
779 return family;
780 }
781
John McCallb4526252011-03-02 01:50:55 +0000782 family = getSelector().getMethodFamily();
783 switch (family) {
784 case OMF_None: break;
785
786 // init only has a conventional meaning for an instance method, and
787 // it has to return an object.
788 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000789 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000790 family = OMF_None;
791 break;
792
793 // alloc/copy/new have a conventional meaning for both class and
794 // instance methods, but they require an object return.
795 case OMF_alloc:
796 case OMF_copy:
797 case OMF_mutableCopy:
798 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000799 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000800 family = OMF_None;
801 break;
802
803 // These selectors have a conventional meaning only for instance methods.
804 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000805 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000806 case OMF_retain:
807 case OMF_release:
808 case OMF_autorelease:
809 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000810 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000811 if (!isInstanceMethod())
812 family = OMF_None;
813 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000814
815 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000816 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000817 family = OMF_None;
818 else {
819 unsigned noParams = param_size();
820 if (noParams < 1 || noParams > 3)
821 family = OMF_None;
822 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000823 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000824 QualType ArgT = (*it);
825 if (!ArgT->isObjCSelType()) {
826 family = OMF_None;
827 break;
828 }
829 while (--noParams) {
830 it++;
831 ArgT = (*it);
832 if (!ArgT->isObjCIdType()) {
833 family = OMF_None;
834 break;
835 }
836 }
837 }
838 }
839 break;
840
John McCallb4526252011-03-02 01:50:55 +0000841 }
842
843 // Cache the result.
844 Family = static_cast<unsigned>(family);
845 return family;
846}
847
Mike Stump11289f42009-09-09 15:08:12 +0000848void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000849 const ObjCInterfaceDecl *OID) {
850 QualType selfTy;
851 if (isInstanceMethod()) {
852 // There may be no interface context due to error in declaration
853 // of the interface (which has been reported). Recover gracefully.
854 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000855 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000856 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000857 } else {
858 selfTy = Context.getObjCIdType();
859 }
860 } else // we have a factory method.
861 selfTy = Context.getObjCClassType();
862
John McCalld4631322011-06-17 06:42:21 +0000863 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000864 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000865
David Blaikiebbafb8a2012-03-11 07:00:24 +0000866 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000867 if (isInstanceMethod()) {
868 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000869
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000870 // 'self' is always __strong. It's actually pseudo-strong except
871 // in init methods (or methods labeled ns_consumes_self), though.
872 Qualifiers qs;
873 qs.setObjCLifetime(Qualifiers::OCL_Strong);
874 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000875
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000876 // In addition, 'self' is const unless this is an init method.
877 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
878 selfTy = selfTy.withConst();
879 selfIsPseudoStrong = true;
880 }
881 }
882 else {
883 assert(isClassMethod());
884 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000885 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000886 selfIsPseudoStrong = true;
887 }
John McCall31168b02011-06-15 23:02:42 +0000888 }
889
890 ImplicitParamDecl *self
891 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
892 &Context.Idents.get("self"), selfTy);
893 setSelfDecl(self);
894
895 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000896 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000897
John McCalld4631322011-06-17 06:42:21 +0000898 if (selfIsPseudoStrong)
899 self->setARCPseudoStrong(true);
900
Mike Stump11289f42009-09-09 15:08:12 +0000901 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
902 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000903 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000904}
905
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000906ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
907 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
908 return ID;
909 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
910 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000911 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000912 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000913 if (isa<ObjCProtocolDecl>(getDeclContext()))
914 return 0;
David Blaikie83d382b2011-09-23 05:06:16 +0000915 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000916}
917
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000918static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
919 const ObjCMethodDecl *Method,
920 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
921 bool MovedToSuper) {
922 if (!Container)
923 return;
924
925 // In categories look for overriden methods from protocols. A method from
926 // category is not "overriden" since it is considered as the "same" method
927 // (same USR) as the one from the interface.
928 if (const ObjCCategoryDecl *
929 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
930 // Check whether we have a matching method at this category but only if we
931 // are at the super class level.
932 if (MovedToSuper)
933 if (ObjCMethodDecl *
934 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000935 Method->isInstanceMethod(),
936 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000937 if (Method != Overridden) {
938 // We found an override at this category; there is no need to look
939 // into its protocols.
940 Methods.push_back(Overridden);
941 return;
942 }
943
Aaron Ballman19a41762014-03-14 12:55:57 +0000944 for (const auto *P : Category->protocols())
945 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000946 return;
947 }
948
949 // Check whether we have a matching method at this level.
950 if (const ObjCMethodDecl *
951 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000952 Method->isInstanceMethod(),
953 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000954 if (Method != Overridden) {
955 // We found an override at this level; there is no need to look
956 // into other protocols or categories.
957 Methods.push_back(Overridden);
958 return;
959 }
960
961 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000962 for (const auto *P : Protocol->protocols())
963 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000964 }
965
966 if (const ObjCInterfaceDecl *
967 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +0000968 for (const auto *P : Interface->protocols())
969 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000970
Aaron Ballman15063e12014-03-13 21:35:02 +0000971 for (const auto *Cat : Interface->known_categories())
972 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000973
974 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
975 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
976 /*MovedToSuper=*/true);
977 }
978}
979
980static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
981 const ObjCMethodDecl *Method,
982 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
983 CollectOverriddenMethodsRecurse(Container, Method, Methods,
984 /*MovedToSuper=*/false);
985}
986
987static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
988 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
989 assert(Method->isOverriding());
990
991 if (const ObjCProtocolDecl *
992 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
993 CollectOverriddenMethods(ProtD, Method, overridden);
994
995 } else if (const ObjCImplDecl *
996 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
997 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
998 if (!ID)
999 return;
1000 // Start searching for overridden methods using the method from the
1001 // interface as starting point.
1002 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001003 Method->isInstanceMethod(),
1004 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001005 Method = IFaceMeth;
1006 CollectOverriddenMethods(ID, Method, overridden);
1007
1008 } else if (const ObjCCategoryDecl *
1009 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1010 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1011 if (!ID)
1012 return;
1013 // Start searching for overridden methods using the method from the
1014 // interface as starting point.
1015 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001016 Method->isInstanceMethod(),
1017 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001018 Method = IFaceMeth;
1019 CollectOverriddenMethods(ID, Method, overridden);
1020
1021 } else {
1022 CollectOverriddenMethods(
1023 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1024 Method, overridden);
1025 }
1026}
1027
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001028void ObjCMethodDecl::getOverriddenMethods(
1029 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1030 const ObjCMethodDecl *Method = this;
1031
1032 if (Method->isRedeclaration()) {
1033 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1034 getMethod(Method->getSelector(), Method->isInstanceMethod());
1035 }
1036
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001037 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001038 collectOverriddenMethodsSlow(Method, Overridden);
1039 assert(!Overridden.empty() &&
1040 "ObjCMethodDecl's overriding bit is not as expected");
1041 }
1042}
1043
Jordan Rose2bd991a2012-10-10 16:42:54 +00001044const ObjCPropertyDecl *
1045ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1046 Selector Sel = getSelector();
1047 unsigned NumArgs = Sel.getNumArgs();
1048 if (NumArgs > 1)
1049 return 0;
1050
Jordan Rose59e34ec2012-10-11 16:02:02 +00001051 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001052 return 0;
1053
1054 if (isPropertyAccessor()) {
1055 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001056 // If container is class extension, find its primary class.
1057 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1058 if (CatDecl->IsClassExtension())
1059 Container = CatDecl->getClassInterface();
1060
Jordan Rose2bd991a2012-10-10 16:42:54 +00001061 bool IsGetter = (NumArgs == 0);
1062
Aaron Ballmand174edf2014-03-13 19:11:50 +00001063 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001064 Selector NextSel = IsGetter ? I->getGetterName()
1065 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001066 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001067 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001068 }
1069
1070 llvm_unreachable("Marked as a property accessor but no property found!");
1071 }
1072
1073 if (!CheckOverrides)
1074 return 0;
1075
1076 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1077 OverridesTy Overrides;
1078 getOverriddenMethods(Overrides);
1079 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1080 I != E; ++I) {
1081 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1082 return Prop;
1083 }
1084
1085 return 0;
1086
1087}
1088
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001089//===----------------------------------------------------------------------===//
1090// ObjCInterfaceDecl
1091//===----------------------------------------------------------------------===//
1092
Douglas Gregord53ae832012-01-17 18:09:05 +00001093ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001094 DeclContext *DC,
1095 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001096 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001097 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001098 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001099 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001100 ObjCInterfaceDecl *Result = new (C, DC)
1101 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001102 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001103 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001104 return Result;
1105}
1106
Richard Smithf7981722013-11-22 09:01:48 +00001107ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001108 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001109 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1110 0, SourceLocation(),
1111 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001112 Result->Data.setInt(!C.getLangOpts().Modules);
1113 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001114}
1115
1116ObjCInterfaceDecl::
1117ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001118 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1119 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001120 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001121 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001122{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001123 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001124
1125 // Copy the 'data' pointer over.
1126 if (PrevDecl)
1127 Data = PrevDecl->Data;
1128
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001129 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001130}
1131
Douglas Gregor73693022010-12-01 23:49:52 +00001132void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001133 assert(data().ExternallyCompleted && "Class is not externally completed");
1134 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001135 getASTContext().getExternalSource()->CompleteType(
1136 const_cast<ObjCInterfaceDecl *>(this));
1137}
1138
1139void ObjCInterfaceDecl::setExternallyCompleted() {
1140 assert(getASTContext().getExternalSource() &&
1141 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001142 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001143 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001144 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001145}
1146
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001147void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001148 // Check for a complete definition and recover if not so.
1149 if (!isThisDeclarationADefinition())
1150 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001151 data().HasDesignatedInitializers = true;
1152}
1153
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001154bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001155 // Check for a complete definition and recover if not so.
1156 if (!isThisDeclarationADefinition())
1157 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001158 if (data().ExternallyCompleted)
1159 LoadExternalDefinition();
1160
1161 return data().HasDesignatedInitializers;
1162}
1163
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001164ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001165 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1166 if (data().ExternallyCompleted)
1167 LoadExternalDefinition();
1168
1169 return getASTContext().getObjCImplementation(
1170 const_cast<ObjCInterfaceDecl*>(Def));
1171 }
1172
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001173 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001174 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001175}
1176
1177void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001178 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001179}
1180
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001181namespace {
1182 struct SynthesizeIvarChunk {
1183 uint64_t Size;
1184 ObjCIvarDecl *Ivar;
1185 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1186 : Size(size), Ivar(ivar) {}
1187 };
1188
1189 bool operator<(const SynthesizeIvarChunk & LHS,
1190 const SynthesizeIvarChunk &RHS) {
1191 return LHS.Size < RHS.Size;
1192 }
1193}
1194
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001195/// all_declared_ivar_begin - return first ivar declared in this class,
1196/// its extensions and its implementation. Lazily build the list on first
1197/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001198///
1199/// Caveat: The list returned by this method reflects the current
1200/// state of the parser. The cache will be updated for every ivar
1201/// added by an extension or the implementation when they are
1202/// encountered.
1203/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001204ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001205 // FIXME: Should make sure no callers ever do this.
1206 if (!hasDefinition())
1207 return 0;
1208
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001209 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001210 if (!data().IvarList) {
1211 if (!ivar_empty()) {
1212 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1213 data().IvarList = *I; ++I;
1214 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001215 curIvar->setNextIvar(*I);
1216 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001217
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001218 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001219 if (!Ext->ivar_empty()) {
1220 ObjCCategoryDecl::ivar_iterator
1221 I = Ext->ivar_begin(),
1222 E = Ext->ivar_end();
1223 if (!data().IvarList) {
1224 data().IvarList = *I; ++I;
1225 curIvar = data().IvarList;
1226 }
1227 for ( ;I != E; curIvar = *I, ++I)
1228 curIvar->setNextIvar(*I);
1229 }
1230 }
1231 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001232 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001233
1234 // cached and complete!
1235 if (!data().IvarListMissingImplementation)
1236 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001237
1238 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001239 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001240 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001241 SmallVector<SynthesizeIvarChunk, 16> layout;
1242 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1243 E = ImplDecl->ivar_end(); I != E; ++I) {
1244 ObjCIvarDecl *IV = *I;
1245 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1246 layout.push_back(SynthesizeIvarChunk(
1247 IV->getASTContext().getTypeSize(IV->getType()), IV));
1248 continue;
1249 }
1250 if (!data().IvarList)
1251 data().IvarList = *I;
1252 else
1253 curIvar->setNextIvar(*I);
1254 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001255 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001256
1257 if (!layout.empty()) {
1258 // Order synthesized ivars by their size.
1259 std::stable_sort(layout.begin(), layout.end());
1260 unsigned Ix = 0, EIx = layout.size();
1261 if (!data().IvarList) {
1262 data().IvarList = layout[0].Ivar; Ix++;
1263 curIvar = data().IvarList;
1264 }
1265 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1266 curIvar->setNextIvar(layout[Ix].Ivar);
1267 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001268 }
1269 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001270 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001271}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001272
1273/// FindCategoryDeclaration - Finds category declaration in the list of
1274/// categories for this class and returns it. Name of the category is passed
1275/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001276///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001277ObjCCategoryDecl *
1278ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001279 // FIXME: Should make sure no callers ever do this.
1280 if (!hasDefinition())
1281 return 0;
1282
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001283 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001284 LoadExternalDefinition();
1285
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001286 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001287 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001288 return Cat;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001289
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001290 return 0;
1291}
1292
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001293ObjCMethodDecl *
1294ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001295 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001296 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001297 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1298 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001299 }
1300
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001301 return 0;
1302}
1303
1304ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001305 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001306 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001307 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1308 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001309 }
1310
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001311 return 0;
1312}
1313
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001314/// ClassImplementsProtocol - Checks that 'lProto' protocol
1315/// has been implemented in IDecl class, its super class or categories (if
1316/// lookupCategory is true).
1317bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1318 bool lookupCategory,
1319 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001320 if (!hasDefinition())
1321 return false;
1322
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001323 ObjCInterfaceDecl *IDecl = this;
1324 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001325 for (auto *PI : IDecl->protocols()){
1326 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001327 return true;
1328 // This is dubious and is added to be compatible with gcc. In gcc, it is
1329 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1330 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1331 // object. This IMO, should be a bug.
1332 // FIXME: Treat this as an extension, and flag this as an error when GCC
1333 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001334 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001335 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001336 return true;
1337 }
Mike Stump11289f42009-09-09 15:08:12 +00001338
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001339 // 2nd, look up the category.
1340 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001341 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001342 for (auto *PI : Cat->protocols())
1343 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001344 return true;
1345 }
Mike Stump11289f42009-09-09 15:08:12 +00001346
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001347 // 3rd, look up the super class(s)
1348 if (IDecl->getSuperClass())
1349 return
1350 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1351 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001352
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001353 return false;
1354}
1355
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001356//===----------------------------------------------------------------------===//
1357// ObjCIvarDecl
1358//===----------------------------------------------------------------------===//
1359
David Blaikie68e081d2011-12-20 02:48:34 +00001360void ObjCIvarDecl::anchor() { }
1361
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001362ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001363 SourceLocation StartLoc,
1364 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001365 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001366 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001367 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001368 if (DC) {
1369 // Ivar's can only appear in interfaces, implementations (via synthesized
1370 // properties), and class extensions (via direct declaration, or synthesized
1371 // properties).
1372 //
1373 // FIXME: This should really be asserting this:
1374 // (isa<ObjCCategoryDecl>(DC) &&
1375 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1376 // but unfortunately we sometimes place ivars into non-class extension
1377 // categories on error. This breaks an AST invariant, and should not be
1378 // fixed.
1379 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1380 isa<ObjCCategoryDecl>(DC)) &&
1381 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001382 // Once a new ivar is created in any of class/class-extension/implementation
1383 // decl contexts, the previously built IvarList must be rebuilt.
1384 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1385 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001386 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001387 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001388 else
1389 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001390 }
1391 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001392 }
1393
Richard Smithf7981722013-11-22 09:01:48 +00001394 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001395 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001396}
1397
Douglas Gregor72172e92012-01-05 21:55:30 +00001398ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001399 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001400 QualType(), 0, ObjCIvarDecl::None, 0, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001401}
1402
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001403const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1404 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001405
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001406 switch (DC->getKind()) {
1407 default:
1408 case ObjCCategoryImpl:
1409 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001410 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001411
1412 // Ivars can only appear in class extension categories.
1413 case ObjCCategory: {
1414 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1415 assert(CD->IsClassExtension() && "invalid container for ivar!");
1416 return CD->getClassInterface();
1417 }
1418
1419 case ObjCImplementation:
1420 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1421
1422 case ObjCInterface:
1423 return cast<ObjCInterfaceDecl>(DC);
1424 }
1425}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001426
1427//===----------------------------------------------------------------------===//
1428// ObjCAtDefsFieldDecl
1429//===----------------------------------------------------------------------===//
1430
David Blaikie68e081d2011-12-20 02:48:34 +00001431void ObjCAtDefsFieldDecl::anchor() { }
1432
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001433ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001434*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1435 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001436 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001437 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001438}
1439
Richard Smithf7981722013-11-22 09:01:48 +00001440ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001441 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001442 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1443 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001444}
1445
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001446//===----------------------------------------------------------------------===//
1447// ObjCProtocolDecl
1448//===----------------------------------------------------------------------===//
1449
David Blaikie68e081d2011-12-20 02:48:34 +00001450void ObjCProtocolDecl::anchor() { }
1451
Douglas Gregor32c17572012-01-01 20:30:41 +00001452ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1453 SourceLocation nameLoc,
1454 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001455 ObjCProtocolDecl *PrevDecl)
1456 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001457{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001458 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001459 if (PrevDecl)
1460 Data = PrevDecl->Data;
1461}
1462
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001463ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001464 IdentifierInfo *Id,
1465 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001466 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001467 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001468 ObjCProtocolDecl *Result =
1469 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001470 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001471 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001472}
1473
Richard Smithf7981722013-11-22 09:01:48 +00001474ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001475 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001476 ObjCProtocolDecl *Result =
1477 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001478 Result->Data.setInt(!C.getLangOpts().Modules);
1479 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001480}
1481
Steve Naroff114aecb2009-03-01 16:12:44 +00001482ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1483 ObjCProtocolDecl *PDecl = this;
1484
1485 if (Name == getIdentifier())
1486 return PDecl;
1487
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001488 for (auto *I : protocols())
1489 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001490 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001491
Steve Naroff114aecb2009-03-01 16:12:44 +00001492 return NULL;
1493}
1494
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001495// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001496// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001497ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1498 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001499 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001500
Douglas Gregoreed49792013-01-17 00:38:46 +00001501 // If there is no definition or the definition is hidden, we don't find
1502 // anything.
1503 const ObjCProtocolDecl *Def = getDefinition();
1504 if (!Def || Def->isHidden())
1505 return NULL;
1506
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001507 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001508 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001509
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001510 for (const auto *I : protocols())
1511 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001512 return MethodDecl;
1513 return NULL;
1514}
1515
Douglas Gregore6e48b12012-01-01 19:29:29 +00001516void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001517 assert(!Data.getPointer() && "Protocol already has a definition!");
1518 Data.setPointer(new (getASTContext()) DefinitionData);
1519 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001520}
1521
1522void ObjCProtocolDecl::startDefinition() {
1523 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001524
1525 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001526 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001527 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001528}
1529
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001530void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1531 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001532
1533 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001534 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001535 // Insert into PM if not there already.
1536 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001537 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001538 }
1539 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001540 for (const auto *PI : PDecl->protocols())
1541 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001542 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001543}
1544
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001545
1546void ObjCProtocolDecl::collectInheritedProtocolProperties(
1547 const ObjCPropertyDecl *Property,
1548 ProtocolPropertyMap &PM) const {
1549 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1550 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001551 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001552 if (Prop == Property)
1553 continue;
1554 if (Prop->getIdentifier() == Property->getIdentifier()) {
1555 PM[PDecl] = Prop;
1556 MatchFound = true;
1557 break;
1558 }
1559 }
1560 // Scan through protocol's protocols which did not have a matching property.
1561 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001562 for (const auto *PI : PDecl->protocols())
1563 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001564 }
1565}
Anna Zaks673d76b2012-10-18 19:17:53 +00001566
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001567//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001568// ObjCCategoryDecl
1569//===----------------------------------------------------------------------===//
1570
David Blaikie68e081d2011-12-20 02:48:34 +00001571void ObjCCategoryDecl::anchor() { }
1572
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001573ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001574 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001575 SourceLocation ClassNameLoc,
1576 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001577 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001578 ObjCInterfaceDecl *IDecl,
1579 SourceLocation IvarLBraceLoc,
1580 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001581 ObjCCategoryDecl *CatDecl =
1582 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1583 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001584 if (IDecl) {
1585 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001586 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001587 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001588 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001589 if (ASTMutationListener *L = C.getASTMutationListener())
1590 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1591 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001592 }
1593
1594 return CatDecl;
1595}
1596
Richard Smithf7981722013-11-22 09:01:48 +00001597ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001598 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001599 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1600 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001601}
1602
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001603ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1604 return getASTContext().getObjCImplementation(
1605 const_cast<ObjCCategoryDecl*>(this));
1606}
1607
1608void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1609 getASTContext().setObjCImplementation(this, ImplD);
1610}
1611
1612
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001613//===----------------------------------------------------------------------===//
1614// ObjCCategoryImplDecl
1615//===----------------------------------------------------------------------===//
1616
David Blaikie68e081d2011-12-20 02:48:34 +00001617void ObjCCategoryImplDecl::anchor() { }
1618
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001619ObjCCategoryImplDecl *
1620ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001621 IdentifierInfo *Id,
1622 ObjCInterfaceDecl *ClassInterface,
1623 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001624 SourceLocation atStartLoc,
1625 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001626 if (ClassInterface && ClassInterface->hasDefinition())
1627 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001628 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1629 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001630}
1631
Douglas Gregor72172e92012-01-05 21:55:30 +00001632ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1633 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001634 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1635 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001636}
1637
Steve Narofff406f4d2009-10-29 21:11:04 +00001638ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001639 // The class interface might be NULL if we are working with invalid code.
1640 if (const ObjCInterfaceDecl *ID = getClassInterface())
1641 return ID->FindCategoryDeclaration(getIdentifier());
1642 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001643}
1644
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001645
David Blaikie68e081d2011-12-20 02:48:34 +00001646void ObjCImplDecl::anchor() { }
1647
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001648void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001649 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001650 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001651 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001652}
1653
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001654void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1655 ASTContext &Ctx = getASTContext();
1656
1657 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001658 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001659 if (IFace)
1660 Ctx.setObjCImplementation(IFace, ImplD);
1661
Duncan Sands49c29ee2009-07-21 07:56:29 +00001662 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001663 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1664 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1665 Ctx.setObjCImplementation(CD, ImplD);
1666 }
1667
1668 ClassInterface = IFace;
1669}
1670
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001671/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001672/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001673/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001674///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001675ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001676FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1677 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001678 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001679 if (PID->getPropertyIvarDecl() &&
1680 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1681 return PID;
1682 }
1683 return 0;
1684}
1685
1686/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001687/// added to the list of those properties \@synthesized/\@dynamic in this
1688/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001689///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001690ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001691FindPropertyImplDecl(IdentifierInfo *Id) const {
1692 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001693 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001694 if (PID->getPropertyDecl()->getIdentifier() == Id)
1695 return PID;
1696 }
1697 return 0;
1698}
1699
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001700raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001701 const ObjCCategoryImplDecl &CID) {
1702 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001703 return OS;
1704}
1705
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001706//===----------------------------------------------------------------------===//
1707// ObjCImplementationDecl
1708//===----------------------------------------------------------------------===//
1709
David Blaikie68e081d2011-12-20 02:48:34 +00001710void ObjCImplementationDecl::anchor() { }
1711
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001712ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001713ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001714 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001715 ObjCInterfaceDecl *SuperDecl,
1716 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001717 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001718 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001719 SourceLocation IvarLBraceLoc,
1720 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001721 if (ClassInterface && ClassInterface->hasDefinition())
1722 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001723 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1724 nameLoc, atStartLoc, superLoc,
1725 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001726}
1727
Douglas Gregor72172e92012-01-05 21:55:30 +00001728ObjCImplementationDecl *
1729ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001730 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1731 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001732}
1733
John McCall0410e572011-07-22 04:15:06 +00001734void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1735 CXXCtorInitializer ** initializers,
1736 unsigned numInitializers) {
1737 if (numInitializers > 0) {
1738 NumIvarInitializers = numInitializers;
1739 CXXCtorInitializer **ivarInitializers =
1740 new (C) CXXCtorInitializer*[NumIvarInitializers];
1741 memcpy(ivarInitializers, initializers,
1742 numInitializers * sizeof(CXXCtorInitializer*));
1743 IvarInitializers = ivarInitializers;
1744 }
1745}
1746
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001747raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001748 const ObjCImplementationDecl &ID) {
1749 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001750 return OS;
1751}
1752
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001753//===----------------------------------------------------------------------===//
1754// ObjCCompatibleAliasDecl
1755//===----------------------------------------------------------------------===//
1756
David Blaikie68e081d2011-12-20 02:48:34 +00001757void ObjCCompatibleAliasDecl::anchor() { }
1758
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001759ObjCCompatibleAliasDecl *
1760ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1761 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001762 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001763 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001764 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001765}
1766
Douglas Gregor72172e92012-01-05 21:55:30 +00001767ObjCCompatibleAliasDecl *
1768ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001769 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001770}
1771
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001772//===----------------------------------------------------------------------===//
1773// ObjCPropertyDecl
1774//===----------------------------------------------------------------------===//
1775
David Blaikie68e081d2011-12-20 02:48:34 +00001776void ObjCPropertyDecl::anchor() { }
1777
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001778ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1779 SourceLocation L,
1780 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001781 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001782 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001783 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001784 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001785 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001786}
1787
Richard Smithf7981722013-11-22 09:01:48 +00001788ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001789 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001790 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1791 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001792}
1793
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001794//===----------------------------------------------------------------------===//
1795// ObjCPropertyImplDecl
1796//===----------------------------------------------------------------------===//
1797
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001798ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001799 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001800 SourceLocation atLoc,
1801 SourceLocation L,
1802 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001803 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001804 ObjCIvarDecl *ivar,
1805 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001806 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1807 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001808}
Chris Lattnered0e1642008-03-17 01:19:02 +00001809
Richard Smithf7981722013-11-22 09:01:48 +00001810ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001811 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001812 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1813 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001814}
1815
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001816SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1817 SourceLocation EndLoc = getLocation();
1818 if (IvarLoc.isValid())
1819 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001820
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001821 return SourceRange(AtLoc, EndLoc);
1822}