blob: 16e063caac443d670be4282e4b1aed6a22a6df20 [file] [log] [blame]
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001//===- DeclObjC.cpp - ObjC Declaration AST Node Implementation ------------===//
Chris Lattner89375192008-03-16 00:19:01 +00002//
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"
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +000018#include "clang/AST/Decl.h"
19#include "clang/AST/DeclBase.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000020#include "clang/AST/Stmt.h"
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +000021#include "clang/AST/Type.h"
22#include "clang/AST/TypeLoc.h"
23#include "clang/Basic/IdentifierTable.h"
24#include "clang/Basic/LLVM.h"
25#include "clang/Basic/LangOptions.h"
26#include "clang/Basic/SourceLocation.h"
27#include "llvm/ADT/None.h"
Anna Zaks454477c2012-09-27 19:45:11 +000028#include "llvm/ADT/SmallString.h"
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +000029#include "llvm/ADT/SmallVector.h"
30#include "llvm/Support/Casting.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
33#include <algorithm>
34#include <cassert>
35#include <cstdint>
36#include <cstring>
37#include <utility>
38
Chris Lattner89375192008-03-16 00:19:01 +000039using namespace clang;
40
Chris Lattner8d8829e2008-03-16 00:49:28 +000041//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000042// ObjCListBase
43//===----------------------------------------------------------------------===//
44
Chris Lattner22298722009-02-20 21:35:13 +000045void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Craig Topper36250ad2014-05-12 05:36:57 +000046 List = nullptr;
Chris Lattner4d1eb762009-02-20 21:16:26 +000047 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000048
Chris Lattner7c981a72009-02-20 21:44:01 +000049 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000050 NumElts = Elts;
51 memcpy(List, InList, sizeof(void*)*Elts);
52}
53
Douglas Gregor002b6712010-01-16 15:02:53 +000054void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
55 const SourceLocation *Locs, ASTContext &Ctx) {
56 if (Elts == 0)
57 return;
58
59 Locations = new (Ctx) SourceLocation[Elts];
60 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
61 set(InList, Elts, Ctx);
62}
63
Chris Lattner4d1eb762009-02-20 21:16:26 +000064//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000065// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000066//===----------------------------------------------------------------------===//
67
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +000068void ObjCContainerDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000069
Fariborz Jahanian68453832009-06-05 18:16:35 +000070/// getIvarDecl - This method looks up an ivar in this ContextDecl.
71///
72ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000073ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Richard Smithcf4bdde2015-02-21 02:45:19 +000074 lookup_result R = lookup(Id);
75 for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000076 Ivar != IvarEnd; ++Ivar) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +000077 if (auto *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
Fariborz Jahanian68453832009-06-05 18:16:35 +000078 return ivar;
79 }
Craig Topper36250ad2014-05-12 05:36:57 +000080 return nullptr;
Fariborz Jahanian68453832009-06-05 18:16:35 +000081}
82
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000083// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000084ObjCMethodDecl *
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000085ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
86 bool AllowHidden) const {
Douglas Gregoreed49792013-01-17 00:38:46 +000087 // If this context is a hidden protocol definition, don't find any
88 // methods there.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +000089 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
Douglas Gregoreed49792013-01-17 00:38:46 +000090 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000091 if (Def->isHidden() && !AllowHidden)
Craig Topper36250ad2014-05-12 05:36:57 +000092 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +000093 }
94
Steve Naroffc4173fa2009-02-22 19:35:57 +000095 // Since instance & class methods can have the same name, the loop below
96 // ensures we get the correct method.
97 //
98 // @interface Whatever
99 // - (int) class_method;
100 // + (float) class_method;
101 // @end
Richard Smithcf4bdde2015-02-21 02:45:19 +0000102 lookup_result R = lookup(Sel);
103 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +0000104 Meth != MethEnd; ++Meth) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000105 auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +0000106 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +0000107 return MD;
108 }
Craig Topper36250ad2014-05-12 05:36:57 +0000109 return nullptr;
Steve Naroff35c62ae2009-01-08 17:28:14 +0000110}
111
Nico Weber4701ffd2014-12-22 05:21:03 +0000112/// \brief This routine returns 'true' if a user declared setter method was
113/// found in the class, its protocols, its super classes or categories.
114/// It also returns 'true' if one of its categories has declared a 'readwrite'
115/// property. This is because, user must provide a setter method for the
116/// category's 'readwrite' property.
117bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
118 const ObjCPropertyDecl *Property) const {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000119 Selector Sel = Property->getSetterName();
Richard Smithcf4bdde2015-02-21 02:45:19 +0000120 lookup_result R = lookup(Sel);
121 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000122 Meth != MethEnd; ++Meth) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000123 auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000124 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
125 return true;
126 }
127
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000128 if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000129 // Also look into categories, including class extensions, looking
130 // for a user declared instance method.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000131 for (const auto *Cat : ID->visible_categories()) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000132 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
133 if (!MD->isImplicit())
134 return true;
135 if (Cat->IsClassExtension())
136 continue;
Nico Weber4701ffd2014-12-22 05:21:03 +0000137 // Also search through the categories looking for a 'readwrite'
138 // declaration of this property. If one found, presumably a setter will
139 // be provided (properties declared in categories will not get
140 // auto-synthesized).
Manman Renefe1bac2016-01-27 20:00:32 +0000141 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000142 if (P->getIdentifier() == Property->getIdentifier()) {
143 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
144 return true;
145 break;
146 }
147 }
148
149 // Also look into protocols, for a user declared instance method.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000150 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000151 if (Proto->HasUserDeclaredSetterMethod(Property))
152 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000153
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000154 // And in its super class.
155 ObjCInterfaceDecl *OSC = ID->getSuperClass();
156 while (OSC) {
157 if (OSC->HasUserDeclaredSetterMethod(Property))
158 return true;
159 OSC = OSC->getSuperClass();
160 }
161 }
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000162 if (const auto *PD = dyn_cast<ObjCProtocolDecl>(this))
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000163 for (const auto *PI : PD->protocols())
164 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000165 return true;
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000166 return false;
167}
168
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000169ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000170ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Manman Ren5b786402016-01-28 18:49:28 +0000171 const IdentifierInfo *propertyID,
172 ObjCPropertyQueryKind queryKind) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000173 // If this context is a hidden protocol definition, don't find any
174 // property.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000175 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000176 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
177 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000178 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000179 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000180
Alex Lorenzf9a28a22017-02-22 23:18:49 +0000181 // If context is class, then lookup property in its visible extensions.
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000182 // This comes before property is looked up in primary class.
183 if (auto *IDecl = dyn_cast<ObjCInterfaceDecl>(DC)) {
Alex Lorenzf9a28a22017-02-22 23:18:49 +0000184 for (const auto *Ext : IDecl->visible_extensions())
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000185 if (ObjCPropertyDecl *PD = ObjCPropertyDecl::findPropertyDecl(Ext,
Manman Ren5b786402016-01-28 18:49:28 +0000186 propertyID,
187 queryKind))
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000188 return PD;
189 }
190
Richard Smithcf4bdde2015-02-21 02:45:19 +0000191 DeclContext::lookup_result R = DC->lookup(propertyID);
Manman Ren5b786402016-01-28 18:49:28 +0000192 ObjCPropertyDecl *classProp = nullptr;
Richard Smithcf4bdde2015-02-21 02:45:19 +0000193 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
David Blaikieff7d47a2012-12-19 00:45:41 +0000194 ++I)
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000195 if (auto *PD = dyn_cast<ObjCPropertyDecl>(*I)) {
Manman Ren5b786402016-01-28 18:49:28 +0000196 // If queryKind is unknown, we return the instance property if one
197 // exists; otherwise we return the class property.
198 if ((queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
199 !PD->isClassProperty()) ||
200 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
201 PD->isClassProperty()) ||
202 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
203 !PD->isClassProperty()))
204 return PD;
205
206 if (PD->isClassProperty())
207 classProp = PD;
208 }
209
210 if (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
211 // We can't find the instance property, return the class property.
212 return classProp;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000213
Craig Topper36250ad2014-05-12 05:36:57 +0000214 return nullptr;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000215}
216
Anna Zaks454477c2012-09-27 19:45:11 +0000217IdentifierInfo *
218ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
219 SmallString<128> ivarName;
220 {
221 llvm::raw_svector_ostream os(ivarName);
222 os << '_' << getIdentifier()->getName();
223 }
224 return &Ctx.Idents.get(ivarName.str());
225}
226
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000227/// FindPropertyDeclaration - Finds declaration of the property given its name
228/// in 'PropertyId' and returns it. It returns 0, if not found.
Jordan Rose210bfe92014-11-19 22:03:46 +0000229ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
Manman Ren5b786402016-01-28 18:49:28 +0000230 const IdentifierInfo *PropertyId,
231 ObjCPropertyQueryKind QueryKind) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000232 // Don't find properties within hidden protocol definitions.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000233 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000234 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
235 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000236 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000237 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000238
239 // Search the extensions of a class first; they override what's in
240 // the class itself.
241 if (const auto *ClassDecl = dyn_cast<ObjCInterfaceDecl>(this)) {
242 for (const auto *Ext : ClassDecl->visible_extensions()) {
Manman Ren5b786402016-01-28 18:49:28 +0000243 if (auto *P = Ext->FindPropertyDeclaration(PropertyId, QueryKind))
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000244 return P;
245 }
246 }
Mike Stump11289f42009-09-09 15:08:12 +0000247
Ted Kremenekddcd1092010-03-15 20:11:53 +0000248 if (ObjCPropertyDecl *PD =
Manman Ren5b786402016-01-28 18:49:28 +0000249 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
250 QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000251 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000252
Ted Kremenekddcd1092010-03-15 20:11:53 +0000253 switch (getKind()) {
254 default:
255 break;
256 case Decl::ObjCProtocol: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000257 const auto *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000258 for (const auto *I : PID->protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000259 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
260 QueryKind))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000261 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000262 break;
263 }
264 case Decl::ObjCInterface: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000265 const auto *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000266 // Look through categories (but not extensions; they were handled above).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000267 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000268 if (!Cat->IsClassExtension())
Manman Ren5b786402016-01-28 18:49:28 +0000269 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(
270 PropertyId, QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000271 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000272 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000273
274 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000275 for (const auto *I : OID->all_referenced_protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000276 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
277 QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000278 return P;
279
280 // Finally, check the super class.
281 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
Manman Ren5b786402016-01-28 18:49:28 +0000282 return superClass->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekddcd1092010-03-15 20:11:53 +0000283 break;
284 }
285 case Decl::ObjCCategory: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000286 const auto *OCD = cast<ObjCCategoryDecl>(this);
Ted Kremenekddcd1092010-03-15 20:11:53 +0000287 // Look through protocols.
288 if (!OCD->IsClassExtension())
Aaron Ballman19a41762014-03-14 12:55:57 +0000289 for (const auto *I : OCD->protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000290 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
291 QueryKind))
Aaron Ballman19a41762014-03-14 12:55:57 +0000292 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000293 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000294 }
295 }
Craig Topper36250ad2014-05-12 05:36:57 +0000296 return nullptr;
Steve Narofff9c65242008-06-05 13:55:23 +0000297}
298
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +0000299void ObjCInterfaceDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000300
Douglas Gregor85f3f952015-07-07 03:57:15 +0000301ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
302 // If this particular declaration has a type parameter list, return it.
303 if (ObjCTypeParamList *written = getTypeParamListAsWritten())
304 return written;
305
306 // If there is a definition, return its type parameter list.
307 if (const ObjCInterfaceDecl *def = getDefinition())
308 return def->getTypeParamListAsWritten();
309
310 // Otherwise, look at previous declarations to determine whether any
311 // of them has a type parameter list, skipping over those
312 // declarations that do not.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000313 for (const ObjCInterfaceDecl *decl = getMostRecentDecl(); decl;
314 decl = decl->getPreviousDecl()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000315 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
316 return written;
317 }
318
319 return nullptr;
320}
321
Douglas Gregorab7f0b32015-07-07 06:20:12 +0000322void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
323 TypeParamList = TPL;
324 if (!TPL)
325 return;
326 // Set the declaration context of each of the type parameters.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000327 for (auto *typeParam : *TypeParamList)
Douglas Gregorab7f0b32015-07-07 06:20:12 +0000328 typeParam->setDeclContext(this);
329}
330
Douglas Gregore9d95f12015-07-07 03:57:35 +0000331ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
332 // FIXME: Should make sure no callers ever do this.
333 if (!hasDefinition())
334 return nullptr;
335
336 if (data().ExternallyCompleted)
337 LoadExternalDefinition();
338
339 if (const ObjCObjectType *superType = getSuperClassType()) {
340 if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
341 if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
342 return superDef;
343
344 return superDecl;
345 }
346 }
347
348 return nullptr;
349}
350
351SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
352 if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
353 return superTInfo->getTypeLoc().getLocStart();
354
355 return SourceLocation();
356}
357
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000358/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
359/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000360/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000361ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000362ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Manman Ren5b786402016-01-28 18:49:28 +0000363 IdentifierInfo *PropertyId,
364 ObjCPropertyQueryKind QueryKind) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000365 // FIXME: Should make sure no callers ever do this.
366 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000367 return nullptr;
368
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000369 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000370 LoadExternalDefinition();
371
Ted Kremenekd133a862010-03-15 20:30:07 +0000372 if (ObjCPropertyDecl *PD =
Manman Ren5b786402016-01-28 18:49:28 +0000373 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
374 QueryKind))
Ted Kremenekd133a862010-03-15 20:30:07 +0000375 return PD;
376
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000377 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000378 for (const auto *I : all_referenced_protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000379 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
380 QueryKind))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000381 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000382
Craig Topper36250ad2014-05-12 05:36:57 +0000383 return nullptr;
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000384}
385
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000386void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
387 PropertyDeclOrder &PO) const {
Manman Ren494ee5b2016-01-28 23:36:05 +0000388 for (auto *Prop : properties()) {
389 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000390 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000391 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000392 for (const auto *Ext : known_extensions()) {
393 const ObjCCategoryDecl *ClassExt = Ext;
Manman Ren494ee5b2016-01-28 23:36:05 +0000394 for (auto *Prop : ClassExt->properties()) {
395 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000396 PO.push_back(Prop);
397 }
398 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000399 for (const auto *PI : all_referenced_protocols())
400 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000401 // Note, the properties declared only in class extensions are still copied
402 // into the main @interface's property list, and therefore we don't
403 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000404}
405
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000406bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
407 const ObjCInterfaceDecl *Class = this;
408 while (Class) {
409 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
410 return true;
411 Class = Class->getSuperClass();
412 }
413 return false;
414}
415
416const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
417 const ObjCInterfaceDecl *Class = this;
418 while (Class) {
419 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
420 return Class;
421 Class = Class->getSuperClass();
422 }
Craig Topper36250ad2014-05-12 05:36:57 +0000423 return nullptr;
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000424}
425
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000426void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
427 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +0000428 ASTContext &C) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000429 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000430 LoadExternalDefinition();
431
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000432 if (data().AllReferencedProtocols.empty() &&
433 data().ReferencedProtocols.empty()) {
434 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000435 return;
436 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000437
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000438 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000439 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000440 // class or its extension are very few.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000441 SmallVector<ObjCProtocolDecl *, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000442 for (unsigned i = 0; i < ExtNum; i++) {
443 bool protocolExists = false;
444 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000445 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000446 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
447 protocolExists = true;
448 break;
449 }
450 }
451 // Do we want to warn on a protocol in extension class which
452 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000453 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000454 ProtocolRefs.push_back(ProtoInExtension);
455 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000456
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000457 if (ProtocolRefs.empty())
458 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000459
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000460 // Merge ProtocolRefs into class's protocol list;
Benjamin Kramerf9890422015-02-17 16:48:30 +0000461 ProtocolRefs.append(all_referenced_protocol_begin(),
462 all_referenced_protocol_end());
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000463
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000464 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000465}
466
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000467const ObjCInterfaceDecl *
468ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
469 const ObjCInterfaceDecl *IFace = this;
470 while (IFace) {
471 if (IFace->hasDesignatedInitializers())
472 return IFace;
473 if (!IFace->inheritsDesignatedInitializers())
474 break;
475 IFace = IFace->getSuperClass();
476 }
Craig Topper36250ad2014-05-12 05:36:57 +0000477 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000478}
479
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000480static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
481 for (const auto *MD : D->instance_methods()) {
482 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
483 return true;
484 }
485 for (const auto *Ext : D->visible_extensions()) {
486 for (const auto *MD : Ext->instance_methods()) {
487 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
488 return true;
489 }
490 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000491 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000492 for (const auto *MD : ImplD->instance_methods()) {
493 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
494 return true;
495 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000496 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000497 return false;
498}
499
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000500bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
501 switch (data().InheritedDesignatedInitializers) {
502 case DefinitionData::IDI_Inherited:
503 return true;
504 case DefinitionData::IDI_NotInherited:
505 return false;
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +0000506 case DefinitionData::IDI_Unknown:
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000507 // If the class introduced initializers we conservatively assume that we
508 // don't know if any of them is a designated initializer to avoid possible
509 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000510 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000511 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000512 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000513 if (auto SuperD = getSuperClass()) {
514 data().InheritedDesignatedInitializers =
515 SuperD->declaresOrInheritsDesignatedInitializers() ?
516 DefinitionData::IDI_Inherited :
517 DefinitionData::IDI_NotInherited;
518 } else {
519 data().InheritedDesignatedInitializers =
520 DefinitionData::IDI_NotInherited;
521 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000522 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000523 assert(data().InheritedDesignatedInitializers
524 != DefinitionData::IDI_Unknown);
525 return data().InheritedDesignatedInitializers ==
526 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000527 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000528
529 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
530}
531
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000532void ObjCInterfaceDecl::getDesignatedInitializers(
533 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000534 // Check for a complete definition and recover if not so.
535 if (!isThisDeclarationADefinition())
536 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000537 if (data().ExternallyCompleted)
538 LoadExternalDefinition();
539
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000540 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000541 if (!IFace)
542 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000543
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000544 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000545 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000546 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000547 for (const auto *Ext : IFace->visible_extensions()) {
548 for (const auto *MD : Ext->instance_methods())
549 if (MD->isThisDeclarationADesignatedInitializer())
550 Methods.push_back(MD);
551 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000552}
553
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000554bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
555 const ObjCMethodDecl **InitMethod) const {
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000556 bool HasCompleteDef = isThisDeclarationADefinition();
557 // During deserialization the data record for the ObjCInterfaceDecl could
558 // be made invariant by reusing the canonical decl. Take this into account
559 // when checking for the complete definition.
560 if (!HasCompleteDef && getCanonicalDecl()->hasDefinition() &&
561 getCanonicalDecl()->getDefinition() == getDefinition())
562 HasCompleteDef = true;
563
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000564 // Check for a complete definition and recover if not so.
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000565 if (!HasCompleteDef)
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000566 return false;
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000567
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000568 if (data().ExternallyCompleted)
569 LoadExternalDefinition();
570
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000571 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000572 if (!IFace)
573 return false;
574
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000575 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000576 if (MD->isThisDeclarationADesignatedInitializer()) {
577 if (InitMethod)
578 *InitMethod = MD;
579 return true;
580 }
581 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000582 for (const auto *Ext : IFace->visible_extensions()) {
583 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
584 if (MD->isThisDeclarationADesignatedInitializer()) {
585 if (InitMethod)
586 *InitMethod = MD;
587 return true;
588 }
589 }
590 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000591 return false;
592}
593
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000594void ObjCInterfaceDecl::allocateDefinitionData() {
595 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000596 Data.setPointer(new (getASTContext()) DefinitionData());
597 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000598
599 // Make the type point at the definition, now that we have one.
600 if (TypeForDecl)
601 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000602}
603
604void ObjCInterfaceDecl::startDefinition() {
605 allocateDefinitionData();
606
Douglas Gregor66b310c2011-12-15 18:03:09 +0000607 // Update all of the declarations with a pointer to the definition.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000608 for (auto *RD : redecls()) {
Aaron Ballman86c93902014-03-06 23:45:36 +0000609 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000610 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000611 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000612}
613
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000614ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
615 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000616 // FIXME: Should make sure no callers ever do this.
617 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000618 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000619
620 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000621 LoadExternalDefinition();
622
Chris Lattner89375192008-03-16 00:19:01 +0000623 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000624 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000625 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000626 clsDeclared = ClassDecl;
627 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000628 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000629
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000630 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000631 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000632 clsDeclared = ClassDecl;
633 return I;
634 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000635 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000636
Chris Lattner89375192008-03-16 00:19:01 +0000637 ClassDecl = ClassDecl->getSuperClass();
638 }
Craig Topper36250ad2014-05-12 05:36:57 +0000639 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000640}
641
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000642/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
643/// class whose name is passed as argument. If it is not one of the super classes
644/// the it returns NULL.
645ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
646 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000647 // FIXME: Should make sure no callers ever do this.
648 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000649 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000650
651 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000652 LoadExternalDefinition();
653
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000654 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000655 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000656 if (ClassDecl->getIdentifier() == ICName)
657 return ClassDecl;
658 ClassDecl = ClassDecl->getSuperClass();
659 }
Craig Topper36250ad2014-05-12 05:36:57 +0000660 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000661}
662
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000663ObjCProtocolDecl *
664ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000665 for (auto *P : all_referenced_protocols())
666 if (P->lookupProtocolNamed(Name))
667 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000668 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000669 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000670}
671
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000672/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000673/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000674/// When argument category "C" is specified, any implicit method found
675/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000676ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000677 bool isInstance,
678 bool shallowCategoryLookup,
679 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000680 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000681{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000682 // FIXME: Should make sure no callers ever do this.
683 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000684 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000685
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000686 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000687 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000688
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000689 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000690 LoadExternalDefinition();
691
Ted Kremenek00781502013-11-23 01:01:29 +0000692 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000693 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000694 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000695 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000696
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000697 // 2. Didn't find one yet - now look through categories.
698 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000699 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000700 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000701 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000702
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000703 // 3. Didn't find one yet - look through primary class's protocols.
704 for (const auto *I : ClassDecl->protocols())
705 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
706 return MethodDecl;
707
708 // 4. Didn't find one yet - now look through categories' protocols
709 if (!shallowCategoryLookup)
710 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000711 // Didn't find one yet - look through protocols.
712 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000713 Cat->getReferencedProtocols();
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000714 for (auto *Protocol : Protocols)
715 if ((MethodDecl = Protocol->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000716 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000717 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000718 }
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000719
720
Ted Kremenek00781502013-11-23 01:01:29 +0000721 if (!followSuper)
Craig Topper36250ad2014-05-12 05:36:57 +0000722 return nullptr;
Ted Kremenek00781502013-11-23 01:01:29 +0000723
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000724 // 5. Get to the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000725 ClassDecl = ClassDecl->getSuperClass();
726 }
Craig Topper36250ad2014-05-12 05:36:57 +0000727 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000728}
729
Anna Zaksc77a3b12012-07-27 19:07:44 +0000730// Will search "local" class/category implementations for a method decl.
731// If failed, then we search in class's root for an instance method.
732// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000733ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
734 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000735 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000736 // FIXME: Should make sure no callers ever do this.
737 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000738 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000739
740 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000741 LoadExternalDefinition();
742
Craig Topper36250ad2014-05-12 05:36:57 +0000743 ObjCMethodDecl *Method = nullptr;
Steve Naroffbb69c942009-10-01 23:46:04 +0000744 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000745 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
746 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000747
748 // Look through local category implementations associated with the class.
749 if (!Method)
Nico Weberf6098392015-03-02 01:12:28 +0000750 Method = getCategoryMethod(Sel, Instance);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000751
752 // Before we give up, check if the selector is an instance method.
753 // But only in the root. This matches gcc's behavior and what the
754 // runtime expects.
755 if (!Instance && !Method && !getSuperClass()) {
756 Method = lookupInstanceMethod(Sel);
757 // Look through local category implementations associated
758 // with the root class.
759 if (!Method)
760 Method = lookupPrivateMethod(Sel, true);
761 }
762
Steve Naroffbb69c942009-10-01 23:46:04 +0000763 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000764 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000765 return Method;
766}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000767
768//===----------------------------------------------------------------------===//
769// ObjCMethodDecl
770//===----------------------------------------------------------------------===//
771
Alp Toker314cc812014-01-25 16:55:45 +0000772ObjCMethodDecl *ObjCMethodDecl::Create(
773 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
774 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
775 DeclContext *contextDecl, bool isInstance, bool isVariadic,
776 bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
777 ImplementationControl impControl, bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000778 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000779 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Richard Smithf7981722013-11-22 09:01:48 +0000780 isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
781 impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000782}
783
Douglas Gregor72172e92012-01-05 21:55:30 +0000784ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000785 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000786 Selector(), QualType(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000787}
788
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000789bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
790 return getMethodFamily() == OMF_init &&
791 hasAttr<ObjCDesignatedInitializerAttr>();
792}
793
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000794bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
795 const ObjCMethodDecl **InitMethod) const {
796 if (getMethodFamily() != OMF_init)
797 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000798 const DeclContext *DC = getDeclContext();
799 if (isa<ObjCProtocolDecl>(DC))
800 return false;
801 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000802 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000803 return false;
804}
805
Douglas Gregora6017bb2012-10-09 17:21:28 +0000806Stmt *ObjCMethodDecl::getBody() const {
807 return Body.get(getASTContext().getExternalSource());
808}
809
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000810void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
811 assert(PrevMethod);
812 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
813 IsRedeclaration = true;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000814 PrevMethod->HasRedeclaration = true;
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000815}
816
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000817void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
818 ArrayRef<ParmVarDecl*> Params,
819 ArrayRef<SourceLocation> SelLocs) {
Craig Topper36250ad2014-05-12 05:36:57 +0000820 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000821 NumParams = Params.size();
822 if (Params.empty() && SelLocs.empty())
823 return;
824
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000825 static_assert(alignof(ParmVarDecl *) >= alignof(SourceLocation),
James Y Knight967eb202015-12-29 22:13:13 +0000826 "Alignment not sufficient for SourceLocation");
827
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000828 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
829 sizeof(SourceLocation) * SelLocs.size();
830 ParamsAndSelLocs = C.Allocate(Size);
831 std::copy(Params.begin(), Params.end(), getParams());
832 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
833}
834
835void ObjCMethodDecl::getSelectorLocs(
836 SmallVectorImpl<SourceLocation> &SelLocs) const {
837 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
838 SelLocs.push_back(getSelectorLoc(i));
839}
840
841void ObjCMethodDecl::setMethodParams(ASTContext &C,
842 ArrayRef<ParmVarDecl*> Params,
843 ArrayRef<SourceLocation> SelLocs) {
844 assert((!SelLocs.empty() || isImplicit()) &&
845 "No selector locs for non-implicit method");
846 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000847 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000848
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000849 SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
850 DeclEndLoc);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000851 if (SelLocsKind != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000852 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000853
854 setParamsAndSelLocs(C, Params, SelLocs);
855}
856
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000857/// \brief A definition will return its interface declaration.
858/// An interface declaration will return its definition.
859/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000860ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000861 ASTContext &Ctx = getASTContext();
Craig Topper36250ad2014-05-12 05:36:57 +0000862 ObjCMethodDecl *Redecl = nullptr;
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000863 if (HasRedeclaration)
864 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000865 if (Redecl)
866 return Redecl;
867
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000868 auto *CtxD = cast<Decl>(getDeclContext());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000869
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000870 if (!CtxD->isInvalidDecl()) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000871 if (auto *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000872 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
873 if (!ImplD->isInvalidDecl())
874 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000875
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000876 } else if (auto *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000877 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
878 if (!ImplD->isInvalidDecl())
879 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000880
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000881 } else if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000882 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
883 if (!IFD->isInvalidDecl())
884 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000885
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000886 } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000887 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
888 if (!CatD->isInvalidDecl())
889 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
890 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000891 }
892
Alex Lorenzff6c34b2016-11-21 11:16:30 +0000893 // Ensure that the discovered method redeclaration has a valid declaration
894 // context. Used to prevent infinite loops when iterating redeclarations in
895 // a partially invalid AST.
896 if (Redecl && cast<Decl>(Redecl->getDeclContext())->isInvalidDecl())
897 Redecl = nullptr;
898
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000899 if (!Redecl && isRedeclaration()) {
900 // This is the last redeclaration, go back to the first method.
901 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
902 isInstanceMethod());
903 }
904
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000905 return Redecl ? Redecl : this;
906}
907
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000908ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000909 auto *CtxD = cast<Decl>(getDeclContext());
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000910
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000911 if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000912 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
913 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
914 isInstanceMethod()))
915 return MD;
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000916 } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000917 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000918 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
919 isInstanceMethod()))
920 return MD;
921 }
922
Manman Rena0042c42016-10-03 21:26:46 +0000923 if (isRedeclaration()) {
924 // It is possible that we have not done deserializing the ObjCMethod yet.
925 ObjCMethodDecl *MD =
926 cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
927 isInstanceMethod());
928 return MD ? MD : this;
929 }
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000930
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000931 return this;
932}
933
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000934SourceLocation ObjCMethodDecl::getLocEnd() const {
935 if (Stmt *Body = getBody())
936 return Body->getLocEnd();
937 return DeclEndLoc;
938}
939
John McCallb4526252011-03-02 01:50:55 +0000940ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000941 auto family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000942 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000943 return family;
944
John McCall86bc21f2011-03-02 11:33:24 +0000945 // Check for an explicit attribute.
946 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
947 // The unfortunate necessity of mapping between enums here is due
948 // to the attributes framework.
949 switch (attr->getFamily()) {
950 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
951 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
952 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
953 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
954 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
955 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
956 }
957 Family = static_cast<unsigned>(family);
958 return family;
959 }
960
John McCallb4526252011-03-02 01:50:55 +0000961 family = getSelector().getMethodFamily();
962 switch (family) {
963 case OMF_None: break;
964
965 // init only has a conventional meaning for an instance method, and
966 // it has to return an object.
967 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000968 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000969 family = OMF_None;
970 break;
971
972 // alloc/copy/new have a conventional meaning for both class and
973 // instance methods, but they require an object return.
974 case OMF_alloc:
975 case OMF_copy:
976 case OMF_mutableCopy:
977 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000978 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000979 family = OMF_None;
980 break;
981
982 // These selectors have a conventional meaning only for instance methods.
983 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000984 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000985 case OMF_retain:
986 case OMF_release:
987 case OMF_autorelease:
988 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000989 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000990 if (!isInstanceMethod())
991 family = OMF_None;
992 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000993
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000994 case OMF_initialize:
995 if (isInstanceMethod() || !getReturnType()->isVoidType())
996 family = OMF_None;
997 break;
998
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000999 case OMF_performSelector:
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001000 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001001 family = OMF_None;
1002 else {
1003 unsigned noParams = param_size();
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001004 if (noParams < 1 || noParams > 3)
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001005 family = OMF_None;
1006 else {
Alp Toker1f307f42014-01-25 17:32:04 +00001007 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001008 QualType ArgT = (*it);
1009 if (!ArgT->isObjCSelType()) {
1010 family = OMF_None;
1011 break;
1012 }
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001013 while (--noParams) {
1014 it++;
1015 ArgT = (*it);
1016 if (!ArgT->isObjCIdType()) {
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001017 family = OMF_None;
1018 break;
1019 }
1020 }
1021 }
1022 }
1023 break;
1024
John McCallb4526252011-03-02 01:50:55 +00001025 }
1026
1027 // Cache the result.
1028 Family = static_cast<unsigned>(family);
1029 return family;
1030}
1031
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001032QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
1033 const ObjCInterfaceDecl *OID,
1034 bool &selfIsPseudoStrong,
1035 bool &selfIsConsumed) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001036 QualType selfTy;
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001037 selfIsPseudoStrong = false;
1038 selfIsConsumed = false;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001039 if (isInstanceMethod()) {
1040 // There may be no interface context due to error in declaration
1041 // of the interface (which has been reported). Recover gracefully.
1042 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +00001043 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001044 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001045 } else {
1046 selfTy = Context.getObjCIdType();
1047 }
1048 } else // we have a factory method.
1049 selfTy = Context.getObjCClassType();
1050
David Blaikiebbafb8a2012-03-11 07:00:24 +00001051 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001052 if (isInstanceMethod()) {
1053 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +00001054
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001055 // 'self' is always __strong. It's actually pseudo-strong except
1056 // in init methods (or methods labeled ns_consumes_self), though.
1057 Qualifiers qs;
1058 qs.setObjCLifetime(Qualifiers::OCL_Strong);
1059 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +00001060
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001061 // In addition, 'self' is const unless this is an init method.
1062 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
1063 selfTy = selfTy.withConst();
1064 selfIsPseudoStrong = true;
1065 }
1066 }
1067 else {
1068 assert(isClassMethod());
1069 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +00001070 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +00001071 selfIsPseudoStrong = true;
1072 }
John McCall31168b02011-06-15 23:02:42 +00001073 }
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001074 return selfTy;
1075}
John McCall31168b02011-06-15 23:02:42 +00001076
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001077void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
1078 const ObjCInterfaceDecl *OID) {
1079 bool selfIsPseudoStrong, selfIsConsumed;
1080 QualType selfTy =
1081 getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
Alexey Bataev56223232017-06-09 13:40:18 +00001082 auto *Self = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1083 &Context.Idents.get("self"), selfTy,
1084 ImplicitParamDecl::ObjCSelf);
1085 setSelfDecl(Self);
John McCall31168b02011-06-15 23:02:42 +00001086
1087 if (selfIsConsumed)
Alexey Bataev56223232017-06-09 13:40:18 +00001088 Self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001089
John McCalld4631322011-06-17 06:42:21 +00001090 if (selfIsPseudoStrong)
Alexey Bataev56223232017-06-09 13:40:18 +00001091 Self->setARCPseudoStrong(true);
John McCalld4631322011-06-17 06:42:21 +00001092
Alexey Bataev56223232017-06-09 13:40:18 +00001093 setCmdDecl(ImplicitParamDecl::Create(
1094 Context, this, SourceLocation(), &Context.Idents.get("_cmd"),
1095 Context.getObjCSelType(), ImplicitParamDecl::ObjCCmd));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001096}
1097
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001098ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001099 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001100 return ID;
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001101 if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001102 return CD->getClassInterface();
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001103 if (auto *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001104 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +00001105 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +00001106 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +00001107 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001108}
1109
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001110SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1111 const auto *TSI = getReturnTypeSourceInfo();
1112 if (TSI)
1113 return TSI->getTypeLoc().getSourceRange();
1114 return SourceRange();
1115}
1116
Douglas Gregor9b7b3e92015-07-07 06:20:27 +00001117QualType ObjCMethodDecl::getSendResultType() const {
1118 ASTContext &Ctx = getASTContext();
1119 return getReturnType().getNonLValueExprType(Ctx)
1120 .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1121}
1122
Douglas Gregore83b9562015-07-07 03:57:53 +00001123QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1124 // FIXME: Handle related result types here.
1125
1126 return getReturnType().getNonLValueExprType(getASTContext())
1127 .substObjCMemberType(receiverType, getDeclContext(),
1128 ObjCSubstitutionContext::Result);
1129}
1130
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001131static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1132 const ObjCMethodDecl *Method,
1133 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1134 bool MovedToSuper) {
1135 if (!Container)
1136 return;
1137
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001138 // In categories look for overridden methods from protocols. A method from
1139 // category is not "overridden" since it is considered as the "same" method
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001140 // (same USR) as the one from the interface.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001141 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001142 // Check whether we have a matching method at this category but only if we
1143 // are at the super class level.
1144 if (MovedToSuper)
1145 if (ObjCMethodDecl *
1146 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001147 Method->isInstanceMethod(),
1148 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001149 if (Method != Overridden) {
1150 // We found an override at this category; there is no need to look
1151 // into its protocols.
1152 Methods.push_back(Overridden);
1153 return;
1154 }
1155
Aaron Ballman19a41762014-03-14 12:55:57 +00001156 for (const auto *P : Category->protocols())
1157 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001158 return;
1159 }
1160
1161 // Check whether we have a matching method at this level.
1162 if (const ObjCMethodDecl *
1163 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001164 Method->isInstanceMethod(),
1165 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001166 if (Method != Overridden) {
1167 // We found an override at this level; there is no need to look
1168 // into other protocols or categories.
1169 Methods.push_back(Overridden);
1170 return;
1171 }
1172
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001173 if (const auto *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001174 for (const auto *P : Protocol->protocols())
1175 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001176 }
1177
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001178 if (const auto *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001179 for (const auto *P : Interface->protocols())
1180 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001181
Aaron Ballman15063e12014-03-13 21:35:02 +00001182 for (const auto *Cat : Interface->known_categories())
1183 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001184
1185 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1186 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1187 /*MovedToSuper=*/true);
1188 }
1189}
1190
1191static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1192 const ObjCMethodDecl *Method,
1193 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1194 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1195 /*MovedToSuper=*/false);
1196}
1197
1198static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1199 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1200 assert(Method->isOverriding());
1201
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001202 if (const auto *ProtD =
1203 dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001204 CollectOverriddenMethods(ProtD, Method, overridden);
1205
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001206 } else if (const auto *IMD =
1207 dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001208 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1209 if (!ID)
1210 return;
1211 // Start searching for overridden methods using the method from the
1212 // interface as starting point.
1213 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001214 Method->isInstanceMethod(),
1215 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001216 Method = IFaceMeth;
1217 CollectOverriddenMethods(ID, Method, overridden);
1218
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001219 } else if (const auto *CatD =
1220 dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001221 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1222 if (!ID)
1223 return;
1224 // Start searching for overridden methods using the method from the
1225 // interface as starting point.
1226 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001227 Method->isInstanceMethod(),
1228 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001229 Method = IFaceMeth;
1230 CollectOverriddenMethods(ID, Method, overridden);
1231
1232 } else {
1233 CollectOverriddenMethods(
1234 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1235 Method, overridden);
1236 }
1237}
1238
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001239void ObjCMethodDecl::getOverriddenMethods(
1240 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1241 const ObjCMethodDecl *Method = this;
1242
1243 if (Method->isRedeclaration()) {
1244 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1245 getMethod(Method->getSelector(), Method->isInstanceMethod());
1246 }
1247
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001248 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001249 collectOverriddenMethodsSlow(Method, Overridden);
1250 assert(!Overridden.empty() &&
1251 "ObjCMethodDecl's overriding bit is not as expected");
1252 }
1253}
1254
Jordan Rose2bd991a2012-10-10 16:42:54 +00001255const ObjCPropertyDecl *
1256ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1257 Selector Sel = getSelector();
1258 unsigned NumArgs = Sel.getNumArgs();
1259 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001260 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001261
Jordan Rose2bd991a2012-10-10 16:42:54 +00001262 if (isPropertyAccessor()) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001263 const auto *Container = cast<ObjCContainerDecl>(getParent());
Jordan Rose2bd991a2012-10-10 16:42:54 +00001264 bool IsGetter = (NumArgs == 0);
Jordan Rose31cf7d42016-03-11 21:14:40 +00001265 bool IsInstance = isInstanceMethod();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001266
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001267 /// Local function that attempts to find a matching property within the
1268 /// given Objective-C container.
1269 auto findMatchingProperty =
1270 [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * {
Jordan Rose31cf7d42016-03-11 21:14:40 +00001271 if (IsInstance) {
1272 for (const auto *I : Container->instance_properties()) {
1273 Selector NextSel = IsGetter ? I->getGetterName()
1274 : I->getSetterName();
1275 if (NextSel == Sel)
1276 return I;
1277 }
1278 } else {
1279 for (const auto *I : Container->class_properties()) {
1280 Selector NextSel = IsGetter ? I->getGetterName()
1281 : I->getSetterName();
1282 if (NextSel == Sel)
1283 return I;
1284 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001285 }
1286
1287 return nullptr;
1288 };
1289
1290 // Look in the container we were given.
1291 if (const auto *Found = findMatchingProperty(Container))
1292 return Found;
1293
1294 // If we're in a category or extension, look in the main class.
1295 const ObjCInterfaceDecl *ClassDecl = nullptr;
1296 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1297 ClassDecl = Category->getClassInterface();
1298 if (const auto *Found = findMatchingProperty(ClassDecl))
1299 return Found;
1300 } else {
1301 // Determine whether the container is a class.
1302 ClassDecl = dyn_cast<ObjCInterfaceDecl>(Container);
1303 }
1304
1305 // If we have a class, check its visible extensions.
1306 if (ClassDecl) {
1307 for (const auto *Ext : ClassDecl->visible_extensions()) {
1308 if (Ext == Container)
1309 continue;
1310
1311 if (const auto *Found = findMatchingProperty(Ext))
1312 return Found;
1313 }
Jordan Rose2bd991a2012-10-10 16:42:54 +00001314 }
1315
1316 llvm_unreachable("Marked as a property accessor but no property found!");
1317 }
1318
1319 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001320 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001321
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001322 using OverridesTy = SmallVector<const ObjCMethodDecl *, 8>;
1323
Jordan Rose2bd991a2012-10-10 16:42:54 +00001324 OverridesTy Overrides;
1325 getOverriddenMethods(Overrides);
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001326 for (const auto *Override : Overrides)
1327 if (const ObjCPropertyDecl *Prop = Override->findPropertyDecl(false))
Jordan Rose2bd991a2012-10-10 16:42:54 +00001328 return Prop;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001329
Craig Topper36250ad2014-05-12 05:36:57 +00001330 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001331}
1332
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001333//===----------------------------------------------------------------------===//
Douglas Gregor85f3f952015-07-07 03:57:15 +00001334// ObjCTypeParamDecl
1335//===----------------------------------------------------------------------===//
1336
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001337void ObjCTypeParamDecl::anchor() {}
Douglas Gregor85f3f952015-07-07 03:57:15 +00001338
1339ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001340 ObjCTypeParamVariance variance,
1341 SourceLocation varianceLoc,
Douglas Gregore83b9562015-07-07 03:57:53 +00001342 unsigned index,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001343 SourceLocation nameLoc,
1344 IdentifierInfo *name,
1345 SourceLocation colonLoc,
1346 TypeSourceInfo *boundInfo) {
Manman Renc5705ba2016-09-13 17:41:05 +00001347 auto *TPDecl =
1348 new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1349 nameLoc, name, colonLoc, boundInfo);
1350 QualType TPType = ctx.getObjCTypeParamType(TPDecl, {});
1351 TPDecl->setTypeForDecl(TPType.getTypePtr());
1352 return TPDecl;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001353}
1354
1355ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1356 unsigned ID) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001357 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1358 ObjCTypeParamVariance::Invariant,
1359 SourceLocation(), 0, SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001360 nullptr, SourceLocation(), nullptr);
1361}
1362
1363SourceRange ObjCTypeParamDecl::getSourceRange() const {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001364 SourceLocation startLoc = VarianceLoc;
1365 if (startLoc.isInvalid())
1366 startLoc = getLocation();
1367
Douglas Gregor85f3f952015-07-07 03:57:15 +00001368 if (hasExplicitBound()) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001369 return SourceRange(startLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001370 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1371 }
1372
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001373 return SourceRange(startLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001374}
1375
1376//===----------------------------------------------------------------------===//
1377// ObjCTypeParamList
1378//===----------------------------------------------------------------------===//
1379ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1380 ArrayRef<ObjCTypeParamDecl *> typeParams,
1381 SourceLocation rAngleLoc)
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001382 : NumParams(typeParams.size()) {
Douglas Gregor0a8890ff2015-07-07 06:20:46 +00001383 Brackets.Begin = lAngleLoc.getRawEncoding();
1384 Brackets.End = rAngleLoc.getRawEncoding();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001385 std::copy(typeParams.begin(), typeParams.end(), begin());
1386}
1387
Douglas Gregor85f3f952015-07-07 03:57:15 +00001388ObjCTypeParamList *ObjCTypeParamList::create(
1389 ASTContext &ctx,
1390 SourceLocation lAngleLoc,
1391 ArrayRef<ObjCTypeParamDecl *> typeParams,
1392 SourceLocation rAngleLoc) {
James Y Knight967eb202015-12-29 22:13:13 +00001393 void *mem =
1394 ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001395 alignof(ObjCTypeParamList));
Douglas Gregor85f3f952015-07-07 03:57:15 +00001396 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1397}
1398
Douglas Gregore83b9562015-07-07 03:57:53 +00001399void ObjCTypeParamList::gatherDefaultTypeArgs(
1400 SmallVectorImpl<QualType> &typeArgs) const {
1401 typeArgs.reserve(size());
1402 for (auto typeParam : *this)
1403 typeArgs.push_back(typeParam->getUnderlyingType());
1404}
1405
Douglas Gregor85f3f952015-07-07 03:57:15 +00001406//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001407// ObjCInterfaceDecl
1408//===----------------------------------------------------------------------===//
1409
Douglas Gregord53ae832012-01-17 18:09:05 +00001410ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001411 DeclContext *DC,
1412 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001413 IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001414 ObjCTypeParamList *typeParamList,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001415 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001416 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001417 bool isInternal){
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001418 auto *Result = new (C, DC)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001419 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1420 isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001421 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001422 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001423 return Result;
1424}
1425
Richard Smith053f6c62014-05-16 23:01:30 +00001426ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001427 unsigned ID) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001428 auto *Result = new (C, ID)
1429 ObjCInterfaceDecl(C, nullptr, SourceLocation(), nullptr, nullptr,
1430 SourceLocation(), nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001431 Result->Data.setInt(!C.getLangOpts().Modules);
1432 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001433}
1434
Richard Smith053f6c62014-05-16 23:01:30 +00001435ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1436 SourceLocation AtLoc, IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001437 ObjCTypeParamList *typeParamList,
Richard Smith053f6c62014-05-16 23:01:30 +00001438 SourceLocation CLoc,
1439 ObjCInterfaceDecl *PrevDecl,
1440 bool IsInternal)
1441 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001442 redeclarable_base(C) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001443 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001444
1445 // Copy the 'data' pointer over.
1446 if (PrevDecl)
1447 Data = PrevDecl->Data;
1448
Richard Smith053f6c62014-05-16 23:01:30 +00001449 setImplicit(IsInternal);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001450
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001451 setTypeParamList(typeParamList);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001452}
1453
Douglas Gregor73693022010-12-01 23:49:52 +00001454void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001455 assert(data().ExternallyCompleted && "Class is not externally completed");
1456 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001457 getASTContext().getExternalSource()->CompleteType(
1458 const_cast<ObjCInterfaceDecl *>(this));
1459}
1460
1461void ObjCInterfaceDecl::setExternallyCompleted() {
1462 assert(getASTContext().getExternalSource() &&
1463 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001464 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001465 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001466 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001467}
1468
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001469void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001470 // Check for a complete definition and recover if not so.
1471 if (!isThisDeclarationADefinition())
1472 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001473 data().HasDesignatedInitializers = true;
1474}
1475
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001476bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001477 // Check for a complete definition and recover if not so.
1478 if (!isThisDeclarationADefinition())
1479 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001480 if (data().ExternallyCompleted)
1481 LoadExternalDefinition();
1482
1483 return data().HasDesignatedInitializers;
1484}
1485
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001486StringRef
1487ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001488 if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001489 return ObjCRTName->getMetadataName();
1490
1491 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001492}
1493
1494StringRef
1495ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001496 if (ObjCInterfaceDecl *ID =
1497 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1498 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001499
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001500 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001501}
1502
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001503ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001504 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1505 if (data().ExternallyCompleted)
1506 LoadExternalDefinition();
1507
1508 return getASTContext().getObjCImplementation(
1509 const_cast<ObjCInterfaceDecl*>(Def));
1510 }
1511
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001512 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001513 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001514}
1515
1516void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001517 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001518}
1519
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001520namespace {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001521
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001522struct SynthesizeIvarChunk {
1523 uint64_t Size;
1524 ObjCIvarDecl *Ivar;
1525
1526 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1527 : Size(size), Ivar(ivar) {}
1528};
1529
1530bool operator<(const SynthesizeIvarChunk & LHS,
1531 const SynthesizeIvarChunk &RHS) {
1532 return LHS.Size < RHS.Size;
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001533}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001534
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001535} // namespace
1536
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001537/// all_declared_ivar_begin - return first ivar declared in this class,
1538/// its extensions and its implementation. Lazily build the list on first
1539/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001540///
1541/// Caveat: The list returned by this method reflects the current
1542/// state of the parser. The cache will be updated for every ivar
1543/// added by an extension or the implementation when they are
1544/// encountered.
1545/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001546ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001547 // FIXME: Should make sure no callers ever do this.
1548 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001549 return nullptr;
1550
1551 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001552 if (!data().IvarList) {
1553 if (!ivar_empty()) {
1554 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1555 data().IvarList = *I; ++I;
1556 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001557 curIvar->setNextIvar(*I);
1558 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001559
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001560 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001561 if (!Ext->ivar_empty()) {
1562 ObjCCategoryDecl::ivar_iterator
1563 I = Ext->ivar_begin(),
1564 E = Ext->ivar_end();
1565 if (!data().IvarList) {
1566 data().IvarList = *I; ++I;
1567 curIvar = data().IvarList;
1568 }
1569 for ( ;I != E; curIvar = *I, ++I)
1570 curIvar->setNextIvar(*I);
1571 }
1572 }
1573 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001574 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001575
1576 // cached and complete!
1577 if (!data().IvarListMissingImplementation)
1578 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001579
1580 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001581 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001582 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001583 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001584 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001585 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1586 layout.push_back(SynthesizeIvarChunk(
1587 IV->getASTContext().getTypeSize(IV->getType()), IV));
1588 continue;
1589 }
1590 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001591 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001592 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001593 curIvar->setNextIvar(IV);
1594 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001595 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001596
1597 if (!layout.empty()) {
1598 // Order synthesized ivars by their size.
1599 std::stable_sort(layout.begin(), layout.end());
1600 unsigned Ix = 0, EIx = layout.size();
1601 if (!data().IvarList) {
1602 data().IvarList = layout[0].Ivar; Ix++;
1603 curIvar = data().IvarList;
1604 }
1605 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1606 curIvar->setNextIvar(layout[Ix].Ivar);
1607 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001608 }
1609 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001610 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001611}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001612
1613/// FindCategoryDeclaration - Finds category declaration in the list of
1614/// categories for this class and returns it. Name of the category is passed
1615/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001616///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001617ObjCCategoryDecl *
1618ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001619 // FIXME: Should make sure no callers ever do this.
1620 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001621 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001622
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001623 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001624 LoadExternalDefinition();
1625
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001626 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001627 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001628 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001629
1630 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001631}
1632
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001633ObjCMethodDecl *
1634ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001635 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001636 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001637 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1638 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001639 }
1640
Craig Topper36250ad2014-05-12 05:36:57 +00001641 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001642}
1643
1644ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001645 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001646 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001647 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1648 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001649 }
Craig Topper36250ad2014-05-12 05:36:57 +00001650
1651 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001652}
1653
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001654/// ClassImplementsProtocol - Checks that 'lProto' protocol
1655/// has been implemented in IDecl class, its super class or categories (if
1656/// lookupCategory is true).
1657bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1658 bool lookupCategory,
1659 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001660 if (!hasDefinition())
1661 return false;
1662
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001663 ObjCInterfaceDecl *IDecl = this;
1664 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001665 for (auto *PI : IDecl->protocols()){
1666 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001667 return true;
1668 // This is dubious and is added to be compatible with gcc. In gcc, it is
1669 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1670 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1671 // object. This IMO, should be a bug.
1672 // FIXME: Treat this as an extension, and flag this as an error when GCC
1673 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001674 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001675 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001676 return true;
1677 }
Mike Stump11289f42009-09-09 15:08:12 +00001678
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001679 // 2nd, look up the category.
1680 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001681 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001682 for (auto *PI : Cat->protocols())
1683 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001684 return true;
1685 }
Mike Stump11289f42009-09-09 15:08:12 +00001686
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001687 // 3rd, look up the super class(s)
1688 if (IDecl->getSuperClass())
1689 return
1690 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1691 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001692
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001693 return false;
1694}
1695
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001696//===----------------------------------------------------------------------===//
1697// ObjCIvarDecl
1698//===----------------------------------------------------------------------===//
1699
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001700void ObjCIvarDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001701
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001702ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001703 SourceLocation StartLoc,
1704 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001705 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001706 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001707 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001708 if (DC) {
1709 // Ivar's can only appear in interfaces, implementations (via synthesized
1710 // properties), and class extensions (via direct declaration, or synthesized
1711 // properties).
1712 //
1713 // FIXME: This should really be asserting this:
1714 // (isa<ObjCCategoryDecl>(DC) &&
1715 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1716 // but unfortunately we sometimes place ivars into non-class extension
1717 // categories on error. This breaks an AST invariant, and should not be
1718 // fixed.
1719 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1720 isa<ObjCCategoryDecl>(DC)) &&
1721 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001722 // Once a new ivar is created in any of class/class-extension/implementation
1723 // decl contexts, the previously built IvarList must be rebuilt.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001724 auto *ID = dyn_cast<ObjCInterfaceDecl>(DC);
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001725 if (!ID) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001726 if (auto *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001727 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001728 else
1729 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001730 }
Craig Topper36250ad2014-05-12 05:36:57 +00001731 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001732 }
1733
Richard Smithf7981722013-11-22 09:01:48 +00001734 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001735 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001736}
1737
Douglas Gregor72172e92012-01-05 21:55:30 +00001738ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001739 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1740 nullptr, QualType(), nullptr,
1741 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001742}
1743
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001744const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001745 const auto *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001746
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001747 switch (DC->getKind()) {
1748 default:
1749 case ObjCCategoryImpl:
1750 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001751 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001752
1753 // Ivars can only appear in class extension categories.
1754 case ObjCCategory: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001755 const auto *CD = cast<ObjCCategoryDecl>(DC);
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001756 assert(CD->IsClassExtension() && "invalid container for ivar!");
1757 return CD->getClassInterface();
1758 }
1759
1760 case ObjCImplementation:
1761 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1762
1763 case ObjCInterface:
1764 return cast<ObjCInterfaceDecl>(DC);
1765 }
1766}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001767
Douglas Gregore83b9562015-07-07 03:57:53 +00001768QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1769 return getType().substObjCMemberType(objectType, getDeclContext(),
1770 ObjCSubstitutionContext::Property);
1771}
1772
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001773//===----------------------------------------------------------------------===//
1774// ObjCAtDefsFieldDecl
1775//===----------------------------------------------------------------------===//
1776
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001777void ObjCAtDefsFieldDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001778
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001779ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001780*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1781 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001782 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001783 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001784}
1785
Richard Smithf7981722013-11-22 09:01:48 +00001786ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001787 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001788 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1789 SourceLocation(), nullptr, QualType(),
1790 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001791}
1792
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001793//===----------------------------------------------------------------------===//
1794// ObjCProtocolDecl
1795//===----------------------------------------------------------------------===//
1796
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001797void ObjCProtocolDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001798
Richard Smith053f6c62014-05-16 23:01:30 +00001799ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1800 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001801 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001802 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001803 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001804 redeclarable_base(C) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001805 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001806 if (PrevDecl)
1807 Data = PrevDecl->Data;
1808}
1809
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001810ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001811 IdentifierInfo *Id,
1812 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001813 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001814 ObjCProtocolDecl *PrevDecl) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001815 auto *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001816 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001817 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001818 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001819}
1820
Richard Smithf7981722013-11-22 09:01:48 +00001821ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001822 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001823 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001824 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001825 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001826 Result->Data.setInt(!C.getLangOpts().Modules);
1827 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001828}
1829
Steve Naroff114aecb2009-03-01 16:12:44 +00001830ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1831 ObjCProtocolDecl *PDecl = this;
1832
1833 if (Name == getIdentifier())
1834 return PDecl;
1835
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001836 for (auto *I : protocols())
1837 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001838 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001839
Craig Topper36250ad2014-05-12 05:36:57 +00001840 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001841}
1842
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001843// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001844// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001845ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1846 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001847 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001848
Douglas Gregoreed49792013-01-17 00:38:46 +00001849 // If there is no definition or the definition is hidden, we don't find
1850 // anything.
1851 const ObjCProtocolDecl *Def = getDefinition();
1852 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001853 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001854
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001855 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001856 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001857
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001858 for (const auto *I : protocols())
1859 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001860 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001861 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001862}
1863
Douglas Gregore6e48b12012-01-01 19:29:29 +00001864void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001865 assert(!Data.getPointer() && "Protocol already has a definition!");
1866 Data.setPointer(new (getASTContext()) DefinitionData);
1867 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001868}
1869
1870void ObjCProtocolDecl::startDefinition() {
1871 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001872
1873 // Update all of the declarations with a pointer to the definition.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001874 for (auto *RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001875 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001876}
1877
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001878void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1879 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001880 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001881 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001882 // Insert into PM if not there already.
Manman Ren494ee5b2016-01-28 23:36:05 +00001883 PM.insert(std::make_pair(
1884 std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
1885 Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001886 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001887 }
1888 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001889 for (const auto *PI : PDecl->protocols())
1890 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001891 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001892}
1893
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001894void ObjCProtocolDecl::collectInheritedProtocolProperties(
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001895 const ObjCPropertyDecl *Property, ProtocolPropertySet &PS,
1896 PropertyDeclOrder &PO) const {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001897 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001898 if (!PS.insert(PDecl).second)
1899 return;
Manman Renefe1bac2016-01-27 20:00:32 +00001900 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001901 if (Prop == Property)
1902 continue;
1903 if (Prop->getIdentifier() == Property->getIdentifier()) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001904 PO.push_back(Prop);
1905 return;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001906 }
1907 }
1908 // Scan through protocol's protocols which did not have a matching property.
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001909 for (const auto *PI : PDecl->protocols())
1910 PI->collectInheritedProtocolProperties(Property, PS, PO);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001911 }
1912}
Anna Zaks673d76b2012-10-18 19:17:53 +00001913
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001914StringRef
1915ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001916 if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001917 return ObjCRTName->getMetadataName();
1918
1919 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001920}
1921
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001922//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001923// ObjCCategoryDecl
1924//===----------------------------------------------------------------------===//
1925
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001926void ObjCCategoryDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001927
Douglas Gregor85f3f952015-07-07 03:57:15 +00001928ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1929 SourceLocation ClassNameLoc,
1930 SourceLocation CategoryNameLoc,
1931 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1932 ObjCTypeParamList *typeParamList,
1933 SourceLocation IvarLBraceLoc,
1934 SourceLocation IvarRBraceLoc)
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001935 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1936 ClassInterface(IDecl), CategoryNameLoc(CategoryNameLoc),
1937 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001938 setTypeParamList(typeParamList);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001939}
1940
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001941ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001942 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001943 SourceLocation ClassNameLoc,
1944 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001945 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001946 ObjCInterfaceDecl *IDecl,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001947 ObjCTypeParamList *typeParamList,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001948 SourceLocation IvarLBraceLoc,
1949 SourceLocation IvarRBraceLoc) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001950 auto *CatDecl =
Richard Smithf7981722013-11-22 09:01:48 +00001951 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001952 IDecl, typeParamList, IvarLBraceLoc,
1953 IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001954 if (IDecl) {
1955 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001956 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001957 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001958 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001959 if (ASTMutationListener *L = C.getASTMutationListener())
1960 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1961 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001962 }
1963
1964 return CatDecl;
1965}
1966
Richard Smithf7981722013-11-22 09:01:48 +00001967ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001968 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001969 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1970 SourceLocation(), SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001971 nullptr, nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001972}
1973
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001974ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1975 return getASTContext().getObjCImplementation(
1976 const_cast<ObjCCategoryDecl*>(this));
1977}
1978
1979void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1980 getASTContext().setObjCImplementation(this, ImplD);
1981}
1982
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001983void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
1984 TypeParamList = TPL;
1985 if (!TPL)
1986 return;
1987 // Set the declaration context of each of the type parameters.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001988 for (auto *typeParam : *TypeParamList)
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001989 typeParam->setDeclContext(this);
1990}
1991
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001992//===----------------------------------------------------------------------===//
1993// ObjCCategoryImplDecl
1994//===----------------------------------------------------------------------===//
1995
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001996void ObjCCategoryImplDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001997
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001998ObjCCategoryImplDecl *
1999ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002000 IdentifierInfo *Id,
2001 ObjCInterfaceDecl *ClassInterface,
2002 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00002003 SourceLocation atStartLoc,
2004 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00002005 if (ClassInterface && ClassInterface->hasDefinition())
2006 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00002007 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
2008 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002009}
2010
Douglas Gregor72172e92012-01-05 21:55:30 +00002011ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
2012 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002013 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
2014 SourceLocation(), SourceLocation(),
2015 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002016}
2017
Steve Narofff406f4d2009-10-29 21:11:04 +00002018ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00002019 // The class interface might be NULL if we are working with invalid code.
2020 if (const ObjCInterfaceDecl *ID = getClassInterface())
2021 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00002022 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00002023}
2024
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002025void ObjCImplDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002026
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002027void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00002028 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002029 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002030 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002031}
2032
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002033void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
2034 ASTContext &Ctx = getASTContext();
2035
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002036 if (auto *ImplD = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002037 if (IFace)
2038 Ctx.setObjCImplementation(IFace, ImplD);
2039
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002040 } else if (auto *ImplD = dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002041 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
2042 Ctx.setObjCImplementation(CD, ImplD);
2043 }
2044
2045 ClassInterface = IFace;
2046}
2047
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002048/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00002049/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00002050/// the implemented property that uses it.
Chris Lattnera9ca0522009-02-28 18:42:10 +00002051ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002052FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00002053 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002054 if (PID->getPropertyIvarDecl() &&
2055 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
2056 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00002057 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002058}
2059
2060/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00002061/// added to the list of those properties \@synthesized/\@dynamic in this
2062/// category \@implementation block.
Chris Lattnera9ca0522009-02-28 18:42:10 +00002063ObjCPropertyImplDecl *ObjCImplDecl::
Manman Ren5b786402016-01-28 18:49:28 +00002064FindPropertyImplDecl(IdentifierInfo *Id,
2065 ObjCPropertyQueryKind QueryKind) const {
2066 ObjCPropertyImplDecl *ClassPropImpl = nullptr;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002067 for (auto *PID : property_impls())
Manman Ren5b786402016-01-28 18:49:28 +00002068 // If queryKind is unknown, we return the instance property if one
2069 // exists; otherwise we return the class property.
2070 if (PID->getPropertyDecl()->getIdentifier() == Id) {
2071 if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
2072 !PID->getPropertyDecl()->isClassProperty()) ||
2073 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
2074 PID->getPropertyDecl()->isClassProperty()) ||
2075 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
2076 !PID->getPropertyDecl()->isClassProperty()))
2077 return PID;
2078
2079 if (PID->getPropertyDecl()->isClassProperty())
2080 ClassPropImpl = PID;
2081 }
2082
2083 if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
2084 // We can't find the instance property, return the class property.
2085 return ClassPropImpl;
2086
Craig Topper36250ad2014-05-12 05:36:57 +00002087 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002088}
2089
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002090raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002091 const ObjCCategoryImplDecl &CID) {
2092 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002093 return OS;
2094}
2095
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002096//===----------------------------------------------------------------------===//
2097// ObjCImplementationDecl
2098//===----------------------------------------------------------------------===//
2099
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002100void ObjCImplementationDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002101
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002102ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00002103ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002104 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002105 ObjCInterfaceDecl *SuperDecl,
2106 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002107 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00002108 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002109 SourceLocation IvarLBraceLoc,
2110 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00002111 if (ClassInterface && ClassInterface->hasDefinition())
2112 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00002113 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
2114 nameLoc, atStartLoc, superLoc,
2115 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002116}
2117
Douglas Gregor72172e92012-01-05 21:55:30 +00002118ObjCImplementationDecl *
2119ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002120 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
2121 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002122}
2123
John McCall0410e572011-07-22 04:15:06 +00002124void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2125 CXXCtorInitializer ** initializers,
2126 unsigned numInitializers) {
2127 if (numInitializers > 0) {
2128 NumIvarInitializers = numInitializers;
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002129 auto **ivarInitializers = new (C) CXXCtorInitializer*[NumIvarInitializers];
John McCall0410e572011-07-22 04:15:06 +00002130 memcpy(ivarInitializers, initializers,
2131 numInitializers * sizeof(CXXCtorInitializer*));
2132 IvarInitializers = ivarInitializers;
2133 }
2134}
2135
Richard Smithc2bb8182015-03-24 06:36:48 +00002136ObjCImplementationDecl::init_const_iterator
2137ObjCImplementationDecl::init_begin() const {
2138 return IvarInitializers.get(getASTContext().getExternalSource());
2139}
2140
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002141raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002142 const ObjCImplementationDecl &ID) {
2143 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002144 return OS;
2145}
2146
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002147//===----------------------------------------------------------------------===//
2148// ObjCCompatibleAliasDecl
2149//===----------------------------------------------------------------------===//
2150
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002151void ObjCCompatibleAliasDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002152
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002153ObjCCompatibleAliasDecl *
2154ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2155 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00002156 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002157 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00002158 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002159}
2160
Douglas Gregor72172e92012-01-05 21:55:30 +00002161ObjCCompatibleAliasDecl *
2162ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002163 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2164 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002165}
2166
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002167//===----------------------------------------------------------------------===//
2168// ObjCPropertyDecl
2169//===----------------------------------------------------------------------===//
2170
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002171void ObjCPropertyDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002172
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002173ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2174 SourceLocation L,
2175 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00002176 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002177 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00002178 QualType T,
2179 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002180 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002181 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2182 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002183}
2184
Richard Smithf7981722013-11-22 09:01:48 +00002185ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002186 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002187 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2188 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00002189 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00002190}
2191
Douglas Gregore83b9562015-07-07 03:57:53 +00002192QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2193 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2194 ObjCSubstitutionContext::Property);
2195}
2196
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002197//===----------------------------------------------------------------------===//
2198// ObjCPropertyImplDecl
2199//===----------------------------------------------------------------------===//
2200
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002201ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002202 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002203 SourceLocation atLoc,
2204 SourceLocation L,
2205 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002206 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002207 ObjCIvarDecl *ivar,
2208 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002209 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2210 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002211}
Chris Lattnered0e1642008-03-17 01:19:02 +00002212
Richard Smithf7981722013-11-22 09:01:48 +00002213ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002214 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002215 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2216 SourceLocation(), nullptr, Dynamic,
2217 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002218}
2219
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002220SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2221 SourceLocation EndLoc = getLocation();
2222 if (IvarLoc.isValid())
2223 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002224
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002225 return SourceRange(AtLoc, EndLoc);
2226}