blob: 5c8b34731f3637cb402f6ccf1151697937bb9cea [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner89375192008-03-16 00:19:01 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Objective-C related Decl classes.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/DeclObjC.h"
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000015#include "clang/AST/ASTMutationListener.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +000017#include "clang/AST/Decl.h"
18#include "clang/AST/DeclBase.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000019#include "clang/AST/Stmt.h"
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +000020#include "clang/AST/Type.h"
21#include "clang/AST/TypeLoc.h"
22#include "clang/Basic/IdentifierTable.h"
23#include "clang/Basic/LLVM.h"
24#include "clang/Basic/LangOptions.h"
25#include "clang/Basic/SourceLocation.h"
26#include "llvm/ADT/None.h"
Anna Zaks454477c2012-09-27 19:45:11 +000027#include "llvm/ADT/SmallString.h"
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +000028#include "llvm/ADT/SmallVector.h"
29#include "llvm/Support/Casting.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/raw_ostream.h"
32#include <algorithm>
33#include <cassert>
34#include <cstdint>
35#include <cstring>
36#include <utility>
37
Chris Lattner89375192008-03-16 00:19:01 +000038using namespace clang;
39
Chris Lattner8d8829e2008-03-16 00:49:28 +000040//===----------------------------------------------------------------------===//
Chris Lattner4d1eb762009-02-20 21:16:26 +000041// ObjCListBase
42//===----------------------------------------------------------------------===//
43
Chris Lattner22298722009-02-20 21:35:13 +000044void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
Craig Topper36250ad2014-05-12 05:36:57 +000045 List = nullptr;
Chris Lattner4d1eb762009-02-20 21:16:26 +000046 if (Elts == 0) return; // Setting to an empty list is a noop.
Mike Stump11289f42009-09-09 15:08:12 +000047
Chris Lattner7c981a72009-02-20 21:44:01 +000048 List = new (Ctx) void*[Elts];
Chris Lattner4d1eb762009-02-20 21:16:26 +000049 NumElts = Elts;
50 memcpy(List, InList, sizeof(void*)*Elts);
51}
52
Fangrui Song6907ce22018-07-30 19:24:48 +000053void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
Douglas Gregor002b6712010-01-16 15:02:53 +000054 const SourceLocation *Locs, ASTContext &Ctx) {
55 if (Elts == 0)
56 return;
57
58 Locations = new (Ctx) SourceLocation[Elts];
59 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
60 set(InList, Elts, Ctx);
61}
62
Chris Lattner4d1eb762009-02-20 21:16:26 +000063//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +000064// ObjCInterfaceDecl
Chris Lattner8d8829e2008-03-16 00:49:28 +000065//===----------------------------------------------------------------------===//
66
Erich Keane9b18eca2018-08-01 21:31:08 +000067ObjCContainerDecl::ObjCContainerDecl(Kind DK, DeclContext *DC,
68 IdentifierInfo *Id, SourceLocation nameLoc,
69 SourceLocation atStartLoc)
70 : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK) {
71 setAtStartLoc(atStartLoc);
72}
73
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +000074void ObjCContainerDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000075
Fariborz Jahanian68453832009-06-05 18:16:35 +000076/// getIvarDecl - This method looks up an ivar in this ContextDecl.
77///
78ObjCIvarDecl *
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000079ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
Richard Smithcf4bdde2015-02-21 02:45:19 +000080 lookup_result R = lookup(Id);
81 for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +000082 Ivar != IvarEnd; ++Ivar) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +000083 if (auto *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
Fariborz Jahanian68453832009-06-05 18:16:35 +000084 return ivar;
85 }
Craig Topper36250ad2014-05-12 05:36:57 +000086 return nullptr;
Fariborz Jahanian68453832009-06-05 18:16:35 +000087}
88
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +000089// Get the local instance/class method declared in this interface.
Douglas Gregorbcced4e2009-04-09 21:40:53 +000090ObjCMethodDecl *
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000091ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
92 bool AllowHidden) const {
Douglas Gregoreed49792013-01-17 00:38:46 +000093 // If this context is a hidden protocol definition, don't find any
94 // methods there.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +000095 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
Douglas Gregoreed49792013-01-17 00:38:46 +000096 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Martin Boehme2e92b392020-06-08 15:37:44 +020097 if (!Def->isUnconditionallyVisible() && !AllowHidden)
Craig Topper36250ad2014-05-12 05:36:57 +000098 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +000099 }
100
Steve Naroffc4173fa2009-02-22 19:35:57 +0000101 // Since instance & class methods can have the same name, the loop below
102 // ensures we get the correct method.
103 //
104 // @interface Whatever
105 // - (int) class_method;
106 // + (float) class_method;
107 // @end
Richard Smithcf4bdde2015-02-21 02:45:19 +0000108 lookup_result R = lookup(Sel);
109 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +0000110 Meth != MethEnd; ++Meth) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000111 auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Argyrios Kyrtzidis6de05602009-07-25 22:15:22 +0000112 if (MD && MD->isInstanceMethod() == isInstance)
Steve Naroffc4173fa2009-02-22 19:35:57 +0000113 return MD;
114 }
Craig Topper36250ad2014-05-12 05:36:57 +0000115 return nullptr;
Steve Naroff35c62ae2009-01-08 17:28:14 +0000116}
117
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000118/// This routine returns 'true' if a user declared setter method was
Nico Weber4701ffd2014-12-22 05:21:03 +0000119/// found in the class, its protocols, its super classes or categories.
120/// It also returns 'true' if one of its categories has declared a 'readwrite'
121/// property. This is because, user must provide a setter method for the
122/// category's 'readwrite' property.
123bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
124 const ObjCPropertyDecl *Property) const {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000125 Selector Sel = Property->getSetterName();
Richard Smithcf4bdde2015-02-21 02:45:19 +0000126 lookup_result R = lookup(Sel);
127 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000128 Meth != MethEnd; ++Meth) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000129 auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000130 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
131 return true;
132 }
133
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000134 if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000135 // Also look into categories, including class extensions, looking
136 // for a user declared instance method.
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000137 for (const auto *Cat : ID->visible_categories()) {
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000138 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
139 if (!MD->isImplicit())
140 return true;
141 if (Cat->IsClassExtension())
142 continue;
Nico Weber4701ffd2014-12-22 05:21:03 +0000143 // Also search through the categories looking for a 'readwrite'
144 // declaration of this property. If one found, presumably a setter will
145 // be provided (properties declared in categories will not get
146 // auto-synthesized).
Manman Renefe1bac2016-01-27 20:00:32 +0000147 for (const auto *P : Cat->properties())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000148 if (P->getIdentifier() == Property->getIdentifier()) {
Puyan Lotfi9721fbf2020-04-23 02:20:56 -0400149 if (P->getPropertyAttributes() &
150 ObjCPropertyAttribute::kind_readwrite)
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000151 return true;
152 break;
153 }
154 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000155
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000156 // Also look into protocols, for a user declared instance method.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000157 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000158 if (Proto->HasUserDeclaredSetterMethod(Property))
159 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000160
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000161 // And in its super class.
162 ObjCInterfaceDecl *OSC = ID->getSuperClass();
163 while (OSC) {
164 if (OSC->HasUserDeclaredSetterMethod(Property))
165 return true;
166 OSC = OSC->getSuperClass();
167 }
168 }
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000169 if (const auto *PD = dyn_cast<ObjCProtocolDecl>(this))
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000170 for (const auto *PI : PD->protocols())
171 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000172 return true;
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000173 return false;
174}
175
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000176ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000177ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Manman Ren5b786402016-01-28 18:49:28 +0000178 const IdentifierInfo *propertyID,
179 ObjCPropertyQueryKind queryKind) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000180 // If this context is a hidden protocol definition, don't find any
181 // property.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000182 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000183 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Martin Boehme2e92b392020-06-08 15:37:44 +0200184 if (!Def->isUnconditionallyVisible())
Craig Topper36250ad2014-05-12 05:36:57 +0000185 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000186 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000187
Alex Lorenzf9a28a22017-02-22 23:18:49 +0000188 // If context is class, then lookup property in its visible extensions.
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000189 // This comes before property is looked up in primary class.
190 if (auto *IDecl = dyn_cast<ObjCInterfaceDecl>(DC)) {
Alex Lorenzf9a28a22017-02-22 23:18:49 +0000191 for (const auto *Ext : IDecl->visible_extensions())
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000192 if (ObjCPropertyDecl *PD = ObjCPropertyDecl::findPropertyDecl(Ext,
Manman Ren5b786402016-01-28 18:49:28 +0000193 propertyID,
194 queryKind))
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000195 return PD;
196 }
197
Richard Smithcf4bdde2015-02-21 02:45:19 +0000198 DeclContext::lookup_result R = DC->lookup(propertyID);
Manman Ren5b786402016-01-28 18:49:28 +0000199 ObjCPropertyDecl *classProp = nullptr;
Richard Smithcf4bdde2015-02-21 02:45:19 +0000200 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
David Blaikieff7d47a2012-12-19 00:45:41 +0000201 ++I)
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000202 if (auto *PD = dyn_cast<ObjCPropertyDecl>(*I)) {
Manman Ren5b786402016-01-28 18:49:28 +0000203 // If queryKind is unknown, we return the instance property if one
204 // exists; otherwise we return the class property.
205 if ((queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
206 !PD->isClassProperty()) ||
207 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
208 PD->isClassProperty()) ||
209 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
210 !PD->isClassProperty()))
211 return PD;
212
213 if (PD->isClassProperty())
214 classProp = PD;
215 }
216
217 if (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
218 // We can't find the instance property, return the class property.
219 return classProp;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000220
Craig Topper36250ad2014-05-12 05:36:57 +0000221 return nullptr;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000222}
223
Anna Zaks454477c2012-09-27 19:45:11 +0000224IdentifierInfo *
225ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
226 SmallString<128> ivarName;
227 {
228 llvm::raw_svector_ostream os(ivarName);
229 os << '_' << getIdentifier()->getName();
230 }
231 return &Ctx.Idents.get(ivarName.str());
232}
233
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000234/// FindPropertyDeclaration - Finds declaration of the property given its name
235/// in 'PropertyId' and returns it. It returns 0, if not found.
Jordan Rose210bfe92014-11-19 22:03:46 +0000236ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
Manman Ren5b786402016-01-28 18:49:28 +0000237 const IdentifierInfo *PropertyId,
238 ObjCPropertyQueryKind QueryKind) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000239 // Don't find properties within hidden protocol definitions.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000240 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000241 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
Martin Boehme2e92b392020-06-08 15:37:44 +0200242 if (!Def->isUnconditionallyVisible())
Craig Topper36250ad2014-05-12 05:36:57 +0000243 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000244 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000245
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000246 // Search the extensions of a class first; they override what's in
247 // the class itself.
248 if (const auto *ClassDecl = dyn_cast<ObjCInterfaceDecl>(this)) {
249 for (const auto *Ext : ClassDecl->visible_extensions()) {
Manman Ren5b786402016-01-28 18:49:28 +0000250 if (auto *P = Ext->FindPropertyDeclaration(PropertyId, QueryKind))
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000251 return P;
252 }
253 }
Mike Stump11289f42009-09-09 15:08:12 +0000254
Ted Kremenekddcd1092010-03-15 20:11:53 +0000255 if (ObjCPropertyDecl *PD =
Manman Ren5b786402016-01-28 18:49:28 +0000256 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
257 QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000258 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000259
Ted Kremenekddcd1092010-03-15 20:11:53 +0000260 switch (getKind()) {
261 default:
262 break;
263 case Decl::ObjCProtocol: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000264 const auto *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000265 for (const auto *I : PID->protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000266 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
267 QueryKind))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000268 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000269 break;
270 }
271 case Decl::ObjCInterface: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000272 const auto *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000273 // Look through categories (but not extensions; they were handled above).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000274 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000275 if (!Cat->IsClassExtension())
Manman Ren5b786402016-01-28 18:49:28 +0000276 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(
277 PropertyId, QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000278 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000279 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000280
281 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000282 for (const auto *I : OID->all_referenced_protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000283 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
284 QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000285 return P;
286
287 // Finally, check the super class.
288 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
Manman Ren5b786402016-01-28 18:49:28 +0000289 return superClass->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekddcd1092010-03-15 20:11:53 +0000290 break;
291 }
292 case Decl::ObjCCategory: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000293 const auto *OCD = cast<ObjCCategoryDecl>(this);
Ted Kremenekddcd1092010-03-15 20:11:53 +0000294 // Look through protocols.
295 if (!OCD->IsClassExtension())
Aaron Ballman19a41762014-03-14 12:55:57 +0000296 for (const auto *I : OCD->protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000297 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
298 QueryKind))
Aaron Ballman19a41762014-03-14 12:55:57 +0000299 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000300 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000301 }
302 }
Craig Topper36250ad2014-05-12 05:36:57 +0000303 return nullptr;
Steve Narofff9c65242008-06-05 13:55:23 +0000304}
305
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +0000306void ObjCInterfaceDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000307
Douglas Gregor85f3f952015-07-07 03:57:15 +0000308ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
309 // If this particular declaration has a type parameter list, return it.
310 if (ObjCTypeParamList *written = getTypeParamListAsWritten())
311 return written;
312
313 // If there is a definition, return its type parameter list.
314 if (const ObjCInterfaceDecl *def = getDefinition())
315 return def->getTypeParamListAsWritten();
316
317 // Otherwise, look at previous declarations to determine whether any
318 // of them has a type parameter list, skipping over those
319 // declarations that do not.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000320 for (const ObjCInterfaceDecl *decl = getMostRecentDecl(); decl;
321 decl = decl->getPreviousDecl()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000322 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
323 return written;
324 }
325
326 return nullptr;
327}
328
Douglas Gregorab7f0b32015-07-07 06:20:12 +0000329void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
330 TypeParamList = TPL;
331 if (!TPL)
332 return;
333 // Set the declaration context of each of the type parameters.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000334 for (auto *typeParam : *TypeParamList)
Douglas Gregorab7f0b32015-07-07 06:20:12 +0000335 typeParam->setDeclContext(this);
336}
337
Douglas Gregore9d95f12015-07-07 03:57:35 +0000338ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
339 // FIXME: Should make sure no callers ever do this.
340 if (!hasDefinition())
341 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +0000342
Douglas Gregore9d95f12015-07-07 03:57:35 +0000343 if (data().ExternallyCompleted)
344 LoadExternalDefinition();
345
346 if (const ObjCObjectType *superType = getSuperClassType()) {
347 if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
348 if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
349 return superDef;
350
351 return superDecl;
352 }
353 }
354
355 return nullptr;
356}
357
358SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
359 if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000360 return superTInfo->getTypeLoc().getBeginLoc();
Fangrui Song6907ce22018-07-30 19:24:48 +0000361
Douglas Gregore9d95f12015-07-07 03:57:35 +0000362 return SourceLocation();
363}
364
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000365/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
366/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000367/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000368ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000369ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Manman Ren5b786402016-01-28 18:49:28 +0000370 IdentifierInfo *PropertyId,
371 ObjCPropertyQueryKind QueryKind) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000372 // FIXME: Should make sure no callers ever do this.
373 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000374 return nullptr;
375
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000376 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000377 LoadExternalDefinition();
378
Ted Kremenekd133a862010-03-15 20:30:07 +0000379 if (ObjCPropertyDecl *PD =
Manman Ren5b786402016-01-28 18:49:28 +0000380 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
381 QueryKind))
Ted Kremenekd133a862010-03-15 20:30:07 +0000382 return PD;
383
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000384 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000385 for (const auto *I : all_referenced_protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000386 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
387 QueryKind))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000388 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000389
Craig Topper36250ad2014-05-12 05:36:57 +0000390 return nullptr;
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000391}
392
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000393void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
394 PropertyDeclOrder &PO) const {
Manman Ren494ee5b2016-01-28 23:36:05 +0000395 for (auto *Prop : properties()) {
396 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000397 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000398 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000399 for (const auto *Ext : known_extensions()) {
400 const ObjCCategoryDecl *ClassExt = Ext;
Manman Ren494ee5b2016-01-28 23:36:05 +0000401 for (auto *Prop : ClassExt->properties()) {
402 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000403 PO.push_back(Prop);
404 }
405 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000406 for (const auto *PI : all_referenced_protocols())
407 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000408 // Note, the properties declared only in class extensions are still copied
409 // into the main @interface's property list, and therefore we don't
410 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000411}
412
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000413bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
414 const ObjCInterfaceDecl *Class = this;
415 while (Class) {
416 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
417 return true;
418 Class = Class->getSuperClass();
419 }
420 return false;
421}
422
423const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
424 const ObjCInterfaceDecl *Class = this;
425 while (Class) {
426 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
427 return Class;
428 Class = Class->getSuperClass();
429 }
Craig Topper36250ad2014-05-12 05:36:57 +0000430 return nullptr;
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000431}
432
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000433void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
434 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +0000435 ASTContext &C) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000436 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000437 LoadExternalDefinition();
438
Fangrui Song6907ce22018-07-30 19:24:48 +0000439 if (data().AllReferencedProtocols.empty() &&
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000440 data().ReferencedProtocols.empty()) {
441 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000442 return;
443 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000444
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000445 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000446 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000447 // class or its extension are very few.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000448 SmallVector<ObjCProtocolDecl *, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000449 for (unsigned i = 0; i < ExtNum; i++) {
450 bool protocolExists = false;
451 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000452 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000453 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
454 protocolExists = true;
455 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000456 }
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000457 }
458 // Do we want to warn on a protocol in extension class which
459 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000460 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000461 ProtocolRefs.push_back(ProtoInExtension);
462 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000463
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000464 if (ProtocolRefs.empty())
465 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000466
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000467 // Merge ProtocolRefs into class's protocol list;
Benjamin Kramerf9890422015-02-17 16:48:30 +0000468 ProtocolRefs.append(all_referenced_protocol_begin(),
469 all_referenced_protocol_end());
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000470
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000471 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000472}
473
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000474const ObjCInterfaceDecl *
475ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
476 const ObjCInterfaceDecl *IFace = this;
477 while (IFace) {
478 if (IFace->hasDesignatedInitializers())
479 return IFace;
480 if (!IFace->inheritsDesignatedInitializers())
481 break;
482 IFace = IFace->getSuperClass();
483 }
Craig Topper36250ad2014-05-12 05:36:57 +0000484 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000485}
486
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000487static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
488 for (const auto *MD : D->instance_methods()) {
489 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
490 return true;
491 }
492 for (const auto *Ext : D->visible_extensions()) {
493 for (const auto *MD : Ext->instance_methods()) {
494 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
495 return true;
496 }
497 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000498 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000499 for (const auto *MD : ImplD->instance_methods()) {
500 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
501 return true;
502 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000503 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000504 return false;
505}
506
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000507bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
508 switch (data().InheritedDesignatedInitializers) {
509 case DefinitionData::IDI_Inherited:
510 return true;
511 case DefinitionData::IDI_NotInherited:
512 return false;
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +0000513 case DefinitionData::IDI_Unknown:
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000514 // If the class introduced initializers we conservatively assume that we
515 // don't know if any of them is a designated initializer to avoid possible
516 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000517 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000518 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000519 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000520 if (auto SuperD = getSuperClass()) {
521 data().InheritedDesignatedInitializers =
522 SuperD->declaresOrInheritsDesignatedInitializers() ?
523 DefinitionData::IDI_Inherited :
524 DefinitionData::IDI_NotInherited;
525 } else {
526 data().InheritedDesignatedInitializers =
527 DefinitionData::IDI_NotInherited;
528 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000529 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000530 assert(data().InheritedDesignatedInitializers
531 != DefinitionData::IDI_Unknown);
532 return data().InheritedDesignatedInitializers ==
533 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000534 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000535
536 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
537}
538
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000539void ObjCInterfaceDecl::getDesignatedInitializers(
540 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000541 // Check for a complete definition and recover if not so.
542 if (!isThisDeclarationADefinition())
543 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000544 if (data().ExternallyCompleted)
545 LoadExternalDefinition();
546
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000547 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000548 if (!IFace)
549 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000550
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000551 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000552 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000553 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000554 for (const auto *Ext : IFace->visible_extensions()) {
555 for (const auto *MD : Ext->instance_methods())
556 if (MD->isThisDeclarationADesignatedInitializer())
557 Methods.push_back(MD);
558 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000559}
560
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000561bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
562 const ObjCMethodDecl **InitMethod) const {
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000563 bool HasCompleteDef = isThisDeclarationADefinition();
564 // During deserialization the data record for the ObjCInterfaceDecl could
565 // be made invariant by reusing the canonical decl. Take this into account
566 // when checking for the complete definition.
567 if (!HasCompleteDef && getCanonicalDecl()->hasDefinition() &&
568 getCanonicalDecl()->getDefinition() == getDefinition())
569 HasCompleteDef = true;
570
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000571 // Check for a complete definition and recover if not so.
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000572 if (!HasCompleteDef)
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000573 return false;
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000574
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000575 if (data().ExternallyCompleted)
576 LoadExternalDefinition();
577
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000578 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000579 if (!IFace)
580 return false;
581
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000582 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000583 if (MD->isThisDeclarationADesignatedInitializer()) {
584 if (InitMethod)
585 *InitMethod = MD;
586 return true;
587 }
588 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000589 for (const auto *Ext : IFace->visible_extensions()) {
590 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
591 if (MD->isThisDeclarationADesignatedInitializer()) {
592 if (InitMethod)
593 *InitMethod = MD;
594 return true;
595 }
596 }
597 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000598 return false;
599}
600
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000601void ObjCInterfaceDecl::allocateDefinitionData() {
602 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000603 Data.setPointer(new (getASTContext()) DefinitionData());
604 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000605
606 // Make the type point at the definition, now that we have one.
607 if (TypeForDecl)
608 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000609}
610
611void ObjCInterfaceDecl::startDefinition() {
612 allocateDefinitionData();
613
Douglas Gregor66b310c2011-12-15 18:03:09 +0000614 // Update all of the declarations with a pointer to the definition.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000615 for (auto *RD : redecls()) {
Aaron Ballman86c93902014-03-06 23:45:36 +0000616 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000617 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000618 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000619}
620
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000621ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
622 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000623 // FIXME: Should make sure no callers ever do this.
624 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000625 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000626
627 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000628 LoadExternalDefinition();
629
Chris Lattner89375192008-03-16 00:19:01 +0000630 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000631 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000632 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000633 clsDeclared = ClassDecl;
634 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000635 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000636
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000637 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000638 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000639 clsDeclared = ClassDecl;
640 return I;
641 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000642 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000643
Chris Lattner89375192008-03-16 00:19:01 +0000644 ClassDecl = ClassDecl->getSuperClass();
645 }
Craig Topper36250ad2014-05-12 05:36:57 +0000646 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000647}
648
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000649/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
650/// class whose name is passed as argument. If it is not one of the super classes
651/// the it returns NULL.
652ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
653 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000654 // FIXME: Should make sure no callers ever do this.
655 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000656 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000657
658 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000659 LoadExternalDefinition();
660
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000661 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000662 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000663 if (ClassDecl->getIdentifier() == ICName)
664 return ClassDecl;
665 ClassDecl = ClassDecl->getSuperClass();
666 }
Craig Topper36250ad2014-05-12 05:36:57 +0000667 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000668}
669
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000670ObjCProtocolDecl *
671ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000672 for (auto *P : all_referenced_protocols())
673 if (P->lookupProtocolNamed(Name))
674 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000675 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000676 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000677}
678
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000679/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000680/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000681/// When argument category "C" is specified, any implicit method found
682/// in this category is ignored.
Fangrui Song6907ce22018-07-30 19:24:48 +0000683ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000684 bool isInstance,
685 bool shallowCategoryLookup,
686 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000687 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000688{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000689 // FIXME: Should make sure no callers ever do this.
690 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000691 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000692
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000693 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000694 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000695
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000696 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000697 LoadExternalDefinition();
698
Ted Kremenek00781502013-11-23 01:01:29 +0000699 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000700 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000701 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000702 return MethodDecl;
Fangrui Song6907ce22018-07-30 19:24:48 +0000703
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000704 // 2. Didn't find one yet - now look through categories.
705 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000706 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000707 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000708 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000709
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000710 // 3. Didn't find one yet - look through primary class's protocols.
711 for (const auto *I : ClassDecl->protocols())
712 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
713 return MethodDecl;
Fangrui Song6907ce22018-07-30 19:24:48 +0000714
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000715 // 4. Didn't find one yet - now look through categories' protocols
716 if (!shallowCategoryLookup)
717 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000718 // Didn't find one yet - look through protocols.
719 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000720 Cat->getReferencedProtocols();
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000721 for (auto *Protocol : Protocols)
722 if ((MethodDecl = Protocol->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000723 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000724 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000725 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000726
727
Ted Kremenek00781502013-11-23 01:01:29 +0000728 if (!followSuper)
Craig Topper36250ad2014-05-12 05:36:57 +0000729 return nullptr;
Ted Kremenek00781502013-11-23 01:01:29 +0000730
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000731 // 5. Get to the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000732 ClassDecl = ClassDecl->getSuperClass();
733 }
Craig Topper36250ad2014-05-12 05:36:57 +0000734 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000735}
736
Anna Zaksc77a3b12012-07-27 19:07:44 +0000737// Will search "local" class/category implementations for a method decl.
738// If failed, then we search in class's root for an instance method.
739// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000740ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
741 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000742 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000743 // FIXME: Should make sure no callers ever do this.
744 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000745 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000746
747 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000748 LoadExternalDefinition();
749
Craig Topper36250ad2014-05-12 05:36:57 +0000750 ObjCMethodDecl *Method = nullptr;
Steve Naroffbb69c942009-10-01 23:46:04 +0000751 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fangrui Song6907ce22018-07-30 19:24:48 +0000752 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000753 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000754
755 // Look through local category implementations associated with the class.
756 if (!Method)
Nico Weberf6098392015-03-02 01:12:28 +0000757 Method = getCategoryMethod(Sel, Instance);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000758
759 // Before we give up, check if the selector is an instance method.
760 // But only in the root. This matches gcc's behavior and what the
761 // runtime expects.
762 if (!Instance && !Method && !getSuperClass()) {
763 Method = lookupInstanceMethod(Sel);
764 // Look through local category implementations associated
765 // with the root class.
766 if (!Method)
767 Method = lookupPrivateMethod(Sel, true);
768 }
769
Steve Naroffbb69c942009-10-01 23:46:04 +0000770 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000771 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000772 return Method;
773}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000774
775//===----------------------------------------------------------------------===//
776// ObjCMethodDecl
777//===----------------------------------------------------------------------===//
778
Adrian Prantl2073dd22019-11-04 14:28:14 -0800779ObjCMethodDecl::ObjCMethodDecl(
780 SourceLocation beginLoc, SourceLocation endLoc, Selector SelInfo,
781 QualType T, TypeSourceInfo *ReturnTInfo, DeclContext *contextDecl,
782 bool isInstance, bool isVariadic, bool isPropertyAccessor,
783 bool isSynthesizedAccessorStub, bool isImplicitlyDeclared, bool isDefined,
784 ImplementationControl impControl, bool HasRelatedResultType)
Erich Keane9b18eca2018-08-01 21:31:08 +0000785 : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
786 DeclContext(ObjCMethod), MethodDeclType(T), ReturnTInfo(ReturnTInfo),
787 DeclEndLoc(endLoc) {
Erich Keane9b18eca2018-08-01 21:31:08 +0000788
789 // Initialized the bits stored in DeclContext.
790 ObjCMethodDeclBits.Family =
791 static_cast<ObjCMethodFamily>(InvalidObjCMethodFamily);
792 setInstanceMethod(isInstance);
793 setVariadic(isVariadic);
794 setPropertyAccessor(isPropertyAccessor);
Adrian Prantl2073dd22019-11-04 14:28:14 -0800795 setSynthesizedAccessorStub(isSynthesizedAccessorStub);
Erich Keane9b18eca2018-08-01 21:31:08 +0000796 setDefined(isDefined);
797 setIsRedeclaration(false);
798 setHasRedeclaration(false);
799 setDeclImplementation(impControl);
800 setObjCDeclQualifier(OBJC_TQ_None);
801 setRelatedResultType(HasRelatedResultType);
802 setSelLocsKind(SelLoc_StandardNoSpace);
803 setOverriding(false);
804 setHasSkippedBody(false);
805
806 setImplicit(isImplicitlyDeclared);
807}
808
Alp Toker314cc812014-01-25 16:55:45 +0000809ObjCMethodDecl *ObjCMethodDecl::Create(
810 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
811 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
812 DeclContext *contextDecl, bool isInstance, bool isVariadic,
Adrian Prantl2073dd22019-11-04 14:28:14 -0800813 bool isPropertyAccessor, bool isSynthesizedAccessorStub,
814 bool isImplicitlyDeclared, bool isDefined, ImplementationControl impControl,
815 bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000816 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000817 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Adrian Prantl2073dd22019-11-04 14:28:14 -0800818 isVariadic, isPropertyAccessor, isSynthesizedAccessorStub,
819 isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000820}
821
Douglas Gregor72172e92012-01-05 21:55:30 +0000822ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000823 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000824 Selector(), QualType(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000825}
826
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -0800827bool ObjCMethodDecl::isDirectMethod() const {
828 return hasAttr<ObjCDirectAttr>();
829}
830
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000831bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
832 return getMethodFamily() == OMF_init &&
833 hasAttr<ObjCDesignatedInitializerAttr>();
834}
835
Erik Pilkington42578572018-09-10 22:20:09 +0000836bool ObjCMethodDecl::definedInNSObject(const ASTContext &Ctx) const {
837 if (const auto *PD = dyn_cast<const ObjCProtocolDecl>(getDeclContext()))
838 return PD->getIdentifier() == Ctx.getNSObjectName();
839 if (const auto *ID = dyn_cast<const ObjCInterfaceDecl>(getDeclContext()))
840 return ID->getIdentifier() == Ctx.getNSObjectName();
841 return false;
842}
843
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000844bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
845 const ObjCMethodDecl **InitMethod) const {
846 if (getMethodFamily() != OMF_init)
847 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000848 const DeclContext *DC = getDeclContext();
849 if (isa<ObjCProtocolDecl>(DC))
850 return false;
851 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000852 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000853 return false;
854}
855
Douglas Gregora6017bb2012-10-09 17:21:28 +0000856Stmt *ObjCMethodDecl::getBody() const {
857 return Body.get(getASTContext().getExternalSource());
858}
859
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000860void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
861 assert(PrevMethod);
862 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
Erich Keane9b18eca2018-08-01 21:31:08 +0000863 setIsRedeclaration(true);
864 PrevMethod->setHasRedeclaration(true);
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000865}
866
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000867void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
868 ArrayRef<ParmVarDecl*> Params,
869 ArrayRef<SourceLocation> SelLocs) {
Craig Topper36250ad2014-05-12 05:36:57 +0000870 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000871 NumParams = Params.size();
872 if (Params.empty() && SelLocs.empty())
873 return;
874
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000875 static_assert(alignof(ParmVarDecl *) >= alignof(SourceLocation),
James Y Knight967eb202015-12-29 22:13:13 +0000876 "Alignment not sufficient for SourceLocation");
877
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000878 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
879 sizeof(SourceLocation) * SelLocs.size();
880 ParamsAndSelLocs = C.Allocate(Size);
881 std::copy(Params.begin(), Params.end(), getParams());
882 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
883}
884
885void ObjCMethodDecl::getSelectorLocs(
886 SmallVectorImpl<SourceLocation> &SelLocs) const {
887 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
888 SelLocs.push_back(getSelectorLoc(i));
889}
890
891void ObjCMethodDecl::setMethodParams(ASTContext &C,
892 ArrayRef<ParmVarDecl*> Params,
893 ArrayRef<SourceLocation> SelLocs) {
894 assert((!SelLocs.empty() || isImplicit()) &&
895 "No selector locs for non-implicit method");
896 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000897 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000898
Erich Keane9b18eca2018-08-01 21:31:08 +0000899 setSelLocsKind(hasStandardSelectorLocs(getSelector(), SelLocs, Params,
900 DeclEndLoc));
901 if (getSelLocsKind() != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000902 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000903
904 setParamsAndSelLocs(C, Params, SelLocs);
905}
906
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000907/// A definition will return its interface declaration.
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000908/// An interface declaration will return its definition.
909/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000910ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000911 ASTContext &Ctx = getASTContext();
Craig Topper36250ad2014-05-12 05:36:57 +0000912 ObjCMethodDecl *Redecl = nullptr;
Erich Keane9b18eca2018-08-01 21:31:08 +0000913 if (hasRedeclaration())
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000914 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000915 if (Redecl)
916 return Redecl;
917
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000918 auto *CtxD = cast<Decl>(getDeclContext());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000919
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000920 if (!CtxD->isInvalidDecl()) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000921 if (auto *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000922 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
923 if (!ImplD->isInvalidDecl())
924 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000925
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000926 } else if (auto *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000927 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
928 if (!ImplD->isInvalidDecl())
929 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000930
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000931 } else if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000932 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
933 if (!IFD->isInvalidDecl())
934 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000935
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000936 } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000937 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
938 if (!CatD->isInvalidDecl())
939 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
940 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000941 }
942
Alex Lorenzff6c34b2016-11-21 11:16:30 +0000943 // Ensure that the discovered method redeclaration has a valid declaration
944 // context. Used to prevent infinite loops when iterating redeclarations in
945 // a partially invalid AST.
946 if (Redecl && cast<Decl>(Redecl->getDeclContext())->isInvalidDecl())
947 Redecl = nullptr;
948
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000949 if (!Redecl && isRedeclaration()) {
950 // This is the last redeclaration, go back to the first method.
951 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
952 isInstanceMethod());
953 }
954
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000955 return Redecl ? Redecl : this;
956}
957
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000958ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000959 auto *CtxD = cast<Decl>(getDeclContext());
Pierre Habouzit42f9d0c2019-12-19 01:25:03 -0800960 const auto &Sel = getSelector();
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000961
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000962 if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
Pierre Habouzita4e18192019-12-17 11:06:17 -0800963 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) {
Pierre Habouzit42f9d0c2019-12-19 01:25:03 -0800964 // When the container is the ObjCImplementationDecl (the primary
965 // @implementation), then the canonical Decl is either in
966 // the class Interface, or in any of its extension.
967 //
968 // So when we don't find it in the ObjCInterfaceDecl,
969 // sift through extensions too.
970 if (ObjCMethodDecl *MD = IFD->getMethod(Sel, isInstanceMethod()))
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000971 return MD;
Pierre Habouzit42f9d0c2019-12-19 01:25:03 -0800972 for (auto *Ext : IFD->known_extensions())
973 if (ObjCMethodDecl *MD = Ext->getMethod(Sel, isInstanceMethod()))
974 return MD;
Pierre Habouzita4e18192019-12-17 11:06:17 -0800975 }
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000976 } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000977 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Pierre Habouzit42f9d0c2019-12-19 01:25:03 -0800978 if (ObjCMethodDecl *MD = CatD->getMethod(Sel, isInstanceMethod()))
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000979 return MD;
980 }
981
Manman Rena0042c42016-10-03 21:26:46 +0000982 if (isRedeclaration()) {
983 // It is possible that we have not done deserializing the ObjCMethod yet.
984 ObjCMethodDecl *MD =
Pierre Habouzit42f9d0c2019-12-19 01:25:03 -0800985 cast<ObjCContainerDecl>(CtxD)->getMethod(Sel, isInstanceMethod());
Manman Rena0042c42016-10-03 21:26:46 +0000986 return MD ? MD : this;
987 }
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000988
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000989 return this;
990}
991
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000992SourceLocation ObjCMethodDecl::getEndLoc() const {
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000993 if (Stmt *Body = getBody())
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000994 return Body->getEndLoc();
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000995 return DeclEndLoc;
996}
997
John McCallb4526252011-03-02 01:50:55 +0000998ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
Erich Keane9b18eca2018-08-01 21:31:08 +0000999 auto family = static_cast<ObjCMethodFamily>(ObjCMethodDeclBits.Family);
John McCallfb55f852011-03-02 21:01:41 +00001000 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +00001001 return family;
1002
John McCall86bc21f2011-03-02 11:33:24 +00001003 // Check for an explicit attribute.
1004 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
1005 // The unfortunate necessity of mapping between enums here is due
1006 // to the attributes framework.
1007 switch (attr->getFamily()) {
1008 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
1009 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
1010 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
1011 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
1012 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
1013 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
1014 }
Erich Keane9b18eca2018-08-01 21:31:08 +00001015 ObjCMethodDeclBits.Family = family;
John McCall86bc21f2011-03-02 11:33:24 +00001016 return family;
1017 }
1018
John McCallb4526252011-03-02 01:50:55 +00001019 family = getSelector().getMethodFamily();
1020 switch (family) {
1021 case OMF_None: break;
1022
1023 // init only has a conventional meaning for an instance method, and
1024 // it has to return an object.
1025 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +00001026 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +00001027 family = OMF_None;
1028 break;
1029
1030 // alloc/copy/new have a conventional meaning for both class and
1031 // instance methods, but they require an object return.
1032 case OMF_alloc:
1033 case OMF_copy:
1034 case OMF_mutableCopy:
1035 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +00001036 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +00001037 family = OMF_None;
1038 break;
1039
1040 // These selectors have a conventional meaning only for instance methods.
1041 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00001042 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +00001043 case OMF_retain:
1044 case OMF_release:
1045 case OMF_autorelease:
1046 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +00001047 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +00001048 if (!isInstanceMethod())
1049 family = OMF_None;
1050 break;
Fangrui Song6907ce22018-07-30 19:24:48 +00001051
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +00001052 case OMF_initialize:
1053 if (isInstanceMethod() || !getReturnType()->isVoidType())
1054 family = OMF_None;
1055 break;
Fangrui Song6907ce22018-07-30 19:24:48 +00001056
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001057 case OMF_performSelector:
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001058 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001059 family = OMF_None;
1060 else {
1061 unsigned noParams = param_size();
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001062 if (noParams < 1 || noParams > 3)
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001063 family = OMF_None;
1064 else {
Alp Toker1f307f42014-01-25 17:32:04 +00001065 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001066 QualType ArgT = (*it);
1067 if (!ArgT->isObjCSelType()) {
1068 family = OMF_None;
1069 break;
1070 }
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001071 while (--noParams) {
1072 it++;
1073 ArgT = (*it);
1074 if (!ArgT->isObjCIdType()) {
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001075 family = OMF_None;
1076 break;
1077 }
1078 }
1079 }
1080 }
1081 break;
Fangrui Song6907ce22018-07-30 19:24:48 +00001082
John McCallb4526252011-03-02 01:50:55 +00001083 }
1084
1085 // Cache the result.
Erich Keane9b18eca2018-08-01 21:31:08 +00001086 ObjCMethodDeclBits.Family = family;
John McCallb4526252011-03-02 01:50:55 +00001087 return family;
1088}
1089
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001090QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
1091 const ObjCInterfaceDecl *OID,
1092 bool &selfIsPseudoStrong,
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08001093 bool &selfIsConsumed) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001094 QualType selfTy;
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001095 selfIsPseudoStrong = false;
1096 selfIsConsumed = false;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001097 if (isInstanceMethod()) {
1098 // There may be no interface context due to error in declaration
1099 // of the interface (which has been reported). Recover gracefully.
1100 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +00001101 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001102 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001103 } else {
1104 selfTy = Context.getObjCIdType();
1105 }
1106 } else // we have a factory method.
1107 selfTy = Context.getObjCClassType();
1108
David Blaikiebbafb8a2012-03-11 07:00:24 +00001109 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001110 if (isInstanceMethod()) {
1111 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +00001112
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001113 // 'self' is always __strong. It's actually pseudo-strong except
1114 // in init methods (or methods labeled ns_consumes_self), though.
1115 Qualifiers qs;
1116 qs.setObjCLifetime(Qualifiers::OCL_Strong);
1117 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +00001118
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001119 // In addition, 'self' is const unless this is an init method.
1120 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
1121 selfTy = selfTy.withConst();
1122 selfIsPseudoStrong = true;
1123 }
1124 }
1125 else {
1126 assert(isClassMethod());
1127 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +00001128 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +00001129 selfIsPseudoStrong = true;
1130 }
John McCall31168b02011-06-15 23:02:42 +00001131 }
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001132 return selfTy;
1133}
John McCall31168b02011-06-15 23:02:42 +00001134
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001135void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
1136 const ObjCInterfaceDecl *OID) {
1137 bool selfIsPseudoStrong, selfIsConsumed;
1138 QualType selfTy =
1139 getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
Alexey Bataev56223232017-06-09 13:40:18 +00001140 auto *Self = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1141 &Context.Idents.get("self"), selfTy,
1142 ImplicitParamDecl::ObjCSelf);
1143 setSelfDecl(Self);
John McCall31168b02011-06-15 23:02:42 +00001144
1145 if (selfIsConsumed)
Alexey Bataev56223232017-06-09 13:40:18 +00001146 Self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001147
John McCalld4631322011-06-17 06:42:21 +00001148 if (selfIsPseudoStrong)
Alexey Bataev56223232017-06-09 13:40:18 +00001149 Self->setARCPseudoStrong(true);
John McCalld4631322011-06-17 06:42:21 +00001150
Alexey Bataev56223232017-06-09 13:40:18 +00001151 setCmdDecl(ImplicitParamDecl::Create(
1152 Context, this, SourceLocation(), &Context.Idents.get("_cmd"),
1153 Context.getObjCSelType(), ImplicitParamDecl::ObjCCmd));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001154}
1155
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001156ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001157 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001158 return ID;
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001159 if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001160 return CD->getClassInterface();
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001161 if (auto *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001162 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +00001163 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +00001164 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +00001165 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001166}
1167
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001168SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1169 const auto *TSI = getReturnTypeSourceInfo();
1170 if (TSI)
1171 return TSI->getTypeLoc().getSourceRange();
1172 return SourceRange();
1173}
1174
Douglas Gregor9b7b3e92015-07-07 06:20:27 +00001175QualType ObjCMethodDecl::getSendResultType() const {
1176 ASTContext &Ctx = getASTContext();
1177 return getReturnType().getNonLValueExprType(Ctx)
1178 .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1179}
1180
Douglas Gregore83b9562015-07-07 03:57:53 +00001181QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1182 // FIXME: Handle related result types here.
1183
1184 return getReturnType().getNonLValueExprType(getASTContext())
1185 .substObjCMemberType(receiverType, getDeclContext(),
1186 ObjCSubstitutionContext::Result);
1187}
1188
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001189static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1190 const ObjCMethodDecl *Method,
1191 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1192 bool MovedToSuper) {
1193 if (!Container)
1194 return;
1195
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001196 // In categories look for overridden methods from protocols. A method from
1197 // category is not "overridden" since it is considered as the "same" method
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001198 // (same USR) as the one from the interface.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001199 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001200 // Check whether we have a matching method at this category but only if we
1201 // are at the super class level.
1202 if (MovedToSuper)
1203 if (ObjCMethodDecl *
1204 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001205 Method->isInstanceMethod(),
1206 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001207 if (Method != Overridden) {
1208 // We found an override at this category; there is no need to look
1209 // into its protocols.
1210 Methods.push_back(Overridden);
1211 return;
1212 }
1213
Aaron Ballman19a41762014-03-14 12:55:57 +00001214 for (const auto *P : Category->protocols())
1215 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001216 return;
1217 }
1218
1219 // Check whether we have a matching method at this level.
1220 if (const ObjCMethodDecl *
1221 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001222 Method->isInstanceMethod(),
1223 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001224 if (Method != Overridden) {
1225 // We found an override at this level; there is no need to look
1226 // into other protocols or categories.
1227 Methods.push_back(Overridden);
1228 return;
1229 }
1230
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001231 if (const auto *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001232 for (const auto *P : Protocol->protocols())
1233 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001234 }
1235
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001236 if (const auto *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001237 for (const auto *P : Interface->protocols())
1238 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001239
Aaron Ballman15063e12014-03-13 21:35:02 +00001240 for (const auto *Cat : Interface->known_categories())
1241 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001242
1243 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1244 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1245 /*MovedToSuper=*/true);
1246 }
1247}
1248
1249static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1250 const ObjCMethodDecl *Method,
1251 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1252 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1253 /*MovedToSuper=*/false);
1254}
1255
1256static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1257 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1258 assert(Method->isOverriding());
1259
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001260 if (const auto *ProtD =
1261 dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001262 CollectOverriddenMethods(ProtD, Method, overridden);
1263
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001264 } else if (const auto *IMD =
1265 dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001266 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1267 if (!ID)
1268 return;
1269 // Start searching for overridden methods using the method from the
1270 // interface as starting point.
1271 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001272 Method->isInstanceMethod(),
1273 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001274 Method = IFaceMeth;
1275 CollectOverriddenMethods(ID, Method, overridden);
1276
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001277 } else if (const auto *CatD =
1278 dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001279 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1280 if (!ID)
1281 return;
1282 // Start searching for overridden methods using the method from the
1283 // interface as starting point.
1284 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001285 Method->isInstanceMethod(),
1286 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001287 Method = IFaceMeth;
1288 CollectOverriddenMethods(ID, Method, overridden);
1289
1290 } else {
1291 CollectOverriddenMethods(
1292 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1293 Method, overridden);
1294 }
1295}
1296
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001297void ObjCMethodDecl::getOverriddenMethods(
1298 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1299 const ObjCMethodDecl *Method = this;
1300
1301 if (Method->isRedeclaration()) {
1302 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1303 getMethod(Method->getSelector(), Method->isInstanceMethod());
1304 }
1305
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001306 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001307 collectOverriddenMethodsSlow(Method, Overridden);
1308 assert(!Overridden.empty() &&
1309 "ObjCMethodDecl's overriding bit is not as expected");
1310 }
1311}
1312
Jordan Rose2bd991a2012-10-10 16:42:54 +00001313const ObjCPropertyDecl *
1314ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1315 Selector Sel = getSelector();
1316 unsigned NumArgs = Sel.getNumArgs();
1317 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001318 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001319
Jordan Rose2bd991a2012-10-10 16:42:54 +00001320 if (isPropertyAccessor()) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001321 const auto *Container = cast<ObjCContainerDecl>(getParent());
Adrian Prantl2073dd22019-11-04 14:28:14 -08001322 // For accessor stubs, go back to the interface.
1323 if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container))
1324 if (isSynthesizedAccessorStub())
1325 Container = ImplDecl->getClassInterface();
1326
Jordan Rose2bd991a2012-10-10 16:42:54 +00001327 bool IsGetter = (NumArgs == 0);
Jordan Rose31cf7d42016-03-11 21:14:40 +00001328 bool IsInstance = isInstanceMethod();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001329
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001330 /// Local function that attempts to find a matching property within the
1331 /// given Objective-C container.
1332 auto findMatchingProperty =
1333 [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * {
Jordan Rose31cf7d42016-03-11 21:14:40 +00001334 if (IsInstance) {
1335 for (const auto *I : Container->instance_properties()) {
1336 Selector NextSel = IsGetter ? I->getGetterName()
1337 : I->getSetterName();
1338 if (NextSel == Sel)
1339 return I;
1340 }
1341 } else {
1342 for (const auto *I : Container->class_properties()) {
1343 Selector NextSel = IsGetter ? I->getGetterName()
1344 : I->getSetterName();
1345 if (NextSel == Sel)
1346 return I;
1347 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001348 }
1349
1350 return nullptr;
1351 };
1352
1353 // Look in the container we were given.
1354 if (const auto *Found = findMatchingProperty(Container))
1355 return Found;
1356
1357 // If we're in a category or extension, look in the main class.
1358 const ObjCInterfaceDecl *ClassDecl = nullptr;
1359 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1360 ClassDecl = Category->getClassInterface();
1361 if (const auto *Found = findMatchingProperty(ClassDecl))
1362 return Found;
1363 } else {
1364 // Determine whether the container is a class.
Simon Pilgrim69993352020-03-12 15:36:33 +00001365 ClassDecl = cast<ObjCInterfaceDecl>(Container);
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001366 }
Simon Pilgrim69993352020-03-12 15:36:33 +00001367 assert(ClassDecl && "Failed to find main class");
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001368
1369 // If we have a class, check its visible extensions.
Simon Pilgrim69993352020-03-12 15:36:33 +00001370 for (const auto *Ext : ClassDecl->visible_extensions()) {
1371 if (Ext == Container)
1372 continue;
1373 if (const auto *Found = findMatchingProperty(Ext))
1374 return Found;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001375 }
1376
Adrian Prantl2073dd22019-11-04 14:28:14 -08001377 assert(isSynthesizedAccessorStub() && "expected an accessor stub");
Simon Pilgrim69993352020-03-12 15:36:33 +00001378
Adrian Prantl2073dd22019-11-04 14:28:14 -08001379 for (const auto *Cat : ClassDecl->known_categories()) {
1380 if (Cat == Container)
1381 continue;
Adrian Prantl2073dd22019-11-04 14:28:14 -08001382 if (const auto *Found = findMatchingProperty(Cat))
1383 return Found;
1384 }
1385
Jordan Rose2bd991a2012-10-10 16:42:54 +00001386 llvm_unreachable("Marked as a property accessor but no property found!");
1387 }
1388
1389 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001390 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001391
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001392 using OverridesTy = SmallVector<const ObjCMethodDecl *, 8>;
1393
Jordan Rose2bd991a2012-10-10 16:42:54 +00001394 OverridesTy Overrides;
1395 getOverriddenMethods(Overrides);
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001396 for (const auto *Override : Overrides)
1397 if (const ObjCPropertyDecl *Prop = Override->findPropertyDecl(false))
Jordan Rose2bd991a2012-10-10 16:42:54 +00001398 return Prop;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001399
Craig Topper36250ad2014-05-12 05:36:57 +00001400 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001401}
1402
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001403//===----------------------------------------------------------------------===//
Douglas Gregor85f3f952015-07-07 03:57:15 +00001404// ObjCTypeParamDecl
1405//===----------------------------------------------------------------------===//
1406
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001407void ObjCTypeParamDecl::anchor() {}
Douglas Gregor85f3f952015-07-07 03:57:15 +00001408
1409ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001410 ObjCTypeParamVariance variance,
1411 SourceLocation varianceLoc,
Douglas Gregore83b9562015-07-07 03:57:53 +00001412 unsigned index,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001413 SourceLocation nameLoc,
1414 IdentifierInfo *name,
1415 SourceLocation colonLoc,
1416 TypeSourceInfo *boundInfo) {
Manman Renc5705ba2016-09-13 17:41:05 +00001417 auto *TPDecl =
1418 new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1419 nameLoc, name, colonLoc, boundInfo);
1420 QualType TPType = ctx.getObjCTypeParamType(TPDecl, {});
1421 TPDecl->setTypeForDecl(TPType.getTypePtr());
1422 return TPDecl;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001423}
1424
1425ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1426 unsigned ID) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001427 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1428 ObjCTypeParamVariance::Invariant,
1429 SourceLocation(), 0, SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001430 nullptr, SourceLocation(), nullptr);
1431}
1432
1433SourceRange ObjCTypeParamDecl::getSourceRange() const {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001434 SourceLocation startLoc = VarianceLoc;
1435 if (startLoc.isInvalid())
1436 startLoc = getLocation();
1437
Douglas Gregor85f3f952015-07-07 03:57:15 +00001438 if (hasExplicitBound()) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001439 return SourceRange(startLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001440 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1441 }
1442
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001443 return SourceRange(startLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001444}
1445
1446//===----------------------------------------------------------------------===//
1447// ObjCTypeParamList
1448//===----------------------------------------------------------------------===//
1449ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1450 ArrayRef<ObjCTypeParamDecl *> typeParams,
1451 SourceLocation rAngleLoc)
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001452 : NumParams(typeParams.size()) {
Douglas Gregor0a8890ff2015-07-07 06:20:46 +00001453 Brackets.Begin = lAngleLoc.getRawEncoding();
1454 Brackets.End = rAngleLoc.getRawEncoding();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001455 std::copy(typeParams.begin(), typeParams.end(), begin());
1456}
1457
Douglas Gregor85f3f952015-07-07 03:57:15 +00001458ObjCTypeParamList *ObjCTypeParamList::create(
1459 ASTContext &ctx,
1460 SourceLocation lAngleLoc,
1461 ArrayRef<ObjCTypeParamDecl *> typeParams,
1462 SourceLocation rAngleLoc) {
James Y Knight967eb202015-12-29 22:13:13 +00001463 void *mem =
1464 ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001465 alignof(ObjCTypeParamList));
Douglas Gregor85f3f952015-07-07 03:57:15 +00001466 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1467}
1468
Douglas Gregore83b9562015-07-07 03:57:53 +00001469void ObjCTypeParamList::gatherDefaultTypeArgs(
1470 SmallVectorImpl<QualType> &typeArgs) const {
1471 typeArgs.reserve(size());
1472 for (auto typeParam : *this)
1473 typeArgs.push_back(typeParam->getUnderlyingType());
1474}
1475
Douglas Gregor85f3f952015-07-07 03:57:15 +00001476//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001477// ObjCInterfaceDecl
1478//===----------------------------------------------------------------------===//
1479
Douglas Gregord53ae832012-01-17 18:09:05 +00001480ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001481 DeclContext *DC,
1482 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001483 IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001484 ObjCTypeParamList *typeParamList,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001485 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001486 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001487 bool isInternal){
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001488 auto *Result = new (C, DC)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001489 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1490 isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001491 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001492 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001493 return Result;
1494}
1495
Richard Smith053f6c62014-05-16 23:01:30 +00001496ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001497 unsigned ID) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001498 auto *Result = new (C, ID)
1499 ObjCInterfaceDecl(C, nullptr, SourceLocation(), nullptr, nullptr,
1500 SourceLocation(), nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001501 Result->Data.setInt(!C.getLangOpts().Modules);
1502 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001503}
1504
Richard Smith053f6c62014-05-16 23:01:30 +00001505ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1506 SourceLocation AtLoc, IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001507 ObjCTypeParamList *typeParamList,
Richard Smith053f6c62014-05-16 23:01:30 +00001508 SourceLocation CLoc,
1509 ObjCInterfaceDecl *PrevDecl,
1510 bool IsInternal)
1511 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001512 redeclarable_base(C) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001513 setPreviousDecl(PrevDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +00001514
Douglas Gregor81252352011-12-16 22:37:11 +00001515 // Copy the 'data' pointer over.
1516 if (PrevDecl)
1517 Data = PrevDecl->Data;
Fangrui Song6907ce22018-07-30 19:24:48 +00001518
Richard Smith053f6c62014-05-16 23:01:30 +00001519 setImplicit(IsInternal);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001520
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001521 setTypeParamList(typeParamList);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001522}
1523
Douglas Gregor73693022010-12-01 23:49:52 +00001524void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001525 assert(data().ExternallyCompleted && "Class is not externally completed");
1526 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001527 getASTContext().getExternalSource()->CompleteType(
1528 const_cast<ObjCInterfaceDecl *>(this));
1529}
1530
1531void ObjCInterfaceDecl::setExternallyCompleted() {
Fangrui Song6907ce22018-07-30 19:24:48 +00001532 assert(getASTContext().getExternalSource() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001533 "Class can't be externally completed without an external source");
Fangrui Song6907ce22018-07-30 19:24:48 +00001534 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001535 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001536 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001537}
1538
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001539void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001540 // Check for a complete definition and recover if not so.
1541 if (!isThisDeclarationADefinition())
1542 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001543 data().HasDesignatedInitializers = true;
1544}
1545
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001546bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001547 // Check for a complete definition and recover if not so.
1548 if (!isThisDeclarationADefinition())
1549 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001550 if (data().ExternallyCompleted)
1551 LoadExternalDefinition();
1552
1553 return data().HasDesignatedInitializers;
1554}
1555
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001556StringRef
1557ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001558 if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001559 return ObjCRTName->getMetadataName();
1560
1561 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001562}
1563
1564StringRef
1565ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001566 if (ObjCInterfaceDecl *ID =
1567 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1568 return ID->getObjCRuntimeNameAsString();
Fangrui Song6907ce22018-07-30 19:24:48 +00001569
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001570 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001571}
1572
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001573ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001574 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1575 if (data().ExternallyCompleted)
1576 LoadExternalDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00001577
Douglas Gregordc9166c2011-12-15 20:29:51 +00001578 return getASTContext().getObjCImplementation(
1579 const_cast<ObjCInterfaceDecl*>(Def));
1580 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001581
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001582 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001583 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001584}
1585
1586void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001587 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001588}
1589
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001590namespace {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001591
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001592struct SynthesizeIvarChunk {
1593 uint64_t Size;
1594 ObjCIvarDecl *Ivar;
1595
1596 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1597 : Size(size), Ivar(ivar) {}
1598};
1599
1600bool operator<(const SynthesizeIvarChunk & LHS,
1601 const SynthesizeIvarChunk &RHS) {
1602 return LHS.Size < RHS.Size;
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001603}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001604
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001605} // namespace
1606
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001607/// all_declared_ivar_begin - return first ivar declared in this class,
1608/// its extensions and its implementation. Lazily build the list on first
1609/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001610///
1611/// Caveat: The list returned by this method reflects the current
1612/// state of the parser. The cache will be updated for every ivar
1613/// added by an extension or the implementation when they are
1614/// encountered.
1615/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001616ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001617 // FIXME: Should make sure no callers ever do this.
1618 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001619 return nullptr;
1620
1621 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001622 if (!data().IvarList) {
1623 if (!ivar_empty()) {
1624 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1625 data().IvarList = *I; ++I;
1626 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001627 curIvar->setNextIvar(*I);
1628 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001629
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001630 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001631 if (!Ext->ivar_empty()) {
1632 ObjCCategoryDecl::ivar_iterator
1633 I = Ext->ivar_begin(),
1634 E = Ext->ivar_end();
1635 if (!data().IvarList) {
1636 data().IvarList = *I; ++I;
1637 curIvar = data().IvarList;
1638 }
1639 for ( ;I != E; curIvar = *I, ++I)
1640 curIvar->setNextIvar(*I);
1641 }
1642 }
1643 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001644 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001645
1646 // cached and complete!
1647 if (!data().IvarListMissingImplementation)
1648 return data().IvarList;
Fangrui Song6907ce22018-07-30 19:24:48 +00001649
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001650 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001651 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001652 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001653 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001654 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001655 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1656 layout.push_back(SynthesizeIvarChunk(
1657 IV->getASTContext().getTypeSize(IV->getType()), IV));
1658 continue;
1659 }
1660 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001661 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001662 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001663 curIvar->setNextIvar(IV);
1664 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001665 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001666
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001667 if (!layout.empty()) {
1668 // Order synthesized ivars by their size.
Fangrui Song899d1392019-04-24 14:43:05 +00001669 llvm::stable_sort(layout);
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001670 unsigned Ix = 0, EIx = layout.size();
1671 if (!data().IvarList) {
1672 data().IvarList = layout[0].Ivar; Ix++;
1673 curIvar = data().IvarList;
1674 }
1675 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1676 curIvar->setNextIvar(layout[Ix].Ivar);
1677 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001678 }
1679 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001680 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001681}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001682
1683/// FindCategoryDeclaration - Finds category declaration in the list of
1684/// categories for this class and returns it. Name of the category is passed
1685/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001686///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001687ObjCCategoryDecl *
1688ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001689 // FIXME: Should make sure no callers ever do this.
1690 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001691 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001692
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001693 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001694 LoadExternalDefinition();
1695
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001696 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001697 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001698 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001699
1700 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001701}
1702
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001703ObjCMethodDecl *
1704ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001705 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001706 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001707 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1708 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001709 }
1710
Craig Topper36250ad2014-05-12 05:36:57 +00001711 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001712}
1713
1714ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001715 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001716 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001717 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1718 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001719 }
Craig Topper36250ad2014-05-12 05:36:57 +00001720
1721 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001722}
1723
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001724/// ClassImplementsProtocol - Checks that 'lProto' protocol
1725/// has been implemented in IDecl class, its super class or categories (if
1726/// lookupCategory is true).
1727bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1728 bool lookupCategory,
1729 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001730 if (!hasDefinition())
1731 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001732
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001733 ObjCInterfaceDecl *IDecl = this;
1734 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001735 for (auto *PI : IDecl->protocols()){
1736 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001737 return true;
1738 // This is dubious and is added to be compatible with gcc. In gcc, it is
1739 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1740 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1741 // object. This IMO, should be a bug.
1742 // FIXME: Treat this as an extension, and flag this as an error when GCC
1743 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001744 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001745 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001746 return true;
1747 }
Mike Stump11289f42009-09-09 15:08:12 +00001748
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001749 // 2nd, look up the category.
1750 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001751 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001752 for (auto *PI : Cat->protocols())
1753 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001754 return true;
1755 }
Mike Stump11289f42009-09-09 15:08:12 +00001756
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001757 // 3rd, look up the super class(s)
1758 if (IDecl->getSuperClass())
1759 return
1760 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1761 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001762
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001763 return false;
1764}
1765
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001766//===----------------------------------------------------------------------===//
1767// ObjCIvarDecl
1768//===----------------------------------------------------------------------===//
1769
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001770void ObjCIvarDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001771
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001772ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001773 SourceLocation StartLoc,
1774 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001775 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001776 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001777 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001778 if (DC) {
1779 // Ivar's can only appear in interfaces, implementations (via synthesized
1780 // properties), and class extensions (via direct declaration, or synthesized
1781 // properties).
1782 //
1783 // FIXME: This should really be asserting this:
1784 // (isa<ObjCCategoryDecl>(DC) &&
1785 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1786 // but unfortunately we sometimes place ivars into non-class extension
1787 // categories on error. This breaks an AST invariant, and should not be
1788 // fixed.
1789 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1790 isa<ObjCCategoryDecl>(DC)) &&
1791 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001792 // Once a new ivar is created in any of class/class-extension/implementation
1793 // decl contexts, the previously built IvarList must be rebuilt.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001794 auto *ID = dyn_cast<ObjCInterfaceDecl>(DC);
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001795 if (!ID) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001796 if (auto *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001797 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001798 else
1799 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001800 }
Craig Topper36250ad2014-05-12 05:36:57 +00001801 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001802 }
1803
Richard Smithf7981722013-11-22 09:01:48 +00001804 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001805 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001806}
1807
Douglas Gregor72172e92012-01-05 21:55:30 +00001808ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001809 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1810 nullptr, QualType(), nullptr,
1811 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001812}
1813
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001814const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001815 const auto *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001816
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001817 switch (DC->getKind()) {
1818 default:
1819 case ObjCCategoryImpl:
1820 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001821 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001822
1823 // Ivars can only appear in class extension categories.
1824 case ObjCCategory: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001825 const auto *CD = cast<ObjCCategoryDecl>(DC);
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001826 assert(CD->IsClassExtension() && "invalid container for ivar!");
1827 return CD->getClassInterface();
1828 }
1829
1830 case ObjCImplementation:
1831 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1832
1833 case ObjCInterface:
1834 return cast<ObjCInterfaceDecl>(DC);
1835 }
1836}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001837
Douglas Gregore83b9562015-07-07 03:57:53 +00001838QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1839 return getType().substObjCMemberType(objectType, getDeclContext(),
1840 ObjCSubstitutionContext::Property);
1841}
1842
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001843//===----------------------------------------------------------------------===//
1844// ObjCAtDefsFieldDecl
1845//===----------------------------------------------------------------------===//
1846
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001847void ObjCAtDefsFieldDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001848
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001849ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001850*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1851 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001852 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001853 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001854}
1855
Richard Smithf7981722013-11-22 09:01:48 +00001856ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001857 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001858 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1859 SourceLocation(), nullptr, QualType(),
1860 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001861}
1862
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001863//===----------------------------------------------------------------------===//
1864// ObjCProtocolDecl
1865//===----------------------------------------------------------------------===//
1866
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001867void ObjCProtocolDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001868
Richard Smith053f6c62014-05-16 23:01:30 +00001869ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1870 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001871 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001872 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001873 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001874 redeclarable_base(C) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001875 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001876 if (PrevDecl)
1877 Data = PrevDecl->Data;
1878}
1879
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001880ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001881 IdentifierInfo *Id,
1882 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001883 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001884 ObjCProtocolDecl *PrevDecl) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001885 auto *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001886 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001887 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001888 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001889}
1890
Richard Smithf7981722013-11-22 09:01:48 +00001891ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001892 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001893 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001894 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001895 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001896 Result->Data.setInt(!C.getLangOpts().Modules);
1897 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001898}
1899
Steve Naroff114aecb2009-03-01 16:12:44 +00001900ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1901 ObjCProtocolDecl *PDecl = this;
1902
1903 if (Name == getIdentifier())
1904 return PDecl;
1905
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001906 for (auto *I : protocols())
1907 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001908 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001909
Craig Topper36250ad2014-05-12 05:36:57 +00001910 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001911}
1912
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001913// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001914// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001915ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1916 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001917 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001918
Douglas Gregoreed49792013-01-17 00:38:46 +00001919 // If there is no definition or the definition is hidden, we don't find
1920 // anything.
1921 const ObjCProtocolDecl *Def = getDefinition();
Martin Boehme2e92b392020-06-08 15:37:44 +02001922 if (!Def || !Def->isUnconditionallyVisible())
Craig Topper36250ad2014-05-12 05:36:57 +00001923 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001924
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001925 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001926 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001927
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001928 for (const auto *I : protocols())
1929 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001930 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001931 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001932}
1933
Douglas Gregore6e48b12012-01-01 19:29:29 +00001934void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001935 assert(!Data.getPointer() && "Protocol already has a definition!");
1936 Data.setPointer(new (getASTContext()) DefinitionData);
1937 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001938}
1939
1940void ObjCProtocolDecl::startDefinition() {
1941 allocateDefinitionData();
Fangrui Song6907ce22018-07-30 19:24:48 +00001942
Douglas Gregora715bff2012-01-01 19:51:50 +00001943 // Update all of the declarations with a pointer to the definition.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001944 for (auto *RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001945 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001946}
1947
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001948void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1949 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001950 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001951 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001952 // Insert into PM if not there already.
Manman Ren494ee5b2016-01-28 23:36:05 +00001953 PM.insert(std::make_pair(
1954 std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
1955 Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001956 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001957 }
1958 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001959 for (const auto *PI : PDecl->protocols())
1960 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001961 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001962}
1963
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001964void ObjCProtocolDecl::collectInheritedProtocolProperties(
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001965 const ObjCPropertyDecl *Property, ProtocolPropertySet &PS,
1966 PropertyDeclOrder &PO) const {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001967 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001968 if (!PS.insert(PDecl).second)
1969 return;
Manman Renefe1bac2016-01-27 20:00:32 +00001970 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001971 if (Prop == Property)
1972 continue;
1973 if (Prop->getIdentifier() == Property->getIdentifier()) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001974 PO.push_back(Prop);
1975 return;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001976 }
1977 }
1978 // Scan through protocol's protocols which did not have a matching property.
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001979 for (const auto *PI : PDecl->protocols())
1980 PI->collectInheritedProtocolProperties(Property, PS, PO);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001981 }
1982}
Anna Zaks673d76b2012-10-18 19:17:53 +00001983
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001984StringRef
1985ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001986 if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001987 return ObjCRTName->getMetadataName();
1988
1989 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001990}
1991
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001992//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001993// ObjCCategoryDecl
1994//===----------------------------------------------------------------------===//
1995
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001996void ObjCCategoryDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001997
Douglas Gregor85f3f952015-07-07 03:57:15 +00001998ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
Fangrui Song6907ce22018-07-30 19:24:48 +00001999 SourceLocation ClassNameLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +00002000 SourceLocation CategoryNameLoc,
2001 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
2002 ObjCTypeParamList *typeParamList,
2003 SourceLocation IvarLBraceLoc,
2004 SourceLocation IvarRBraceLoc)
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002005 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
2006 ClassInterface(IDecl), CategoryNameLoc(CategoryNameLoc),
2007 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002008 setTypeParamList(typeParamList);
Douglas Gregor85f3f952015-07-07 03:57:15 +00002009}
2010
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002011ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00002012 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00002013 SourceLocation ClassNameLoc,
2014 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00002015 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002016 ObjCInterfaceDecl *IDecl,
Douglas Gregor85f3f952015-07-07 03:57:15 +00002017 ObjCTypeParamList *typeParamList,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002018 SourceLocation IvarLBraceLoc,
2019 SourceLocation IvarRBraceLoc) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002020 auto *CatDecl =
Richard Smithf7981722013-11-22 09:01:48 +00002021 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00002022 IDecl, typeParamList, IvarLBraceLoc,
2023 IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00002024 if (IDecl) {
2025 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002026 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00002027 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002028 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00002029 if (ASTMutationListener *L = C.getASTMutationListener())
2030 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
2031 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00002032 }
2033
2034 return CatDecl;
2035}
2036
Richard Smithf7981722013-11-22 09:01:48 +00002037ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002038 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002039 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
2040 SourceLocation(), SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00002041 nullptr, nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002042}
2043
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002044ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
2045 return getASTContext().getObjCImplementation(
2046 const_cast<ObjCCategoryDecl*>(this));
2047}
2048
2049void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
2050 getASTContext().setObjCImplementation(this, ImplD);
2051}
2052
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002053void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
2054 TypeParamList = TPL;
2055 if (!TPL)
2056 return;
2057 // Set the declaration context of each of the type parameters.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002058 for (auto *typeParam : *TypeParamList)
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002059 typeParam->setDeclContext(this);
2060}
2061
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002062//===----------------------------------------------------------------------===//
2063// ObjCCategoryImplDecl
2064//===----------------------------------------------------------------------===//
2065
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002066void ObjCCategoryImplDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002067
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002068ObjCCategoryImplDecl *
2069ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002070 IdentifierInfo *Id,
2071 ObjCInterfaceDecl *ClassInterface,
2072 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00002073 SourceLocation atStartLoc,
2074 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00002075 if (ClassInterface && ClassInterface->hasDefinition())
2076 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00002077 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
2078 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002079}
2080
Fangrui Song6907ce22018-07-30 19:24:48 +00002081ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002082 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002083 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
2084 SourceLocation(), SourceLocation(),
2085 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002086}
2087
Steve Narofff406f4d2009-10-29 21:11:04 +00002088ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00002089 // The class interface might be NULL if we are working with invalid code.
2090 if (const ObjCInterfaceDecl *ID = getClassInterface())
2091 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00002092 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00002093}
2094
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002095void ObjCImplDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002096
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002097void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00002098 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002099 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002100 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002101}
2102
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002103void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
2104 ASTContext &Ctx = getASTContext();
2105
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002106 if (auto *ImplD = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002107 if (IFace)
2108 Ctx.setObjCImplementation(IFace, ImplD);
2109
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002110 } else if (auto *ImplD = dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002111 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
2112 Ctx.setObjCImplementation(CD, ImplD);
2113 }
2114
2115 ClassInterface = IFace;
2116}
2117
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002118/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00002119/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00002120/// the implemented property that uses it.
Chris Lattnera9ca0522009-02-28 18:42:10 +00002121ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002122FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00002123 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002124 if (PID->getPropertyIvarDecl() &&
2125 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
2126 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00002127 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002128}
2129
2130/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00002131/// added to the list of those properties \@synthesized/\@dynamic in this
2132/// category \@implementation block.
Chris Lattnera9ca0522009-02-28 18:42:10 +00002133ObjCPropertyImplDecl *ObjCImplDecl::
Manman Ren5b786402016-01-28 18:49:28 +00002134FindPropertyImplDecl(IdentifierInfo *Id,
2135 ObjCPropertyQueryKind QueryKind) const {
2136 ObjCPropertyImplDecl *ClassPropImpl = nullptr;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002137 for (auto *PID : property_impls())
Manman Ren5b786402016-01-28 18:49:28 +00002138 // If queryKind is unknown, we return the instance property if one
2139 // exists; otherwise we return the class property.
2140 if (PID->getPropertyDecl()->getIdentifier() == Id) {
2141 if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
2142 !PID->getPropertyDecl()->isClassProperty()) ||
2143 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
2144 PID->getPropertyDecl()->isClassProperty()) ||
2145 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
2146 !PID->getPropertyDecl()->isClassProperty()))
2147 return PID;
2148
2149 if (PID->getPropertyDecl()->isClassProperty())
2150 ClassPropImpl = PID;
2151 }
2152
2153 if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
2154 // We can't find the instance property, return the class property.
2155 return ClassPropImpl;
2156
Craig Topper36250ad2014-05-12 05:36:57 +00002157 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002158}
2159
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002160raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002161 const ObjCCategoryImplDecl &CID) {
2162 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002163 return OS;
2164}
2165
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002166//===----------------------------------------------------------------------===//
2167// ObjCImplementationDecl
2168//===----------------------------------------------------------------------===//
2169
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002170void ObjCImplementationDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002171
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002172ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00002173ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002174 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002175 ObjCInterfaceDecl *SuperDecl,
2176 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002177 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00002178 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002179 SourceLocation IvarLBraceLoc,
2180 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00002181 if (ClassInterface && ClassInterface->hasDefinition())
2182 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00002183 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
2184 nameLoc, atStartLoc, superLoc,
2185 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002186}
2187
Douglas Gregor72172e92012-01-05 21:55:30 +00002188ObjCImplementationDecl *
2189ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002190 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
2191 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002192}
2193
John McCall0410e572011-07-22 04:15:06 +00002194void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2195 CXXCtorInitializer ** initializers,
2196 unsigned numInitializers) {
2197 if (numInitializers > 0) {
2198 NumIvarInitializers = numInitializers;
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002199 auto **ivarInitializers = new (C) CXXCtorInitializer*[NumIvarInitializers];
John McCall0410e572011-07-22 04:15:06 +00002200 memcpy(ivarInitializers, initializers,
2201 numInitializers * sizeof(CXXCtorInitializer*));
2202 IvarInitializers = ivarInitializers;
2203 }
2204}
2205
Richard Smithc2bb8182015-03-24 06:36:48 +00002206ObjCImplementationDecl::init_const_iterator
2207ObjCImplementationDecl::init_begin() const {
2208 return IvarInitializers.get(getASTContext().getExternalSource());
2209}
2210
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002211raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002212 const ObjCImplementationDecl &ID) {
2213 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002214 return OS;
2215}
2216
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002217//===----------------------------------------------------------------------===//
2218// ObjCCompatibleAliasDecl
2219//===----------------------------------------------------------------------===//
2220
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002221void ObjCCompatibleAliasDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002222
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002223ObjCCompatibleAliasDecl *
2224ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2225 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00002226 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002227 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00002228 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002229}
2230
Douglas Gregor72172e92012-01-05 21:55:30 +00002231ObjCCompatibleAliasDecl *
2232ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002233 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2234 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002235}
2236
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002237//===----------------------------------------------------------------------===//
2238// ObjCPropertyDecl
2239//===----------------------------------------------------------------------===//
2240
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002241void ObjCPropertyDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002242
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002243ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2244 SourceLocation L,
2245 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00002246 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002247 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00002248 QualType T,
2249 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002250 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002251 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2252 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002253}
2254
Richard Smithf7981722013-11-22 09:01:48 +00002255ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002256 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002257 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2258 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00002259 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00002260}
2261
Douglas Gregore83b9562015-07-07 03:57:53 +00002262QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2263 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2264 ObjCSubstitutionContext::Property);
2265}
2266
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002267//===----------------------------------------------------------------------===//
2268// ObjCPropertyImplDecl
2269//===----------------------------------------------------------------------===//
2270
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002271ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002272 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002273 SourceLocation atLoc,
2274 SourceLocation L,
2275 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002276 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002277 ObjCIvarDecl *ivar,
2278 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002279 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2280 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002281}
Chris Lattnered0e1642008-03-17 01:19:02 +00002282
Richard Smithf7981722013-11-22 09:01:48 +00002283ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002284 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002285 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2286 SourceLocation(), nullptr, Dynamic,
2287 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002288}
2289
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002290SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2291 SourceLocation EndLoc = getLocation();
2292 if (IvarLoc.isValid())
2293 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002294
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002295 return SourceRange(AtLoc, EndLoc);
2296}