blob: dd96d39917ce6e954a55dc01a122f17e75ec6022 [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())
230 for (ObjCCategoryDecl::protocol_iterator
231 I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
232 if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
233 return P;
234
235 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000236 }
237 }
Steve Narofff9c65242008-06-05 13:55:23 +0000238 return 0;
239}
240
David Blaikie68e081d2011-12-20 02:48:34 +0000241void ObjCInterfaceDecl::anchor() { }
242
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000243/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
244/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000245/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000246///
247ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000248ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000249 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000250 // FIXME: Should make sure no callers ever do this.
251 if (!hasDefinition())
252 return 0;
253
254 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000255 LoadExternalDefinition();
256
Ted Kremenekd133a862010-03-15 20:30:07 +0000257 if (ObjCPropertyDecl *PD =
258 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
259 return PD;
260
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000261 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000262 for (const auto *I : all_referenced_protocols())
263 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000264 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000265
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000266 return 0;
267}
268
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000269void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
270 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000271 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000272 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000273 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000274 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000275 for (const auto *PI : all_referenced_protocols())
276 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000277 // Note, the properties declared only in class extensions are still copied
278 // into the main @interface's property list, and therefore we don't
279 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000280}
281
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000282bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
283 const ObjCInterfaceDecl *Class = this;
284 while (Class) {
285 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
286 return true;
287 Class = Class->getSuperClass();
288 }
289 return false;
290}
291
292const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
293 const ObjCInterfaceDecl *Class = this;
294 while (Class) {
295 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
296 return Class;
297 Class = Class->getSuperClass();
298 }
299 return 0;
300}
301
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000302void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
303 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
304 ASTContext &C)
305{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000306 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000307 LoadExternalDefinition();
308
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000309 if (data().AllReferencedProtocols.empty() &&
310 data().ReferencedProtocols.empty()) {
311 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000312 return;
313 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000314
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000315 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000316 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000317 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000318 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000319 for (unsigned i = 0; i < ExtNum; i++) {
320 bool protocolExists = false;
321 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000322 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000323 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
324 protocolExists = true;
325 break;
326 }
327 }
328 // Do we want to warn on a protocol in extension class which
329 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000330 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000331 ProtocolRefs.push_back(ProtoInExtension);
332 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000333
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000334 if (ProtocolRefs.empty())
335 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000336
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000337 // Merge ProtocolRefs into class's protocol list;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000338 for (auto *P : all_referenced_protocols()) {
339 ProtocolRefs.push_back(P);
Douglas Gregor002b6712010-01-16 15:02:53 +0000340 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000341
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000342 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000343}
344
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000345const ObjCInterfaceDecl *
346ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
347 const ObjCInterfaceDecl *IFace = this;
348 while (IFace) {
349 if (IFace->hasDesignatedInitializers())
350 return IFace;
351 if (!IFace->inheritsDesignatedInitializers())
352 break;
353 IFace = IFace->getSuperClass();
354 }
355 return 0;
356}
357
358bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
359 switch (data().InheritedDesignatedInitializers) {
360 case DefinitionData::IDI_Inherited:
361 return true;
362 case DefinitionData::IDI_NotInherited:
363 return false;
364 case DefinitionData::IDI_Unknown: {
365 bool isIntroducingInitializers = false;
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000366 for (const auto *MD : instance_methods()) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000367 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding()) {
368 isIntroducingInitializers = true;
369 break;
370 }
371 }
372 // If the class introduced initializers we conservatively assume that we
373 // don't know if any of them is a designated initializer to avoid possible
374 // misleading warnings.
375 if (isIntroducingInitializers) {
376 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
377 return false;
378 } else {
379 data().InheritedDesignatedInitializers = DefinitionData::IDI_Inherited;
380 return true;
381 }
382 }
383 }
384
385 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
386}
387
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000388void ObjCInterfaceDecl::getDesignatedInitializers(
389 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000390 // Check for a complete definition and recover if not so.
391 if (!isThisDeclarationADefinition())
392 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000393 if (data().ExternallyCompleted)
394 LoadExternalDefinition();
395
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000396 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000397 if (!IFace)
398 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000399
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000400 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000401 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000402 Methods.push_back(MD);
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000403}
404
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000405bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
406 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000407 // Check for a complete definition and recover if not so.
408 if (!isThisDeclarationADefinition())
409 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000410 if (data().ExternallyCompleted)
411 LoadExternalDefinition();
412
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000413 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000414 if (!IFace)
415 return false;
416
Argyrios Kyrtzidise919fc22013-12-10 18:36:43 +0000417 if (const ObjCMethodDecl *MD = IFace->getMethod(Sel, /*isInstance=*/true)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000418 if (MD->isThisDeclarationADesignatedInitializer()) {
419 if (InitMethod)
420 *InitMethod = MD;
421 return true;
422 }
423 }
424 return false;
425}
426
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000427void ObjCInterfaceDecl::allocateDefinitionData() {
428 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000429 Data.setPointer(new (getASTContext()) DefinitionData());
430 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000431
432 // Make the type point at the definition, now that we have one.
433 if (TypeForDecl)
434 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000435}
436
437void ObjCInterfaceDecl::startDefinition() {
438 allocateDefinitionData();
439
Douglas Gregor66b310c2011-12-15 18:03:09 +0000440 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000441 for (auto RD : redecls()) {
442 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000443 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000444 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000445}
446
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000447ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
448 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000449 // FIXME: Should make sure no callers ever do this.
450 if (!hasDefinition())
451 return 0;
452
453 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000454 LoadExternalDefinition();
455
Chris Lattner89375192008-03-16 00:19:01 +0000456 ObjCInterfaceDecl* ClassDecl = this;
457 while (ClassDecl != NULL) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000458 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000459 clsDeclared = ClassDecl;
460 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000461 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000462
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000463 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000464 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000465 clsDeclared = ClassDecl;
466 return I;
467 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000468 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000469
Chris Lattner89375192008-03-16 00:19:01 +0000470 ClassDecl = ClassDecl->getSuperClass();
471 }
472 return NULL;
473}
474
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000475/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
476/// class whose name is passed as argument. If it is not one of the super classes
477/// the it returns NULL.
478ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
479 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000480 // FIXME: Should make sure no callers ever do this.
481 if (!hasDefinition())
482 return 0;
483
484 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000485 LoadExternalDefinition();
486
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000487 ObjCInterfaceDecl* ClassDecl = this;
488 while (ClassDecl != NULL) {
489 if (ClassDecl->getIdentifier() == ICName)
490 return ClassDecl;
491 ClassDecl = ClassDecl->getSuperClass();
492 }
493 return NULL;
494}
495
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000496ObjCProtocolDecl *
497ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000498 for (auto *P : all_referenced_protocols())
499 if (P->lookupProtocolNamed(Name))
500 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000501 ObjCInterfaceDecl *SuperClass = getSuperClass();
502 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : NULL;
503}
504
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000505/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000506/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000507/// When argument category "C" is specified, any implicit method found
508/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000509ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000510 bool isInstance,
511 bool shallowCategoryLookup,
512 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000513 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000514{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000515 // FIXME: Should make sure no callers ever do this.
516 if (!hasDefinition())
517 return 0;
518
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000519 const ObjCInterfaceDecl* ClassDecl = this;
Chris Lattner89375192008-03-16 00:19:01 +0000520 ObjCMethodDecl *MethodDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000521
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000522 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000523 LoadExternalDefinition();
524
Ted Kremenek00781502013-11-23 01:01:29 +0000525 while (ClassDecl) {
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000526 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000527 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +0000528
Chris Lattner89375192008-03-16 00:19:01 +0000529 // Didn't find one yet - look through protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000530 for (const auto *I : ClassDecl->protocols())
531 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000532 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000533
534 // Didn't find one yet - now look through categories.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000535 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000536 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000537 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000538 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000539
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000540 if (!shallowCategoryLookup) {
541 // Didn't find one yet - look through protocols.
542 const ObjCList<ObjCProtocolDecl> &Protocols =
543 Cat->getReferencedProtocols();
544 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
545 E = Protocols.end(); I != E; ++I)
546 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000547 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000548 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000549 }
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000550 }
Ted Kremenek00781502013-11-23 01:01:29 +0000551
552 if (!followSuper)
553 return NULL;
554
555 // Get the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000556 ClassDecl = ClassDecl->getSuperClass();
557 }
558 return NULL;
559}
560
Anna Zaksc77a3b12012-07-27 19:07:44 +0000561// Will search "local" class/category implementations for a method decl.
562// If failed, then we search in class's root for an instance method.
563// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000564ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
565 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000566 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000567 // FIXME: Should make sure no callers ever do this.
568 if (!hasDefinition())
569 return 0;
570
571 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000572 LoadExternalDefinition();
573
Steve Naroffbb69c942009-10-01 23:46:04 +0000574 ObjCMethodDecl *Method = 0;
575 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000576 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
577 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000578
579 // Look through local category implementations associated with the class.
580 if (!Method)
581 Method = Instance ? getCategoryInstanceMethod(Sel)
582 : getCategoryClassMethod(Sel);
583
584 // Before we give up, check if the selector is an instance method.
585 // But only in the root. This matches gcc's behavior and what the
586 // runtime expects.
587 if (!Instance && !Method && !getSuperClass()) {
588 Method = lookupInstanceMethod(Sel);
589 // Look through local category implementations associated
590 // with the root class.
591 if (!Method)
592 Method = lookupPrivateMethod(Sel, true);
593 }
594
Steve Naroffbb69c942009-10-01 23:46:04 +0000595 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000596 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000597 return Method;
598}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000599
600//===----------------------------------------------------------------------===//
601// ObjCMethodDecl
602//===----------------------------------------------------------------------===//
603
Alp Toker314cc812014-01-25 16:55:45 +0000604ObjCMethodDecl *ObjCMethodDecl::Create(
605 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
606 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
607 DeclContext *contextDecl, bool isInstance, bool isVariadic,
608 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
609 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000610 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000611 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000612 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
613 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000614}
615
Douglas Gregor72172e92012-01-05 21:55:30 +0000616ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000617 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
618 Selector(), QualType(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000619}
620
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000621bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
622 return getMethodFamily() == OMF_init &&
623 hasAttr<ObjCDesignatedInitializerAttr>();
624}
625
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000626bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
627 const ObjCMethodDecl **InitMethod) const {
628 if (getMethodFamily() != OMF_init)
629 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000630 const DeclContext *DC = getDeclContext();
631 if (isa<ObjCProtocolDecl>(DC))
632 return false;
633 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000634 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000635 return false;
636}
637
Douglas Gregora6017bb2012-10-09 17:21:28 +0000638Stmt *ObjCMethodDecl::getBody() const {
639 return Body.get(getASTContext().getExternalSource());
640}
641
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000642void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
643 assert(PrevMethod);
644 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
645 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000646 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000647}
648
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000649void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
650 ArrayRef<ParmVarDecl*> Params,
651 ArrayRef<SourceLocation> SelLocs) {
652 ParamsAndSelLocs = 0;
653 NumParams = Params.size();
654 if (Params.empty() && SelLocs.empty())
655 return;
656
657 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
658 sizeof(SourceLocation) * SelLocs.size();
659 ParamsAndSelLocs = C.Allocate(Size);
660 std::copy(Params.begin(), Params.end(), getParams());
661 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
662}
663
664void ObjCMethodDecl::getSelectorLocs(
665 SmallVectorImpl<SourceLocation> &SelLocs) const {
666 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
667 SelLocs.push_back(getSelectorLoc(i));
668}
669
670void ObjCMethodDecl::setMethodParams(ASTContext &C,
671 ArrayRef<ParmVarDecl*> Params,
672 ArrayRef<SourceLocation> SelLocs) {
673 assert((!SelLocs.empty() || isImplicit()) &&
674 "No selector locs for non-implicit method");
675 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000676 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000677
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000678 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
679 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000680 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000681 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000682
683 setParamsAndSelLocs(C, Params, SelLocs);
684}
685
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000686/// \brief A definition will return its interface declaration.
687/// An interface declaration will return its definition.
688/// Otherwise it will return itself.
689ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
690 ASTContext &Ctx = getASTContext();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000691 ObjCMethodDecl *Redecl = 0;
692 if (HasRedeclaration)
693 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000694 if (Redecl)
695 return Redecl;
696
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000697 Decl *CtxD = cast<Decl>(getDeclContext());
698
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000699 if (!CtxD->isInvalidDecl()) {
700 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
701 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
702 if (!ImplD->isInvalidDecl())
703 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000704
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000705 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
706 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
707 if (!ImplD->isInvalidDecl())
708 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000709
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000710 } else if (ObjCImplementationDecl *ImplD =
711 dyn_cast<ObjCImplementationDecl>(CtxD)) {
712 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
713 if (!IFD->isInvalidDecl())
714 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000715
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000716 } else if (ObjCCategoryImplDecl *CImplD =
717 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
718 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
719 if (!CatD->isInvalidDecl())
720 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
721 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000722 }
723
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000724 if (!Redecl && isRedeclaration()) {
725 // This is the last redeclaration, go back to the first method.
726 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
727 isInstanceMethod());
728 }
729
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000730 return Redecl ? Redecl : this;
731}
732
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000733ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
734 Decl *CtxD = cast<Decl>(getDeclContext());
735
736 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
737 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
738 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
739 isInstanceMethod()))
740 return MD;
741
742 } else if (ObjCCategoryImplDecl *CImplD =
743 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000744 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000745 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
746 isInstanceMethod()))
747 return MD;
748 }
749
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000750 if (isRedeclaration())
751 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
752 isInstanceMethod());
753
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000754 return this;
755}
756
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000757SourceLocation ObjCMethodDecl::getLocEnd() const {
758 if (Stmt *Body = getBody())
759 return Body->getLocEnd();
760 return DeclEndLoc;
761}
762
John McCallb4526252011-03-02 01:50:55 +0000763ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
764 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000765 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000766 return family;
767
John McCall86bc21f2011-03-02 11:33:24 +0000768 // Check for an explicit attribute.
769 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
770 // The unfortunate necessity of mapping between enums here is due
771 // to the attributes framework.
772 switch (attr->getFamily()) {
773 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
774 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
775 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
776 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
777 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
778 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
779 }
780 Family = static_cast<unsigned>(family);
781 return family;
782 }
783
John McCallb4526252011-03-02 01:50:55 +0000784 family = getSelector().getMethodFamily();
785 switch (family) {
786 case OMF_None: break;
787
788 // init only has a conventional meaning for an instance method, and
789 // it has to return an object.
790 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000791 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000792 family = OMF_None;
793 break;
794
795 // alloc/copy/new have a conventional meaning for both class and
796 // instance methods, but they require an object return.
797 case OMF_alloc:
798 case OMF_copy:
799 case OMF_mutableCopy:
800 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000801 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000802 family = OMF_None;
803 break;
804
805 // These selectors have a conventional meaning only for instance methods.
806 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000807 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000808 case OMF_retain:
809 case OMF_release:
810 case OMF_autorelease:
811 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000812 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000813 if (!isInstanceMethod())
814 family = OMF_None;
815 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000816
817 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000818 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000819 family = OMF_None;
820 else {
821 unsigned noParams = param_size();
822 if (noParams < 1 || noParams > 3)
823 family = OMF_None;
824 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000825 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000826 QualType ArgT = (*it);
827 if (!ArgT->isObjCSelType()) {
828 family = OMF_None;
829 break;
830 }
831 while (--noParams) {
832 it++;
833 ArgT = (*it);
834 if (!ArgT->isObjCIdType()) {
835 family = OMF_None;
836 break;
837 }
838 }
839 }
840 }
841 break;
842
John McCallb4526252011-03-02 01:50:55 +0000843 }
844
845 // Cache the result.
846 Family = static_cast<unsigned>(family);
847 return family;
848}
849
Mike Stump11289f42009-09-09 15:08:12 +0000850void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000851 const ObjCInterfaceDecl *OID) {
852 QualType selfTy;
853 if (isInstanceMethod()) {
854 // There may be no interface context due to error in declaration
855 // of the interface (which has been reported). Recover gracefully.
856 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000857 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000858 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000859 } else {
860 selfTy = Context.getObjCIdType();
861 }
862 } else // we have a factory method.
863 selfTy = Context.getObjCClassType();
864
John McCalld4631322011-06-17 06:42:21 +0000865 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000866 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000867
David Blaikiebbafb8a2012-03-11 07:00:24 +0000868 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000869 if (isInstanceMethod()) {
870 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000871
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000872 // 'self' is always __strong. It's actually pseudo-strong except
873 // in init methods (or methods labeled ns_consumes_self), though.
874 Qualifiers qs;
875 qs.setObjCLifetime(Qualifiers::OCL_Strong);
876 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000877
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000878 // In addition, 'self' is const unless this is an init method.
879 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
880 selfTy = selfTy.withConst();
881 selfIsPseudoStrong = true;
882 }
883 }
884 else {
885 assert(isClassMethod());
886 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000887 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000888 selfIsPseudoStrong = true;
889 }
John McCall31168b02011-06-15 23:02:42 +0000890 }
891
892 ImplicitParamDecl *self
893 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
894 &Context.Idents.get("self"), selfTy);
895 setSelfDecl(self);
896
897 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000898 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000899
John McCalld4631322011-06-17 06:42:21 +0000900 if (selfIsPseudoStrong)
901 self->setARCPseudoStrong(true);
902
Mike Stump11289f42009-09-09 15:08:12 +0000903 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
904 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000905 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000906}
907
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000908ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
909 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
910 return ID;
911 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
912 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000913 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000914 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000915 if (isa<ObjCProtocolDecl>(getDeclContext()))
916 return 0;
David Blaikie83d382b2011-09-23 05:06:16 +0000917 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000918}
919
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000920static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
921 const ObjCMethodDecl *Method,
922 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
923 bool MovedToSuper) {
924 if (!Container)
925 return;
926
927 // In categories look for overriden methods from protocols. A method from
928 // category is not "overriden" since it is considered as the "same" method
929 // (same USR) as the one from the interface.
930 if (const ObjCCategoryDecl *
931 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
932 // Check whether we have a matching method at this category but only if we
933 // are at the super class level.
934 if (MovedToSuper)
935 if (ObjCMethodDecl *
936 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000937 Method->isInstanceMethod(),
938 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000939 if (Method != Overridden) {
940 // We found an override at this category; there is no need to look
941 // into its protocols.
942 Methods.push_back(Overridden);
943 return;
944 }
945
946 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
947 PEnd = Category->protocol_end();
948 P != PEnd; ++P)
949 CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
950 return;
951 }
952
953 // Check whether we have a matching method at this level.
954 if (const ObjCMethodDecl *
955 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000956 Method->isInstanceMethod(),
957 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000958 if (Method != Overridden) {
959 // We found an override at this level; there is no need to look
960 // into other protocols or categories.
961 Methods.push_back(Overridden);
962 return;
963 }
964
965 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000966 for (const auto *P : Protocol->protocols())
967 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000968 }
969
970 if (const ObjCInterfaceDecl *
971 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +0000972 for (const auto *P : Interface->protocols())
973 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000974
Aaron Ballman15063e12014-03-13 21:35:02 +0000975 for (const auto *Cat : Interface->known_categories())
976 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000977
978 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
979 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
980 /*MovedToSuper=*/true);
981 }
982}
983
984static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
985 const ObjCMethodDecl *Method,
986 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
987 CollectOverriddenMethodsRecurse(Container, Method, Methods,
988 /*MovedToSuper=*/false);
989}
990
991static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
992 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
993 assert(Method->isOverriding());
994
995 if (const ObjCProtocolDecl *
996 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
997 CollectOverriddenMethods(ProtD, Method, overridden);
998
999 } else if (const ObjCImplDecl *
1000 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1001 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1002 if (!ID)
1003 return;
1004 // Start searching for overridden methods using the method from the
1005 // interface as starting point.
1006 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001007 Method->isInstanceMethod(),
1008 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001009 Method = IFaceMeth;
1010 CollectOverriddenMethods(ID, Method, overridden);
1011
1012 } else if (const ObjCCategoryDecl *
1013 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1014 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1015 if (!ID)
1016 return;
1017 // Start searching for overridden methods using the method from the
1018 // interface as starting point.
1019 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001020 Method->isInstanceMethod(),
1021 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001022 Method = IFaceMeth;
1023 CollectOverriddenMethods(ID, Method, overridden);
1024
1025 } else {
1026 CollectOverriddenMethods(
1027 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1028 Method, overridden);
1029 }
1030}
1031
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001032void ObjCMethodDecl::getOverriddenMethods(
1033 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1034 const ObjCMethodDecl *Method = this;
1035
1036 if (Method->isRedeclaration()) {
1037 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1038 getMethod(Method->getSelector(), Method->isInstanceMethod());
1039 }
1040
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001041 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001042 collectOverriddenMethodsSlow(Method, Overridden);
1043 assert(!Overridden.empty() &&
1044 "ObjCMethodDecl's overriding bit is not as expected");
1045 }
1046}
1047
Jordan Rose2bd991a2012-10-10 16:42:54 +00001048const ObjCPropertyDecl *
1049ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1050 Selector Sel = getSelector();
1051 unsigned NumArgs = Sel.getNumArgs();
1052 if (NumArgs > 1)
1053 return 0;
1054
Jordan Rose59e34ec2012-10-11 16:02:02 +00001055 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Jordan Rose2bd991a2012-10-10 16:42:54 +00001056 return 0;
1057
1058 if (isPropertyAccessor()) {
1059 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001060 // If container is class extension, find its primary class.
1061 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1062 if (CatDecl->IsClassExtension())
1063 Container = CatDecl->getClassInterface();
1064
Jordan Rose2bd991a2012-10-10 16:42:54 +00001065 bool IsGetter = (NumArgs == 0);
1066
Aaron Ballmand174edf2014-03-13 19:11:50 +00001067 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001068 Selector NextSel = IsGetter ? I->getGetterName()
1069 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001070 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001071 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001072 }
1073
1074 llvm_unreachable("Marked as a property accessor but no property found!");
1075 }
1076
1077 if (!CheckOverrides)
1078 return 0;
1079
1080 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1081 OverridesTy Overrides;
1082 getOverriddenMethods(Overrides);
1083 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1084 I != E; ++I) {
1085 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1086 return Prop;
1087 }
1088
1089 return 0;
1090
1091}
1092
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001093//===----------------------------------------------------------------------===//
1094// ObjCInterfaceDecl
1095//===----------------------------------------------------------------------===//
1096
Douglas Gregord53ae832012-01-17 18:09:05 +00001097ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001098 DeclContext *DC,
1099 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001100 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001101 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001102 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001103 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001104 ObjCInterfaceDecl *Result = new (C, DC)
1105 ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001106 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001107 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001108 return Result;
1109}
1110
Richard Smithf7981722013-11-22 09:01:48 +00001111ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001112 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001113 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(0, SourceLocation(),
1114 0, SourceLocation(),
1115 0, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001116 Result->Data.setInt(!C.getLangOpts().Modules);
1117 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001118}
1119
1120ObjCInterfaceDecl::
1121ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
Douglas Gregor81252352011-12-16 22:37:11 +00001122 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1123 bool isInternal)
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001124 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
Douglas Gregordc9166c2011-12-15 20:29:51 +00001125 TypeForDecl(0), Data()
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001126{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001127 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001128
1129 // Copy the 'data' pointer over.
1130 if (PrevDecl)
1131 Data = PrevDecl->Data;
1132
Argyrios Kyrtzidisae8e7922011-11-15 06:20:21 +00001133 setImplicit(isInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001134}
1135
Douglas Gregor73693022010-12-01 23:49:52 +00001136void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001137 assert(data().ExternallyCompleted && "Class is not externally completed");
1138 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001139 getASTContext().getExternalSource()->CompleteType(
1140 const_cast<ObjCInterfaceDecl *>(this));
1141}
1142
1143void ObjCInterfaceDecl::setExternallyCompleted() {
1144 assert(getASTContext().getExternalSource() &&
1145 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001146 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001147 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001148 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001149}
1150
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001151void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001152 // Check for a complete definition and recover if not so.
1153 if (!isThisDeclarationADefinition())
1154 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001155 data().HasDesignatedInitializers = true;
1156}
1157
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001158bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001159 // Check for a complete definition and recover if not so.
1160 if (!isThisDeclarationADefinition())
1161 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001162 if (data().ExternallyCompleted)
1163 LoadExternalDefinition();
1164
1165 return data().HasDesignatedInitializers;
1166}
1167
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001168ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001169 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1170 if (data().ExternallyCompleted)
1171 LoadExternalDefinition();
1172
1173 return getASTContext().getObjCImplementation(
1174 const_cast<ObjCInterfaceDecl*>(Def));
1175 }
1176
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001177 // FIXME: Should make sure no callers ever do this.
Douglas Gregordc9166c2011-12-15 20:29:51 +00001178 return 0;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001179}
1180
1181void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001182 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001183}
1184
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001185namespace {
1186 struct SynthesizeIvarChunk {
1187 uint64_t Size;
1188 ObjCIvarDecl *Ivar;
1189 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1190 : Size(size), Ivar(ivar) {}
1191 };
1192
1193 bool operator<(const SynthesizeIvarChunk & LHS,
1194 const SynthesizeIvarChunk &RHS) {
1195 return LHS.Size < RHS.Size;
1196 }
1197}
1198
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001199/// all_declared_ivar_begin - return first ivar declared in this class,
1200/// its extensions and its implementation. Lazily build the list on first
1201/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001202///
1203/// Caveat: The list returned by this method reflects the current
1204/// state of the parser. The cache will be updated for every ivar
1205/// added by an extension or the implementation when they are
1206/// encountered.
1207/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001208ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001209 // FIXME: Should make sure no callers ever do this.
1210 if (!hasDefinition())
1211 return 0;
1212
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001213 ObjCIvarDecl *curIvar = 0;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001214 if (!data().IvarList) {
1215 if (!ivar_empty()) {
1216 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1217 data().IvarList = *I; ++I;
1218 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001219 curIvar->setNextIvar(*I);
1220 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001221
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001222 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001223 if (!Ext->ivar_empty()) {
1224 ObjCCategoryDecl::ivar_iterator
1225 I = Ext->ivar_begin(),
1226 E = Ext->ivar_end();
1227 if (!data().IvarList) {
1228 data().IvarList = *I; ++I;
1229 curIvar = data().IvarList;
1230 }
1231 for ( ;I != E; curIvar = *I, ++I)
1232 curIvar->setNextIvar(*I);
1233 }
1234 }
1235 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001236 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001237
1238 // cached and complete!
1239 if (!data().IvarListMissingImplementation)
1240 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001241
1242 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001243 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001244 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001245 SmallVector<SynthesizeIvarChunk, 16> layout;
1246 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1247 E = ImplDecl->ivar_end(); I != E; ++I) {
1248 ObjCIvarDecl *IV = *I;
1249 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1250 layout.push_back(SynthesizeIvarChunk(
1251 IV->getASTContext().getTypeSize(IV->getType()), IV));
1252 continue;
1253 }
1254 if (!data().IvarList)
1255 data().IvarList = *I;
1256 else
1257 curIvar->setNextIvar(*I);
1258 curIvar = *I;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001259 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001260
1261 if (!layout.empty()) {
1262 // Order synthesized ivars by their size.
1263 std::stable_sort(layout.begin(), layout.end());
1264 unsigned Ix = 0, EIx = layout.size();
1265 if (!data().IvarList) {
1266 data().IvarList = layout[0].Ivar; Ix++;
1267 curIvar = data().IvarList;
1268 }
1269 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1270 curIvar->setNextIvar(layout[Ix].Ivar);
1271 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001272 }
1273 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001274 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001275}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001276
1277/// FindCategoryDeclaration - Finds category declaration in the list of
1278/// categories for this class and returns it. Name of the category is passed
1279/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001280///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001281ObjCCategoryDecl *
1282ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001283 // FIXME: Should make sure no callers ever do this.
1284 if (!hasDefinition())
1285 return 0;
1286
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001287 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001288 LoadExternalDefinition();
1289
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001290 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001291 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001292 return Cat;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001293
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001294 return 0;
1295}
1296
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001297ObjCMethodDecl *
1298ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001299 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001300 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001301 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1302 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001303 }
1304
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001305 return 0;
1306}
1307
1308ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001309 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001310 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001311 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1312 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001313 }
1314
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001315 return 0;
1316}
1317
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001318/// ClassImplementsProtocol - Checks that 'lProto' protocol
1319/// has been implemented in IDecl class, its super class or categories (if
1320/// lookupCategory is true).
1321bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1322 bool lookupCategory,
1323 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001324 if (!hasDefinition())
1325 return false;
1326
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001327 ObjCInterfaceDecl *IDecl = this;
1328 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001329 for (auto *PI : IDecl->protocols()){
1330 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001331 return true;
1332 // This is dubious and is added to be compatible with gcc. In gcc, it is
1333 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1334 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1335 // object. This IMO, should be a bug.
1336 // FIXME: Treat this as an extension, and flag this as an error when GCC
1337 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001338 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001339 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001340 return true;
1341 }
Mike Stump11289f42009-09-09 15:08:12 +00001342
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001343 // 2nd, look up the category.
1344 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001345 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001346 for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1347 E = Cat->protocol_end();
1348 PI != E; ++PI)
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001349 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1350 return true;
1351 }
Mike Stump11289f42009-09-09 15:08:12 +00001352
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001353 // 3rd, look up the super class(s)
1354 if (IDecl->getSuperClass())
1355 return
1356 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1357 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001358
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001359 return false;
1360}
1361
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001362//===----------------------------------------------------------------------===//
1363// ObjCIvarDecl
1364//===----------------------------------------------------------------------===//
1365
David Blaikie68e081d2011-12-20 02:48:34 +00001366void ObjCIvarDecl::anchor() { }
1367
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001368ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001369 SourceLocation StartLoc,
1370 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001371 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001372 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001373 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001374 if (DC) {
1375 // Ivar's can only appear in interfaces, implementations (via synthesized
1376 // properties), and class extensions (via direct declaration, or synthesized
1377 // properties).
1378 //
1379 // FIXME: This should really be asserting this:
1380 // (isa<ObjCCategoryDecl>(DC) &&
1381 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1382 // but unfortunately we sometimes place ivars into non-class extension
1383 // categories on error. This breaks an AST invariant, and should not be
1384 // fixed.
1385 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1386 isa<ObjCCategoryDecl>(DC)) &&
1387 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001388 // Once a new ivar is created in any of class/class-extension/implementation
1389 // decl contexts, the previously built IvarList must be rebuilt.
1390 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1391 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001392 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001393 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001394 else
1395 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001396 }
1397 ID->setIvarList(0);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001398 }
1399
Richard Smithf7981722013-11-22 09:01:48 +00001400 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001401 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001402}
1403
Douglas Gregor72172e92012-01-05 21:55:30 +00001404ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001405 return new (C, ID) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001406 QualType(), 0, ObjCIvarDecl::None, 0, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001407}
1408
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001409const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1410 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001411
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001412 switch (DC->getKind()) {
1413 default:
1414 case ObjCCategoryImpl:
1415 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001416 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001417
1418 // Ivars can only appear in class extension categories.
1419 case ObjCCategory: {
1420 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1421 assert(CD->IsClassExtension() && "invalid container for ivar!");
1422 return CD->getClassInterface();
1423 }
1424
1425 case ObjCImplementation:
1426 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1427
1428 case ObjCInterface:
1429 return cast<ObjCInterfaceDecl>(DC);
1430 }
1431}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001432
1433//===----------------------------------------------------------------------===//
1434// ObjCAtDefsFieldDecl
1435//===----------------------------------------------------------------------===//
1436
David Blaikie68e081d2011-12-20 02:48:34 +00001437void ObjCAtDefsFieldDecl::anchor() { }
1438
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001439ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001440*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1441 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001442 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001443 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001444}
1445
Richard Smithf7981722013-11-22 09:01:48 +00001446ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001447 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001448 return new (C, ID) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1449 0, QualType(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001450}
1451
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001452//===----------------------------------------------------------------------===//
1453// ObjCProtocolDecl
1454//===----------------------------------------------------------------------===//
1455
David Blaikie68e081d2011-12-20 02:48:34 +00001456void ObjCProtocolDecl::anchor() { }
1457
Douglas Gregor32c17572012-01-01 20:30:41 +00001458ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1459 SourceLocation nameLoc,
1460 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001461 ObjCProtocolDecl *PrevDecl)
1462 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
Douglas Gregor32c17572012-01-01 20:30:41 +00001463{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001464 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001465 if (PrevDecl)
1466 Data = PrevDecl->Data;
1467}
1468
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001469ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001470 IdentifierInfo *Id,
1471 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001472 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001473 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001474 ObjCProtocolDecl *Result =
1475 new (C, DC) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001476 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001477 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001478}
1479
Richard Smithf7981722013-11-22 09:01:48 +00001480ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001481 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001482 ObjCProtocolDecl *Result =
1483 new (C, ID) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(), 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001484 Result->Data.setInt(!C.getLangOpts().Modules);
1485 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001486}
1487
Steve Naroff114aecb2009-03-01 16:12:44 +00001488ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1489 ObjCProtocolDecl *PDecl = this;
1490
1491 if (Name == getIdentifier())
1492 return PDecl;
1493
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001494 for (auto *I : protocols())
1495 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001496 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001497
Steve Naroff114aecb2009-03-01 16:12:44 +00001498 return NULL;
1499}
1500
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001501// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001502// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001503ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1504 bool isInstance) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001505 ObjCMethodDecl *MethodDecl = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001506
Douglas Gregoreed49792013-01-17 00:38:46 +00001507 // If there is no definition or the definition is hidden, we don't find
1508 // anything.
1509 const ObjCProtocolDecl *Def = getDefinition();
1510 if (!Def || Def->isHidden())
1511 return NULL;
1512
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001513 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001514 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001515
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001516 for (const auto *I : protocols())
1517 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001518 return MethodDecl;
1519 return NULL;
1520}
1521
Douglas Gregore6e48b12012-01-01 19:29:29 +00001522void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001523 assert(!Data.getPointer() && "Protocol already has a definition!");
1524 Data.setPointer(new (getASTContext()) DefinitionData);
1525 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001526}
1527
1528void ObjCProtocolDecl::startDefinition() {
1529 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001530
1531 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001532 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001533 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001534}
1535
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001536void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1537 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001538
1539 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001540 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001541 // Insert into PM if not there already.
1542 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001543 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001544 }
1545 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001546 for (const auto *PI : PDecl->protocols())
1547 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001548 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001549}
1550
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001551
1552void ObjCProtocolDecl::collectInheritedProtocolProperties(
1553 const ObjCPropertyDecl *Property,
1554 ProtocolPropertyMap &PM) const {
1555 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1556 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001557 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001558 if (Prop == Property)
1559 continue;
1560 if (Prop->getIdentifier() == Property->getIdentifier()) {
1561 PM[PDecl] = Prop;
1562 MatchFound = true;
1563 break;
1564 }
1565 }
1566 // Scan through protocol's protocols which did not have a matching property.
1567 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001568 for (const auto *PI : PDecl->protocols())
1569 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001570 }
1571}
Anna Zaks673d76b2012-10-18 19:17:53 +00001572
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001573//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001574// ObjCCategoryDecl
1575//===----------------------------------------------------------------------===//
1576
David Blaikie68e081d2011-12-20 02:48:34 +00001577void ObjCCategoryDecl::anchor() { }
1578
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001579ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001580 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001581 SourceLocation ClassNameLoc,
1582 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001583 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001584 ObjCInterfaceDecl *IDecl,
1585 SourceLocation IvarLBraceLoc,
1586 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001587 ObjCCategoryDecl *CatDecl =
1588 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1589 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001590 if (IDecl) {
1591 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001592 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001593 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001594 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001595 if (ASTMutationListener *L = C.getASTMutationListener())
1596 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1597 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001598 }
1599
1600 return CatDecl;
1601}
1602
Richard Smithf7981722013-11-22 09:01:48 +00001603ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001604 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001605 return new (C, ID) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1606 SourceLocation(), 0, 0);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001607}
1608
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001609ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1610 return getASTContext().getObjCImplementation(
1611 const_cast<ObjCCategoryDecl*>(this));
1612}
1613
1614void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1615 getASTContext().setObjCImplementation(this, ImplD);
1616}
1617
1618
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001619//===----------------------------------------------------------------------===//
1620// ObjCCategoryImplDecl
1621//===----------------------------------------------------------------------===//
1622
David Blaikie68e081d2011-12-20 02:48:34 +00001623void ObjCCategoryImplDecl::anchor() { }
1624
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001625ObjCCategoryImplDecl *
1626ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001627 IdentifierInfo *Id,
1628 ObjCInterfaceDecl *ClassInterface,
1629 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001630 SourceLocation atStartLoc,
1631 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001632 if (ClassInterface && ClassInterface->hasDefinition())
1633 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001634 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1635 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001636}
1637
Douglas Gregor72172e92012-01-05 21:55:30 +00001638ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1639 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001640 return new (C, ID) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1641 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001642}
1643
Steve Narofff406f4d2009-10-29 21:11:04 +00001644ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001645 // The class interface might be NULL if we are working with invalid code.
1646 if (const ObjCInterfaceDecl *ID = getClassInterface())
1647 return ID->FindCategoryDeclaration(getIdentifier());
1648 return 0;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001649}
1650
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001651
David Blaikie68e081d2011-12-20 02:48:34 +00001652void ObjCImplDecl::anchor() { }
1653
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001654void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001655 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001656 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001657 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001658}
1659
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001660void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1661 ASTContext &Ctx = getASTContext();
1662
1663 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001664 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001665 if (IFace)
1666 Ctx.setObjCImplementation(IFace, ImplD);
1667
Duncan Sands49c29ee2009-07-21 07:56:29 +00001668 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001669 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1670 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1671 Ctx.setObjCImplementation(CD, ImplD);
1672 }
1673
1674 ClassInterface = IFace;
1675}
1676
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001677/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001678/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001679/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001680///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001681ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001682FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1683 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001684 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001685 if (PID->getPropertyIvarDecl() &&
1686 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1687 return PID;
1688 }
1689 return 0;
1690}
1691
1692/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001693/// added to the list of those properties \@synthesized/\@dynamic in this
1694/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001695///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001696ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001697FindPropertyImplDecl(IdentifierInfo *Id) const {
1698 for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
David Blaikie40ed2972012-06-06 20:45:41 +00001699 ObjCPropertyImplDecl *PID = *i;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001700 if (PID->getPropertyDecl()->getIdentifier() == Id)
1701 return PID;
1702 }
1703 return 0;
1704}
1705
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001706raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001707 const ObjCCategoryImplDecl &CID) {
1708 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001709 return OS;
1710}
1711
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001712//===----------------------------------------------------------------------===//
1713// ObjCImplementationDecl
1714//===----------------------------------------------------------------------===//
1715
David Blaikie68e081d2011-12-20 02:48:34 +00001716void ObjCImplementationDecl::anchor() { }
1717
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001718ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001719ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001720 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001721 ObjCInterfaceDecl *SuperDecl,
1722 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001723 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001724 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001725 SourceLocation IvarLBraceLoc,
1726 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001727 if (ClassInterface && ClassInterface->hasDefinition())
1728 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001729 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1730 nameLoc, atStartLoc, superLoc,
1731 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001732}
1733
Douglas Gregor72172e92012-01-05 21:55:30 +00001734ObjCImplementationDecl *
1735ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001736 return new (C, ID) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1737 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001738}
1739
John McCall0410e572011-07-22 04:15:06 +00001740void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1741 CXXCtorInitializer ** initializers,
1742 unsigned numInitializers) {
1743 if (numInitializers > 0) {
1744 NumIvarInitializers = numInitializers;
1745 CXXCtorInitializer **ivarInitializers =
1746 new (C) CXXCtorInitializer*[NumIvarInitializers];
1747 memcpy(ivarInitializers, initializers,
1748 numInitializers * sizeof(CXXCtorInitializer*));
1749 IvarInitializers = ivarInitializers;
1750 }
1751}
1752
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001753raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001754 const ObjCImplementationDecl &ID) {
1755 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001756 return OS;
1757}
1758
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001759//===----------------------------------------------------------------------===//
1760// ObjCCompatibleAliasDecl
1761//===----------------------------------------------------------------------===//
1762
David Blaikie68e081d2011-12-20 02:48:34 +00001763void ObjCCompatibleAliasDecl::anchor() { }
1764
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001765ObjCCompatibleAliasDecl *
1766ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1767 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001768 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001769 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001770 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001771}
1772
Douglas Gregor72172e92012-01-05 21:55:30 +00001773ObjCCompatibleAliasDecl *
1774ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001775 return new (C, ID) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001776}
1777
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001778//===----------------------------------------------------------------------===//
1779// ObjCPropertyDecl
1780//===----------------------------------------------------------------------===//
1781
David Blaikie68e081d2011-12-20 02:48:34 +00001782void ObjCPropertyDecl::anchor() { }
1783
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001784ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1785 SourceLocation L,
1786 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001787 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001788 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001789 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001790 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001791 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001792}
1793
Richard Smithf7981722013-11-22 09:01:48 +00001794ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001795 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001796 return new (C, ID) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1797 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001798}
1799
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001800//===----------------------------------------------------------------------===//
1801// ObjCPropertyImplDecl
1802//===----------------------------------------------------------------------===//
1803
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001804ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001805 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001806 SourceLocation atLoc,
1807 SourceLocation L,
1808 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001809 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001810 ObjCIvarDecl *ivar,
1811 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001812 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1813 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001814}
Chris Lattnered0e1642008-03-17 01:19:02 +00001815
Richard Smithf7981722013-11-22 09:01:48 +00001816ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001817 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001818 return new (C, ID) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1819 0, Dynamic, 0, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001820}
1821
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001822SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1823 SourceLocation EndLoc = getLocation();
1824 if (IvarLoc.isValid())
1825 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001826
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001827 return SourceRange(AtLoc, EndLoc);
1828}