blob: fdbac00fc9426aeaf042a77de61174f94cfcff12 [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 {
Richard Smithcf4bdde2015-02-21 02:45:19 +000057 lookup_result R = lookup(Id);
58 for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000059 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 //
Richard Smithcf4bdde2015-02-21 02:45:19 +000086 lookup_result R = lookup(Sel);
87 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000088 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
Nico Weber4701ffd2014-12-22 05:21:03 +000096/// \brief This routine returns 'true' if a user declared setter method was
97/// 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'
99/// property. This is because, user must provide a setter method for the
100/// category's 'readwrite' property.
101bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
102 const ObjCPropertyDecl *Property) const {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000103 Selector Sel = Property->getSetterName();
Richard Smithcf4bdde2015-02-21 02:45:19 +0000104 lookup_result R = lookup(Sel);
105 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000106 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;
Nico Weber4701ffd2014-12-22 05:21:03 +0000121 // Also search through the categories looking for a 'readwrite'
122 // declaration of this property. If one found, presumably a setter will
123 // be provided (properties declared in categories will not get
124 // auto-synthesized).
Manman Renefe1bac2016-01-27 20:00:32 +0000125 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000126 if (P->getIdentifier() == Property->getIdentifier()) {
127 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
128 return true;
129 break;
130 }
131 }
132
133 // Also look into protocols, for a user declared instance method.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000134 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000135 if (Proto->HasUserDeclaredSetterMethod(Property))
136 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000137
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000138 // And in its super class.
139 ObjCInterfaceDecl *OSC = ID->getSuperClass();
140 while (OSC) {
141 if (OSC->HasUserDeclaredSetterMethod(Property))
142 return true;
143 OSC = OSC->getSuperClass();
144 }
145 }
146 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000147 for (const auto *PI : PD->protocols())
148 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000149 return true;
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000150 return false;
151}
152
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000153ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000154ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Manman Ren5b786402016-01-28 18:49:28 +0000155 const IdentifierInfo *propertyID,
156 ObjCPropertyQueryKind queryKind) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000157 // If this context is a hidden protocol definition, don't find any
158 // property.
159 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
160 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
161 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000162 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000163 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000164
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000165 // If context is class, then lookup property in its extensions.
166 // This comes before property is looked up in primary class.
167 if (auto *IDecl = dyn_cast<ObjCInterfaceDecl>(DC)) {
168 for (const auto *Ext : IDecl->known_extensions())
169 if (ObjCPropertyDecl *PD = ObjCPropertyDecl::findPropertyDecl(Ext,
Manman Ren5b786402016-01-28 18:49:28 +0000170 propertyID,
171 queryKind))
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000172 return PD;
173 }
174
Richard Smithcf4bdde2015-02-21 02:45:19 +0000175 DeclContext::lookup_result R = DC->lookup(propertyID);
Manman Ren5b786402016-01-28 18:49:28 +0000176 ObjCPropertyDecl *classProp = nullptr;
Richard Smithcf4bdde2015-02-21 02:45:19 +0000177 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
David Blaikieff7d47a2012-12-19 00:45:41 +0000178 ++I)
Manman Ren5b786402016-01-28 18:49:28 +0000179 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I)) {
180 // If queryKind is unknown, we return the instance property if one
181 // exists; otherwise we return the class property.
182 if ((queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
183 !PD->isClassProperty()) ||
184 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
185 PD->isClassProperty()) ||
186 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
187 !PD->isClassProperty()))
188 return PD;
189
190 if (PD->isClassProperty())
191 classProp = PD;
192 }
193
194 if (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
195 // We can't find the instance property, return the class property.
196 return classProp;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000197
Craig Topper36250ad2014-05-12 05:36:57 +0000198 return nullptr;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000199}
200
Anna Zaks454477c2012-09-27 19:45:11 +0000201IdentifierInfo *
202ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
203 SmallString<128> ivarName;
204 {
205 llvm::raw_svector_ostream os(ivarName);
206 os << '_' << getIdentifier()->getName();
207 }
208 return &Ctx.Idents.get(ivarName.str());
209}
210
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000211/// FindPropertyDeclaration - Finds declaration of the property given its name
212/// in 'PropertyId' and returns it. It returns 0, if not found.
Jordan Rose210bfe92014-11-19 22:03:46 +0000213ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
Manman Ren5b786402016-01-28 18:49:28 +0000214 const IdentifierInfo *PropertyId,
215 ObjCPropertyQueryKind QueryKind) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000216 // Don't find properties within hidden protocol definitions.
217 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
218 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
219 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000220 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000221 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000222
223 // Search the extensions of a class first; they override what's in
224 // the class itself.
225 if (const auto *ClassDecl = dyn_cast<ObjCInterfaceDecl>(this)) {
226 for (const auto *Ext : ClassDecl->visible_extensions()) {
Manman Ren5b786402016-01-28 18:49:28 +0000227 if (auto *P = Ext->FindPropertyDeclaration(PropertyId, QueryKind))
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000228 return P;
229 }
230 }
Mike Stump11289f42009-09-09 15:08:12 +0000231
Ted Kremenekddcd1092010-03-15 20:11:53 +0000232 if (ObjCPropertyDecl *PD =
Manman Ren5b786402016-01-28 18:49:28 +0000233 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
234 QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000235 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000236
Ted Kremenekddcd1092010-03-15 20:11:53 +0000237 switch (getKind()) {
238 default:
239 break;
240 case Decl::ObjCProtocol: {
241 const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000242 for (const auto *I : PID->protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000243 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
244 QueryKind))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000245 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000246 break;
247 }
248 case Decl::ObjCInterface: {
249 const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000250 // Look through categories (but not extensions; they were handled above).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000251 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000252 if (!Cat->IsClassExtension())
Manman Ren5b786402016-01-28 18:49:28 +0000253 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(
254 PropertyId, QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000255 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000256 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000257
258 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000259 for (const auto *I : OID->all_referenced_protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000260 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
261 QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000262 return P;
263
264 // Finally, check the super class.
265 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
Manman Ren5b786402016-01-28 18:49:28 +0000266 return superClass->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekddcd1092010-03-15 20:11:53 +0000267 break;
268 }
269 case Decl::ObjCCategory: {
270 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
271 // Look through protocols.
272 if (!OCD->IsClassExtension())
Aaron Ballman19a41762014-03-14 12:55:57 +0000273 for (const auto *I : OCD->protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000274 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
275 QueryKind))
Aaron Ballman19a41762014-03-14 12:55:57 +0000276 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000277 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000278 }
279 }
Craig Topper36250ad2014-05-12 05:36:57 +0000280 return nullptr;
Steve Narofff9c65242008-06-05 13:55:23 +0000281}
282
David Blaikie68e081d2011-12-20 02:48:34 +0000283void ObjCInterfaceDecl::anchor() { }
284
Douglas Gregor85f3f952015-07-07 03:57:15 +0000285ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
286 // If this particular declaration has a type parameter list, return it.
287 if (ObjCTypeParamList *written = getTypeParamListAsWritten())
288 return written;
289
290 // If there is a definition, return its type parameter list.
291 if (const ObjCInterfaceDecl *def = getDefinition())
292 return def->getTypeParamListAsWritten();
293
294 // Otherwise, look at previous declarations to determine whether any
295 // of them has a type parameter list, skipping over those
296 // declarations that do not.
Douglas Gregorc5e07f52015-07-07 03:58:01 +0000297 for (auto decl = getMostRecentDecl(); decl; decl = decl->getPreviousDecl()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000298 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
299 return written;
300 }
301
302 return nullptr;
303}
304
Douglas Gregorab7f0b32015-07-07 06:20:12 +0000305void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
306 TypeParamList = TPL;
307 if (!TPL)
308 return;
309 // Set the declaration context of each of the type parameters.
310 for (auto typeParam : *TypeParamList)
311 typeParam->setDeclContext(this);
312}
313
Douglas Gregore9d95f12015-07-07 03:57:35 +0000314ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
315 // FIXME: Should make sure no callers ever do this.
316 if (!hasDefinition())
317 return nullptr;
318
319 if (data().ExternallyCompleted)
320 LoadExternalDefinition();
321
322 if (const ObjCObjectType *superType = getSuperClassType()) {
323 if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
324 if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
325 return superDef;
326
327 return superDecl;
328 }
329 }
330
331 return nullptr;
332}
333
334SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
335 if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
336 return superTInfo->getTypeLoc().getLocStart();
337
338 return SourceLocation();
339}
340
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000341/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
342/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000343/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000344///
345ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000346ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Manman Ren5b786402016-01-28 18:49:28 +0000347 IdentifierInfo *PropertyId,
348 ObjCPropertyQueryKind QueryKind) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000349 // FIXME: Should make sure no callers ever do this.
350 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000351 return nullptr;
352
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000353 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000354 LoadExternalDefinition();
355
Ted Kremenekd133a862010-03-15 20:30:07 +0000356 if (ObjCPropertyDecl *PD =
Manman Ren5b786402016-01-28 18:49:28 +0000357 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
358 QueryKind))
Ted Kremenekd133a862010-03-15 20:30:07 +0000359 return PD;
360
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000361 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000362 for (const auto *I : all_referenced_protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000363 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
364 QueryKind))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000365 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000366
Craig Topper36250ad2014-05-12 05:36:57 +0000367 return nullptr;
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000368}
369
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000370void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
371 PropertyDeclOrder &PO) const {
Manman Ren494ee5b2016-01-28 23:36:05 +0000372 for (auto *Prop : properties()) {
373 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000374 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000375 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000376 for (const auto *Ext : known_extensions()) {
377 const ObjCCategoryDecl *ClassExt = Ext;
Manman Ren494ee5b2016-01-28 23:36:05 +0000378 for (auto *Prop : ClassExt->properties()) {
379 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000380 PO.push_back(Prop);
381 }
382 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000383 for (const auto *PI : all_referenced_protocols())
384 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000385 // Note, the properties declared only in class extensions are still copied
386 // into the main @interface's property list, and therefore we don't
387 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000388}
389
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000390bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
391 const ObjCInterfaceDecl *Class = this;
392 while (Class) {
393 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
394 return true;
395 Class = Class->getSuperClass();
396 }
397 return false;
398}
399
400const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
401 const ObjCInterfaceDecl *Class = this;
402 while (Class) {
403 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
404 return Class;
405 Class = Class->getSuperClass();
406 }
Craig Topper36250ad2014-05-12 05:36:57 +0000407 return nullptr;
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000408}
409
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000410void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
411 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
412 ASTContext &C)
413{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000414 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000415 LoadExternalDefinition();
416
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000417 if (data().AllReferencedProtocols.empty() &&
418 data().ReferencedProtocols.empty()) {
419 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000420 return;
421 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000422
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000423 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000424 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000425 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000426 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000427 for (unsigned i = 0; i < ExtNum; i++) {
428 bool protocolExists = false;
429 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000430 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000431 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
432 protocolExists = true;
433 break;
434 }
435 }
436 // Do we want to warn on a protocol in extension class which
437 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000438 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000439 ProtocolRefs.push_back(ProtoInExtension);
440 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000441
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000442 if (ProtocolRefs.empty())
443 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000444
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000445 // Merge ProtocolRefs into class's protocol list;
Benjamin Kramerf9890422015-02-17 16:48:30 +0000446 ProtocolRefs.append(all_referenced_protocol_begin(),
447 all_referenced_protocol_end());
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000448
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000449 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000450}
451
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000452const ObjCInterfaceDecl *
453ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
454 const ObjCInterfaceDecl *IFace = this;
455 while (IFace) {
456 if (IFace->hasDesignatedInitializers())
457 return IFace;
458 if (!IFace->inheritsDesignatedInitializers())
459 break;
460 IFace = IFace->getSuperClass();
461 }
Craig Topper36250ad2014-05-12 05:36:57 +0000462 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000463}
464
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000465static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
466 for (const auto *MD : D->instance_methods()) {
467 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
468 return true;
469 }
470 for (const auto *Ext : D->visible_extensions()) {
471 for (const auto *MD : Ext->instance_methods()) {
472 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
473 return true;
474 }
475 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000476 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000477 for (const auto *MD : ImplD->instance_methods()) {
478 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
479 return true;
480 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000481 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000482 return false;
483}
484
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000485bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
486 switch (data().InheritedDesignatedInitializers) {
487 case DefinitionData::IDI_Inherited:
488 return true;
489 case DefinitionData::IDI_NotInherited:
490 return false;
491 case DefinitionData::IDI_Unknown: {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000492 // If the class introduced initializers we conservatively assume that we
493 // don't know if any of them is a designated initializer to avoid possible
494 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000495 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000496 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000497 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000498 if (auto SuperD = getSuperClass()) {
499 data().InheritedDesignatedInitializers =
500 SuperD->declaresOrInheritsDesignatedInitializers() ?
501 DefinitionData::IDI_Inherited :
502 DefinitionData::IDI_NotInherited;
503 } else {
504 data().InheritedDesignatedInitializers =
505 DefinitionData::IDI_NotInherited;
506 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000507 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000508 assert(data().InheritedDesignatedInitializers
509 != DefinitionData::IDI_Unknown);
510 return data().InheritedDesignatedInitializers ==
511 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000512 }
513 }
514
515 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
516}
517
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000518void ObjCInterfaceDecl::getDesignatedInitializers(
519 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000520 // Check for a complete definition and recover if not so.
521 if (!isThisDeclarationADefinition())
522 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000523 if (data().ExternallyCompleted)
524 LoadExternalDefinition();
525
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000526 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000527 if (!IFace)
528 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000529
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000530 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000531 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000532 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000533 for (const auto *Ext : IFace->visible_extensions()) {
534 for (const auto *MD : Ext->instance_methods())
535 if (MD->isThisDeclarationADesignatedInitializer())
536 Methods.push_back(MD);
537 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000538}
539
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000540bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
541 const ObjCMethodDecl **InitMethod) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000542 // Check for a complete definition and recover if not so.
543 if (!isThisDeclarationADefinition())
544 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000545 if (data().ExternallyCompleted)
546 LoadExternalDefinition();
547
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000548 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000549 if (!IFace)
550 return false;
551
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000552 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000553 if (MD->isThisDeclarationADesignatedInitializer()) {
554 if (InitMethod)
555 *InitMethod = MD;
556 return true;
557 }
558 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000559 for (const auto *Ext : IFace->visible_extensions()) {
560 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
561 if (MD->isThisDeclarationADesignatedInitializer()) {
562 if (InitMethod)
563 *InitMethod = MD;
564 return true;
565 }
566 }
567 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000568 return false;
569}
570
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000571void ObjCInterfaceDecl::allocateDefinitionData() {
572 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000573 Data.setPointer(new (getASTContext()) DefinitionData());
574 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000575
576 // Make the type point at the definition, now that we have one.
577 if (TypeForDecl)
578 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000579}
580
581void ObjCInterfaceDecl::startDefinition() {
582 allocateDefinitionData();
583
Douglas Gregor66b310c2011-12-15 18:03:09 +0000584 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000585 for (auto RD : redecls()) {
586 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000587 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000588 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000589}
590
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000591ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
592 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000593 // FIXME: Should make sure no callers ever do this.
594 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000595 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000596
597 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000598 LoadExternalDefinition();
599
Chris Lattner89375192008-03-16 00:19:01 +0000600 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000601 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000602 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000603 clsDeclared = ClassDecl;
604 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000605 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000606
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000607 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000608 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000609 clsDeclared = ClassDecl;
610 return I;
611 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000612 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000613
Chris Lattner89375192008-03-16 00:19:01 +0000614 ClassDecl = ClassDecl->getSuperClass();
615 }
Craig Topper36250ad2014-05-12 05:36:57 +0000616 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000617}
618
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000619/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
620/// class whose name is passed as argument. If it is not one of the super classes
621/// the it returns NULL.
622ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
623 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000624 // FIXME: Should make sure no callers ever do this.
625 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000626 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000627
628 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000629 LoadExternalDefinition();
630
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000631 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000632 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000633 if (ClassDecl->getIdentifier() == ICName)
634 return ClassDecl;
635 ClassDecl = ClassDecl->getSuperClass();
636 }
Craig Topper36250ad2014-05-12 05:36:57 +0000637 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000638}
639
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000640ObjCProtocolDecl *
641ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000642 for (auto *P : all_referenced_protocols())
643 if (P->lookupProtocolNamed(Name))
644 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000645 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000646 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000647}
648
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000649/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000650/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000651/// When argument category "C" is specified, any implicit method found
652/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000653ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000654 bool isInstance,
655 bool shallowCategoryLookup,
656 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000657 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000658{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000659 // FIXME: Should make sure no callers ever do this.
660 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000661 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000662
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000663 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000664 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000665
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000666 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000667 LoadExternalDefinition();
668
Ted Kremenek00781502013-11-23 01:01:29 +0000669 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000670 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000671 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000672 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000673
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000674 // 2. Didn't find one yet - now look through categories.
675 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000676 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000677 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000678 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000679
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000680 // 3. Didn't find one yet - look through primary class's protocols.
681 for (const auto *I : ClassDecl->protocols())
682 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
683 return MethodDecl;
684
685 // 4. Didn't find one yet - now look through categories' protocols
686 if (!shallowCategoryLookup)
687 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000688 // Didn't find one yet - look through protocols.
689 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000690 Cat->getReferencedProtocols();
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000691 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
692 E = Protocols.end(); I != E; ++I)
693 if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000694 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000695 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000696 }
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000697
698
Ted Kremenek00781502013-11-23 01:01:29 +0000699 if (!followSuper)
Craig Topper36250ad2014-05-12 05:36:57 +0000700 return nullptr;
Ted Kremenek00781502013-11-23 01:01:29 +0000701
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000702 // 5. Get to the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000703 ClassDecl = ClassDecl->getSuperClass();
704 }
Craig Topper36250ad2014-05-12 05:36:57 +0000705 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000706}
707
Anna Zaksc77a3b12012-07-27 19:07:44 +0000708// Will search "local" class/category implementations for a method decl.
709// If failed, then we search in class's root for an instance method.
710// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000711ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
712 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000713 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000714 // FIXME: Should make sure no callers ever do this.
715 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000716 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000717
718 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000719 LoadExternalDefinition();
720
Craig Topper36250ad2014-05-12 05:36:57 +0000721 ObjCMethodDecl *Method = nullptr;
Steve Naroffbb69c942009-10-01 23:46:04 +0000722 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000723 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
724 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000725
726 // Look through local category implementations associated with the class.
727 if (!Method)
Nico Weberf6098392015-03-02 01:12:28 +0000728 Method = getCategoryMethod(Sel, Instance);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000729
730 // Before we give up, check if the selector is an instance method.
731 // But only in the root. This matches gcc's behavior and what the
732 // runtime expects.
733 if (!Instance && !Method && !getSuperClass()) {
734 Method = lookupInstanceMethod(Sel);
735 // Look through local category implementations associated
736 // with the root class.
737 if (!Method)
738 Method = lookupPrivateMethod(Sel, true);
739 }
740
Steve Naroffbb69c942009-10-01 23:46:04 +0000741 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000742 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000743 return Method;
744}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000745
746//===----------------------------------------------------------------------===//
747// ObjCMethodDecl
748//===----------------------------------------------------------------------===//
749
Alp Toker314cc812014-01-25 16:55:45 +0000750ObjCMethodDecl *ObjCMethodDecl::Create(
751 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
752 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
753 DeclContext *contextDecl, bool isInstance, bool isVariadic,
754 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
755 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000756 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000757 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000758 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
759 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000760}
761
Douglas Gregor72172e92012-01-05 21:55:30 +0000762ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000763 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000764 Selector(), QualType(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000765}
766
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000767bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
768 return getMethodFamily() == OMF_init &&
769 hasAttr<ObjCDesignatedInitializerAttr>();
770}
771
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000772bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
773 const ObjCMethodDecl **InitMethod) const {
774 if (getMethodFamily() != OMF_init)
775 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000776 const DeclContext *DC = getDeclContext();
777 if (isa<ObjCProtocolDecl>(DC))
778 return false;
779 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000780 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000781 return false;
782}
783
Douglas Gregora6017bb2012-10-09 17:21:28 +0000784Stmt *ObjCMethodDecl::getBody() const {
785 return Body.get(getASTContext().getExternalSource());
786}
787
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000788void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
789 assert(PrevMethod);
790 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
791 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000792 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000793}
794
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000795void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
796 ArrayRef<ParmVarDecl*> Params,
797 ArrayRef<SourceLocation> SelLocs) {
Craig Topper36250ad2014-05-12 05:36:57 +0000798 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000799 NumParams = Params.size();
800 if (Params.empty() && SelLocs.empty())
801 return;
802
James Y Knight967eb202015-12-29 22:13:13 +0000803 static_assert(llvm::AlignOf<ParmVarDecl *>::Alignment >=
804 llvm::AlignOf<SourceLocation>::Alignment,
805 "Alignment not sufficient for SourceLocation");
806
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000807 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
808 sizeof(SourceLocation) * SelLocs.size();
809 ParamsAndSelLocs = C.Allocate(Size);
810 std::copy(Params.begin(), Params.end(), getParams());
811 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
812}
813
814void ObjCMethodDecl::getSelectorLocs(
815 SmallVectorImpl<SourceLocation> &SelLocs) const {
816 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
817 SelLocs.push_back(getSelectorLoc(i));
818}
819
820void ObjCMethodDecl::setMethodParams(ASTContext &C,
821 ArrayRef<ParmVarDecl*> Params,
822 ArrayRef<SourceLocation> SelLocs) {
823 assert((!SelLocs.empty() || isImplicit()) &&
824 "No selector locs for non-implicit method");
825 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000826 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000827
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000828 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
829 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000830 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000831 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000832
833 setParamsAndSelLocs(C, Params, SelLocs);
834}
835
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000836/// \brief A definition will return its interface declaration.
837/// An interface declaration will return its definition.
838/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000839ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000840 ASTContext &Ctx = getASTContext();
Craig Topper36250ad2014-05-12 05:36:57 +0000841 ObjCMethodDecl *Redecl = nullptr;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000842 if (HasRedeclaration)
843 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000844 if (Redecl)
845 return Redecl;
846
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000847 Decl *CtxD = cast<Decl>(getDeclContext());
848
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000849 if (!CtxD->isInvalidDecl()) {
850 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
851 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
852 if (!ImplD->isInvalidDecl())
853 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000854
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000855 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
856 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
857 if (!ImplD->isInvalidDecl())
858 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000859
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000860 } else if (ObjCImplementationDecl *ImplD =
861 dyn_cast<ObjCImplementationDecl>(CtxD)) {
862 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
863 if (!IFD->isInvalidDecl())
864 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000865
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000866 } else if (ObjCCategoryImplDecl *CImplD =
867 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
868 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
869 if (!CatD->isInvalidDecl())
870 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
871 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000872 }
873
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000874 if (!Redecl && isRedeclaration()) {
875 // This is the last redeclaration, go back to the first method.
876 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
877 isInstanceMethod());
878 }
879
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000880 return Redecl ? Redecl : this;
881}
882
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000883ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
884 Decl *CtxD = cast<Decl>(getDeclContext());
885
886 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
887 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
888 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
889 isInstanceMethod()))
890 return MD;
891
892 } else if (ObjCCategoryImplDecl *CImplD =
893 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000894 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000895 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
896 isInstanceMethod()))
897 return MD;
898 }
899
Manman Rena0042c42016-10-03 21:26:46 +0000900 if (isRedeclaration()) {
901 // It is possible that we have not done deserializing the ObjCMethod yet.
902 ObjCMethodDecl *MD =
903 cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
904 isInstanceMethod());
905 return MD ? MD : this;
906 }
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000907
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000908 return this;
909}
910
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000911SourceLocation ObjCMethodDecl::getLocEnd() const {
912 if (Stmt *Body = getBody())
913 return Body->getLocEnd();
914 return DeclEndLoc;
915}
916
John McCallb4526252011-03-02 01:50:55 +0000917ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
918 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000919 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000920 return family;
921
John McCall86bc21f2011-03-02 11:33:24 +0000922 // Check for an explicit attribute.
923 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
924 // The unfortunate necessity of mapping between enums here is due
925 // to the attributes framework.
926 switch (attr->getFamily()) {
927 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
928 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
929 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
930 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
931 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
932 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
933 }
934 Family = static_cast<unsigned>(family);
935 return family;
936 }
937
John McCallb4526252011-03-02 01:50:55 +0000938 family = getSelector().getMethodFamily();
939 switch (family) {
940 case OMF_None: break;
941
942 // init only has a conventional meaning for an instance method, and
943 // it has to return an object.
944 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000945 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000946 family = OMF_None;
947 break;
948
949 // alloc/copy/new have a conventional meaning for both class and
950 // instance methods, but they require an object return.
951 case OMF_alloc:
952 case OMF_copy:
953 case OMF_mutableCopy:
954 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000955 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000956 family = OMF_None;
957 break;
958
959 // These selectors have a conventional meaning only for instance methods.
960 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000961 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000962 case OMF_retain:
963 case OMF_release:
964 case OMF_autorelease:
965 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000966 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000967 if (!isInstanceMethod())
968 family = OMF_None;
969 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000970
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000971 case OMF_initialize:
972 if (isInstanceMethod() || !getReturnType()->isVoidType())
973 family = OMF_None;
974 break;
975
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000976 case OMF_performSelector:
Alp Toker314cc812014-01-25 16:55:45 +0000977 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000978 family = OMF_None;
979 else {
980 unsigned noParams = param_size();
981 if (noParams < 1 || noParams > 3)
982 family = OMF_None;
983 else {
Alp Toker1f307f42014-01-25 17:32:04 +0000984 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000985 QualType ArgT = (*it);
986 if (!ArgT->isObjCSelType()) {
987 family = OMF_None;
988 break;
989 }
990 while (--noParams) {
991 it++;
992 ArgT = (*it);
993 if (!ArgT->isObjCIdType()) {
994 family = OMF_None;
995 break;
996 }
997 }
998 }
999 }
1000 break;
1001
John McCallb4526252011-03-02 01:50:55 +00001002 }
1003
1004 // Cache the result.
1005 Family = static_cast<unsigned>(family);
1006 return family;
1007}
1008
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001009QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
1010 const ObjCInterfaceDecl *OID,
1011 bool &selfIsPseudoStrong,
1012 bool &selfIsConsumed) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001013 QualType selfTy;
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001014 selfIsPseudoStrong = false;
1015 selfIsConsumed = false;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001016 if (isInstanceMethod()) {
1017 // There may be no interface context due to error in declaration
1018 // of the interface (which has been reported). Recover gracefully.
1019 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +00001020 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001021 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001022 } else {
1023 selfTy = Context.getObjCIdType();
1024 }
1025 } else // we have a factory method.
1026 selfTy = Context.getObjCClassType();
1027
David Blaikiebbafb8a2012-03-11 07:00:24 +00001028 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001029 if (isInstanceMethod()) {
1030 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +00001031
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001032 // 'self' is always __strong. It's actually pseudo-strong except
1033 // in init methods (or methods labeled ns_consumes_self), though.
1034 Qualifiers qs;
1035 qs.setObjCLifetime(Qualifiers::OCL_Strong);
1036 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +00001037
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001038 // In addition, 'self' is const unless this is an init method.
1039 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
1040 selfTy = selfTy.withConst();
1041 selfIsPseudoStrong = true;
1042 }
1043 }
1044 else {
1045 assert(isClassMethod());
1046 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +00001047 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +00001048 selfIsPseudoStrong = true;
1049 }
John McCall31168b02011-06-15 23:02:42 +00001050 }
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001051 return selfTy;
1052}
John McCall31168b02011-06-15 23:02:42 +00001053
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001054void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
1055 const ObjCInterfaceDecl *OID) {
1056 bool selfIsPseudoStrong, selfIsConsumed;
1057 QualType selfTy =
1058 getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
John McCall31168b02011-06-15 23:02:42 +00001059 ImplicitParamDecl *self
1060 = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1061 &Context.Idents.get("self"), selfTy);
1062 setSelfDecl(self);
1063
1064 if (selfIsConsumed)
Aaron Ballman36a53502014-01-16 13:03:14 +00001065 self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001066
John McCalld4631322011-06-17 06:42:21 +00001067 if (selfIsPseudoStrong)
1068 self->setARCPseudoStrong(true);
1069
Mike Stump11289f42009-09-09 15:08:12 +00001070 setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
1071 &Context.Idents.get("_cmd"),
Steve Naroff04f2d142009-04-20 15:06:07 +00001072 Context.getObjCSelType()));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001073}
1074
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001075ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
1076 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
1077 return ID;
1078 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1079 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +00001080 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001081 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +00001082 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +00001083 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +00001084 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001085}
1086
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001087SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1088 const auto *TSI = getReturnTypeSourceInfo();
1089 if (TSI)
1090 return TSI->getTypeLoc().getSourceRange();
1091 return SourceRange();
1092}
1093
Douglas Gregor9b7b3e92015-07-07 06:20:27 +00001094QualType ObjCMethodDecl::getSendResultType() const {
1095 ASTContext &Ctx = getASTContext();
1096 return getReturnType().getNonLValueExprType(Ctx)
1097 .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1098}
1099
Douglas Gregore83b9562015-07-07 03:57:53 +00001100QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1101 // FIXME: Handle related result types here.
1102
1103 return getReturnType().getNonLValueExprType(getASTContext())
1104 .substObjCMemberType(receiverType, getDeclContext(),
1105 ObjCSubstitutionContext::Result);
1106}
1107
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001108static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1109 const ObjCMethodDecl *Method,
1110 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1111 bool MovedToSuper) {
1112 if (!Container)
1113 return;
1114
1115 // In categories look for overriden methods from protocols. A method from
1116 // category is not "overriden" since it is considered as the "same" method
1117 // (same USR) as the one from the interface.
1118 if (const ObjCCategoryDecl *
1119 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1120 // Check whether we have a matching method at this category but only if we
1121 // are at the super class level.
1122 if (MovedToSuper)
1123 if (ObjCMethodDecl *
1124 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001125 Method->isInstanceMethod(),
1126 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001127 if (Method != Overridden) {
1128 // We found an override at this category; there is no need to look
1129 // into its protocols.
1130 Methods.push_back(Overridden);
1131 return;
1132 }
1133
Aaron Ballman19a41762014-03-14 12:55:57 +00001134 for (const auto *P : Category->protocols())
1135 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001136 return;
1137 }
1138
1139 // Check whether we have a matching method at this level.
1140 if (const ObjCMethodDecl *
1141 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001142 Method->isInstanceMethod(),
1143 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001144 if (Method != Overridden) {
1145 // We found an override at this level; there is no need to look
1146 // into other protocols or categories.
1147 Methods.push_back(Overridden);
1148 return;
1149 }
1150
1151 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001152 for (const auto *P : Protocol->protocols())
1153 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001154 }
1155
1156 if (const ObjCInterfaceDecl *
1157 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001158 for (const auto *P : Interface->protocols())
1159 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001160
Aaron Ballman15063e12014-03-13 21:35:02 +00001161 for (const auto *Cat : Interface->known_categories())
1162 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001163
1164 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1165 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1166 /*MovedToSuper=*/true);
1167 }
1168}
1169
1170static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1171 const ObjCMethodDecl *Method,
1172 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1173 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1174 /*MovedToSuper=*/false);
1175}
1176
1177static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1178 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1179 assert(Method->isOverriding());
1180
1181 if (const ObjCProtocolDecl *
1182 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1183 CollectOverriddenMethods(ProtD, Method, overridden);
1184
1185 } else if (const ObjCImplDecl *
1186 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1187 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1188 if (!ID)
1189 return;
1190 // Start searching for overridden methods using the method from the
1191 // interface as starting point.
1192 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001193 Method->isInstanceMethod(),
1194 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001195 Method = IFaceMeth;
1196 CollectOverriddenMethods(ID, Method, overridden);
1197
1198 } else if (const ObjCCategoryDecl *
1199 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1200 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1201 if (!ID)
1202 return;
1203 // Start searching for overridden methods using the method from the
1204 // interface as starting point.
1205 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001206 Method->isInstanceMethod(),
1207 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001208 Method = IFaceMeth;
1209 CollectOverriddenMethods(ID, Method, overridden);
1210
1211 } else {
1212 CollectOverriddenMethods(
1213 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1214 Method, overridden);
1215 }
1216}
1217
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001218void ObjCMethodDecl::getOverriddenMethods(
1219 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1220 const ObjCMethodDecl *Method = this;
1221
1222 if (Method->isRedeclaration()) {
1223 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1224 getMethod(Method->getSelector(), Method->isInstanceMethod());
1225 }
1226
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001227 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001228 collectOverriddenMethodsSlow(Method, Overridden);
1229 assert(!Overridden.empty() &&
1230 "ObjCMethodDecl's overriding bit is not as expected");
1231 }
1232}
1233
Jordan Rose2bd991a2012-10-10 16:42:54 +00001234const ObjCPropertyDecl *
1235ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1236 Selector Sel = getSelector();
1237 unsigned NumArgs = Sel.getNumArgs();
1238 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001239 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001240
Jordan Rose2bd991a2012-10-10 16:42:54 +00001241 if (isPropertyAccessor()) {
1242 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
1243 bool IsGetter = (NumArgs == 0);
Jordan Rose31cf7d42016-03-11 21:14:40 +00001244 bool IsInstance = isInstanceMethod();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001245
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001246 /// Local function that attempts to find a matching property within the
1247 /// given Objective-C container.
1248 auto findMatchingProperty =
1249 [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * {
Jordan Rose31cf7d42016-03-11 21:14:40 +00001250 if (IsInstance) {
1251 for (const auto *I : Container->instance_properties()) {
1252 Selector NextSel = IsGetter ? I->getGetterName()
1253 : I->getSetterName();
1254 if (NextSel == Sel)
1255 return I;
1256 }
1257 } else {
1258 for (const auto *I : Container->class_properties()) {
1259 Selector NextSel = IsGetter ? I->getGetterName()
1260 : I->getSetterName();
1261 if (NextSel == Sel)
1262 return I;
1263 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001264 }
1265
1266 return nullptr;
1267 };
1268
1269 // Look in the container we were given.
1270 if (const auto *Found = findMatchingProperty(Container))
1271 return Found;
1272
1273 // If we're in a category or extension, look in the main class.
1274 const ObjCInterfaceDecl *ClassDecl = nullptr;
1275 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1276 ClassDecl = Category->getClassInterface();
1277 if (const auto *Found = findMatchingProperty(ClassDecl))
1278 return Found;
1279 } else {
1280 // Determine whether the container is a class.
1281 ClassDecl = dyn_cast<ObjCInterfaceDecl>(Container);
1282 }
1283
1284 // If we have a class, check its visible extensions.
1285 if (ClassDecl) {
1286 for (const auto *Ext : ClassDecl->visible_extensions()) {
1287 if (Ext == Container)
1288 continue;
1289
1290 if (const auto *Found = findMatchingProperty(Ext))
1291 return Found;
1292 }
Jordan Rose2bd991a2012-10-10 16:42:54 +00001293 }
1294
1295 llvm_unreachable("Marked as a property accessor but no property found!");
1296 }
1297
1298 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001299 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001300
1301 typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1302 OverridesTy Overrides;
1303 getOverriddenMethods(Overrides);
1304 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1305 I != E; ++I) {
1306 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1307 return Prop;
1308 }
1309
Craig Topper36250ad2014-05-12 05:36:57 +00001310 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001311}
1312
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001313//===----------------------------------------------------------------------===//
Douglas Gregor85f3f952015-07-07 03:57:15 +00001314// ObjCTypeParamDecl
1315//===----------------------------------------------------------------------===//
1316
1317void ObjCTypeParamDecl::anchor() { }
1318
1319ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001320 ObjCTypeParamVariance variance,
1321 SourceLocation varianceLoc,
Douglas Gregore83b9562015-07-07 03:57:53 +00001322 unsigned index,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001323 SourceLocation nameLoc,
1324 IdentifierInfo *name,
1325 SourceLocation colonLoc,
1326 TypeSourceInfo *boundInfo) {
Manman Renc5705ba2016-09-13 17:41:05 +00001327 auto *TPDecl =
1328 new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1329 nameLoc, name, colonLoc, boundInfo);
1330 QualType TPType = ctx.getObjCTypeParamType(TPDecl, {});
1331 TPDecl->setTypeForDecl(TPType.getTypePtr());
1332 return TPDecl;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001333}
1334
1335ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1336 unsigned ID) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001337 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1338 ObjCTypeParamVariance::Invariant,
1339 SourceLocation(), 0, SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001340 nullptr, SourceLocation(), nullptr);
1341}
1342
1343SourceRange ObjCTypeParamDecl::getSourceRange() const {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001344 SourceLocation startLoc = VarianceLoc;
1345 if (startLoc.isInvalid())
1346 startLoc = getLocation();
1347
Douglas Gregor85f3f952015-07-07 03:57:15 +00001348 if (hasExplicitBound()) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001349 return SourceRange(startLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001350 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1351 }
1352
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001353 return SourceRange(startLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001354}
1355
1356//===----------------------------------------------------------------------===//
1357// ObjCTypeParamList
1358//===----------------------------------------------------------------------===//
1359ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1360 ArrayRef<ObjCTypeParamDecl *> typeParams,
1361 SourceLocation rAngleLoc)
Douglas Gregor0a8890ff2015-07-07 06:20:46 +00001362 : NumParams(typeParams.size())
Douglas Gregor85f3f952015-07-07 03:57:15 +00001363{
Douglas Gregor0a8890ff2015-07-07 06:20:46 +00001364 Brackets.Begin = lAngleLoc.getRawEncoding();
1365 Brackets.End = rAngleLoc.getRawEncoding();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001366 std::copy(typeParams.begin(), typeParams.end(), begin());
1367}
1368
1369
1370ObjCTypeParamList *ObjCTypeParamList::create(
1371 ASTContext &ctx,
1372 SourceLocation lAngleLoc,
1373 ArrayRef<ObjCTypeParamDecl *> typeParams,
1374 SourceLocation rAngleLoc) {
James Y Knight967eb202015-12-29 22:13:13 +00001375 void *mem =
1376 ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
1377 llvm::alignOf<ObjCTypeParamList>());
Douglas Gregor85f3f952015-07-07 03:57:15 +00001378 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1379}
1380
Douglas Gregore83b9562015-07-07 03:57:53 +00001381void ObjCTypeParamList::gatherDefaultTypeArgs(
1382 SmallVectorImpl<QualType> &typeArgs) const {
1383 typeArgs.reserve(size());
1384 for (auto typeParam : *this)
1385 typeArgs.push_back(typeParam->getUnderlyingType());
1386}
1387
Douglas Gregor85f3f952015-07-07 03:57:15 +00001388//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001389// ObjCInterfaceDecl
1390//===----------------------------------------------------------------------===//
1391
Douglas Gregord53ae832012-01-17 18:09:05 +00001392ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001393 DeclContext *DC,
1394 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001395 IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001396 ObjCTypeParamList *typeParamList,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001397 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001398 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001399 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001400 ObjCInterfaceDecl *Result = new (C, DC)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001401 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1402 isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001403 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001404 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001405 return Result;
1406}
1407
Richard Smith053f6c62014-05-16 23:01:30 +00001408ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001409 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001410 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001411 SourceLocation(),
1412 nullptr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001413 nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001414 SourceLocation(),
1415 nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001416 Result->Data.setInt(!C.getLangOpts().Modules);
1417 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001418}
1419
Richard Smith053f6c62014-05-16 23:01:30 +00001420ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1421 SourceLocation AtLoc, IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001422 ObjCTypeParamList *typeParamList,
Richard Smith053f6c62014-05-16 23:01:30 +00001423 SourceLocation CLoc,
1424 ObjCInterfaceDecl *PrevDecl,
1425 bool IsInternal)
1426 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001427 redeclarable_base(C), TypeForDecl(nullptr), TypeParamList(nullptr),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001428 Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001429 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001430
1431 // Copy the 'data' pointer over.
1432 if (PrevDecl)
1433 Data = PrevDecl->Data;
1434
Richard Smith053f6c62014-05-16 23:01:30 +00001435 setImplicit(IsInternal);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001436
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001437 setTypeParamList(typeParamList);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001438}
1439
Douglas Gregor73693022010-12-01 23:49:52 +00001440void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001441 assert(data().ExternallyCompleted && "Class is not externally completed");
1442 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001443 getASTContext().getExternalSource()->CompleteType(
1444 const_cast<ObjCInterfaceDecl *>(this));
1445}
1446
1447void ObjCInterfaceDecl::setExternallyCompleted() {
1448 assert(getASTContext().getExternalSource() &&
1449 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001450 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001451 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001452 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001453}
1454
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001455void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001456 // Check for a complete definition and recover if not so.
1457 if (!isThisDeclarationADefinition())
1458 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001459 data().HasDesignatedInitializers = true;
1460}
1461
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001462bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001463 // Check for a complete definition and recover if not so.
1464 if (!isThisDeclarationADefinition())
1465 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001466 if (data().ExternallyCompleted)
1467 LoadExternalDefinition();
1468
1469 return data().HasDesignatedInitializers;
1470}
1471
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001472StringRef
1473ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001474 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1475 return ObjCRTName->getMetadataName();
1476
1477 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001478}
1479
1480StringRef
1481ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001482 if (ObjCInterfaceDecl *ID =
1483 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1484 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001485
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001486 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001487}
1488
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001489ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001490 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1491 if (data().ExternallyCompleted)
1492 LoadExternalDefinition();
1493
1494 return getASTContext().getObjCImplementation(
1495 const_cast<ObjCInterfaceDecl*>(Def));
1496 }
1497
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001498 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001499 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001500}
1501
1502void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001503 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001504}
1505
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001506namespace {
1507 struct SynthesizeIvarChunk {
1508 uint64_t Size;
1509 ObjCIvarDecl *Ivar;
1510 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1511 : Size(size), Ivar(ivar) {}
1512 };
1513
1514 bool operator<(const SynthesizeIvarChunk & LHS,
1515 const SynthesizeIvarChunk &RHS) {
1516 return LHS.Size < RHS.Size;
1517 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001518}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001519
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001520/// all_declared_ivar_begin - return first ivar declared in this class,
1521/// its extensions and its implementation. Lazily build the list on first
1522/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001523///
1524/// Caveat: The list returned by this method reflects the current
1525/// state of the parser. The cache will be updated for every ivar
1526/// added by an extension or the implementation when they are
1527/// encountered.
1528/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001529ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001530 // FIXME: Should make sure no callers ever do this.
1531 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001532 return nullptr;
1533
1534 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001535 if (!data().IvarList) {
1536 if (!ivar_empty()) {
1537 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1538 data().IvarList = *I; ++I;
1539 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001540 curIvar->setNextIvar(*I);
1541 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001542
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001543 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001544 if (!Ext->ivar_empty()) {
1545 ObjCCategoryDecl::ivar_iterator
1546 I = Ext->ivar_begin(),
1547 E = Ext->ivar_end();
1548 if (!data().IvarList) {
1549 data().IvarList = *I; ++I;
1550 curIvar = data().IvarList;
1551 }
1552 for ( ;I != E; curIvar = *I, ++I)
1553 curIvar->setNextIvar(*I);
1554 }
1555 }
1556 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001557 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001558
1559 // cached and complete!
1560 if (!data().IvarListMissingImplementation)
1561 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001562
1563 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001564 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001565 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001566 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001567 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001568 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1569 layout.push_back(SynthesizeIvarChunk(
1570 IV->getASTContext().getTypeSize(IV->getType()), IV));
1571 continue;
1572 }
1573 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001574 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001575 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001576 curIvar->setNextIvar(IV);
1577 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001578 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001579
1580 if (!layout.empty()) {
1581 // Order synthesized ivars by their size.
1582 std::stable_sort(layout.begin(), layout.end());
1583 unsigned Ix = 0, EIx = layout.size();
1584 if (!data().IvarList) {
1585 data().IvarList = layout[0].Ivar; Ix++;
1586 curIvar = data().IvarList;
1587 }
1588 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1589 curIvar->setNextIvar(layout[Ix].Ivar);
1590 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001591 }
1592 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001593 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001594}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001595
1596/// FindCategoryDeclaration - Finds category declaration in the list of
1597/// categories for this class and returns it. Name of the category is passed
1598/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001599///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001600ObjCCategoryDecl *
1601ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001602 // FIXME: Should make sure no callers ever do this.
1603 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001604 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001605
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001606 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001607 LoadExternalDefinition();
1608
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001609 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001610 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001611 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001612
1613 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001614}
1615
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001616ObjCMethodDecl *
1617ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001618 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001619 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001620 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1621 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001622 }
1623
Craig Topper36250ad2014-05-12 05:36:57 +00001624 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001625}
1626
1627ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001628 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001629 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001630 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1631 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001632 }
Craig Topper36250ad2014-05-12 05:36:57 +00001633
1634 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001635}
1636
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001637/// ClassImplementsProtocol - Checks that 'lProto' protocol
1638/// has been implemented in IDecl class, its super class or categories (if
1639/// lookupCategory is true).
1640bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1641 bool lookupCategory,
1642 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001643 if (!hasDefinition())
1644 return false;
1645
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001646 ObjCInterfaceDecl *IDecl = this;
1647 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001648 for (auto *PI : IDecl->protocols()){
1649 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001650 return true;
1651 // This is dubious and is added to be compatible with gcc. In gcc, it is
1652 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1653 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1654 // object. This IMO, should be a bug.
1655 // FIXME: Treat this as an extension, and flag this as an error when GCC
1656 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001657 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001658 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001659 return true;
1660 }
Mike Stump11289f42009-09-09 15:08:12 +00001661
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001662 // 2nd, look up the category.
1663 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001664 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001665 for (auto *PI : Cat->protocols())
1666 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001667 return true;
1668 }
Mike Stump11289f42009-09-09 15:08:12 +00001669
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001670 // 3rd, look up the super class(s)
1671 if (IDecl->getSuperClass())
1672 return
1673 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1674 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001675
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001676 return false;
1677}
1678
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001679//===----------------------------------------------------------------------===//
1680// ObjCIvarDecl
1681//===----------------------------------------------------------------------===//
1682
David Blaikie68e081d2011-12-20 02:48:34 +00001683void ObjCIvarDecl::anchor() { }
1684
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001685ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001686 SourceLocation StartLoc,
1687 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001688 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001689 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001690 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001691 if (DC) {
1692 // Ivar's can only appear in interfaces, implementations (via synthesized
1693 // properties), and class extensions (via direct declaration, or synthesized
1694 // properties).
1695 //
1696 // FIXME: This should really be asserting this:
1697 // (isa<ObjCCategoryDecl>(DC) &&
1698 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1699 // but unfortunately we sometimes place ivars into non-class extension
1700 // categories on error. This breaks an AST invariant, and should not be
1701 // fixed.
1702 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1703 isa<ObjCCategoryDecl>(DC)) &&
1704 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001705 // Once a new ivar is created in any of class/class-extension/implementation
1706 // decl contexts, the previously built IvarList must be rebuilt.
1707 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1708 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001709 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001710 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001711 else
1712 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001713 }
Craig Topper36250ad2014-05-12 05:36:57 +00001714 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001715 }
1716
Richard Smithf7981722013-11-22 09:01:48 +00001717 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001718 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001719}
1720
Douglas Gregor72172e92012-01-05 21:55:30 +00001721ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001722 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1723 nullptr, QualType(), nullptr,
1724 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001725}
1726
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001727const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1728 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001729
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001730 switch (DC->getKind()) {
1731 default:
1732 case ObjCCategoryImpl:
1733 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001734 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001735
1736 // Ivars can only appear in class extension categories.
1737 case ObjCCategory: {
1738 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1739 assert(CD->IsClassExtension() && "invalid container for ivar!");
1740 return CD->getClassInterface();
1741 }
1742
1743 case ObjCImplementation:
1744 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1745
1746 case ObjCInterface:
1747 return cast<ObjCInterfaceDecl>(DC);
1748 }
1749}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001750
Douglas Gregore83b9562015-07-07 03:57:53 +00001751QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1752 return getType().substObjCMemberType(objectType, getDeclContext(),
1753 ObjCSubstitutionContext::Property);
1754}
1755
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001756//===----------------------------------------------------------------------===//
1757// ObjCAtDefsFieldDecl
1758//===----------------------------------------------------------------------===//
1759
David Blaikie68e081d2011-12-20 02:48:34 +00001760void ObjCAtDefsFieldDecl::anchor() { }
1761
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001762ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001763*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1764 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001765 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001766 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001767}
1768
Richard Smithf7981722013-11-22 09:01:48 +00001769ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001770 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001771 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1772 SourceLocation(), nullptr, QualType(),
1773 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001774}
1775
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001776//===----------------------------------------------------------------------===//
1777// ObjCProtocolDecl
1778//===----------------------------------------------------------------------===//
1779
David Blaikie68e081d2011-12-20 02:48:34 +00001780void ObjCProtocolDecl::anchor() { }
1781
Richard Smith053f6c62014-05-16 23:01:30 +00001782ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1783 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001784 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001785 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001786 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1787 redeclarable_base(C), Data() {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001788 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001789 if (PrevDecl)
1790 Data = PrevDecl->Data;
1791}
1792
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001793ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001794 IdentifierInfo *Id,
1795 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001796 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001797 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001798 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001799 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001800 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001801 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001802}
1803
Richard Smithf7981722013-11-22 09:01:48 +00001804ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001805 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001806 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001807 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001808 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001809 Result->Data.setInt(!C.getLangOpts().Modules);
1810 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001811}
1812
Steve Naroff114aecb2009-03-01 16:12:44 +00001813ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1814 ObjCProtocolDecl *PDecl = this;
1815
1816 if (Name == getIdentifier())
1817 return PDecl;
1818
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001819 for (auto *I : protocols())
1820 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001821 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001822
Craig Topper36250ad2014-05-12 05:36:57 +00001823 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001824}
1825
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001826// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001827// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001828ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1829 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001830 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001831
Douglas Gregoreed49792013-01-17 00:38:46 +00001832 // If there is no definition or the definition is hidden, we don't find
1833 // anything.
1834 const ObjCProtocolDecl *Def = getDefinition();
1835 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001836 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001837
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001838 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001839 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001840
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001841 for (const auto *I : protocols())
1842 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001843 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001844 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001845}
1846
Douglas Gregore6e48b12012-01-01 19:29:29 +00001847void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001848 assert(!Data.getPointer() && "Protocol already has a definition!");
1849 Data.setPointer(new (getASTContext()) DefinitionData);
1850 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001851}
1852
1853void ObjCProtocolDecl::startDefinition() {
1854 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001855
1856 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001857 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001858 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001859}
1860
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001861void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1862 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001863
1864 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001865 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001866 // Insert into PM if not there already.
Manman Ren494ee5b2016-01-28 23:36:05 +00001867 PM.insert(std::make_pair(
1868 std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
1869 Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001870 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001871 }
1872 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001873 for (const auto *PI : PDecl->protocols())
1874 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001875 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001876}
1877
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001878
1879void ObjCProtocolDecl::collectInheritedProtocolProperties(
1880 const ObjCPropertyDecl *Property,
1881 ProtocolPropertyMap &PM) const {
1882 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1883 bool MatchFound = false;
Manman Renefe1bac2016-01-27 20:00:32 +00001884 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001885 if (Prop == Property)
1886 continue;
1887 if (Prop->getIdentifier() == Property->getIdentifier()) {
1888 PM[PDecl] = Prop;
1889 MatchFound = true;
1890 break;
1891 }
1892 }
1893 // Scan through protocol's protocols which did not have a matching property.
1894 if (!MatchFound)
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001895 for (const auto *PI : PDecl->protocols())
1896 PI->collectInheritedProtocolProperties(Property, PM);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001897 }
1898}
Anna Zaks673d76b2012-10-18 19:17:53 +00001899
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001900StringRef
1901ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001902 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1903 return ObjCRTName->getMetadataName();
1904
1905 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001906}
1907
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001908//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001909// ObjCCategoryDecl
1910//===----------------------------------------------------------------------===//
1911
David Blaikie68e081d2011-12-20 02:48:34 +00001912void ObjCCategoryDecl::anchor() { }
1913
Douglas Gregor85f3f952015-07-07 03:57:15 +00001914ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1915 SourceLocation ClassNameLoc,
1916 SourceLocation CategoryNameLoc,
1917 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1918 ObjCTypeParamList *typeParamList,
1919 SourceLocation IvarLBraceLoc,
1920 SourceLocation IvarRBraceLoc)
1921 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001922 ClassInterface(IDecl), TypeParamList(nullptr),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001923 NextClassCategory(nullptr), CategoryNameLoc(CategoryNameLoc),
1924 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc)
1925{
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001926 setTypeParamList(typeParamList);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001927}
1928
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001929ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001930 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001931 SourceLocation ClassNameLoc,
1932 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001933 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001934 ObjCInterfaceDecl *IDecl,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001935 ObjCTypeParamList *typeParamList,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001936 SourceLocation IvarLBraceLoc,
1937 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001938 ObjCCategoryDecl *CatDecl =
1939 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001940 IDecl, typeParamList, IvarLBraceLoc,
1941 IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001942 if (IDecl) {
1943 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001944 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001945 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001946 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001947 if (ASTMutationListener *L = C.getASTMutationListener())
1948 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1949 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001950 }
1951
1952 return CatDecl;
1953}
1954
Richard Smithf7981722013-11-22 09:01:48 +00001955ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001956 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001957 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1958 SourceLocation(), SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001959 nullptr, nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001960}
1961
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001962ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1963 return getASTContext().getObjCImplementation(
1964 const_cast<ObjCCategoryDecl*>(this));
1965}
1966
1967void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1968 getASTContext().setObjCImplementation(this, ImplD);
1969}
1970
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001971void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
1972 TypeParamList = TPL;
1973 if (!TPL)
1974 return;
1975 // Set the declaration context of each of the type parameters.
1976 for (auto typeParam : *TypeParamList)
1977 typeParam->setDeclContext(this);
1978}
1979
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001980
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001981//===----------------------------------------------------------------------===//
1982// ObjCCategoryImplDecl
1983//===----------------------------------------------------------------------===//
1984
David Blaikie68e081d2011-12-20 02:48:34 +00001985void ObjCCategoryImplDecl::anchor() { }
1986
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001987ObjCCategoryImplDecl *
1988ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001989 IdentifierInfo *Id,
1990 ObjCInterfaceDecl *ClassInterface,
1991 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00001992 SourceLocation atStartLoc,
1993 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00001994 if (ClassInterface && ClassInterface->hasDefinition())
1995 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00001996 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
1997 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001998}
1999
Douglas Gregor72172e92012-01-05 21:55:30 +00002000ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
2001 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002002 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
2003 SourceLocation(), SourceLocation(),
2004 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002005}
2006
Steve Narofff406f4d2009-10-29 21:11:04 +00002007ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00002008 // The class interface might be NULL if we are working with invalid code.
2009 if (const ObjCInterfaceDecl *ID = getClassInterface())
2010 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00002011 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00002012}
2013
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002014
David Blaikie68e081d2011-12-20 02:48:34 +00002015void ObjCImplDecl::anchor() { }
2016
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002017void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00002018 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002019 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002020 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002021}
2022
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002023void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
2024 ASTContext &Ctx = getASTContext();
2025
2026 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00002027 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002028 if (IFace)
2029 Ctx.setObjCImplementation(IFace, ImplD);
2030
Duncan Sands49c29ee2009-07-21 07:56:29 +00002031 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002032 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
2033 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
2034 Ctx.setObjCImplementation(CD, ImplD);
2035 }
2036
2037 ClassInterface = IFace;
2038}
2039
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002040/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00002041/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00002042/// the implemented property that uses it.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002043///
Chris Lattnera9ca0522009-02-28 18:42:10 +00002044ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002045FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00002046 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002047 if (PID->getPropertyIvarDecl() &&
2048 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
2049 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00002050 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002051}
2052
2053/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00002054/// added to the list of those properties \@synthesized/\@dynamic in this
2055/// category \@implementation block.
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002056///
Chris Lattnera9ca0522009-02-28 18:42:10 +00002057ObjCPropertyImplDecl *ObjCImplDecl::
Manman Ren5b786402016-01-28 18:49:28 +00002058FindPropertyImplDecl(IdentifierInfo *Id,
2059 ObjCPropertyQueryKind QueryKind) const {
2060 ObjCPropertyImplDecl *ClassPropImpl = nullptr;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002061 for (auto *PID : property_impls())
Manman Ren5b786402016-01-28 18:49:28 +00002062 // If queryKind is unknown, we return the instance property if one
2063 // exists; otherwise we return the class property.
2064 if (PID->getPropertyDecl()->getIdentifier() == Id) {
2065 if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
2066 !PID->getPropertyDecl()->isClassProperty()) ||
2067 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
2068 PID->getPropertyDecl()->isClassProperty()) ||
2069 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
2070 !PID->getPropertyDecl()->isClassProperty()))
2071 return PID;
2072
2073 if (PID->getPropertyDecl()->isClassProperty())
2074 ClassPropImpl = PID;
2075 }
2076
2077 if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
2078 // We can't find the instance property, return the class property.
2079 return ClassPropImpl;
2080
Craig Topper36250ad2014-05-12 05:36:57 +00002081 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002082}
2083
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002084raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002085 const ObjCCategoryImplDecl &CID) {
2086 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002087 return OS;
2088}
2089
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002090//===----------------------------------------------------------------------===//
2091// ObjCImplementationDecl
2092//===----------------------------------------------------------------------===//
2093
David Blaikie68e081d2011-12-20 02:48:34 +00002094void ObjCImplementationDecl::anchor() { }
2095
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002096ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00002097ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002098 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002099 ObjCInterfaceDecl *SuperDecl,
2100 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002101 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00002102 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002103 SourceLocation IvarLBraceLoc,
2104 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00002105 if (ClassInterface && ClassInterface->hasDefinition())
2106 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00002107 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
2108 nameLoc, atStartLoc, superLoc,
2109 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002110}
2111
Douglas Gregor72172e92012-01-05 21:55:30 +00002112ObjCImplementationDecl *
2113ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002114 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
2115 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002116}
2117
John McCall0410e572011-07-22 04:15:06 +00002118void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2119 CXXCtorInitializer ** initializers,
2120 unsigned numInitializers) {
2121 if (numInitializers > 0) {
2122 NumIvarInitializers = numInitializers;
2123 CXXCtorInitializer **ivarInitializers =
2124 new (C) CXXCtorInitializer*[NumIvarInitializers];
2125 memcpy(ivarInitializers, initializers,
2126 numInitializers * sizeof(CXXCtorInitializer*));
2127 IvarInitializers = ivarInitializers;
2128 }
2129}
2130
Richard Smithc2bb8182015-03-24 06:36:48 +00002131ObjCImplementationDecl::init_const_iterator
2132ObjCImplementationDecl::init_begin() const {
2133 return IvarInitializers.get(getASTContext().getExternalSource());
2134}
2135
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002136raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002137 const ObjCImplementationDecl &ID) {
2138 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002139 return OS;
2140}
2141
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002142//===----------------------------------------------------------------------===//
2143// ObjCCompatibleAliasDecl
2144//===----------------------------------------------------------------------===//
2145
David Blaikie68e081d2011-12-20 02:48:34 +00002146void ObjCCompatibleAliasDecl::anchor() { }
2147
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002148ObjCCompatibleAliasDecl *
2149ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2150 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00002151 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002152 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00002153 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002154}
2155
Douglas Gregor72172e92012-01-05 21:55:30 +00002156ObjCCompatibleAliasDecl *
2157ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002158 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2159 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002160}
2161
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002162//===----------------------------------------------------------------------===//
2163// ObjCPropertyDecl
2164//===----------------------------------------------------------------------===//
2165
David Blaikie68e081d2011-12-20 02:48:34 +00002166void ObjCPropertyDecl::anchor() { }
2167
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002168ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2169 SourceLocation L,
2170 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00002171 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002172 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00002173 QualType T,
2174 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002175 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002176 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2177 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002178}
2179
Richard Smithf7981722013-11-22 09:01:48 +00002180ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002181 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002182 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2183 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00002184 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00002185}
2186
Douglas Gregore83b9562015-07-07 03:57:53 +00002187QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2188 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2189 ObjCSubstitutionContext::Property);
2190}
2191
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002192//===----------------------------------------------------------------------===//
2193// ObjCPropertyImplDecl
2194//===----------------------------------------------------------------------===//
2195
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002196ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002197 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002198 SourceLocation atLoc,
2199 SourceLocation L,
2200 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002201 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002202 ObjCIvarDecl *ivar,
2203 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002204 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2205 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002206}
Chris Lattnered0e1642008-03-17 01:19:02 +00002207
Richard Smithf7981722013-11-22 09:01:48 +00002208ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002209 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002210 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2211 SourceLocation(), nullptr, Dynamic,
2212 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002213}
2214
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002215SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2216 SourceLocation EndLoc = getLocation();
2217 if (IvarLoc.isValid())
2218 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002219
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002220 return SourceRange(AtLoc, EndLoc);
2221}