blob: f95d5def47accbda69a0ca6cc54022c5dad20514 [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) {
Fariborz Jahanian68453832009-06-05 18:16:35 +000077 if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
78 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.
89 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
90 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) {
Steve Naroffc4173fa2009-02-22 19:35:57 +0000105 ObjCMethodDecl *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) {
123 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
124 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
125 return true;
126 }
127
128 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
129 // 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 }
162 if (const ObjCProtocolDecl *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.
175 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
176 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)
Manman Ren5b786402016-01-28 18:49:28 +0000195 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I)) {
196 // 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.
233 if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
234 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: {
257 const ObjCProtocolDecl *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: {
265 const ObjCInterfaceDecl *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: {
286 const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
287 // 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.
Douglas Gregorc5e07f52015-07-07 03:58:01 +0000313 for (auto decl = getMostRecentDecl(); decl; decl = decl->getPreviousDecl()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000314 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
315 return written;
316 }
317
318 return nullptr;
319}
320
Douglas Gregorab7f0b32015-07-07 06:20:12 +0000321void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
322 TypeParamList = TPL;
323 if (!TPL)
324 return;
325 // Set the declaration context of each of the type parameters.
326 for (auto typeParam : *TypeParamList)
327 typeParam->setDeclContext(this);
328}
329
Douglas Gregore9d95f12015-07-07 03:57:35 +0000330ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
331 // FIXME: Should make sure no callers ever do this.
332 if (!hasDefinition())
333 return nullptr;
334
335 if (data().ExternallyCompleted)
336 LoadExternalDefinition();
337
338 if (const ObjCObjectType *superType = getSuperClassType()) {
339 if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
340 if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
341 return superDef;
342
343 return superDecl;
344 }
345 }
346
347 return nullptr;
348}
349
350SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
351 if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
352 return superTInfo->getTypeLoc().getLocStart();
353
354 return SourceLocation();
355}
356
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000357/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
358/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000359/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000360ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000361ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Manman Ren5b786402016-01-28 18:49:28 +0000362 IdentifierInfo *PropertyId,
363 ObjCPropertyQueryKind QueryKind) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000364 // FIXME: Should make sure no callers ever do this.
365 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000366 return nullptr;
367
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000368 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000369 LoadExternalDefinition();
370
Ted Kremenekd133a862010-03-15 20:30:07 +0000371 if (ObjCPropertyDecl *PD =
Manman Ren5b786402016-01-28 18:49:28 +0000372 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
373 QueryKind))
Ted Kremenekd133a862010-03-15 20:30:07 +0000374 return PD;
375
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000376 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000377 for (const auto *I : all_referenced_protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000378 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
379 QueryKind))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000380 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000381
Craig Topper36250ad2014-05-12 05:36:57 +0000382 return nullptr;
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000383}
384
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000385void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
386 PropertyDeclOrder &PO) const {
Manman Ren494ee5b2016-01-28 23:36:05 +0000387 for (auto *Prop : properties()) {
388 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000389 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000390 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000391 for (const auto *Ext : known_extensions()) {
392 const ObjCCategoryDecl *ClassExt = Ext;
Manman Ren494ee5b2016-01-28 23:36:05 +0000393 for (auto *Prop : ClassExt->properties()) {
394 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000395 PO.push_back(Prop);
396 }
397 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000398 for (const auto *PI : all_referenced_protocols())
399 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000400 // Note, the properties declared only in class extensions are still copied
401 // into the main @interface's property list, and therefore we don't
402 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000403}
404
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000405bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
406 const ObjCInterfaceDecl *Class = this;
407 while (Class) {
408 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
409 return true;
410 Class = Class->getSuperClass();
411 }
412 return false;
413}
414
415const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
416 const ObjCInterfaceDecl *Class = this;
417 while (Class) {
418 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
419 return Class;
420 Class = Class->getSuperClass();
421 }
Craig Topper36250ad2014-05-12 05:36:57 +0000422 return nullptr;
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000423}
424
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000425void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
426 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +0000427 ASTContext &C) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000428 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000429 LoadExternalDefinition();
430
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000431 if (data().AllReferencedProtocols.empty() &&
432 data().ReferencedProtocols.empty()) {
433 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000434 return;
435 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000436
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000437 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000438 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000439 // class or its extension are very few.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000440 SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000441 for (unsigned i = 0; i < ExtNum; i++) {
442 bool protocolExists = false;
443 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000444 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000445 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
446 protocolExists = true;
447 break;
448 }
449 }
450 // Do we want to warn on a protocol in extension class which
451 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000452 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000453 ProtocolRefs.push_back(ProtoInExtension);
454 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000455
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000456 if (ProtocolRefs.empty())
457 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000458
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000459 // Merge ProtocolRefs into class's protocol list;
Benjamin Kramerf9890422015-02-17 16:48:30 +0000460 ProtocolRefs.append(all_referenced_protocol_begin(),
461 all_referenced_protocol_end());
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000462
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000463 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000464}
465
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000466const ObjCInterfaceDecl *
467ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
468 const ObjCInterfaceDecl *IFace = this;
469 while (IFace) {
470 if (IFace->hasDesignatedInitializers())
471 return IFace;
472 if (!IFace->inheritsDesignatedInitializers())
473 break;
474 IFace = IFace->getSuperClass();
475 }
Craig Topper36250ad2014-05-12 05:36:57 +0000476 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000477}
478
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000479static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
480 for (const auto *MD : D->instance_methods()) {
481 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
482 return true;
483 }
484 for (const auto *Ext : D->visible_extensions()) {
485 for (const auto *MD : Ext->instance_methods()) {
486 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
487 return true;
488 }
489 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000490 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000491 for (const auto *MD : ImplD->instance_methods()) {
492 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
493 return true;
494 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000495 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000496 return false;
497}
498
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000499bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
500 switch (data().InheritedDesignatedInitializers) {
501 case DefinitionData::IDI_Inherited:
502 return true;
503 case DefinitionData::IDI_NotInherited:
504 return false;
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +0000505 case DefinitionData::IDI_Unknown:
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000506 // If the class introduced initializers we conservatively assume that we
507 // don't know if any of them is a designated initializer to avoid possible
508 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000509 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000510 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000511 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000512 if (auto SuperD = getSuperClass()) {
513 data().InheritedDesignatedInitializers =
514 SuperD->declaresOrInheritsDesignatedInitializers() ?
515 DefinitionData::IDI_Inherited :
516 DefinitionData::IDI_NotInherited;
517 } else {
518 data().InheritedDesignatedInitializers =
519 DefinitionData::IDI_NotInherited;
520 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000521 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000522 assert(data().InheritedDesignatedInitializers
523 != DefinitionData::IDI_Unknown);
524 return data().InheritedDesignatedInitializers ==
525 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000526 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000527
528 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
529}
530
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000531void ObjCInterfaceDecl::getDesignatedInitializers(
532 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000533 // Check for a complete definition and recover if not so.
534 if (!isThisDeclarationADefinition())
535 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000536 if (data().ExternallyCompleted)
537 LoadExternalDefinition();
538
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000539 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000540 if (!IFace)
541 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000542
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000543 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000544 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000545 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000546 for (const auto *Ext : IFace->visible_extensions()) {
547 for (const auto *MD : Ext->instance_methods())
548 if (MD->isThisDeclarationADesignatedInitializer())
549 Methods.push_back(MD);
550 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000551}
552
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000553bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
554 const ObjCMethodDecl **InitMethod) const {
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000555 bool HasCompleteDef = isThisDeclarationADefinition();
556 // During deserialization the data record for the ObjCInterfaceDecl could
557 // be made invariant by reusing the canonical decl. Take this into account
558 // when checking for the complete definition.
559 if (!HasCompleteDef && getCanonicalDecl()->hasDefinition() &&
560 getCanonicalDecl()->getDefinition() == getDefinition())
561 HasCompleteDef = true;
562
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000563 // Check for a complete definition and recover if not so.
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000564 if (!HasCompleteDef)
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000565 return false;
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000566
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000567 if (data().ExternallyCompleted)
568 LoadExternalDefinition();
569
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000570 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000571 if (!IFace)
572 return false;
573
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000574 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000575 if (MD->isThisDeclarationADesignatedInitializer()) {
576 if (InitMethod)
577 *InitMethod = MD;
578 return true;
579 }
580 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000581 for (const auto *Ext : IFace->visible_extensions()) {
582 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
583 if (MD->isThisDeclarationADesignatedInitializer()) {
584 if (InitMethod)
585 *InitMethod = MD;
586 return true;
587 }
588 }
589 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000590 return false;
591}
592
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000593void ObjCInterfaceDecl::allocateDefinitionData() {
594 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000595 Data.setPointer(new (getASTContext()) DefinitionData());
596 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000597
598 // Make the type point at the definition, now that we have one.
599 if (TypeForDecl)
600 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000601}
602
603void ObjCInterfaceDecl::startDefinition() {
604 allocateDefinitionData();
605
Douglas Gregor66b310c2011-12-15 18:03:09 +0000606 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +0000607 for (auto RD : redecls()) {
608 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000609 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000610 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000611}
612
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000613ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
614 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000615 // FIXME: Should make sure no callers ever do this.
616 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000617 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000618
619 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000620 LoadExternalDefinition();
621
Chris Lattner89375192008-03-16 00:19:01 +0000622 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000623 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000624 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000625 clsDeclared = ClassDecl;
626 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000627 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000628
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000629 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000630 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000631 clsDeclared = ClassDecl;
632 return I;
633 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000634 }
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000635
Chris Lattner89375192008-03-16 00:19:01 +0000636 ClassDecl = ClassDecl->getSuperClass();
637 }
Craig Topper36250ad2014-05-12 05:36:57 +0000638 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000639}
640
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000641/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
642/// class whose name is passed as argument. If it is not one of the super classes
643/// the it returns NULL.
644ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
645 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000646 // FIXME: Should make sure no callers ever do this.
647 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000648 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000649
650 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000651 LoadExternalDefinition();
652
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000653 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000654 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000655 if (ClassDecl->getIdentifier() == ICName)
656 return ClassDecl;
657 ClassDecl = ClassDecl->getSuperClass();
658 }
Craig Topper36250ad2014-05-12 05:36:57 +0000659 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000660}
661
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000662ObjCProtocolDecl *
663ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000664 for (auto *P : all_referenced_protocols())
665 if (P->lookupProtocolNamed(Name))
666 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000667 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000668 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000669}
670
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000671/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000672/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000673/// When argument category "C" is specified, any implicit method found
674/// in this category is ignored.
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000675ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000676 bool isInstance,
677 bool shallowCategoryLookup,
678 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000679 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000680{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000681 // FIXME: Should make sure no callers ever do this.
682 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000683 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000684
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000685 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000686 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000687
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000688 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000689 LoadExternalDefinition();
690
Ted Kremenek00781502013-11-23 01:01:29 +0000691 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000692 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000693 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000694 return MethodDecl;
Fariborz Jahanianc806b902012-04-05 22:14:12 +0000695
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000696 // 2. Didn't find one yet - now look through categories.
697 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000698 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000699 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000700 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000701
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000702 // 3. Didn't find one yet - look through primary class's protocols.
703 for (const auto *I : ClassDecl->protocols())
704 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
705 return MethodDecl;
706
707 // 4. Didn't find one yet - now look through categories' protocols
708 if (!shallowCategoryLookup)
709 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000710 // Didn't find one yet - look through protocols.
711 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000712 Cat->getReferencedProtocols();
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000713 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
714 E = Protocols.end(); I != E; ++I)
715 if ((MethodDecl = (*I)->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
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000868 Decl *CtxD = cast<Decl>(getDeclContext());
869
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000870 if (!CtxD->isInvalidDecl()) {
871 if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
872 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
873 if (!ImplD->isInvalidDecl())
874 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000875
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000876 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
877 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
878 if (!ImplD->isInvalidDecl())
879 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000880
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000881 } else if (ObjCImplementationDecl *ImplD =
882 dyn_cast<ObjCImplementationDecl>(CtxD)) {
883 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
884 if (!IFD->isInvalidDecl())
885 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000886
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000887 } else if (ObjCCategoryImplDecl *CImplD =
888 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
889 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
890 if (!CatD->isInvalidDecl())
891 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
892 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000893 }
894
Alex Lorenzff6c34b2016-11-21 11:16:30 +0000895 // Ensure that the discovered method redeclaration has a valid declaration
896 // context. Used to prevent infinite loops when iterating redeclarations in
897 // a partially invalid AST.
898 if (Redecl && cast<Decl>(Redecl->getDeclContext())->isInvalidDecl())
899 Redecl = nullptr;
900
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000901 if (!Redecl && isRedeclaration()) {
902 // This is the last redeclaration, go back to the first method.
903 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
904 isInstanceMethod());
905 }
906
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000907 return Redecl ? Redecl : this;
908}
909
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000910ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
911 Decl *CtxD = cast<Decl>(getDeclContext());
912
913 if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
914 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
915 if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
916 isInstanceMethod()))
917 return MD;
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000918 } else if (ObjCCategoryImplDecl *CImplD =
919 dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000920 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000921 if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
922 isInstanceMethod()))
923 return MD;
924 }
925
Manman Rena0042c42016-10-03 21:26:46 +0000926 if (isRedeclaration()) {
927 // It is possible that we have not done deserializing the ObjCMethod yet.
928 ObjCMethodDecl *MD =
929 cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
930 isInstanceMethod());
931 return MD ? MD : this;
932 }
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000933
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000934 return this;
935}
936
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000937SourceLocation ObjCMethodDecl::getLocEnd() const {
938 if (Stmt *Body = getBody())
939 return Body->getLocEnd();
940 return DeclEndLoc;
941}
942
John McCallb4526252011-03-02 01:50:55 +0000943ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
944 ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
John McCallfb55f852011-03-02 21:01:41 +0000945 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +0000946 return family;
947
John McCall86bc21f2011-03-02 11:33:24 +0000948 // Check for an explicit attribute.
949 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
950 // The unfortunate necessity of mapping between enums here is due
951 // to the attributes framework.
952 switch (attr->getFamily()) {
953 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
954 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
955 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
956 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
957 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
958 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
959 }
960 Family = static_cast<unsigned>(family);
961 return family;
962 }
963
John McCallb4526252011-03-02 01:50:55 +0000964 family = getSelector().getMethodFamily();
965 switch (family) {
966 case OMF_None: break;
967
968 // init only has a conventional meaning for an instance method, and
969 // it has to return an object.
970 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +0000971 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000972 family = OMF_None;
973 break;
974
975 // alloc/copy/new have a conventional meaning for both class and
976 // instance methods, but they require an object return.
977 case OMF_alloc:
978 case OMF_copy:
979 case OMF_mutableCopy:
980 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +0000981 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +0000982 family = OMF_None;
983 break;
984
985 // These selectors have a conventional meaning only for instance methods.
986 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000987 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +0000988 case OMF_retain:
989 case OMF_release:
990 case OMF_autorelease:
991 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +0000992 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +0000993 if (!isInstanceMethod())
994 family = OMF_None;
995 break;
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000996
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000997 case OMF_initialize:
998 if (isInstanceMethod() || !getReturnType()->isVoidType())
999 family = OMF_None;
1000 break;
1001
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001002 case OMF_performSelector:
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001003 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001004 family = OMF_None;
1005 else {
1006 unsigned noParams = param_size();
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001007 if (noParams < 1 || noParams > 3)
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001008 family = OMF_None;
1009 else {
Alp Toker1f307f42014-01-25 17:32:04 +00001010 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001011 QualType ArgT = (*it);
1012 if (!ArgT->isObjCSelType()) {
1013 family = OMF_None;
1014 break;
1015 }
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001016 while (--noParams) {
1017 it++;
1018 ArgT = (*it);
1019 if (!ArgT->isObjCIdType()) {
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001020 family = OMF_None;
1021 break;
1022 }
1023 }
1024 }
1025 }
1026 break;
1027
John McCallb4526252011-03-02 01:50:55 +00001028 }
1029
1030 // Cache the result.
1031 Family = static_cast<unsigned>(family);
1032 return family;
1033}
1034
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001035QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
1036 const ObjCInterfaceDecl *OID,
1037 bool &selfIsPseudoStrong,
1038 bool &selfIsConsumed) {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001039 QualType selfTy;
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001040 selfIsPseudoStrong = false;
1041 selfIsConsumed = false;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001042 if (isInstanceMethod()) {
1043 // There may be no interface context due to error in declaration
1044 // of the interface (which has been reported). Recover gracefully.
1045 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +00001046 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001047 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001048 } else {
1049 selfTy = Context.getObjCIdType();
1050 }
1051 } else // we have a factory method.
1052 selfTy = Context.getObjCClassType();
1053
David Blaikiebbafb8a2012-03-11 07:00:24 +00001054 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001055 if (isInstanceMethod()) {
1056 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +00001057
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001058 // 'self' is always __strong. It's actually pseudo-strong except
1059 // in init methods (or methods labeled ns_consumes_self), though.
1060 Qualifiers qs;
1061 qs.setObjCLifetime(Qualifiers::OCL_Strong);
1062 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +00001063
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001064 // In addition, 'self' is const unless this is an init method.
1065 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
1066 selfTy = selfTy.withConst();
1067 selfIsPseudoStrong = true;
1068 }
1069 }
1070 else {
1071 assert(isClassMethod());
1072 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +00001073 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +00001074 selfIsPseudoStrong = true;
1075 }
John McCall31168b02011-06-15 23:02:42 +00001076 }
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001077 return selfTy;
1078}
John McCall31168b02011-06-15 23:02:42 +00001079
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001080void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
1081 const ObjCInterfaceDecl *OID) {
1082 bool selfIsPseudoStrong, selfIsConsumed;
1083 QualType selfTy =
1084 getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
Alexey Bataev56223232017-06-09 13:40:18 +00001085 auto *Self = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1086 &Context.Idents.get("self"), selfTy,
1087 ImplicitParamDecl::ObjCSelf);
1088 setSelfDecl(Self);
John McCall31168b02011-06-15 23:02:42 +00001089
1090 if (selfIsConsumed)
Alexey Bataev56223232017-06-09 13:40:18 +00001091 Self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001092
John McCalld4631322011-06-17 06:42:21 +00001093 if (selfIsPseudoStrong)
Alexey Bataev56223232017-06-09 13:40:18 +00001094 Self->setARCPseudoStrong(true);
John McCalld4631322011-06-17 06:42:21 +00001095
Alexey Bataev56223232017-06-09 13:40:18 +00001096 setCmdDecl(ImplicitParamDecl::Create(
1097 Context, this, SourceLocation(), &Context.Idents.get("_cmd"),
1098 Context.getObjCSelType(), ImplicitParamDecl::ObjCCmd));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001099}
1100
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001101ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
1102 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
1103 return ID;
1104 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1105 return CD->getClassInterface();
Argyrios Kyrtzidis2cee40d2009-07-28 05:10:52 +00001106 if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001107 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +00001108 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +00001109 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +00001110 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001111}
1112
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001113SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1114 const auto *TSI = getReturnTypeSourceInfo();
1115 if (TSI)
1116 return TSI->getTypeLoc().getSourceRange();
1117 return SourceRange();
1118}
1119
Douglas Gregor9b7b3e92015-07-07 06:20:27 +00001120QualType ObjCMethodDecl::getSendResultType() const {
1121 ASTContext &Ctx = getASTContext();
1122 return getReturnType().getNonLValueExprType(Ctx)
1123 .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1124}
1125
Douglas Gregore83b9562015-07-07 03:57:53 +00001126QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1127 // FIXME: Handle related result types here.
1128
1129 return getReturnType().getNonLValueExprType(getASTContext())
1130 .substObjCMemberType(receiverType, getDeclContext(),
1131 ObjCSubstitutionContext::Result);
1132}
1133
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001134static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1135 const ObjCMethodDecl *Method,
1136 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1137 bool MovedToSuper) {
1138 if (!Container)
1139 return;
1140
1141 // In categories look for overriden methods from protocols. A method from
1142 // category is not "overriden" since it is considered as the "same" method
1143 // (same USR) as the one from the interface.
1144 if (const ObjCCategoryDecl *
1145 Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1146 // Check whether we have a matching method at this category but only if we
1147 // are at the super class level.
1148 if (MovedToSuper)
1149 if (ObjCMethodDecl *
1150 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001151 Method->isInstanceMethod(),
1152 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001153 if (Method != Overridden) {
1154 // We found an override at this category; there is no need to look
1155 // into its protocols.
1156 Methods.push_back(Overridden);
1157 return;
1158 }
1159
Aaron Ballman19a41762014-03-14 12:55:57 +00001160 for (const auto *P : Category->protocols())
1161 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001162 return;
1163 }
1164
1165 // Check whether we have a matching method at this level.
1166 if (const ObjCMethodDecl *
1167 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001168 Method->isInstanceMethod(),
1169 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001170 if (Method != Overridden) {
1171 // We found an override at this level; there is no need to look
1172 // into other protocols or categories.
1173 Methods.push_back(Overridden);
1174 return;
1175 }
1176
1177 if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001178 for (const auto *P : Protocol->protocols())
1179 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001180 }
1181
1182 if (const ObjCInterfaceDecl *
1183 Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001184 for (const auto *P : Interface->protocols())
1185 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001186
Aaron Ballman15063e12014-03-13 21:35:02 +00001187 for (const auto *Cat : Interface->known_categories())
1188 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001189
1190 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1191 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1192 /*MovedToSuper=*/true);
1193 }
1194}
1195
1196static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1197 const ObjCMethodDecl *Method,
1198 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1199 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1200 /*MovedToSuper=*/false);
1201}
1202
1203static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1204 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1205 assert(Method->isOverriding());
1206
1207 if (const ObjCProtocolDecl *
1208 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1209 CollectOverriddenMethods(ProtD, Method, overridden);
1210
1211 } else if (const ObjCImplDecl *
1212 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1213 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1214 if (!ID)
1215 return;
1216 // Start searching for overridden methods using the method from the
1217 // interface as starting point.
1218 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001219 Method->isInstanceMethod(),
1220 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001221 Method = IFaceMeth;
1222 CollectOverriddenMethods(ID, Method, overridden);
1223
1224 } else if (const ObjCCategoryDecl *
1225 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1226 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1227 if (!ID)
1228 return;
1229 // Start searching for overridden methods using the method from the
1230 // interface as starting point.
1231 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001232 Method->isInstanceMethod(),
1233 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001234 Method = IFaceMeth;
1235 CollectOverriddenMethods(ID, Method, overridden);
1236
1237 } else {
1238 CollectOverriddenMethods(
1239 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1240 Method, overridden);
1241 }
1242}
1243
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001244void ObjCMethodDecl::getOverriddenMethods(
1245 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1246 const ObjCMethodDecl *Method = this;
1247
1248 if (Method->isRedeclaration()) {
1249 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1250 getMethod(Method->getSelector(), Method->isInstanceMethod());
1251 }
1252
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001253 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001254 collectOverriddenMethodsSlow(Method, Overridden);
1255 assert(!Overridden.empty() &&
1256 "ObjCMethodDecl's overriding bit is not as expected");
1257 }
1258}
1259
Jordan Rose2bd991a2012-10-10 16:42:54 +00001260const ObjCPropertyDecl *
1261ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1262 Selector Sel = getSelector();
1263 unsigned NumArgs = Sel.getNumArgs();
1264 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001265 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001266
Jordan Rose2bd991a2012-10-10 16:42:54 +00001267 if (isPropertyAccessor()) {
1268 const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
1269 bool IsGetter = (NumArgs == 0);
Jordan Rose31cf7d42016-03-11 21:14:40 +00001270 bool IsInstance = isInstanceMethod();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001271
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001272 /// Local function that attempts to find a matching property within the
1273 /// given Objective-C container.
1274 auto findMatchingProperty =
1275 [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * {
Jordan Rose31cf7d42016-03-11 21:14:40 +00001276 if (IsInstance) {
1277 for (const auto *I : Container->instance_properties()) {
1278 Selector NextSel = IsGetter ? I->getGetterName()
1279 : I->getSetterName();
1280 if (NextSel == Sel)
1281 return I;
1282 }
1283 } else {
1284 for (const auto *I : Container->class_properties()) {
1285 Selector NextSel = IsGetter ? I->getGetterName()
1286 : I->getSetterName();
1287 if (NextSel == Sel)
1288 return I;
1289 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001290 }
1291
1292 return nullptr;
1293 };
1294
1295 // Look in the container we were given.
1296 if (const auto *Found = findMatchingProperty(Container))
1297 return Found;
1298
1299 // If we're in a category or extension, look in the main class.
1300 const ObjCInterfaceDecl *ClassDecl = nullptr;
1301 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1302 ClassDecl = Category->getClassInterface();
1303 if (const auto *Found = findMatchingProperty(ClassDecl))
1304 return Found;
1305 } else {
1306 // Determine whether the container is a class.
1307 ClassDecl = dyn_cast<ObjCInterfaceDecl>(Container);
1308 }
1309
1310 // If we have a class, check its visible extensions.
1311 if (ClassDecl) {
1312 for (const auto *Ext : ClassDecl->visible_extensions()) {
1313 if (Ext == Container)
1314 continue;
1315
1316 if (const auto *Found = findMatchingProperty(Ext))
1317 return Found;
1318 }
Jordan Rose2bd991a2012-10-10 16:42:54 +00001319 }
1320
1321 llvm_unreachable("Marked as a property accessor but no property found!");
1322 }
1323
1324 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001325 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001326
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001327 using OverridesTy = SmallVector<const ObjCMethodDecl *, 8>;
1328
Jordan Rose2bd991a2012-10-10 16:42:54 +00001329 OverridesTy Overrides;
1330 getOverriddenMethods(Overrides);
1331 for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1332 I != E; ++I) {
1333 if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1334 return Prop;
1335 }
1336
Craig Topper36250ad2014-05-12 05:36:57 +00001337 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001338}
1339
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001340//===----------------------------------------------------------------------===//
Douglas Gregor85f3f952015-07-07 03:57:15 +00001341// ObjCTypeParamDecl
1342//===----------------------------------------------------------------------===//
1343
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001344void ObjCTypeParamDecl::anchor() {}
Douglas Gregor85f3f952015-07-07 03:57:15 +00001345
1346ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001347 ObjCTypeParamVariance variance,
1348 SourceLocation varianceLoc,
Douglas Gregore83b9562015-07-07 03:57:53 +00001349 unsigned index,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001350 SourceLocation nameLoc,
1351 IdentifierInfo *name,
1352 SourceLocation colonLoc,
1353 TypeSourceInfo *boundInfo) {
Manman Renc5705ba2016-09-13 17:41:05 +00001354 auto *TPDecl =
1355 new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1356 nameLoc, name, colonLoc, boundInfo);
1357 QualType TPType = ctx.getObjCTypeParamType(TPDecl, {});
1358 TPDecl->setTypeForDecl(TPType.getTypePtr());
1359 return TPDecl;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001360}
1361
1362ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1363 unsigned ID) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001364 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1365 ObjCTypeParamVariance::Invariant,
1366 SourceLocation(), 0, SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001367 nullptr, SourceLocation(), nullptr);
1368}
1369
1370SourceRange ObjCTypeParamDecl::getSourceRange() const {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001371 SourceLocation startLoc = VarianceLoc;
1372 if (startLoc.isInvalid())
1373 startLoc = getLocation();
1374
Douglas Gregor85f3f952015-07-07 03:57:15 +00001375 if (hasExplicitBound()) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001376 return SourceRange(startLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001377 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1378 }
1379
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001380 return SourceRange(startLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001381}
1382
1383//===----------------------------------------------------------------------===//
1384// ObjCTypeParamList
1385//===----------------------------------------------------------------------===//
1386ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1387 ArrayRef<ObjCTypeParamDecl *> typeParams,
1388 SourceLocation rAngleLoc)
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001389 : NumParams(typeParams.size()) {
Douglas Gregor0a8890ff2015-07-07 06:20:46 +00001390 Brackets.Begin = lAngleLoc.getRawEncoding();
1391 Brackets.End = rAngleLoc.getRawEncoding();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001392 std::copy(typeParams.begin(), typeParams.end(), begin());
1393}
1394
Douglas Gregor85f3f952015-07-07 03:57:15 +00001395ObjCTypeParamList *ObjCTypeParamList::create(
1396 ASTContext &ctx,
1397 SourceLocation lAngleLoc,
1398 ArrayRef<ObjCTypeParamDecl *> typeParams,
1399 SourceLocation rAngleLoc) {
James Y Knight967eb202015-12-29 22:13:13 +00001400 void *mem =
1401 ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001402 alignof(ObjCTypeParamList));
Douglas Gregor85f3f952015-07-07 03:57:15 +00001403 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1404}
1405
Douglas Gregore83b9562015-07-07 03:57:53 +00001406void ObjCTypeParamList::gatherDefaultTypeArgs(
1407 SmallVectorImpl<QualType> &typeArgs) const {
1408 typeArgs.reserve(size());
1409 for (auto typeParam : *this)
1410 typeArgs.push_back(typeParam->getUnderlyingType());
1411}
1412
Douglas Gregor85f3f952015-07-07 03:57:15 +00001413//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001414// ObjCInterfaceDecl
1415//===----------------------------------------------------------------------===//
1416
Douglas Gregord53ae832012-01-17 18:09:05 +00001417ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001418 DeclContext *DC,
1419 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001420 IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001421 ObjCTypeParamList *typeParamList,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001422 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001423 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001424 bool isInternal){
Richard Smithf7981722013-11-22 09:01:48 +00001425 ObjCInterfaceDecl *Result = new (C, DC)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001426 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1427 isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001428 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001429 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001430 return Result;
1431}
1432
Richard Smith053f6c62014-05-16 23:01:30 +00001433ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001434 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001435 ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001436 SourceLocation(),
1437 nullptr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001438 nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001439 SourceLocation(),
1440 nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001441 Result->Data.setInt(!C.getLangOpts().Modules);
1442 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001443}
1444
Richard Smith053f6c62014-05-16 23:01:30 +00001445ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1446 SourceLocation AtLoc, IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001447 ObjCTypeParamList *typeParamList,
Richard Smith053f6c62014-05-16 23:01:30 +00001448 SourceLocation CLoc,
1449 ObjCInterfaceDecl *PrevDecl,
1450 bool IsInternal)
1451 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001452 redeclarable_base(C) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001453 setPreviousDecl(PrevDecl);
Douglas Gregor81252352011-12-16 22:37:11 +00001454
1455 // Copy the 'data' pointer over.
1456 if (PrevDecl)
1457 Data = PrevDecl->Data;
1458
Richard Smith053f6c62014-05-16 23:01:30 +00001459 setImplicit(IsInternal);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001460
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001461 setTypeParamList(typeParamList);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001462}
1463
Douglas Gregor73693022010-12-01 23:49:52 +00001464void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001465 assert(data().ExternallyCompleted && "Class is not externally completed");
1466 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001467 getASTContext().getExternalSource()->CompleteType(
1468 const_cast<ObjCInterfaceDecl *>(this));
1469}
1470
1471void ObjCInterfaceDecl::setExternallyCompleted() {
1472 assert(getASTContext().getExternalSource() &&
1473 "Class can't be externally completed without an external source");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001474 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001475 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001476 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001477}
1478
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001479void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001480 // Check for a complete definition and recover if not so.
1481 if (!isThisDeclarationADefinition())
1482 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001483 data().HasDesignatedInitializers = true;
1484}
1485
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001486bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001487 // Check for a complete definition and recover if not so.
1488 if (!isThisDeclarationADefinition())
1489 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001490 if (data().ExternallyCompleted)
1491 LoadExternalDefinition();
1492
1493 return data().HasDesignatedInitializers;
1494}
1495
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001496StringRef
1497ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001498 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1499 return ObjCRTName->getMetadataName();
1500
1501 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001502}
1503
1504StringRef
1505ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001506 if (ObjCInterfaceDecl *ID =
1507 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1508 return ID->getObjCRuntimeNameAsString();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001509
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001510 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001511}
1512
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001513ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001514 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1515 if (data().ExternallyCompleted)
1516 LoadExternalDefinition();
1517
1518 return getASTContext().getObjCImplementation(
1519 const_cast<ObjCInterfaceDecl*>(Def));
1520 }
1521
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001522 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001523 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001524}
1525
1526void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001527 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001528}
1529
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001530namespace {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001531
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001532struct SynthesizeIvarChunk {
1533 uint64_t Size;
1534 ObjCIvarDecl *Ivar;
1535
1536 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1537 : Size(size), Ivar(ivar) {}
1538};
1539
1540bool operator<(const SynthesizeIvarChunk & LHS,
1541 const SynthesizeIvarChunk &RHS) {
1542 return LHS.Size < RHS.Size;
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001543}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001544
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001545} // namespace
1546
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001547/// all_declared_ivar_begin - return first ivar declared in this class,
1548/// its extensions and its implementation. Lazily build the list on first
1549/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001550///
1551/// Caveat: The list returned by this method reflects the current
1552/// state of the parser. The cache will be updated for every ivar
1553/// added by an extension or the implementation when they are
1554/// encountered.
1555/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001556ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001557 // FIXME: Should make sure no callers ever do this.
1558 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001559 return nullptr;
1560
1561 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001562 if (!data().IvarList) {
1563 if (!ivar_empty()) {
1564 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1565 data().IvarList = *I; ++I;
1566 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001567 curIvar->setNextIvar(*I);
1568 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001569
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001570 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001571 if (!Ext->ivar_empty()) {
1572 ObjCCategoryDecl::ivar_iterator
1573 I = Ext->ivar_begin(),
1574 E = Ext->ivar_end();
1575 if (!data().IvarList) {
1576 data().IvarList = *I; ++I;
1577 curIvar = data().IvarList;
1578 }
1579 for ( ;I != E; curIvar = *I, ++I)
1580 curIvar->setNextIvar(*I);
1581 }
1582 }
1583 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001584 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001585
1586 // cached and complete!
1587 if (!data().IvarListMissingImplementation)
1588 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001589
1590 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001591 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001592 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001593 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001594 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001595 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1596 layout.push_back(SynthesizeIvarChunk(
1597 IV->getASTContext().getTypeSize(IV->getType()), IV));
1598 continue;
1599 }
1600 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001601 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001602 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001603 curIvar->setNextIvar(IV);
1604 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001605 }
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001606
1607 if (!layout.empty()) {
1608 // Order synthesized ivars by their size.
1609 std::stable_sort(layout.begin(), layout.end());
1610 unsigned Ix = 0, EIx = layout.size();
1611 if (!data().IvarList) {
1612 data().IvarList = layout[0].Ivar; Ix++;
1613 curIvar = data().IvarList;
1614 }
1615 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1616 curIvar->setNextIvar(layout[Ix].Ivar);
1617 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001618 }
1619 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001620 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001621}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001622
1623/// FindCategoryDeclaration - Finds category declaration in the list of
1624/// categories for this class and returns it. Name of the category is passed
1625/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001626///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001627ObjCCategoryDecl *
1628ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001629 // FIXME: Should make sure no callers ever do this.
1630 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001631 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001632
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001633 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001634 LoadExternalDefinition();
1635
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001636 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001637 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001638 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001639
1640 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001641}
1642
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001643ObjCMethodDecl *
1644ObjCInterfaceDecl::getCategoryInstanceMethod(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->getInstanceMethod(Sel))
1648 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001649 }
1650
Craig Topper36250ad2014-05-12 05:36:57 +00001651 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001652}
1653
1654ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001655 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001656 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001657 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1658 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001659 }
Craig Topper36250ad2014-05-12 05:36:57 +00001660
1661 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001662}
1663
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001664/// ClassImplementsProtocol - Checks that 'lProto' protocol
1665/// has been implemented in IDecl class, its super class or categories (if
1666/// lookupCategory is true).
1667bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1668 bool lookupCategory,
1669 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001670 if (!hasDefinition())
1671 return false;
1672
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001673 ObjCInterfaceDecl *IDecl = this;
1674 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001675 for (auto *PI : IDecl->protocols()){
1676 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001677 return true;
1678 // This is dubious and is added to be compatible with gcc. In gcc, it is
1679 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1680 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1681 // object. This IMO, should be a bug.
1682 // FIXME: Treat this as an extension, and flag this as an error when GCC
1683 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001684 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001685 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001686 return true;
1687 }
Mike Stump11289f42009-09-09 15:08:12 +00001688
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001689 // 2nd, look up the category.
1690 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001691 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001692 for (auto *PI : Cat->protocols())
1693 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001694 return true;
1695 }
Mike Stump11289f42009-09-09 15:08:12 +00001696
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001697 // 3rd, look up the super class(s)
1698 if (IDecl->getSuperClass())
1699 return
1700 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1701 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001702
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001703 return false;
1704}
1705
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001706//===----------------------------------------------------------------------===//
1707// ObjCIvarDecl
1708//===----------------------------------------------------------------------===//
1709
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001710void ObjCIvarDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001711
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001712ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001713 SourceLocation StartLoc,
1714 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001715 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001716 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001717 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001718 if (DC) {
1719 // Ivar's can only appear in interfaces, implementations (via synthesized
1720 // properties), and class extensions (via direct declaration, or synthesized
1721 // properties).
1722 //
1723 // FIXME: This should really be asserting this:
1724 // (isa<ObjCCategoryDecl>(DC) &&
1725 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1726 // but unfortunately we sometimes place ivars into non-class extension
1727 // categories on error. This breaks an AST invariant, and should not be
1728 // fixed.
1729 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1730 isa<ObjCCategoryDecl>(DC)) &&
1731 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001732 // Once a new ivar is created in any of class/class-extension/implementation
1733 // decl contexts, the previously built IvarList must be rebuilt.
1734 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1735 if (!ID) {
Eric Christopherf8378ca2012-07-19 22:22:55 +00001736 if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001737 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001738 else
1739 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001740 }
Craig Topper36250ad2014-05-12 05:36:57 +00001741 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001742 }
1743
Richard Smithf7981722013-11-22 09:01:48 +00001744 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001745 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001746}
1747
Douglas Gregor72172e92012-01-05 21:55:30 +00001748ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001749 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1750 nullptr, QualType(), nullptr,
1751 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001752}
1753
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001754const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1755 const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001756
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001757 switch (DC->getKind()) {
1758 default:
1759 case ObjCCategoryImpl:
1760 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001761 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001762
1763 // Ivars can only appear in class extension categories.
1764 case ObjCCategory: {
1765 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1766 assert(CD->IsClassExtension() && "invalid container for ivar!");
1767 return CD->getClassInterface();
1768 }
1769
1770 case ObjCImplementation:
1771 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1772
1773 case ObjCInterface:
1774 return cast<ObjCInterfaceDecl>(DC);
1775 }
1776}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001777
Douglas Gregore83b9562015-07-07 03:57:53 +00001778QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1779 return getType().substObjCMemberType(objectType, getDeclContext(),
1780 ObjCSubstitutionContext::Property);
1781}
1782
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001783//===----------------------------------------------------------------------===//
1784// ObjCAtDefsFieldDecl
1785//===----------------------------------------------------------------------===//
1786
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001787void ObjCAtDefsFieldDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001788
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001789ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001790*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1791 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001792 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001793 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001794}
1795
Richard Smithf7981722013-11-22 09:01:48 +00001796ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001797 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001798 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1799 SourceLocation(), nullptr, QualType(),
1800 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001801}
1802
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001803//===----------------------------------------------------------------------===//
1804// ObjCProtocolDecl
1805//===----------------------------------------------------------------------===//
1806
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001807void ObjCProtocolDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001808
Richard Smith053f6c62014-05-16 23:01:30 +00001809ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1810 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001811 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001812 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001813 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001814 redeclarable_base(C) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001815 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001816 if (PrevDecl)
1817 Data = PrevDecl->Data;
1818}
1819
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001820ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001821 IdentifierInfo *Id,
1822 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001823 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001824 ObjCProtocolDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001825 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001826 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001827 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001828 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001829}
1830
Richard Smithf7981722013-11-22 09:01:48 +00001831ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001832 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001833 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001834 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001835 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001836 Result->Data.setInt(!C.getLangOpts().Modules);
1837 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001838}
1839
Steve Naroff114aecb2009-03-01 16:12:44 +00001840ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1841 ObjCProtocolDecl *PDecl = this;
1842
1843 if (Name == getIdentifier())
1844 return PDecl;
1845
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001846 for (auto *I : protocols())
1847 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001848 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001849
Craig Topper36250ad2014-05-12 05:36:57 +00001850 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001851}
1852
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001853// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001854// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001855ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1856 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001857 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001858
Douglas Gregoreed49792013-01-17 00:38:46 +00001859 // If there is no definition or the definition is hidden, we don't find
1860 // anything.
1861 const ObjCProtocolDecl *Def = getDefinition();
1862 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001863 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001864
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001865 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001866 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001867
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001868 for (const auto *I : protocols())
1869 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001870 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001871 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001872}
1873
Douglas Gregore6e48b12012-01-01 19:29:29 +00001874void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001875 assert(!Data.getPointer() && "Protocol already has a definition!");
1876 Data.setPointer(new (getASTContext()) DefinitionData);
1877 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001878}
1879
1880void ObjCProtocolDecl::startDefinition() {
1881 allocateDefinitionData();
Douglas Gregora715bff2012-01-01 19:51:50 +00001882
1883 // Update all of the declarations with a pointer to the definition.
Aaron Ballman86c93902014-03-06 23:45:36 +00001884 for (auto RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001885 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001886}
1887
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001888void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1889 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001890 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001891 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001892 // Insert into PM if not there already.
Manman Ren494ee5b2016-01-28 23:36:05 +00001893 PM.insert(std::make_pair(
1894 std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
1895 Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001896 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001897 }
1898 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001899 for (const auto *PI : PDecl->protocols())
1900 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001901 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001902}
1903
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001904void ObjCProtocolDecl::collectInheritedProtocolProperties(
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001905 const ObjCPropertyDecl *Property, ProtocolPropertySet &PS,
1906 PropertyDeclOrder &PO) const {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001907 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001908 if (!PS.insert(PDecl).second)
1909 return;
Manman Renefe1bac2016-01-27 20:00:32 +00001910 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001911 if (Prop == Property)
1912 continue;
1913 if (Prop->getIdentifier() == Property->getIdentifier()) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001914 PO.push_back(Prop);
1915 return;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001916 }
1917 }
1918 // Scan through protocol's protocols which did not have a matching property.
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001919 for (const auto *PI : PDecl->protocols())
1920 PI->collectInheritedProtocolProperties(Property, PS, PO);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001921 }
1922}
Anna Zaks673d76b2012-10-18 19:17:53 +00001923
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001924StringRef
1925ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001926 if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1927 return ObjCRTName->getMetadataName();
1928
1929 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001930}
1931
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001932//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001933// ObjCCategoryDecl
1934//===----------------------------------------------------------------------===//
1935
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001936void ObjCCategoryDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001937
Douglas Gregor85f3f952015-07-07 03:57:15 +00001938ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1939 SourceLocation ClassNameLoc,
1940 SourceLocation CategoryNameLoc,
1941 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1942 ObjCTypeParamList *typeParamList,
1943 SourceLocation IvarLBraceLoc,
1944 SourceLocation IvarRBraceLoc)
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001945 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1946 ClassInterface(IDecl), CategoryNameLoc(CategoryNameLoc),
1947 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001948 setTypeParamList(typeParamList);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001949}
1950
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001951ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00001952 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00001953 SourceLocation ClassNameLoc,
1954 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001955 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001956 ObjCInterfaceDecl *IDecl,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001957 ObjCTypeParamList *typeParamList,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00001958 SourceLocation IvarLBraceLoc,
1959 SourceLocation IvarRBraceLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00001960 ObjCCategoryDecl *CatDecl =
1961 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001962 IDecl, typeParamList, IvarLBraceLoc,
1963 IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001964 if (IDecl) {
1965 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001966 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001967 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001968 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001969 if (ASTMutationListener *L = C.getASTMutationListener())
1970 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1971 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00001972 }
1973
1974 return CatDecl;
1975}
1976
Richard Smithf7981722013-11-22 09:01:48 +00001977ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001978 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001979 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
1980 SourceLocation(), SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001981 nullptr, nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001982}
1983
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001984ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1985 return getASTContext().getObjCImplementation(
1986 const_cast<ObjCCategoryDecl*>(this));
1987}
1988
1989void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1990 getASTContext().setObjCImplementation(this, ImplD);
1991}
1992
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001993void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
1994 TypeParamList = TPL;
1995 if (!TPL)
1996 return;
1997 // Set the declaration context of each of the type parameters.
1998 for (auto typeParam : *TypeParamList)
1999 typeParam->setDeclContext(this);
2000}
2001
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002002
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002003//===----------------------------------------------------------------------===//
2004// ObjCCategoryImplDecl
2005//===----------------------------------------------------------------------===//
2006
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002007void ObjCCategoryImplDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002008
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002009ObjCCategoryImplDecl *
2010ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002011 IdentifierInfo *Id,
2012 ObjCInterfaceDecl *ClassInterface,
2013 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00002014 SourceLocation atStartLoc,
2015 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00002016 if (ClassInterface && ClassInterface->hasDefinition())
2017 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00002018 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
2019 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002020}
2021
Douglas Gregor72172e92012-01-05 21:55:30 +00002022ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
2023 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002024 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
2025 SourceLocation(), SourceLocation(),
2026 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002027}
2028
Steve Narofff406f4d2009-10-29 21:11:04 +00002029ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00002030 // The class interface might be NULL if we are working with invalid code.
2031 if (const ObjCInterfaceDecl *ID = getClassInterface())
2032 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00002033 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00002034}
2035
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002036void ObjCImplDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002037
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002038void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00002039 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002040 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002041 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002042}
2043
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002044void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
2045 ASTContext &Ctx = getASTContext();
2046
2047 if (ObjCImplementationDecl *ImplD
Duncan Sands49c29ee2009-07-21 07:56:29 +00002048 = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002049 if (IFace)
2050 Ctx.setObjCImplementation(IFace, ImplD);
2051
Duncan Sands49c29ee2009-07-21 07:56:29 +00002052 } else if (ObjCCategoryImplDecl *ImplD =
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002053 dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
2054 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
2055 Ctx.setObjCImplementation(CD, ImplD);
2056 }
2057
2058 ClassInterface = IFace;
2059}
2060
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002061/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00002062/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00002063/// the implemented property that uses it.
Chris Lattnera9ca0522009-02-28 18:42:10 +00002064ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002065FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00002066 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002067 if (PID->getPropertyIvarDecl() &&
2068 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
2069 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00002070 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002071}
2072
2073/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00002074/// added to the list of those properties \@synthesized/\@dynamic in this
2075/// category \@implementation block.
Chris Lattnera9ca0522009-02-28 18:42:10 +00002076ObjCPropertyImplDecl *ObjCImplDecl::
Manman Ren5b786402016-01-28 18:49:28 +00002077FindPropertyImplDecl(IdentifierInfo *Id,
2078 ObjCPropertyQueryKind QueryKind) const {
2079 ObjCPropertyImplDecl *ClassPropImpl = nullptr;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002080 for (auto *PID : property_impls())
Manman Ren5b786402016-01-28 18:49:28 +00002081 // If queryKind is unknown, we return the instance property if one
2082 // exists; otherwise we return the class property.
2083 if (PID->getPropertyDecl()->getIdentifier() == Id) {
2084 if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
2085 !PID->getPropertyDecl()->isClassProperty()) ||
2086 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
2087 PID->getPropertyDecl()->isClassProperty()) ||
2088 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
2089 !PID->getPropertyDecl()->isClassProperty()))
2090 return PID;
2091
2092 if (PID->getPropertyDecl()->isClassProperty())
2093 ClassPropImpl = PID;
2094 }
2095
2096 if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
2097 // We can't find the instance property, return the class property.
2098 return ClassPropImpl;
2099
Craig Topper36250ad2014-05-12 05:36:57 +00002100 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002101}
2102
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002103raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002104 const ObjCCategoryImplDecl &CID) {
2105 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002106 return OS;
2107}
2108
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002109//===----------------------------------------------------------------------===//
2110// ObjCImplementationDecl
2111//===----------------------------------------------------------------------===//
2112
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002113void ObjCImplementationDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002114
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002115ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00002116ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002117 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002118 ObjCInterfaceDecl *SuperDecl,
2119 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002120 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00002121 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002122 SourceLocation IvarLBraceLoc,
2123 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00002124 if (ClassInterface && ClassInterface->hasDefinition())
2125 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00002126 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
2127 nameLoc, atStartLoc, superLoc,
2128 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002129}
2130
Douglas Gregor72172e92012-01-05 21:55:30 +00002131ObjCImplementationDecl *
2132ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002133 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
2134 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002135}
2136
John McCall0410e572011-07-22 04:15:06 +00002137void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2138 CXXCtorInitializer ** initializers,
2139 unsigned numInitializers) {
2140 if (numInitializers > 0) {
2141 NumIvarInitializers = numInitializers;
2142 CXXCtorInitializer **ivarInitializers =
2143 new (C) CXXCtorInitializer*[NumIvarInitializers];
2144 memcpy(ivarInitializers, initializers,
2145 numInitializers * sizeof(CXXCtorInitializer*));
2146 IvarInitializers = ivarInitializers;
2147 }
2148}
2149
Richard Smithc2bb8182015-03-24 06:36:48 +00002150ObjCImplementationDecl::init_const_iterator
2151ObjCImplementationDecl::init_begin() const {
2152 return IvarInitializers.get(getASTContext().getExternalSource());
2153}
2154
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002155raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002156 const ObjCImplementationDecl &ID) {
2157 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002158 return OS;
2159}
2160
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002161//===----------------------------------------------------------------------===//
2162// ObjCCompatibleAliasDecl
2163//===----------------------------------------------------------------------===//
2164
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002165void ObjCCompatibleAliasDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002166
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002167ObjCCompatibleAliasDecl *
2168ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2169 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00002170 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002171 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00002172 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002173}
2174
Douglas Gregor72172e92012-01-05 21:55:30 +00002175ObjCCompatibleAliasDecl *
2176ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002177 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2178 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002179}
2180
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002181//===----------------------------------------------------------------------===//
2182// ObjCPropertyDecl
2183//===----------------------------------------------------------------------===//
2184
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002185void ObjCPropertyDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002186
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002187ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2188 SourceLocation L,
2189 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00002190 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002191 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00002192 QualType T,
2193 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002194 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002195 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2196 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002197}
2198
Richard Smithf7981722013-11-22 09:01:48 +00002199ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002200 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002201 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2202 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00002203 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00002204}
2205
Douglas Gregore83b9562015-07-07 03:57:53 +00002206QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2207 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2208 ObjCSubstitutionContext::Property);
2209}
2210
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002211//===----------------------------------------------------------------------===//
2212// ObjCPropertyImplDecl
2213//===----------------------------------------------------------------------===//
2214
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002215ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002216 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002217 SourceLocation atLoc,
2218 SourceLocation L,
2219 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002220 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002221 ObjCIvarDecl *ivar,
2222 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002223 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2224 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002225}
Chris Lattnered0e1642008-03-17 01:19:02 +00002226
Richard Smithf7981722013-11-22 09:01:48 +00002227ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002228 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002229 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2230 SourceLocation(), nullptr, Dynamic,
2231 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002232}
2233
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002234SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2235 SourceLocation EndLoc = getLocation();
2236 if (IvarLoc.isValid())
2237 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002238
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002239 return SourceRange(AtLoc, EndLoc);
2240}