blob: cc6560787ca9902a7f8212b46d849603dac76d8d [file] [log] [blame]
Chris Lattner89375192008-03-16 00:19:01 +00001//===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000016#include "clang/AST/ASTMutationListener.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/Stmt.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000019#include "llvm/ADT/STLExtras.h"
Anna Zaks454477c2012-09-27 19:45:11 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner89375192008-03-16 00:19:01 +000021using namespace clang;
22
Chris Lattner8d8829e2008-03-16 00:49:28 +000023//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000024// ObjCListBase
25//===----------------------------------------------------------------------===//
26
Chris Lattner22298722009-02-20 21:35:13 +000027void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Craig Topper36250ad2014-05-12 05:36:57 +000028 List = nullptr;
Chris Lattner4d1eb762009-02-20 21:16:26 +000029 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000030
31
Chris Lattner7c981a72009-02-20 21:44:01 +000032 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000033 NumElts = Elts;
34 memcpy(List, InList, sizeof(void*)*Elts);
35}
36
Douglas Gregor002b6712010-01-16 15:02:53 +000037void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
38 const SourceLocation *Locs, ASTContext &Ctx) {
39 if (Elts == 0)
40 return;
41
42 Locations = new (Ctx) SourceLocation[Elts];
43 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
44 set(InList, Elts, Ctx);
45}
46
Chris Lattner4d1eb762009-02-20 21:16:26 +000047//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000048// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000049//===----------------------------------------------------------------------===//
50
David Blaikie68e081d2011-12-20 02:48:34 +000051void ObjCContainerDecl::anchor() { }
52
Fariborz Jahanian68453832009-06-05 18:16:35 +000053/// getIvarDecl - This method looks up an ivar in this ContextDecl.
54///
55ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000056ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
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 }
Craig Topper36250ad2014-05-12 05:36:57 +000063 return nullptr;
Fariborz Jahanian68453832009-06-05 18:16:35 +000064}
65
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000066// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000067ObjCMethodDecl *
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000068ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69 bool AllowHidden) const {
Douglas Gregoreed49792013-01-17 00:38:46 +000070 // If this context is a hidden protocol definition, don't find any
71 // methods there.
72 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
73 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000074 if (Def->isHidden() && !AllowHidden)
Craig Topper36250ad2014-05-12 05:36:57 +000075 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +000076 }
77
Steve Naroffc4173fa2009-02-22 19:35:57 +000078 // Since instance & class methods can have the same name, the loop below
79 // ensures we get the correct method.
80 //
81 // @interface Whatever
82 // - (int) class_method;
83 // + (float) class_method;
84 // @end
85 //
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 }
Craig Topper36250ad2014-05-12 05:36:57 +000093 return nullptr;
Steve Naroff35c62ae2009-01-08 17:28:14 +000094}
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())
Craig Topper36250ad2014-05-12 05:36:57 +0000160 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000161 }
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
Craig Topper36250ad2014-05-12 05:36:57 +0000169 return nullptr;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000170}
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())
Craig Topper36250ad2014-05-12 05:36:57 +0000190 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000191 }
Mike Stump11289f42009-09-09 15:08:12 +0000192
Ted Kremenekddcd1092010-03-15 20:11:53 +0000193 if (ObjCPropertyDecl *PD =
194 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
195 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000196
Ted Kremenekddcd1092010-03-15 20:11:53 +0000197 switch (getKind()) {
198 default:
199 break;
200 case Decl::ObjCProtocol: {
201 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000202 for (const auto *I : PID->protocols())
203 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000204 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000205 break;
206 }
207 case Decl::ObjCInterface: {
208 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000209 // Look through categories (but not extensions).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000210 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000211 if (!Cat->IsClassExtension())
212 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
213 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000214 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000215
216 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000217 for (const auto *I : OID->all_referenced_protocols())
218 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000219 return P;
220
221 // Finally, check the super class.
222 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
223 return superClass->FindPropertyDeclaration(PropertyId);
224 break;
225 }
226 case Decl::ObjCCategory: {
227 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
228 // Look through protocols.
229 if (!OCD->IsClassExtension())
Aaron Ballman19a41762014-03-14 12:55:57 +0000230 for (const auto *I : OCD->protocols())
231 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
232 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000233 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000234 }
235 }
Craig Topper36250ad2014-05-12 05:36:57 +0000236 return nullptr;
Steve Narofff9c65242008-06-05 13:55:23 +0000237}
238
David Blaikie68e081d2011-12-20 02:48:34 +0000239void ObjCInterfaceDecl::anchor() { }
240
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000241/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
242/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000243/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000244///
245ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000246ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000247 IdentifierInfo *PropertyId) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000248 // FIXME: Should make sure no callers ever do this.
249 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000250 return nullptr;
251
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000252 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000253 LoadExternalDefinition();
254
Ted Kremenekd133a862010-03-15 20:30:07 +0000255 if (ObjCPropertyDecl *PD =
256 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
257 return PD;
258
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000259 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000260 for (const auto *I : all_referenced_protocols())
261 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000262 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000263
Craig Topper36250ad2014-05-12 05:36:57 +0000264 return nullptr;
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000265}
266
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000267void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
268 PropertyDeclOrder &PO) const {
Aaron Ballmand174edf2014-03-13 19:11:50 +0000269 for (auto *Prop : properties()) {
Anna Zaks673d76b2012-10-18 19:17:53 +0000270 PM[Prop->getIdentifier()] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000271 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000272 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000273 for (const auto *PI : all_referenced_protocols())
274 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000275 // Note, the properties declared only in class extensions are still copied
276 // into the main @interface's property list, and therefore we don't
277 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000278}
279
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000280bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
281 const ObjCInterfaceDecl *Class = this;
282 while (Class) {
283 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
284 return true;
285 Class = Class->getSuperClass();
286 }
287 return false;
288}
289
290const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
291 const ObjCInterfaceDecl *Class = this;
292 while (Class) {
293 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
294 return Class;
295 Class = Class->getSuperClass();
296 }
Craig Topper36250ad2014-05-12 05:36:57 +0000297 return nullptr;
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000298}
299
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000300void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
301 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
302 ASTContext &C)
303{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000304 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000305 LoadExternalDefinition();
306
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000307 if (data().AllReferencedProtocols.empty() &&
308 data().ReferencedProtocols.empty()) {
309 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000310 return;
311 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000312
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000313 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000314 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000315 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000316 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000317 for (unsigned i = 0; i < ExtNum; i++) {
318 bool protocolExists = false;
319 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000320 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000321 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
322 protocolExists = true;
323 break;
324 }
325 }
326 // Do we want to warn on a protocol in extension class which
327 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000328 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000329 ProtocolRefs.push_back(ProtoInExtension);
330 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000331
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000332 if (ProtocolRefs.empty())
333 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000334
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000335 // Merge ProtocolRefs into class's protocol list;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000336 for (auto *P : all_referenced_protocols()) {
337 ProtocolRefs.push_back(P);
Douglas Gregor002b6712010-01-16 15:02:53 +0000338 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000339
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000340 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000341}
342
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000343const ObjCInterfaceDecl *
344ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
345 const ObjCInterfaceDecl *IFace = this;
346 while (IFace) {
347 if (IFace->hasDesignatedInitializers())
348 return IFace;
349 if (!IFace->inheritsDesignatedInitializers())
350 break;
351 IFace = IFace->getSuperClass();
352 }
Craig Topper36250ad2014-05-12 05:36:57 +0000353 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000354}
355
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000356static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
357 for (const auto *MD : D->instance_methods()) {
358 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
359 return true;
360 }
361 for (const auto *Ext : D->visible_extensions()) {
362 for (const auto *MD : Ext->instance_methods()) {
363 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
364 return true;
365 }
366 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000367 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000368 for (const auto *MD : ImplD->instance_methods()) {
369 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
370 return true;
371 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000372 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000373 return false;
374}
375
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000376bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
377 switch (data().InheritedDesignatedInitializers) {
378 case DefinitionData::IDI_Inherited:
379 return true;
380 case DefinitionData::IDI_NotInherited:
381 return false;
382 case DefinitionData::IDI_Unknown: {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000383 // If the class introduced initializers we conservatively assume that we
384 // don't know if any of them is a designated initializer to avoid possible
385 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000386 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000387 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000388 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000389 if (auto SuperD = getSuperClass()) {
390 data().InheritedDesignatedInitializers =
391 SuperD->declaresOrInheritsDesignatedInitializers() ?
392 DefinitionData::IDI_Inherited :
393 DefinitionData::IDI_NotInherited;
394 } else {
395 data().InheritedDesignatedInitializers =
396 DefinitionData::IDI_NotInherited;
397 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000398 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000399 assert(data().InheritedDesignatedInitializers
400 != DefinitionData::IDI_Unknown);
401 return data().InheritedDesignatedInitializers ==
402 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000403 }
404 }
405
406 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
407}
408
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000409void ObjCInterfaceDecl::getDesignatedInitializers(
410 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000411 // Check for a complete definition and recover if not so.
412 if (!isThisDeclarationADefinition())
413 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000414 if (data().ExternallyCompleted)
415 LoadExternalDefinition();
416
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000417 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000418 if (!IFace)
419 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000420
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000421 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000422 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000423 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000424 for (const auto *Ext : IFace->visible_extensions()) {
425 for (const auto *MD : Ext->instance_methods())
426 if (MD->isThisDeclarationADesignatedInitializer())
427 Methods.push_back(MD);
428 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000429}
430
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000431bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
432 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000433 // Check for a complete definition and recover if not so.
434 if (!isThisDeclarationADefinition())
435 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000436 if (data().ExternallyCompleted)
437 LoadExternalDefinition();
438
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000439 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000440 if (!IFace)
441 return false;
442
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000443 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000444 if (MD->isThisDeclarationADesignatedInitializer()) {
445 if (InitMethod)
446 *InitMethod = MD;
447 return true;
448 }
449 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000450 for (const auto *Ext : IFace->visible_extensions()) {
451 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
452 if (MD->isThisDeclarationADesignatedInitializer()) {
453 if (InitMethod)
454 *InitMethod = MD;
455 return true;
456 }
457 }
458 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000459 return false;
460}
461
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000462void ObjCInterfaceDecl::allocateDefinitionData() {
463 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000464 Data.setPointer(new (getASTContext()) DefinitionData());
465 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000466
467 // Make the type point at the definition, now that we have one.
468 if (TypeForDecl)
469 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000470}
471
472void ObjCInterfaceDecl::startDefinition() {
473 allocateDefinitionData();
474
Douglas Gregor66b310c2011-12-15 18:03:09 +0000475 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000476 for (auto RD : redecls()) {
477 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000478 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000479 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000480}
481
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000482ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
483 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000484 // FIXME: Should make sure no callers ever do this.
485 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000486 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000487
488 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000489 LoadExternalDefinition();
490
Chris Lattner89375192008-03-16 00:19:01 +0000491 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000492 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000493 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000494 clsDeclared = ClassDecl;
495 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000496 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000497
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000498 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000499 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000500 clsDeclared = ClassDecl;
501 return I;
502 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000503 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000504
Chris Lattner89375192008-03-16 00:19:01 +0000505 ClassDecl = ClassDecl->getSuperClass();
506 }
Craig Topper36250ad2014-05-12 05:36:57 +0000507 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000508}
509
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000510/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
511/// class whose name is passed as argument. If it is not one of the super classes
512/// the it returns NULL.
513ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
514 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000515 // FIXME: Should make sure no callers ever do this.
516 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000517 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000518
519 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000520 LoadExternalDefinition();
521
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000522 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000523 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000524 if (ClassDecl->getIdentifier() == ICName)
525 return ClassDecl;
526 ClassDecl = ClassDecl->getSuperClass();
527 }
Craig Topper36250ad2014-05-12 05:36:57 +0000528 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000529}
530
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000531ObjCProtocolDecl *
532ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000533 for (auto *P : all_referenced_protocols())
534 if (P->lookupProtocolNamed(Name))
535 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000536 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000537 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000538}
539
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000540/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000541/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000542/// When argument category "C" is specified, any implicit method found
543/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000544ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000545 bool isInstance,
546 bool shallowCategoryLookup,
547 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000548 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000549{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000550 // FIXME: Should make sure no callers ever do this.
551 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000552 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000553
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000554 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000555 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000556
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000557 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000558 LoadExternalDefinition();
559
Ted Kremenek00781502013-11-23 01:01:29 +0000560 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000561 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000562 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000563 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000564
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000565 // 2. Didn't find one yet - now look through categories.
566 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000567 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000568 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000569 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000570
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000571 // 3. Didn't find one yet - look through primary class's protocols.
572 for (const auto *I : ClassDecl->protocols())
573 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
574 return MethodDecl;
575
576 // 4. Didn't find one yet - now look through categories' protocols
577 if (!shallowCategoryLookup)
578 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000579 // Didn't find one yet - look through protocols.
580 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000581 Cat->getReferencedProtocols();
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000582 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
583 E = Protocols.end(); I != E; ++I)
584 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000585 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000586 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000587 }
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000588
589
Ted Kremenek00781502013-11-23 01:01:29 +0000590 if (!followSuper)
Craig Topper36250ad2014-05-12 05:36:57 +0000591 return nullptr;
Ted Kremenek00781502013-11-23 01:01:29 +0000592
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000593 // 5. Get to the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000594 ClassDecl = ClassDecl->getSuperClass();
595 }
Craig Topper36250ad2014-05-12 05:36:57 +0000596 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000597}
598
Anna Zaksc77a3b12012-07-27 19:07:44 +0000599// Will search "local" class/category implementations for a method decl.
600// If failed, then we search in class's root for an instance method.
601// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000602ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
603 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000604 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000605 // FIXME: Should make sure no callers ever do this.
606 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000607 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000608
609 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000610 LoadExternalDefinition();
611
Craig Topper36250ad2014-05-12 05:36:57 +0000612 ObjCMethodDecl *Method = nullptr;
Steve Naroffbb69c942009-10-01 23:46:04 +0000613 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000614 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
615 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000616
617 // Look through local category implementations associated with the class.
618 if (!Method)
619 Method = Instance ? getCategoryInstanceMethod(Sel)
620 : getCategoryClassMethod(Sel);
621
622 // Before we give up, check if the selector is an instance method.
623 // But only in the root. This matches gcc's behavior and what the
624 // runtime expects.
625 if (!Instance && !Method && !getSuperClass()) {
626 Method = lookupInstanceMethod(Sel);
627 // Look through local category implementations associated
628 // with the root class.
629 if (!Method)
630 Method = lookupPrivateMethod(Sel, true);
631 }
632
Steve Naroffbb69c942009-10-01 23:46:04 +0000633 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000634 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000635 return Method;
636}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000637
638//===----------------------------------------------------------------------===//
639// ObjCMethodDecl
640//===----------------------------------------------------------------------===//
641
Alp Toker314cc812014-01-25 16:55:45 +0000642ObjCMethodDecl *ObjCMethodDecl::Create(
643 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
644 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
645 DeclContext *contextDecl, bool isInstance, bool isVariadic,
646 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
647 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000648 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000649 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000650 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
651 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000652}
653
Douglas Gregor72172e92012-01-05 21:55:30 +0000654ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000655 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000656 Selector(), QualType(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000657}
658
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000659bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
660 return getMethodFamily() == OMF_init &&
661 hasAttr<ObjCDesignatedInitializerAttr>();
662}
663
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000664bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
665 const ObjCMethodDecl **InitMethod) const {
666 if (getMethodFamily() != OMF_init)
667 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000668 const DeclContext *DC = getDeclContext();
669 if (isa<ObjCProtocolDecl>(DC))
670 return false;
671 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000672 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000673 return false;
674}
675
Douglas Gregora6017bb2012-10-09 17:21:28 +0000676Stmt *ObjCMethodDecl::getBody() const {
677 return Body.get(getASTContext().getExternalSource());
678}
679
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000680void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
681 assert(PrevMethod);
682 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
683 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000684 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000685}
686
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000687void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
688 ArrayRef<ParmVarDecl*> Params,
689 ArrayRef<SourceLocation> SelLocs) {
Craig Topper36250ad2014-05-12 05:36:57 +0000690 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000691 NumParams = Params.size();
692 if (Params.empty() && SelLocs.empty())
693 return;
694
695 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
696 sizeof(SourceLocation) * SelLocs.size();
697 ParamsAndSelLocs = C.Allocate(Size);
698 std::copy(Params.begin(), Params.end(), getParams());
699 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
700}
701
702void ObjCMethodDecl::getSelectorLocs(
703 SmallVectorImpl<SourceLocation> &SelLocs) const {
704 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
705 SelLocs.push_back(getSelectorLoc(i));
706}
707
708void ObjCMethodDecl::setMethodParams(ASTContext &C,
709 ArrayRef<ParmVarDecl*> Params,
710 ArrayRef<SourceLocation> SelLocs) {
711 assert((!SelLocs.empty() || isImplicit()) &&
712 "No selector locs for non-implicit method");
713 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000714 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000715
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000716 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
717 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000718 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000719 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000720
721 setParamsAndSelLocs(C, Params, SelLocs);
722}
723
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000724/// \brief A definition will return its interface declaration.
725/// An interface declaration will return its definition.
726/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000727ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000728 ASTContext &Ctx = getASTContext();
Craig Topper36250ad2014-05-12 05:36:57 +0000729 ObjCMethodDecl *Redecl = nullptr;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000730 if (HasRedeclaration)
731 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000732 if (Redecl)
733 return Redecl;
734
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000735 Decl *CtxD = cast<Decl>(getDeclContext());
736
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000737 if (!CtxD->isInvalidDecl()) {
738 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
739 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
740 if (!ImplD->isInvalidDecl())
741 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000742
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000743 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
744 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
745 if (!ImplD->isInvalidDecl())
746 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000747
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000748 } else if (ObjCImplementationDecl *ImplD =
749 dyn_cast<ObjCImplementationDecl>(CtxD)) {
750 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
751 if (!IFD->isInvalidDecl())
752 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000753
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000754 } else if (ObjCCategoryImplDecl *CImplD =
755 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
756 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
757 if (!CatD->isInvalidDecl())
758 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
759 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000760 }
761
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000762 if (!Redecl && isRedeclaration()) {
763 // This is the last redeclaration, go back to the first method.
764 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
765 isInstanceMethod());
766 }
767
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000768 return Redecl ? Redecl : this;
769}
770
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000771ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
772 Decl *CtxD = cast<Decl>(getDeclContext());
773
774 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
775 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
776 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
777 isInstanceMethod()))
778 return MD;
779
780 } else if (ObjCCategoryImplDecl *CImplD =
781 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000782 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000783 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
784 isInstanceMethod()))
785 return MD;
786 }
787
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000788 if (isRedeclaration())
789 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
790 isInstanceMethod());
791
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000792 return this;
793}
794
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000795SourceLocation ObjCMethodDecl::getLocEnd() const {
796 if (Stmt *Body = getBody())
797 return Body->getLocEnd();
798 return DeclEndLoc;
799}
800
John McCallb4526252011-03-02 01:50:55 +0000801ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
802 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000803 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000804 return family;
805
John McCall86bc21f2011-03-02 11:33:24 +0000806 // Check for an explicit attribute.
807 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
808 // The unfortunate necessity of mapping between enums here is due
809 // to the attributes framework.
810 switch (attr->getFamily()) {
811 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
812 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
813 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
814 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
815 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
816 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
817 }
818 Family = static_cast<unsigned>(family);
819 return family;
820 }
821
John McCallb4526252011-03-02 01:50:55 +0000822 family = getSelector().getMethodFamily();
823 switch (family) {
824 case OMF_None: break;
825
826 // init only has a conventional meaning for an instance method, and
827 // it has to return an object.
828 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000829 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000830 family = OMF_None;
831 break;
832
833 // alloc/copy/new have a conventional meaning for both class and
834 // instance methods, but they require an object return.
835 case OMF_alloc:
836 case OMF_copy:
837 case OMF_mutableCopy:
838 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000839 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000840 family = OMF_None;
841 break;
842
843 // These selectors have a conventional meaning only for instance methods.
844 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000845 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000846 case OMF_retain:
847 case OMF_release:
848 case OMF_autorelease:
849 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000850 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000851 if (!isInstanceMethod())
852 family = OMF_None;
853 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000854
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000855 case OMF_initialize:
856 if (isInstanceMethod() || !getReturnType()->isVoidType())
857 family = OMF_None;
858 break;
859
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000860 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000861 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000862 family = OMF_None;
863 else {
864 unsigned noParams = param_size();
865 if (noParams < 1 || noParams > 3)
866 family = OMF_None;
867 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000868 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000869 QualType ArgT = (*it);
870 if (!ArgT->isObjCSelType()) {
871 family = OMF_None;
872 break;
873 }
874 while (--noParams) {
875 it++;
876 ArgT = (*it);
877 if (!ArgT->isObjCIdType()) {
878 family = OMF_None;
879 break;
880 }
881 }
882 }
883 }
884 break;
885
John McCallb4526252011-03-02 01:50:55 +0000886 }
887
888 // Cache the result.
889 Family = static_cast<unsigned>(family);
890 return family;
891}
892
Mike Stump11289f42009-09-09 15:08:12 +0000893void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000894 const ObjCInterfaceDecl *OID) {
895 QualType selfTy;
896 if (isInstanceMethod()) {
897 // There may be no interface context due to error in declaration
898 // of the interface (which has been reported). Recover gracefully.
899 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +0000900 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000901 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000902 } else {
903 selfTy = Context.getObjCIdType();
904 }
905 } else // we have a factory method.
906 selfTy = Context.getObjCClassType();
907
John McCalld4631322011-06-17 06:42:21 +0000908 bool selfIsPseudoStrong = false;
John McCall31168b02011-06-15 23:02:42 +0000909 bool selfIsConsumed = false;
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000910
David Blaikiebbafb8a2012-03-11 07:00:24 +0000911 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000912 if (isInstanceMethod()) {
913 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +0000914
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000915 // 'self' is always __strong. It's actually pseudo-strong except
916 // in init methods (or methods labeled ns_consumes_self), though.
917 Qualifiers qs;
918 qs.setObjCLifetime(Qualifiers::OCL_Strong);
919 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +0000920
Ted Kremenek1fcdaa92011-11-14 21:59:25 +0000921 // In addition, 'self' is const unless this is an init method.
922 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
923 selfTy = selfTy.withConst();
924 selfIsPseudoStrong = true;
925 }
926 }
927 else {
928 assert(isClassMethod());
929 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +0000930 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +0000931 selfIsPseudoStrong = true;
932 }
John McCall31168b02011-06-15 23:02:42 +0000933 }
934
935 ImplicitParamDecl *self
936 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
937 &Context.Idents.get("self"), selfTy);
938 setSelfDecl(self);
939
940 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +0000941 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000942
John McCalld4631322011-06-17 06:42:21 +0000943 if (selfIsPseudoStrong)
944 self->setARCPseudoStrong(true);
945
Mike Stump11289f42009-09-09 15:08:12 +0000946 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
947 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +0000948 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000949}
950
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000951ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
952 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
953 return ID;
954 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
955 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +0000956 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000957 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +0000958 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +0000959 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +0000960 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +0000961}
962
Aaron Ballman4bfa0de2014-08-01 12:58:11 +0000963SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
964 const auto *TSI = getReturnTypeSourceInfo();
965 if (TSI)
966 return TSI->getTypeLoc().getSourceRange();
967 return SourceRange();
968}
969
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000970static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
971 const ObjCMethodDecl *Method,
972 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
973 bool MovedToSuper) {
974 if (!Container)
975 return;
976
977 // In categories look for overriden methods from protocols. A method from
978 // category is not "overriden" since it is considered as the "same" method
979 // (same USR) as the one from the interface.
980 if (const ObjCCategoryDecl *
981 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
982 // Check whether we have a matching method at this category but only if we
983 // are at the super class level.
984 if (MovedToSuper)
985 if (ObjCMethodDecl *
986 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +0000987 Method->isInstanceMethod(),
988 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000989 if (Method != Overridden) {
990 // We found an override at this category; there is no need to look
991 // into its protocols.
992 Methods.push_back(Overridden);
993 return;
994 }
995
Aaron Ballman19a41762014-03-14 12:55:57 +0000996 for (const auto *P : Category->protocols())
997 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +0000998 return;
999 }
1000
1001 // Check whether we have a matching method at this level.
1002 if (const ObjCMethodDecl *
1003 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001004 Method->isInstanceMethod(),
1005 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001006 if (Method != Overridden) {
1007 // We found an override at this level; there is no need to look
1008 // into other protocols or categories.
1009 Methods.push_back(Overridden);
1010 return;
1011 }
1012
1013 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001014 for (const auto *P : Protocol->protocols())
1015 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001016 }
1017
1018 if (const ObjCInterfaceDecl *
1019 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001020 for (const auto *P : Interface->protocols())
1021 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001022
Aaron Ballman15063e12014-03-13 21:35:02 +00001023 for (const auto *Cat : Interface->known_categories())
1024 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001025
1026 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1027 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1028 /*MovedToSuper=*/true);
1029 }
1030}
1031
1032static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1033 const ObjCMethodDecl *Method,
1034 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1035 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1036 /*MovedToSuper=*/false);
1037}
1038
1039static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1040 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1041 assert(Method->isOverriding());
1042
1043 if (const ObjCProtocolDecl *
1044 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1045 CollectOverriddenMethods(ProtD, Method, overridden);
1046
1047 } else if (const ObjCImplDecl *
1048 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1049 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1050 if (!ID)
1051 return;
1052 // Start searching for overridden methods using the method from the
1053 // interface as starting point.
1054 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001055 Method->isInstanceMethod(),
1056 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001057 Method = IFaceMeth;
1058 CollectOverriddenMethods(ID, Method, overridden);
1059
1060 } else if (const ObjCCategoryDecl *
1061 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1062 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1063 if (!ID)
1064 return;
1065 // Start searching for overridden methods using the method from the
1066 // interface as starting point.
1067 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001068 Method->isInstanceMethod(),
1069 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001070 Method = IFaceMeth;
1071 CollectOverriddenMethods(ID, Method, overridden);
1072
1073 } else {
1074 CollectOverriddenMethods(
1075 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1076 Method, overridden);
1077 }
1078}
1079
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001080void ObjCMethodDecl::getOverriddenMethods(
1081 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1082 const ObjCMethodDecl *Method = this;
1083
1084 if (Method->isRedeclaration()) {
1085 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1086 getMethod(Method->getSelector(), Method->isInstanceMethod());
1087 }
1088
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001089 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001090 collectOverriddenMethodsSlow(Method, Overridden);
1091 assert(!Overridden.empty() &&
1092 "ObjCMethodDecl's overriding bit is not as expected");
1093 }
1094}
1095
Jordan Rose2bd991a2012-10-10 16:42:54 +00001096const ObjCPropertyDecl *
1097ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1098 Selector Sel = getSelector();
1099 unsigned NumArgs = Sel.getNumArgs();
1100 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001101 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001102
Jordan Rose59e34ec2012-10-11 16:02:02 +00001103 if (!isInstanceMethod() || getMethodFamily() != OMF_None)
Craig Topper36250ad2014-05-12 05:36:57 +00001104 return nullptr;
1105
Jordan Rose2bd991a2012-10-10 16:42:54 +00001106 if (isPropertyAccessor()) {
1107 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
Fariborz Jahanian37494a12013-01-12 00:28:34 +00001108 // If container is class extension, find its primary class.
1109 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
1110 if (CatDecl->IsClassExtension())
1111 Container = CatDecl->getClassInterface();
1112
Jordan Rose2bd991a2012-10-10 16:42:54 +00001113 bool IsGetter = (NumArgs == 0);
1114
Aaron Ballmand174edf2014-03-13 19:11:50 +00001115 for (const auto *I : Container->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001116 Selector NextSel = IsGetter ? I->getGetterName()
1117 : I->getSetterName();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001118 if (NextSel == Sel)
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001119 return I;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001120 }
1121
1122 llvm_unreachable("Marked as a property accessor but no property found!");
1123 }
1124
1125 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001126 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001127
1128 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1129 OverridesTy Overrides;
1130 getOverriddenMethods(Overrides);
1131 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1132 I != E; ++I) {
1133 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1134 return Prop;
1135 }
1136
Craig Topper36250ad2014-05-12 05:36:57 +00001137 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001138}
1139
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001140//===----------------------------------------------------------------------===//
1141// ObjCInterfaceDecl
1142//===----------------------------------------------------------------------===//
1143
Douglas Gregord53ae832012-01-17 18:09:05 +00001144ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001145 DeclContext *DC,
1146 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001147 IdentifierInfo *Id,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001148 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001149 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001150 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001151 ObjCInterfaceDecl *Result = new (C, DC)
Richard Smith053f6c62014-05-16 23:01:30 +00001152 ObjCInterfaceDecl(C, DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001153 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001154 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001155 return Result;
1156}
1157
Richard Smith053f6c62014-05-16 23:01:30 +00001158ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001159 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001160 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001161 SourceLocation(),
1162 nullptr,
1163 SourceLocation(),
1164 nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001165 Result->Data.setInt(!C.getLangOpts().Modules);
1166 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001167}
1168
Richard Smith053f6c62014-05-16 23:01:30 +00001169ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1170 SourceLocation AtLoc, IdentifierInfo *Id,
1171 SourceLocation CLoc,
1172 ObjCInterfaceDecl *PrevDecl,
1173 bool IsInternal)
1174 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
1175 redeclarable_base(C), TypeForDecl(nullptr), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001176 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001177
1178 // Copy the 'data' pointer over.
1179 if (PrevDecl)
1180 Data = PrevDecl->Data;
1181
Richard Smith053f6c62014-05-16 23:01:30 +00001182 setImplicit(IsInternal);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001183}
1184
Douglas Gregor73693022010-12-01 23:49:52 +00001185void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001186 assert(data().ExternallyCompleted && "Class is not externally completed");
1187 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001188 getASTContext().getExternalSource()->CompleteType(
1189 const_cast<ObjCInterfaceDecl *>(this));
1190}
1191
1192void ObjCInterfaceDecl::setExternallyCompleted() {
1193 assert(getASTContext().getExternalSource() &&
1194 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001195 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001196 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001197 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001198}
1199
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001200void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001201 // Check for a complete definition and recover if not so.
1202 if (!isThisDeclarationADefinition())
1203 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001204 data().HasDesignatedInitializers = true;
1205}
1206
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001207bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001208 // Check for a complete definition and recover if not so.
1209 if (!isThisDeclarationADefinition())
1210 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001211 if (data().ExternallyCompleted)
1212 LoadExternalDefinition();
1213
1214 return data().HasDesignatedInitializers;
1215}
1216
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001217StringRef
1218ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001219 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1220 return ObjCRTName->getMetadataName();
1221
1222 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001223}
1224
1225StringRef
1226ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001227 if (ObjCInterfaceDecl *ID =
1228 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1229 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001230
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001231 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001232}
1233
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001234ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001235 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1236 if (data().ExternallyCompleted)
1237 LoadExternalDefinition();
1238
1239 return getASTContext().getObjCImplementation(
1240 const_cast<ObjCInterfaceDecl*>(Def));
1241 }
1242
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001243 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001244 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001245}
1246
1247void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001248 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001249}
1250
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001251namespace {
1252 struct SynthesizeIvarChunk {
1253 uint64_t Size;
1254 ObjCIvarDecl *Ivar;
1255 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1256 : Size(size), Ivar(ivar) {}
1257 };
1258
1259 bool operator<(const SynthesizeIvarChunk & LHS,
1260 const SynthesizeIvarChunk &RHS) {
1261 return LHS.Size < RHS.Size;
1262 }
1263}
1264
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001265/// all_declared_ivar_begin - return first ivar declared in this class,
1266/// its extensions and its implementation. Lazily build the list on first
1267/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001268///
1269/// Caveat: The list returned by this method reflects the current
1270/// state of the parser. The cache will be updated for every ivar
1271/// added by an extension or the implementation when they are
1272/// encountered.
1273/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001274ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001275 // FIXME: Should make sure no callers ever do this.
1276 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001277 return nullptr;
1278
1279 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001280 if (!data().IvarList) {
1281 if (!ivar_empty()) {
1282 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1283 data().IvarList = *I; ++I;
1284 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001285 curIvar->setNextIvar(*I);
1286 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001287
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001288 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001289 if (!Ext->ivar_empty()) {
1290 ObjCCategoryDecl::ivar_iterator
1291 I = Ext->ivar_begin(),
1292 E = Ext->ivar_end();
1293 if (!data().IvarList) {
1294 data().IvarList = *I; ++I;
1295 curIvar = data().IvarList;
1296 }
1297 for ( ;I != E; curIvar = *I, ++I)
1298 curIvar->setNextIvar(*I);
1299 }
1300 }
1301 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001302 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001303
1304 // cached and complete!
1305 if (!data().IvarListMissingImplementation)
1306 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001307
1308 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001309 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001310 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001311 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001312 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001313 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1314 layout.push_back(SynthesizeIvarChunk(
1315 IV->getASTContext().getTypeSize(IV->getType()), IV));
1316 continue;
1317 }
1318 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001319 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001320 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001321 curIvar->setNextIvar(IV);
1322 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001323 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001324
1325 if (!layout.empty()) {
1326 // Order synthesized ivars by their size.
1327 std::stable_sort(layout.begin(), layout.end());
1328 unsigned Ix = 0, EIx = layout.size();
1329 if (!data().IvarList) {
1330 data().IvarList = layout[0].Ivar; Ix++;
1331 curIvar = data().IvarList;
1332 }
1333 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1334 curIvar->setNextIvar(layout[Ix].Ivar);
1335 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001336 }
1337 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001338 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001339}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001340
1341/// FindCategoryDeclaration - Finds category declaration in the list of
1342/// categories for this class and returns it. Name of the category is passed
1343/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001344///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001345ObjCCategoryDecl *
1346ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001347 // FIXME: Should make sure no callers ever do this.
1348 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001349 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001350
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001351 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001352 LoadExternalDefinition();
1353
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001354 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001355 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001356 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001357
1358 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001359}
1360
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001361ObjCMethodDecl *
1362ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001363 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001364 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001365 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1366 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001367 }
1368
Craig Topper36250ad2014-05-12 05:36:57 +00001369 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001370}
1371
1372ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001373 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001374 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001375 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1376 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001377 }
Craig Topper36250ad2014-05-12 05:36:57 +00001378
1379 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001380}
1381
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001382/// ClassImplementsProtocol - Checks that 'lProto' protocol
1383/// has been implemented in IDecl class, its super class or categories (if
1384/// lookupCategory is true).
1385bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1386 bool lookupCategory,
1387 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001388 if (!hasDefinition())
1389 return false;
1390
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001391 ObjCInterfaceDecl *IDecl = this;
1392 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001393 for (auto *PI : IDecl->protocols()){
1394 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001395 return true;
1396 // This is dubious and is added to be compatible with gcc. In gcc, it is
1397 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1398 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1399 // object. This IMO, should be a bug.
1400 // FIXME: Treat this as an extension, and flag this as an error when GCC
1401 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001402 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001403 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001404 return true;
1405 }
Mike Stump11289f42009-09-09 15:08:12 +00001406
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001407 // 2nd, look up the category.
1408 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001409 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001410 for (auto *PI : Cat->protocols())
1411 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001412 return true;
1413 }
Mike Stump11289f42009-09-09 15:08:12 +00001414
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001415 // 3rd, look up the super class(s)
1416 if (IDecl->getSuperClass())
1417 return
1418 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1419 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001420
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001421 return false;
1422}
1423
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001424//===----------------------------------------------------------------------===//
1425// ObjCIvarDecl
1426//===----------------------------------------------------------------------===//
1427
David Blaikie68e081d2011-12-20 02:48:34 +00001428void ObjCIvarDecl::anchor() { }
1429
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001430ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001431 SourceLocation StartLoc,
1432 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001433 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001434 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001435 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001436 if (DC) {
1437 // Ivar's can only appear in interfaces, implementations (via synthesized
1438 // properties), and class extensions (via direct declaration, or synthesized
1439 // properties).
1440 //
1441 // FIXME: This should really be asserting this:
1442 // (isa<ObjCCategoryDecl>(DC) &&
1443 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1444 // but unfortunately we sometimes place ivars into non-class extension
1445 // categories on error. This breaks an AST invariant, and should not be
1446 // fixed.
1447 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1448 isa<ObjCCategoryDecl>(DC)) &&
1449 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001450 // Once a new ivar is created in any of class/class-extension/implementation
1451 // decl contexts, the previously built IvarList must be rebuilt.
1452 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1453 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001454 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001455 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001456 else
1457 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001458 }
Craig Topper36250ad2014-05-12 05:36:57 +00001459 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001460 }
1461
Richard Smithf7981722013-11-22 09:01:48 +00001462 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001463 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001464}
1465
Douglas Gregor72172e92012-01-05 21:55:30 +00001466ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001467 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1468 nullptr, QualType(), nullptr,
1469 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001470}
1471
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001472const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1473 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001474
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001475 switch (DC->getKind()) {
1476 default:
1477 case ObjCCategoryImpl:
1478 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001479 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001480
1481 // Ivars can only appear in class extension categories.
1482 case ObjCCategory: {
1483 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1484 assert(CD->IsClassExtension() && "invalid container for ivar!");
1485 return CD->getClassInterface();
1486 }
1487
1488 case ObjCImplementation:
1489 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1490
1491 case ObjCInterface:
1492 return cast<ObjCInterfaceDecl>(DC);
1493 }
1494}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001495
1496//===----------------------------------------------------------------------===//
1497// ObjCAtDefsFieldDecl
1498//===----------------------------------------------------------------------===//
1499
David Blaikie68e081d2011-12-20 02:48:34 +00001500void ObjCAtDefsFieldDecl::anchor() { }
1501
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001502ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001503*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1504 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001505 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001506 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001507}
1508
Richard Smithf7981722013-11-22 09:01:48 +00001509ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001510 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001511 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1512 SourceLocation(), nullptr, QualType(),
1513 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001514}
1515
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001516//===----------------------------------------------------------------------===//
1517// ObjCProtocolDecl
1518//===----------------------------------------------------------------------===//
1519
David Blaikie68e081d2011-12-20 02:48:34 +00001520void ObjCProtocolDecl::anchor() { }
1521
Richard Smith053f6c62014-05-16 23:01:30 +00001522ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1523 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001524 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001525 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001526 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1527 redeclarable_base(C), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001528 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001529 if (PrevDecl)
1530 Data = PrevDecl->Data;
1531}
1532
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001533ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001534 IdentifierInfo *Id,
1535 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001536 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001537 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001538 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001539 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001540 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001541 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001542}
1543
Richard Smithf7981722013-11-22 09:01:48 +00001544ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001545 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001546 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001547 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001548 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001549 Result->Data.setInt(!C.getLangOpts().Modules);
1550 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001551}
1552
Steve Naroff114aecb2009-03-01 16:12:44 +00001553ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1554 ObjCProtocolDecl *PDecl = this;
1555
1556 if (Name == getIdentifier())
1557 return PDecl;
1558
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001559 for (auto *I : protocols())
1560 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001561 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001562
Craig Topper36250ad2014-05-12 05:36:57 +00001563 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001564}
1565
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001566// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001567// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001568ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1569 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001570 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001571
Douglas Gregoreed49792013-01-17 00:38:46 +00001572 // If there is no definition or the definition is hidden, we don't find
1573 // anything.
1574 const ObjCProtocolDecl *Def = getDefinition();
1575 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001576 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001577
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001578 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001579 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001580
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001581 for (const auto *I : protocols())
1582 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001583 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001584 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001585}
1586
Douglas Gregore6e48b12012-01-01 19:29:29 +00001587void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001588 assert(!Data.getPointer() && "Protocol already has a definition!");
1589 Data.setPointer(new (getASTContext()) DefinitionData);
1590 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001591}
1592
1593void ObjCProtocolDecl::startDefinition() {
1594 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001595
1596 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001597 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001598 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001599}
1600
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001601void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1602 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001603
1604 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001605 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001606 // Insert into PM if not there already.
1607 PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001608 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001609 }
1610 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001611 for (const auto *PI : PDecl->protocols())
1612 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001613 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001614}
1615
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001616
1617void ObjCProtocolDecl::collectInheritedProtocolProperties(
1618 const ObjCPropertyDecl *Property,
1619 ProtocolPropertyMap &PM) const {
1620 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1621 bool MatchFound = false;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001622 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001623 if (Prop == Property)
1624 continue;
1625 if (Prop->getIdentifier() == Property->getIdentifier()) {
1626 PM[PDecl] = Prop;
1627 MatchFound = true;
1628 break;
1629 }
1630 }
1631 // Scan through protocol's protocols which did not have a matching property.
1632 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001633 for (const auto *PI : PDecl->protocols())
1634 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001635 }
1636}
Anna Zaks673d76b2012-10-18 19:17:53 +00001637
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001638StringRef
1639ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001640 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1641 return ObjCRTName->getMetadataName();
1642
1643 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001644}
1645
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001646//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001647// ObjCCategoryDecl
1648//===----------------------------------------------------------------------===//
1649
David Blaikie68e081d2011-12-20 02:48:34 +00001650void ObjCCategoryDecl::anchor() { }
1651
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001652ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001653 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001654 SourceLocation ClassNameLoc,
1655 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001656 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001657 ObjCInterfaceDecl *IDecl,
1658 SourceLocation IvarLBraceLoc,
1659 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001660 ObjCCategoryDecl *CatDecl =
1661 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
1662 IDecl, IvarLBraceLoc, IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001663 if (IDecl) {
1664 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001665 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001666 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001667 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001668 if (ASTMutationListener *L = C.getASTMutationListener())
1669 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1670 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001671 }
1672
1673 return CatDecl;
1674}
1675
Richard Smithf7981722013-11-22 09:01:48 +00001676ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001677 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001678 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1679 SourceLocation(), SourceLocation(),
1680 nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001681}
1682
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001683ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1684 return getASTContext().getObjCImplementation(
1685 const_cast<ObjCCategoryDecl*>(this));
1686}
1687
1688void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1689 getASTContext().setObjCImplementation(this, ImplD);
1690}
1691
1692
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001693//===----------------------------------------------------------------------===//
1694// ObjCCategoryImplDecl
1695//===----------------------------------------------------------------------===//
1696
David Blaikie68e081d2011-12-20 02:48:34 +00001697void ObjCCategoryImplDecl::anchor() { }
1698
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001699ObjCCategoryImplDecl *
1700ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001701 IdentifierInfo *Id,
1702 ObjCInterfaceDecl *ClassInterface,
1703 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001704 SourceLocation atStartLoc,
1705 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001706 if (ClassInterface && ClassInterface->hasDefinition())
1707 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001708 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1709 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001710}
1711
Douglas Gregor72172e92012-01-05 21:55:30 +00001712ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1713 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001714 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
1715 SourceLocation(), SourceLocation(),
1716 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001717}
1718
Steve Narofff406f4d2009-10-29 21:11:04 +00001719ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00001720 // The class interface might be NULL if we are working with invalid code.
1721 if (const ObjCInterfaceDecl *ID = getClassInterface())
1722 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00001723 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00001724}
1725
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001726
David Blaikie68e081d2011-12-20 02:48:34 +00001727void ObjCImplDecl::anchor() { }
1728
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001729void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00001730 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001731 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001732 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00001733}
1734
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001735void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1736 ASTContext &Ctx = getASTContext();
1737
1738 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00001739 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001740 if (IFace)
1741 Ctx.setObjCImplementation(IFace, ImplD);
1742
Duncan Sands49c29ee2009-07-21 07:56:29 +00001743 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001744 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1745 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1746 Ctx.setObjCImplementation(CD, ImplD);
1747 }
1748
1749 ClassInterface = IFace;
1750}
1751
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001752/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00001753/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00001754/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001755///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001756ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001757FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001758 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001759 if (PID->getPropertyIvarDecl() &&
1760 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1761 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001762 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001763}
1764
1765/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00001766/// added to the list of those properties \@synthesized/\@dynamic in this
1767/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001768///
Chris Lattnera9ca0522009-02-28 18:42:10 +00001769ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001770FindPropertyImplDecl(IdentifierInfo *Id) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00001771 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001772 if (PID->getPropertyDecl()->getIdentifier() == Id)
1773 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00001774 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001775}
1776
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001777raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001778 const ObjCCategoryImplDecl &CID) {
1779 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001780 return OS;
1781}
1782
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001783//===----------------------------------------------------------------------===//
1784// ObjCImplementationDecl
1785//===----------------------------------------------------------------------===//
1786
David Blaikie68e081d2011-12-20 02:48:34 +00001787void ObjCImplementationDecl::anchor() { }
1788
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001789ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00001790ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001791 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001792 ObjCInterfaceDecl *SuperDecl,
1793 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001794 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001795 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001796 SourceLocation IvarLBraceLoc,
1797 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001798 if (ClassInterface && ClassInterface->hasDefinition())
1799 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001800 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1801 nameLoc, atStartLoc, superLoc,
1802 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001803}
1804
Douglas Gregor72172e92012-01-05 21:55:30 +00001805ObjCImplementationDecl *
1806ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001807 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
1808 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001809}
1810
John McCall0410e572011-07-22 04:15:06 +00001811void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1812 CXXCtorInitializer ** initializers,
1813 unsigned numInitializers) {
1814 if (numInitializers > 0) {
1815 NumIvarInitializers = numInitializers;
1816 CXXCtorInitializer **ivarInitializers =
1817 new (C) CXXCtorInitializer*[NumIvarInitializers];
1818 memcpy(ivarInitializers, initializers,
1819 numInitializers * sizeof(CXXCtorInitializer*));
1820 IvarInitializers = ivarInitializers;
1821 }
1822}
1823
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001824raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00001825 const ObjCImplementationDecl &ID) {
1826 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001827 return OS;
1828}
1829
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001830//===----------------------------------------------------------------------===//
1831// ObjCCompatibleAliasDecl
1832//===----------------------------------------------------------------------===//
1833
David Blaikie68e081d2011-12-20 02:48:34 +00001834void ObjCCompatibleAliasDecl::anchor() { }
1835
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001836ObjCCompatibleAliasDecl *
1837ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1838 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00001839 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001840 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00001841 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001842}
1843
Douglas Gregor72172e92012-01-05 21:55:30 +00001844ObjCCompatibleAliasDecl *
1845ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001846 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
1847 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001848}
1849
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001850//===----------------------------------------------------------------------===//
1851// ObjCPropertyDecl
1852//===----------------------------------------------------------------------===//
1853
David Blaikie68e081d2011-12-20 02:48:34 +00001854void ObjCPropertyDecl::anchor() { }
1855
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001856ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1857 SourceLocation L,
1858 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001859 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001860 SourceLocation LParenLoc,
John McCall339bb662010-06-04 20:50:08 +00001861 TypeSourceInfo *T,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001862 PropertyControl propControl) {
Richard Smithf7981722013-11-22 09:01:48 +00001863 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001864}
1865
Richard Smithf7981722013-11-22 09:01:48 +00001866ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001867 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001868 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
1869 SourceLocation(), SourceLocation(),
1870 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001871}
1872
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001873//===----------------------------------------------------------------------===//
1874// ObjCPropertyImplDecl
1875//===----------------------------------------------------------------------===//
1876
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001877ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001878 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001879 SourceLocation atLoc,
1880 SourceLocation L,
1881 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00001882 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001883 ObjCIvarDecl *ivar,
1884 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001885 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1886 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00001887}
Chris Lattnered0e1642008-03-17 01:19:02 +00001888
Richard Smithf7981722013-11-22 09:01:48 +00001889ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001890 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001891 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
1892 SourceLocation(), nullptr, Dynamic,
1893 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001894}
1895
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001896SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1897 SourceLocation EndLoc = getLocation();
1898 if (IvarLoc.isValid())
1899 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00001900
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001901 return SourceRange(AtLoc, EndLoc);
1902}