blob: 6492f07eb5b04778dfe90a6c67302871be65555f [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())
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +000097 if (Def->isHidden() && !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()) {
149 if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
150 return true;
151 break;
152 }
153 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000154
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000155 // Also look into protocols, for a user declared instance method.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000156 for (const auto *Proto : ID->all_referenced_protocols())
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000157 if (Proto->HasUserDeclaredSetterMethod(Property))
158 return true;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000159
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000160 // And in its super class.
161 ObjCInterfaceDecl *OSC = ID->getSuperClass();
162 while (OSC) {
163 if (OSC->HasUserDeclaredSetterMethod(Property))
164 return true;
165 OSC = OSC->getSuperClass();
166 }
167 }
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000168 if (const auto *PD = dyn_cast<ObjCProtocolDecl>(this))
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000169 for (const auto *PI : PD->protocols())
170 if (PI->HasUserDeclaredSetterMethod(Property))
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000171 return true;
Fariborz Jahanian1446b342013-03-21 20:50:53 +0000172 return false;
173}
174
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000175ObjCPropertyDecl *
Ted Kremenekddcd1092010-03-15 20:11:53 +0000176ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
Manman Ren5b786402016-01-28 18:49:28 +0000177 const IdentifierInfo *propertyID,
178 ObjCPropertyQueryKind queryKind) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000179 // If this context is a hidden protocol definition, don't find any
180 // property.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000181 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000182 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
183 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000184 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000185 }
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000186
Alex Lorenzf9a28a22017-02-22 23:18:49 +0000187 // If context is class, then lookup property in its visible extensions.
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000188 // This comes before property is looked up in primary class.
189 if (auto *IDecl = dyn_cast<ObjCInterfaceDecl>(DC)) {
Alex Lorenzf9a28a22017-02-22 23:18:49 +0000190 for (const auto *Ext : IDecl->visible_extensions())
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000191 if (ObjCPropertyDecl *PD = ObjCPropertyDecl::findPropertyDecl(Ext,
Manman Ren5b786402016-01-28 18:49:28 +0000192 propertyID,
193 queryKind))
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000194 return PD;
195 }
196
Richard Smithcf4bdde2015-02-21 02:45:19 +0000197 DeclContext::lookup_result R = DC->lookup(propertyID);
Manman Ren5b786402016-01-28 18:49:28 +0000198 ObjCPropertyDecl *classProp = nullptr;
Richard Smithcf4bdde2015-02-21 02:45:19 +0000199 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
David Blaikieff7d47a2012-12-19 00:45:41 +0000200 ++I)
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000201 if (auto *PD = dyn_cast<ObjCPropertyDecl>(*I)) {
Manman Ren5b786402016-01-28 18:49:28 +0000202 // If queryKind is unknown, we return the instance property if one
203 // exists; otherwise we return the class property.
204 if ((queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
205 !PD->isClassProperty()) ||
206 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
207 PD->isClassProperty()) ||
208 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
209 !PD->isClassProperty()))
210 return PD;
211
212 if (PD->isClassProperty())
213 classProp = PD;
214 }
215
216 if (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
217 // We can't find the instance property, return the class property.
218 return classProp;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000219
Craig Topper36250ad2014-05-12 05:36:57 +0000220 return nullptr;
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000221}
222
Anna Zaks454477c2012-09-27 19:45:11 +0000223IdentifierInfo *
224ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
225 SmallString<128> ivarName;
226 {
227 llvm::raw_svector_ostream os(ivarName);
228 os << '_' << getIdentifier()->getName();
229 }
230 return &Ctx.Idents.get(ivarName.str());
231}
232
Fariborz Jahaniana054e992008-04-21 19:04:53 +0000233/// FindPropertyDeclaration - Finds declaration of the property given its name
234/// in 'PropertyId' and returns it. It returns 0, if not found.
Jordan Rose210bfe92014-11-19 22:03:46 +0000235ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
Manman Ren5b786402016-01-28 18:49:28 +0000236 const IdentifierInfo *PropertyId,
237 ObjCPropertyQueryKind QueryKind) const {
Douglas Gregoreed49792013-01-17 00:38:46 +0000238 // Don't find properties within hidden protocol definitions.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000239 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
Douglas Gregoreed49792013-01-17 00:38:46 +0000240 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
241 if (Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +0000242 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +0000243 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000244
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000245 // Search the extensions of a class first; they override what's in
246 // the class itself.
247 if (const auto *ClassDecl = dyn_cast<ObjCInterfaceDecl>(this)) {
248 for (const auto *Ext : ClassDecl->visible_extensions()) {
Manman Ren5b786402016-01-28 18:49:28 +0000249 if (auto *P = Ext->FindPropertyDeclaration(PropertyId, QueryKind))
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000250 return P;
251 }
252 }
Mike Stump11289f42009-09-09 15:08:12 +0000253
Ted Kremenekddcd1092010-03-15 20:11:53 +0000254 if (ObjCPropertyDecl *PD =
Manman Ren5b786402016-01-28 18:49:28 +0000255 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
256 QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000257 return PD;
Mike Stump11289f42009-09-09 15:08:12 +0000258
Ted Kremenekddcd1092010-03-15 20:11:53 +0000259 switch (getKind()) {
260 default:
261 break;
262 case Decl::ObjCProtocol: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000263 const auto *PID = cast<ObjCProtocolDecl>(this);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000264 for (const auto *I : PID->protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000265 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
266 QueryKind))
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000267 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000268 break;
269 }
270 case Decl::ObjCInterface: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000271 const auto *OID = cast<ObjCInterfaceDecl>(this);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000272 // Look through categories (but not extensions; they were handled above).
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000273 for (const auto *Cat : OID->visible_categories()) {
Ted Kremenekddcd1092010-03-15 20:11:53 +0000274 if (!Cat->IsClassExtension())
Manman Ren5b786402016-01-28 18:49:28 +0000275 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(
276 PropertyId, QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000277 return P;
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000278 }
Ted Kremenekddcd1092010-03-15 20:11:53 +0000279
280 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000281 for (const auto *I : OID->all_referenced_protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000282 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
283 QueryKind))
Ted Kremenekddcd1092010-03-15 20:11:53 +0000284 return P;
285
286 // Finally, check the super class.
287 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
Manman Ren5b786402016-01-28 18:49:28 +0000288 return superClass->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekddcd1092010-03-15 20:11:53 +0000289 break;
290 }
291 case Decl::ObjCCategory: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000292 const auto *OCD = cast<ObjCCategoryDecl>(this);
Ted Kremenekddcd1092010-03-15 20:11:53 +0000293 // Look through protocols.
294 if (!OCD->IsClassExtension())
Aaron Ballman19a41762014-03-14 12:55:57 +0000295 for (const auto *I : OCD->protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000296 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
297 QueryKind))
Aaron Ballman19a41762014-03-14 12:55:57 +0000298 return P;
Ted Kremenekddcd1092010-03-15 20:11:53 +0000299 break;
Fariborz Jahaniandab04842009-01-19 18:16:19 +0000300 }
301 }
Craig Topper36250ad2014-05-12 05:36:57 +0000302 return nullptr;
Steve Narofff9c65242008-06-05 13:55:23 +0000303}
304
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +0000305void ObjCInterfaceDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000306
Douglas Gregor85f3f952015-07-07 03:57:15 +0000307ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
308 // If this particular declaration has a type parameter list, return it.
309 if (ObjCTypeParamList *written = getTypeParamListAsWritten())
310 return written;
311
312 // If there is a definition, return its type parameter list.
313 if (const ObjCInterfaceDecl *def = getDefinition())
314 return def->getTypeParamListAsWritten();
315
316 // Otherwise, look at previous declarations to determine whether any
317 // of them has a type parameter list, skipping over those
318 // declarations that do not.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000319 for (const ObjCInterfaceDecl *decl = getMostRecentDecl(); decl;
320 decl = decl->getPreviousDecl()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000321 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
322 return written;
323 }
324
325 return nullptr;
326}
327
Douglas Gregorab7f0b32015-07-07 06:20:12 +0000328void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
329 TypeParamList = TPL;
330 if (!TPL)
331 return;
332 // Set the declaration context of each of the type parameters.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000333 for (auto *typeParam : *TypeParamList)
Douglas Gregorab7f0b32015-07-07 06:20:12 +0000334 typeParam->setDeclContext(this);
335}
336
Douglas Gregore9d95f12015-07-07 03:57:35 +0000337ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
338 // FIXME: Should make sure no callers ever do this.
339 if (!hasDefinition())
340 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +0000341
Douglas Gregore9d95f12015-07-07 03:57:35 +0000342 if (data().ExternallyCompleted)
343 LoadExternalDefinition();
344
345 if (const ObjCObjectType *superType = getSuperClassType()) {
346 if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
347 if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
348 return superDef;
349
350 return superDecl;
351 }
352 }
353
354 return nullptr;
355}
356
357SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
358 if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000359 return superTInfo->getTypeLoc().getBeginLoc();
Fangrui Song6907ce22018-07-30 19:24:48 +0000360
Douglas Gregore9d95f12015-07-07 03:57:35 +0000361 return SourceLocation();
362}
363
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000364/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
365/// with name 'PropertyId' in the primary class; including those in protocols
Ted Kremenekd133a862010-03-15 20:30:07 +0000366/// (direct or indirect) used by the primary class.
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000367ObjCPropertyDecl *
Ted Kremenekd133a862010-03-15 20:30:07 +0000368ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
Manman Ren5b786402016-01-28 18:49:28 +0000369 IdentifierInfo *PropertyId,
370 ObjCPropertyQueryKind QueryKind) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000371 // FIXME: Should make sure no callers ever do this.
372 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000373 return nullptr;
374
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000375 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000376 LoadExternalDefinition();
377
Ted Kremenekd133a862010-03-15 20:30:07 +0000378 if (ObjCPropertyDecl *PD =
Manman Ren5b786402016-01-28 18:49:28 +0000379 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
380 QueryKind))
Ted Kremenekd133a862010-03-15 20:30:07 +0000381 return PD;
382
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000383 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000384 for (const auto *I : all_referenced_protocols())
Manman Ren5b786402016-01-28 18:49:28 +0000385 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
386 QueryKind))
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000387 return P;
Ted Kremenekd133a862010-03-15 20:30:07 +0000388
Craig Topper36250ad2014-05-12 05:36:57 +0000389 return nullptr;
Fariborz Jahaniande8db162009-11-02 22:45:15 +0000390}
391
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000392void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
393 PropertyDeclOrder &PO) const {
Manman Ren494ee5b2016-01-28 23:36:05 +0000394 for (auto *Prop : properties()) {
395 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000396 PO.push_back(Prop);
Anna Zaks673d76b2012-10-18 19:17:53 +0000397 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000398 for (const auto *Ext : known_extensions()) {
399 const ObjCCategoryDecl *ClassExt = Ext;
Manman Ren494ee5b2016-01-28 23:36:05 +0000400 for (auto *Prop : ClassExt->properties()) {
401 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000402 PO.push_back(Prop);
403 }
404 }
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000405 for (const auto *PI : all_referenced_protocols())
406 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks408f7d02012-10-31 01:18:22 +0000407 // Note, the properties declared only in class extensions are still copied
408 // into the main @interface's property list, and therefore we don't
409 // explicitly, have to search class extension properties.
Anna Zaks673d76b2012-10-18 19:17:53 +0000410}
411
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000412bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
413 const ObjCInterfaceDecl *Class = this;
414 while (Class) {
415 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
416 return true;
417 Class = Class->getSuperClass();
418 }
419 return false;
420}
421
422const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
423 const ObjCInterfaceDecl *Class = this;
424 while (Class) {
425 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
426 return Class;
427 Class = Class->getSuperClass();
428 }
Craig Topper36250ad2014-05-12 05:36:57 +0000429 return nullptr;
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000430}
431
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000432void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
433 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +0000434 ASTContext &C) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000435 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000436 LoadExternalDefinition();
437
Fangrui Song6907ce22018-07-30 19:24:48 +0000438 if (data().AllReferencedProtocols.empty() &&
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000439 data().ReferencedProtocols.empty()) {
440 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000441 return;
442 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000443
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000444 // Check for duplicate protocol in class's protocol list.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000445 // This is O(n*m). But it is extremely rare and number of protocols in
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000446 // class or its extension are very few.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000447 SmallVector<ObjCProtocolDecl *, 8> ProtocolRefs;
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000448 for (unsigned i = 0; i < ExtNum; i++) {
449 bool protocolExists = false;
450 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000451 for (auto *Proto : all_referenced_protocols()) {
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000452 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
453 protocolExists = true;
454 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000455 }
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000456 }
457 // Do we want to warn on a protocol in extension class which
458 // already exist in the class? Probably not.
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000459 if (!protocolExists)
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000460 ProtocolRefs.push_back(ProtoInExtension);
461 }
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000462
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000463 if (ProtocolRefs.empty())
464 return;
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000465
Fariborz Jahanian8764c742009-10-05 21:32:49 +0000466 // Merge ProtocolRefs into class's protocol list;
Benjamin Kramerf9890422015-02-17 16:48:30 +0000467 ProtocolRefs.append(all_referenced_protocol_begin(),
468 all_referenced_protocol_end());
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000469
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000470 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000471}
472
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000473const ObjCInterfaceDecl *
474ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
475 const ObjCInterfaceDecl *IFace = this;
476 while (IFace) {
477 if (IFace->hasDesignatedInitializers())
478 return IFace;
479 if (!IFace->inheritsDesignatedInitializers())
480 break;
481 IFace = IFace->getSuperClass();
482 }
Craig Topper36250ad2014-05-12 05:36:57 +0000483 return nullptr;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000484}
485
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000486static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
487 for (const auto *MD : D->instance_methods()) {
488 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
489 return true;
490 }
491 for (const auto *Ext : D->visible_extensions()) {
492 for (const auto *MD : Ext->instance_methods()) {
493 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
494 return true;
495 }
496 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000497 if (const auto *ImplD = D->getImplementation()) {
Argyrios Kyrtzidisc7479602014-04-16 18:45:32 +0000498 for (const auto *MD : ImplD->instance_methods()) {
499 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
500 return true;
501 }
Argyrios Kyrtzidis441f6262014-04-16 18:32:42 +0000502 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000503 return false;
504}
505
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000506bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
507 switch (data().InheritedDesignatedInitializers) {
508 case DefinitionData::IDI_Inherited:
509 return true;
510 case DefinitionData::IDI_NotInherited:
511 return false;
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +0000512 case DefinitionData::IDI_Unknown:
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000513 // If the class introduced initializers we conservatively assume that we
514 // don't know if any of them is a designated initializer to avoid possible
515 // misleading warnings.
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000516 if (isIntroducingInitializers(this)) {
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000517 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000518 } else {
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000519 if (auto SuperD = getSuperClass()) {
520 data().InheritedDesignatedInitializers =
521 SuperD->declaresOrInheritsDesignatedInitializers() ?
522 DefinitionData::IDI_Inherited :
523 DefinitionData::IDI_NotInherited;
524 } else {
525 data().InheritedDesignatedInitializers =
526 DefinitionData::IDI_NotInherited;
527 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000528 }
Argyrios Kyrtzidis357b36a2014-04-26 21:28:41 +0000529 assert(data().InheritedDesignatedInitializers
530 != DefinitionData::IDI_Unknown);
531 return data().InheritedDesignatedInitializers ==
532 DefinitionData::IDI_Inherited;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000533 }
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000534
535 llvm_unreachable("unexpected InheritedDesignatedInitializers value");
536}
537
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000538void ObjCInterfaceDecl::getDesignatedInitializers(
539 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000540 // Check for a complete definition and recover if not so.
541 if (!isThisDeclarationADefinition())
542 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000543 if (data().ExternallyCompleted)
544 LoadExternalDefinition();
545
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000546 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000547 if (!IFace)
548 return;
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000549
Aaron Ballmanf26acce2014-03-13 19:50:17 +0000550 for (const auto *MD : IFace->instance_methods())
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000551 if (MD->isThisDeclarationADesignatedInitializer())
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000552 Methods.push_back(MD);
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000553 for (const auto *Ext : IFace->visible_extensions()) {
554 for (const auto *MD : Ext->instance_methods())
555 if (MD->isThisDeclarationADesignatedInitializer())
556 Methods.push_back(MD);
557 }
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +0000558}
559
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000560bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
561 const ObjCMethodDecl **InitMethod) const {
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000562 bool HasCompleteDef = isThisDeclarationADefinition();
563 // During deserialization the data record for the ObjCInterfaceDecl could
564 // be made invariant by reusing the canonical decl. Take this into account
565 // when checking for the complete definition.
566 if (!HasCompleteDef && getCanonicalDecl()->hasDefinition() &&
567 getCanonicalDecl()->getDefinition() == getDefinition())
568 HasCompleteDef = true;
569
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000570 // Check for a complete definition and recover if not so.
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000571 if (!HasCompleteDef)
Fariborz Jahanian0c325312014-03-11 18:56:18 +0000572 return false;
Bruno Cardoso Lopesfaaeae52017-04-26 05:06:20 +0000573
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000574 if (data().ExternallyCompleted)
575 LoadExternalDefinition();
576
Argyrios Kyrtzidisb9a405b2013-12-05 07:07:03 +0000577 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000578 if (!IFace)
579 return false;
580
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000581 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000582 if (MD->isThisDeclarationADesignatedInitializer()) {
583 if (InitMethod)
584 *InitMethod = MD;
585 return true;
586 }
587 }
Argyrios Kyrtzidis6af9bc52014-03-28 22:45:38 +0000588 for (const auto *Ext : IFace->visible_extensions()) {
589 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
590 if (MD->isThisDeclarationADesignatedInitializer()) {
591 if (InitMethod)
592 *InitMethod = MD;
593 return true;
594 }
595 }
596 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000597 return false;
598}
599
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000600void ObjCInterfaceDecl::allocateDefinitionData() {
601 assert(!hasDefinition() && "ObjC class already has a definition");
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000602 Data.setPointer(new (getASTContext()) DefinitionData());
603 Data.getPointer()->Definition = this;
Douglas Gregor7671e532011-12-16 16:34:57 +0000604
605 // Make the type point at the definition, now that we have one.
606 if (TypeForDecl)
607 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000608}
609
610void ObjCInterfaceDecl::startDefinition() {
611 allocateDefinitionData();
612
Douglas Gregor66b310c2011-12-15 18:03:09 +0000613 // Update all of the declarations with a pointer to the definition.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000614 for (auto *RD : redecls()) {
Aaron Ballman86c93902014-03-06 23:45:36 +0000615 if (RD != this)
Douglas Gregora323c4c2011-12-15 18:17:27 +0000616 RD->Data = Data;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000617 }
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +0000618}
619
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000620ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
621 ObjCInterfaceDecl *&clsDeclared) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000622 // FIXME: Should make sure no callers ever do this.
623 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000624 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000625
626 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000627 LoadExternalDefinition();
628
Chris Lattner89375192008-03-16 00:19:01 +0000629 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000630 while (ClassDecl != nullptr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000631 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
Fariborz Jahanian68453832009-06-05 18:16:35 +0000632 clsDeclared = ClassDecl;
633 return I;
Chris Lattner89375192008-03-16 00:19:01 +0000634 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000635
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000636 for (const auto *Ext : ClassDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000637 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000638 clsDeclared = ClassDecl;
639 return I;
640 }
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000641 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000642
Chris Lattner89375192008-03-16 00:19:01 +0000643 ClassDecl = ClassDecl->getSuperClass();
644 }
Craig Topper36250ad2014-05-12 05:36:57 +0000645 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000646}
647
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000648/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
649/// class whose name is passed as argument. If it is not one of the super classes
650/// the it returns NULL.
651ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
652 const IdentifierInfo*ICName) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000653 // FIXME: Should make sure no callers ever do this.
654 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000655 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000656
657 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000658 LoadExternalDefinition();
659
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000660 ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000661 while (ClassDecl != nullptr) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000662 if (ClassDecl->getIdentifier() == ICName)
663 return ClassDecl;
664 ClassDecl = ClassDecl->getSuperClass();
665 }
Craig Topper36250ad2014-05-12 05:36:57 +0000666 return nullptr;
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +0000667}
668
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000669ObjCProtocolDecl *
670ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000671 for (auto *P : all_referenced_protocols())
672 if (P->lookupProtocolNamed(Name))
673 return P;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000674 ObjCInterfaceDecl *SuperClass = getSuperClass();
Craig Topper36250ad2014-05-12 05:36:57 +0000675 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
Fariborz Jahanian56f48d02013-07-10 21:30:22 +0000676}
677
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000678/// lookupMethod - This method returns an instance/class method by looking in
Chris Lattner89375192008-03-16 00:19:01 +0000679/// the class, its categories, and its super classes (using a linear search).
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000680/// When argument category "C" is specified, any implicit method found
681/// in this category is ignored.
Fangrui Song6907ce22018-07-30 19:24:48 +0000682ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
Ted Kremenek00781502013-11-23 01:01:29 +0000683 bool isInstance,
684 bool shallowCategoryLookup,
685 bool followSuper,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +0000686 const ObjCCategoryDecl *C) const
Ted Kremenek00781502013-11-23 01:01:29 +0000687{
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000688 // FIXME: Should make sure no callers ever do this.
689 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000690 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000691
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000692 const ObjCInterfaceDecl* ClassDecl = this;
Craig Topper36250ad2014-05-12 05:36:57 +0000693 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000694
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000695 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +0000696 LoadExternalDefinition();
697
Ted Kremenek00781502013-11-23 01:01:29 +0000698 while (ClassDecl) {
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000699 // 1. Look through primary class.
Argyrios Kyrtzidis553376b2009-07-25 22:15:51 +0000700 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
Chris Lattner89375192008-03-16 00:19:01 +0000701 return MethodDecl;
Fangrui Song6907ce22018-07-30 19:24:48 +0000702
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000703 // 2. Didn't find one yet - now look through categories.
704 for (const auto *Cat : ClassDecl->visible_categories())
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000705 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000706 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000707 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000708
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000709 // 3. Didn't find one yet - look through primary class's protocols.
710 for (const auto *I : ClassDecl->protocols())
711 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
712 return MethodDecl;
Fangrui Song6907ce22018-07-30 19:24:48 +0000713
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000714 // 4. Didn't find one yet - now look through categories' protocols
715 if (!shallowCategoryLookup)
716 for (const auto *Cat : ClassDecl->visible_categories()) {
Fariborz Jahanian73e244a2013-04-25 21:59:34 +0000717 // Didn't find one yet - look through protocols.
718 const ObjCList<ObjCProtocolDecl> &Protocols =
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000719 Cat->getReferencedProtocols();
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000720 for (auto *Protocol : Protocols)
721 if ((MethodDecl = Protocol->lookupMethod(Sel, isInstance)))
Aaron Ballman3fe486a2014-03-13 21:23:55 +0000722 if (C != Cat || !MethodDecl->isImplicit())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +0000723 return MethodDecl;
Fariborz Jahanian29082a52012-02-09 21:30:24 +0000724 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000725
726
Ted Kremenek00781502013-11-23 01:01:29 +0000727 if (!followSuper)
Craig Topper36250ad2014-05-12 05:36:57 +0000728 return nullptr;
Ted Kremenek00781502013-11-23 01:01:29 +0000729
Fariborz Jahanian7e3e2912014-08-27 20:34:29 +0000730 // 5. Get to the super class (if any).
Chris Lattner89375192008-03-16 00:19:01 +0000731 ClassDecl = ClassDecl->getSuperClass();
732 }
Craig Topper36250ad2014-05-12 05:36:57 +0000733 return nullptr;
Chris Lattner89375192008-03-16 00:19:01 +0000734}
735
Anna Zaksc77a3b12012-07-27 19:07:44 +0000736// Will search "local" class/category implementations for a method decl.
737// If failed, then we search in class's root for an instance method.
738// Returns 0 if no method is found.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000739ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
740 const Selector &Sel,
Anna Zaks7044adc2012-07-30 20:31:21 +0000741 bool Instance) const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000742 // FIXME: Should make sure no callers ever do this.
743 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +0000744 return nullptr;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000745
746 if (data().ExternallyCompleted)
Argyrios Kyrtzidis4e8b1362011-10-19 02:25:16 +0000747 LoadExternalDefinition();
748
Craig Topper36250ad2014-05-12 05:36:57 +0000749 ObjCMethodDecl *Method = nullptr;
Steve Naroffbb69c942009-10-01 23:46:04 +0000750 if (ObjCImplementationDecl *ImpDecl = getImplementation())
Fangrui Song6907ce22018-07-30 19:24:48 +0000751 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000752 : ImpDecl->getClassMethod(Sel);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000753
754 // Look through local category implementations associated with the class.
755 if (!Method)
Nico Weberf6098392015-03-02 01:12:28 +0000756 Method = getCategoryMethod(Sel, Instance);
Anna Zaksc77a3b12012-07-27 19:07:44 +0000757
758 // Before we give up, check if the selector is an instance method.
759 // But only in the root. This matches gcc's behavior and what the
760 // runtime expects.
761 if (!Instance && !Method && !getSuperClass()) {
762 Method = lookupInstanceMethod(Sel);
763 // Look through local category implementations associated
764 // with the root class.
765 if (!Method)
766 Method = lookupPrivateMethod(Sel, true);
767 }
768
Steve Naroffbb69c942009-10-01 23:46:04 +0000769 if (!Method && getSuperClass())
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000770 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
Steve Naroffbb69c942009-10-01 23:46:04 +0000771 return Method;
772}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +0000773
774//===----------------------------------------------------------------------===//
775// ObjCMethodDecl
776//===----------------------------------------------------------------------===//
777
Adrian Prantl2073dd22019-11-04 14:28:14 -0800778ObjCMethodDecl::ObjCMethodDecl(
779 SourceLocation beginLoc, SourceLocation endLoc, Selector SelInfo,
780 QualType T, TypeSourceInfo *ReturnTInfo, DeclContext *contextDecl,
781 bool isInstance, bool isVariadic, bool isPropertyAccessor,
782 bool isSynthesizedAccessorStub, bool isImplicitlyDeclared, bool isDefined,
783 ImplementationControl impControl, bool HasRelatedResultType)
Erich Keane9b18eca2018-08-01 21:31:08 +0000784 : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
785 DeclContext(ObjCMethod), MethodDeclType(T), ReturnTInfo(ReturnTInfo),
786 DeclEndLoc(endLoc) {
Erich Keane9b18eca2018-08-01 21:31:08 +0000787
788 // Initialized the bits stored in DeclContext.
789 ObjCMethodDeclBits.Family =
790 static_cast<ObjCMethodFamily>(InvalidObjCMethodFamily);
791 setInstanceMethod(isInstance);
792 setVariadic(isVariadic);
793 setPropertyAccessor(isPropertyAccessor);
Adrian Prantl2073dd22019-11-04 14:28:14 -0800794 setSynthesizedAccessorStub(isSynthesizedAccessorStub);
Erich Keane9b18eca2018-08-01 21:31:08 +0000795 setDefined(isDefined);
796 setIsRedeclaration(false);
797 setHasRedeclaration(false);
798 setDeclImplementation(impControl);
799 setObjCDeclQualifier(OBJC_TQ_None);
800 setRelatedResultType(HasRelatedResultType);
801 setSelLocsKind(SelLoc_StandardNoSpace);
802 setOverriding(false);
803 setHasSkippedBody(false);
804
805 setImplicit(isImplicitlyDeclared);
806}
807
Alp Toker314cc812014-01-25 16:55:45 +0000808ObjCMethodDecl *ObjCMethodDecl::Create(
809 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
810 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
811 DeclContext *contextDecl, bool isInstance, bool isVariadic,
Adrian Prantl2073dd22019-11-04 14:28:14 -0800812 bool isPropertyAccessor, bool isSynthesizedAccessorStub,
813 bool isImplicitlyDeclared, bool isDefined, ImplementationControl impControl,
814 bool HasRelatedResultType) {
Richard Smithf7981722013-11-22 09:01:48 +0000815 return new (C, contextDecl) ObjCMethodDecl(
Alp Toker314cc812014-01-25 16:55:45 +0000816 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
Adrian Prantl2073dd22019-11-04 14:28:14 -0800817 isVariadic, isPropertyAccessor, isSynthesizedAccessorStub,
818 isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType);
Chris Lattner89375192008-03-16 00:19:01 +0000819}
820
Douglas Gregor72172e92012-01-05 21:55:30 +0000821ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000822 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000823 Selector(), QualType(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000824}
825
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -0800826bool ObjCMethodDecl::isDirectMethod() const {
827 return hasAttr<ObjCDirectAttr>();
828}
829
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000830bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
831 return getMethodFamily() == OMF_init &&
832 hasAttr<ObjCDesignatedInitializerAttr>();
833}
834
Erik Pilkington42578572018-09-10 22:20:09 +0000835bool ObjCMethodDecl::definedInNSObject(const ASTContext &Ctx) const {
836 if (const auto *PD = dyn_cast<const ObjCProtocolDecl>(getDeclContext()))
837 return PD->getIdentifier() == Ctx.getNSObjectName();
838 if (const auto *ID = dyn_cast<const ObjCInterfaceDecl>(getDeclContext()))
839 return ID->getIdentifier() == Ctx.getNSObjectName();
840 return false;
841}
842
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000843bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
844 const ObjCMethodDecl **InitMethod) const {
845 if (getMethodFamily() != OMF_init)
846 return false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000847 const DeclContext *DC = getDeclContext();
848 if (isa<ObjCProtocolDecl>(DC))
849 return false;
850 if (const ObjCInterfaceDecl *ID = getClassInterface())
Argyrios Kyrtzidisfcded9b2013-12-03 21:11:43 +0000851 return ID->isDesignatedInitializer(getSelector(), InitMethod);
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000852 return false;
853}
854
Douglas Gregora6017bb2012-10-09 17:21:28 +0000855Stmt *ObjCMethodDecl::getBody() const {
856 return Body.get(getASTContext().getExternalSource());
857}
858
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000859void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
860 assert(PrevMethod);
861 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
Erich Keane9b18eca2018-08-01 21:31:08 +0000862 setIsRedeclaration(true);
863 PrevMethod->setHasRedeclaration(true);
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000864}
865
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000866void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
867 ArrayRef<ParmVarDecl*> Params,
868 ArrayRef<SourceLocation> SelLocs) {
Craig Topper36250ad2014-05-12 05:36:57 +0000869 ParamsAndSelLocs = nullptr;
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000870 NumParams = Params.size();
871 if (Params.empty() && SelLocs.empty())
872 return;
873
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000874 static_assert(alignof(ParmVarDecl *) >= alignof(SourceLocation),
James Y Knight967eb202015-12-29 22:13:13 +0000875 "Alignment not sufficient for SourceLocation");
876
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000877 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
878 sizeof(SourceLocation) * SelLocs.size();
879 ParamsAndSelLocs = C.Allocate(Size);
880 std::copy(Params.begin(), Params.end(), getParams());
881 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
882}
883
884void ObjCMethodDecl::getSelectorLocs(
885 SmallVectorImpl<SourceLocation> &SelLocs) const {
886 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
887 SelLocs.push_back(getSelectorLoc(i));
888}
889
890void ObjCMethodDecl::setMethodParams(ASTContext &C,
891 ArrayRef<ParmVarDecl*> Params,
892 ArrayRef<SourceLocation> SelLocs) {
893 assert((!SelLocs.empty() || isImplicit()) &&
894 "No selector locs for non-implicit method");
895 if (isImplicit())
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000896 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000897
Erich Keane9b18eca2018-08-01 21:31:08 +0000898 setSelLocsKind(hasStandardSelectorLocs(getSelector(), SelLocs, Params,
899 DeclEndLoc));
900 if (getSelLocsKind() != SelLoc_NonStandard)
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000901 return setParamsAndSelLocs(C, Params, llvm::None);
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000902
903 setParamsAndSelLocs(C, Params, SelLocs);
904}
905
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000906/// A definition will return its interface declaration.
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000907/// An interface declaration will return its definition.
908/// Otherwise it will return itself.
Richard Smithd7af8a32014-05-10 01:17:36 +0000909ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000910 ASTContext &Ctx = getASTContext();
Craig Topper36250ad2014-05-12 05:36:57 +0000911 ObjCMethodDecl *Redecl = nullptr;
Erich Keane9b18eca2018-08-01 21:31:08 +0000912 if (hasRedeclaration())
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000913 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
Argyrios Kyrtzidisc5e829c2011-10-14 06:48:06 +0000914 if (Redecl)
915 return Redecl;
916
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000917 auto *CtxD = cast<Decl>(getDeclContext());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000918
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000919 if (!CtxD->isInvalidDecl()) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000920 if (auto *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000921 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
922 if (!ImplD->isInvalidDecl())
923 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000924
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000925 } else if (auto *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000926 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
927 if (!ImplD->isInvalidDecl())
928 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000929
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000930 } else if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000931 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
932 if (!IFD->isInvalidDecl())
933 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +0000934
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000935 } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000936 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
937 if (!CatD->isInvalidDecl())
938 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
939 }
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000940 }
941
Alex Lorenzff6c34b2016-11-21 11:16:30 +0000942 // Ensure that the discovered method redeclaration has a valid declaration
943 // context. Used to prevent infinite loops when iterating redeclarations in
944 // a partially invalid AST.
945 if (Redecl && cast<Decl>(Redecl->getDeclContext())->isInvalidDecl())
946 Redecl = nullptr;
947
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +0000948 if (!Redecl && isRedeclaration()) {
949 // This is the last redeclaration, go back to the first method.
950 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
951 isInstanceMethod());
952 }
953
Argyrios Kyrtzidisa8cf0be2009-07-21 00:06:36 +0000954 return Redecl ? Redecl : this;
955}
956
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000957ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000958 auto *CtxD = cast<Decl>(getDeclContext());
Pierre Habouzit42f9d0c2019-12-19 01:25:03 -0800959 const auto &Sel = getSelector();
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000960
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000961 if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
Pierre Habouzita4e18192019-12-17 11:06:17 -0800962 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) {
Pierre Habouzit42f9d0c2019-12-19 01:25:03 -0800963 // When the container is the ObjCImplementationDecl (the primary
964 // @implementation), then the canonical Decl is either in
965 // the class Interface, or in any of its extension.
966 //
967 // So when we don't find it in the ObjCInterfaceDecl,
968 // sift through extensions too.
969 if (ObjCMethodDecl *MD = IFD->getMethod(Sel, isInstanceMethod()))
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000970 return MD;
Pierre Habouzit42f9d0c2019-12-19 01:25:03 -0800971 for (auto *Ext : IFD->known_extensions())
972 if (ObjCMethodDecl *MD = Ext->getMethod(Sel, isInstanceMethod()))
973 return MD;
Pierre Habouzita4e18192019-12-17 11:06:17 -0800974 }
Eugene Zelenko2a1ba942018-04-09 22:14:10 +0000975 } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
Steve Narofff406f4d2009-10-29 21:11:04 +0000976 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
Pierre Habouzit42f9d0c2019-12-19 01:25:03 -0800977 if (ObjCMethodDecl *MD = CatD->getMethod(Sel, isInstanceMethod()))
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000978 return MD;
979 }
980
Manman Rena0042c42016-10-03 21:26:46 +0000981 if (isRedeclaration()) {
982 // It is possible that we have not done deserializing the ObjCMethod yet.
983 ObjCMethodDecl *MD =
Pierre Habouzit42f9d0c2019-12-19 01:25:03 -0800984 cast<ObjCContainerDecl>(CtxD)->getMethod(Sel, isInstanceMethod());
Manman Rena0042c42016-10-03 21:26:46 +0000985 return MD ? MD : this;
986 }
Argyrios Kyrtzidis690dccd2011-10-17 19:48:09 +0000987
Argyrios Kyrtzidisf390c432009-07-28 05:11:17 +0000988 return this;
989}
990
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000991SourceLocation ObjCMethodDecl::getEndLoc() const {
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000992 if (Stmt *Body = getBody())
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000993 return Body->getEndLoc();
Argyrios Kyrtzidis33b4bfc2012-06-16 00:46:02 +0000994 return DeclEndLoc;
995}
996
John McCallb4526252011-03-02 01:50:55 +0000997ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
Erich Keane9b18eca2018-08-01 21:31:08 +0000998 auto family = static_cast<ObjCMethodFamily>(ObjCMethodDeclBits.Family);
John McCallfb55f852011-03-02 21:01:41 +0000999 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
John McCallb4526252011-03-02 01:50:55 +00001000 return family;
1001
John McCall86bc21f2011-03-02 11:33:24 +00001002 // Check for an explicit attribute.
1003 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
1004 // The unfortunate necessity of mapping between enums here is due
1005 // to the attributes framework.
1006 switch (attr->getFamily()) {
1007 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
1008 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
1009 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
1010 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
1011 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
1012 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
1013 }
Erich Keane9b18eca2018-08-01 21:31:08 +00001014 ObjCMethodDeclBits.Family = family;
John McCall86bc21f2011-03-02 11:33:24 +00001015 return family;
1016 }
1017
John McCallb4526252011-03-02 01:50:55 +00001018 family = getSelector().getMethodFamily();
1019 switch (family) {
1020 case OMF_None: break;
1021
1022 // init only has a conventional meaning for an instance method, and
1023 // it has to return an object.
1024 case OMF_init:
Alp Toker314cc812014-01-25 16:55:45 +00001025 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +00001026 family = OMF_None;
1027 break;
1028
1029 // alloc/copy/new have a conventional meaning for both class and
1030 // instance methods, but they require an object return.
1031 case OMF_alloc:
1032 case OMF_copy:
1033 case OMF_mutableCopy:
1034 case OMF_new:
Alp Toker314cc812014-01-25 16:55:45 +00001035 if (!getReturnType()->isObjCObjectPointerType())
John McCallb4526252011-03-02 01:50:55 +00001036 family = OMF_None;
1037 break;
1038
1039 // These selectors have a conventional meaning only for instance methods.
1040 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00001041 case OMF_finalize:
John McCallb4526252011-03-02 01:50:55 +00001042 case OMF_retain:
1043 case OMF_release:
1044 case OMF_autorelease:
1045 case OMF_retainCount:
Douglas Gregor33823722011-06-11 01:09:30 +00001046 case OMF_self:
John McCallb4526252011-03-02 01:50:55 +00001047 if (!isInstanceMethod())
1048 family = OMF_None;
1049 break;
Fangrui Song6907ce22018-07-30 19:24:48 +00001050
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +00001051 case OMF_initialize:
1052 if (isInstanceMethod() || !getReturnType()->isVoidType())
1053 family = OMF_None;
1054 break;
Fangrui Song6907ce22018-07-30 19:24:48 +00001055
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001056 case OMF_performSelector:
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001057 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001058 family = OMF_None;
1059 else {
1060 unsigned noParams = param_size();
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001061 if (noParams < 1 || noParams > 3)
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001062 family = OMF_None;
1063 else {
Alp Toker1f307f42014-01-25 17:32:04 +00001064 ObjCMethodDecl::param_type_iterator it = param_type_begin();
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001065 QualType ArgT = (*it);
1066 if (!ArgT->isObjCSelType()) {
1067 family = OMF_None;
1068 break;
1069 }
Alex Lorenz5ffe4e12017-03-23 10:46:05 +00001070 while (--noParams) {
1071 it++;
1072 ArgT = (*it);
1073 if (!ArgT->isObjCIdType()) {
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001074 family = OMF_None;
1075 break;
1076 }
1077 }
1078 }
1079 }
1080 break;
Fangrui Song6907ce22018-07-30 19:24:48 +00001081
John McCallb4526252011-03-02 01:50:55 +00001082 }
1083
1084 // Cache the result.
Erich Keane9b18eca2018-08-01 21:31:08 +00001085 ObjCMethodDeclBits.Family = family;
John McCallb4526252011-03-02 01:50:55 +00001086 return family;
1087}
1088
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001089QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
1090 const ObjCInterfaceDecl *OID,
1091 bool &selfIsPseudoStrong,
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08001092 bool &selfIsConsumed) const {
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001093 QualType selfTy;
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001094 selfIsPseudoStrong = false;
1095 selfIsConsumed = false;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001096 if (isInstanceMethod()) {
1097 // There may be no interface context due to error in declaration
1098 // of the interface (which has been reported). Recover gracefully.
1099 if (OID) {
Daniel Dunbaraefc2b92009-04-22 04:34:53 +00001100 selfTy = Context.getObjCInterfaceType(OID);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001101 selfTy = Context.getObjCObjectPointerType(selfTy);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001102 } else {
1103 selfTy = Context.getObjCIdType();
1104 }
1105 } else // we have a factory method.
1106 selfTy = Context.getObjCClassType();
1107
David Blaikiebbafb8a2012-03-11 07:00:24 +00001108 if (Context.getLangOpts().ObjCAutoRefCount) {
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001109 if (isInstanceMethod()) {
1110 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
John McCall31168b02011-06-15 23:02:42 +00001111
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001112 // 'self' is always __strong. It's actually pseudo-strong except
1113 // in init methods (or methods labeled ns_consumes_self), though.
1114 Qualifiers qs;
1115 qs.setObjCLifetime(Qualifiers::OCL_Strong);
1116 selfTy = Context.getQualifiedType(selfTy, qs);
John McCall31168b02011-06-15 23:02:42 +00001117
Ted Kremenek1fcdaa92011-11-14 21:59:25 +00001118 // In addition, 'self' is const unless this is an init method.
1119 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
1120 selfTy = selfTy.withConst();
1121 selfIsPseudoStrong = true;
1122 }
1123 }
1124 else {
1125 assert(isClassMethod());
1126 // 'self' is always const in class methods.
John McCall31168b02011-06-15 23:02:42 +00001127 selfTy = selfTy.withConst();
John McCalld4631322011-06-17 06:42:21 +00001128 selfIsPseudoStrong = true;
1129 }
John McCall31168b02011-06-15 23:02:42 +00001130 }
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001131 return selfTy;
1132}
John McCall31168b02011-06-15 23:02:42 +00001133
Adrian Prantl2ecf89e2015-07-08 22:15:59 +00001134void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
1135 const ObjCInterfaceDecl *OID) {
1136 bool selfIsPseudoStrong, selfIsConsumed;
1137 QualType selfTy =
1138 getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
Alexey Bataev56223232017-06-09 13:40:18 +00001139 auto *Self = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1140 &Context.Idents.get("self"), selfTy,
1141 ImplicitParamDecl::ObjCSelf);
1142 setSelfDecl(Self);
John McCall31168b02011-06-15 23:02:42 +00001143
1144 if (selfIsConsumed)
Alexey Bataev56223232017-06-09 13:40:18 +00001145 Self->addAttr(NSConsumedAttr::CreateImplicit(Context));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001146
John McCalld4631322011-06-17 06:42:21 +00001147 if (selfIsPseudoStrong)
Alexey Bataev56223232017-06-09 13:40:18 +00001148 Self->setARCPseudoStrong(true);
John McCalld4631322011-06-17 06:42:21 +00001149
Alexey Bataev56223232017-06-09 13:40:18 +00001150 setCmdDecl(ImplicitParamDecl::Create(
1151 Context, this, SourceLocation(), &Context.Idents.get("_cmd"),
1152 Context.getObjCSelType(), ImplicitParamDecl::ObjCCmd));
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001153}
1154
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001155ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001156 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001157 return ID;
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001158 if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001159 return CD->getClassInterface();
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001160 if (auto *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001161 return IMD->getClassInterface();
Fariborz Jahanian7a583022014-03-04 22:57:32 +00001162 if (isa<ObjCProtocolDecl>(getDeclContext()))
Craig Topper36250ad2014-05-12 05:36:57 +00001163 return nullptr;
David Blaikie83d382b2011-09-23 05:06:16 +00001164 llvm_unreachable("unknown method context");
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001165}
1166
Aaron Ballman4bfa0de2014-08-01 12:58:11 +00001167SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1168 const auto *TSI = getReturnTypeSourceInfo();
1169 if (TSI)
1170 return TSI->getTypeLoc().getSourceRange();
1171 return SourceRange();
1172}
1173
Douglas Gregor9b7b3e92015-07-07 06:20:27 +00001174QualType ObjCMethodDecl::getSendResultType() const {
1175 ASTContext &Ctx = getASTContext();
1176 return getReturnType().getNonLValueExprType(Ctx)
1177 .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1178}
1179
Douglas Gregore83b9562015-07-07 03:57:53 +00001180QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1181 // FIXME: Handle related result types here.
1182
1183 return getReturnType().getNonLValueExprType(getASTContext())
1184 .substObjCMemberType(receiverType, getDeclContext(),
1185 ObjCSubstitutionContext::Result);
1186}
1187
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001188static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1189 const ObjCMethodDecl *Method,
1190 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1191 bool MovedToSuper) {
1192 if (!Container)
1193 return;
1194
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001195 // In categories look for overridden methods from protocols. A method from
1196 // category is not "overridden" since it is considered as the "same" method
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001197 // (same USR) as the one from the interface.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001198 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001199 // Check whether we have a matching method at this category but only if we
1200 // are at the super class level.
1201 if (MovedToSuper)
1202 if (ObjCMethodDecl *
1203 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001204 Method->isInstanceMethod(),
1205 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001206 if (Method != Overridden) {
1207 // We found an override at this category; there is no need to look
1208 // into its protocols.
1209 Methods.push_back(Overridden);
1210 return;
1211 }
1212
Aaron Ballman19a41762014-03-14 12:55:57 +00001213 for (const auto *P : Category->protocols())
1214 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001215 return;
1216 }
1217
1218 // Check whether we have a matching method at this level.
1219 if (const ObjCMethodDecl *
1220 Overridden = Container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001221 Method->isInstanceMethod(),
1222 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001223 if (Method != Overridden) {
1224 // We found an override at this level; there is no need to look
1225 // into other protocols or categories.
1226 Methods.push_back(Overridden);
1227 return;
1228 }
1229
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001230 if (const auto *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001231 for (const auto *P : Protocol->protocols())
1232 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001233 }
1234
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001235 if (const auto *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
Aaron Ballmana49c5062014-03-13 20:29:09 +00001236 for (const auto *P : Interface->protocols())
1237 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001238
Aaron Ballman15063e12014-03-13 21:35:02 +00001239 for (const auto *Cat : Interface->known_categories())
1240 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001241
1242 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1243 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1244 /*MovedToSuper=*/true);
1245 }
1246}
1247
1248static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1249 const ObjCMethodDecl *Method,
1250 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1251 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1252 /*MovedToSuper=*/false);
1253}
1254
1255static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1256 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1257 assert(Method->isOverriding());
1258
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001259 if (const auto *ProtD =
1260 dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001261 CollectOverriddenMethods(ProtD, Method, overridden);
1262
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001263 } else if (const auto *IMD =
1264 dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001265 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1266 if (!ID)
1267 return;
1268 // Start searching for overridden methods using the method from the
1269 // interface as starting point.
1270 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001271 Method->isInstanceMethod(),
1272 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001273 Method = IFaceMeth;
1274 CollectOverriddenMethods(ID, Method, overridden);
1275
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001276 } else if (const auto *CatD =
1277 dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001278 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1279 if (!ID)
1280 return;
1281 // Start searching for overridden methods using the method from the
1282 // interface as starting point.
1283 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00001284 Method->isInstanceMethod(),
1285 /*AllowHidden=*/true))
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001286 Method = IFaceMeth;
1287 CollectOverriddenMethods(ID, Method, overridden);
1288
1289 } else {
1290 CollectOverriddenMethods(
1291 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1292 Method, overridden);
1293 }
1294}
1295
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001296void ObjCMethodDecl::getOverriddenMethods(
1297 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1298 const ObjCMethodDecl *Method = this;
1299
1300 if (Method->isRedeclaration()) {
1301 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1302 getMethod(Method->getSelector(), Method->isInstanceMethod());
1303 }
1304
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00001305 if (Method->isOverriding()) {
Argyrios Kyrtzidis353f6a42012-10-09 18:19:01 +00001306 collectOverriddenMethodsSlow(Method, Overridden);
1307 assert(!Overridden.empty() &&
1308 "ObjCMethodDecl's overriding bit is not as expected");
1309 }
1310}
1311
Jordan Rose2bd991a2012-10-10 16:42:54 +00001312const ObjCPropertyDecl *
1313ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1314 Selector Sel = getSelector();
1315 unsigned NumArgs = Sel.getNumArgs();
1316 if (NumArgs > 1)
Craig Topper36250ad2014-05-12 05:36:57 +00001317 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001318
Jordan Rose2bd991a2012-10-10 16:42:54 +00001319 if (isPropertyAccessor()) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001320 const auto *Container = cast<ObjCContainerDecl>(getParent());
Adrian Prantl2073dd22019-11-04 14:28:14 -08001321 // For accessor stubs, go back to the interface.
1322 if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container))
1323 if (isSynthesizedAccessorStub())
1324 Container = ImplDecl->getClassInterface();
1325
Jordan Rose2bd991a2012-10-10 16:42:54 +00001326 bool IsGetter = (NumArgs == 0);
Jordan Rose31cf7d42016-03-11 21:14:40 +00001327 bool IsInstance = isInstanceMethod();
Jordan Rose2bd991a2012-10-10 16:42:54 +00001328
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001329 /// Local function that attempts to find a matching property within the
1330 /// given Objective-C container.
1331 auto findMatchingProperty =
1332 [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * {
Jordan Rose31cf7d42016-03-11 21:14:40 +00001333 if (IsInstance) {
1334 for (const auto *I : Container->instance_properties()) {
1335 Selector NextSel = IsGetter ? I->getGetterName()
1336 : I->getSetterName();
1337 if (NextSel == Sel)
1338 return I;
1339 }
1340 } else {
1341 for (const auto *I : Container->class_properties()) {
1342 Selector NextSel = IsGetter ? I->getGetterName()
1343 : I->getSetterName();
1344 if (NextSel == Sel)
1345 return I;
1346 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001347 }
1348
1349 return nullptr;
1350 };
1351
1352 // Look in the container we were given.
1353 if (const auto *Found = findMatchingProperty(Container))
1354 return Found;
1355
1356 // If we're in a category or extension, look in the main class.
1357 const ObjCInterfaceDecl *ClassDecl = nullptr;
1358 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1359 ClassDecl = Category->getClassInterface();
1360 if (const auto *Found = findMatchingProperty(ClassDecl))
1361 return Found;
1362 } else {
1363 // Determine whether the container is a class.
Simon Pilgrim69993352020-03-12 15:36:33 +00001364 ClassDecl = cast<ObjCInterfaceDecl>(Container);
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001365 }
Simon Pilgrim69993352020-03-12 15:36:33 +00001366 assert(ClassDecl && "Failed to find main class");
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001367
1368 // If we have a class, check its visible extensions.
Simon Pilgrim69993352020-03-12 15:36:33 +00001369 for (const auto *Ext : ClassDecl->visible_extensions()) {
1370 if (Ext == Container)
1371 continue;
1372 if (const auto *Found = findMatchingProperty(Ext))
1373 return Found;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001374 }
1375
Adrian Prantl2073dd22019-11-04 14:28:14 -08001376 assert(isSynthesizedAccessorStub() && "expected an accessor stub");
Simon Pilgrim69993352020-03-12 15:36:33 +00001377
Adrian Prantl2073dd22019-11-04 14:28:14 -08001378 for (const auto *Cat : ClassDecl->known_categories()) {
1379 if (Cat == Container)
1380 continue;
Adrian Prantl2073dd22019-11-04 14:28:14 -08001381 if (const auto *Found = findMatchingProperty(Cat))
1382 return Found;
1383 }
1384
Jordan Rose2bd991a2012-10-10 16:42:54 +00001385 llvm_unreachable("Marked as a property accessor but no property found!");
1386 }
1387
1388 if (!CheckOverrides)
Craig Topper36250ad2014-05-12 05:36:57 +00001389 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001390
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001391 using OverridesTy = SmallVector<const ObjCMethodDecl *, 8>;
1392
Jordan Rose2bd991a2012-10-10 16:42:54 +00001393 OverridesTy Overrides;
1394 getOverriddenMethods(Overrides);
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001395 for (const auto *Override : Overrides)
1396 if (const ObjCPropertyDecl *Prop = Override->findPropertyDecl(false))
Jordan Rose2bd991a2012-10-10 16:42:54 +00001397 return Prop;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001398
Craig Topper36250ad2014-05-12 05:36:57 +00001399 return nullptr;
Jordan Rose2bd991a2012-10-10 16:42:54 +00001400}
1401
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001402//===----------------------------------------------------------------------===//
Douglas Gregor85f3f952015-07-07 03:57:15 +00001403// ObjCTypeParamDecl
1404//===----------------------------------------------------------------------===//
1405
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001406void ObjCTypeParamDecl::anchor() {}
Douglas Gregor85f3f952015-07-07 03:57:15 +00001407
1408ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001409 ObjCTypeParamVariance variance,
1410 SourceLocation varianceLoc,
Douglas Gregore83b9562015-07-07 03:57:53 +00001411 unsigned index,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001412 SourceLocation nameLoc,
1413 IdentifierInfo *name,
1414 SourceLocation colonLoc,
1415 TypeSourceInfo *boundInfo) {
Manman Renc5705ba2016-09-13 17:41:05 +00001416 auto *TPDecl =
1417 new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1418 nameLoc, name, colonLoc, boundInfo);
1419 QualType TPType = ctx.getObjCTypeParamType(TPDecl, {});
1420 TPDecl->setTypeForDecl(TPType.getTypePtr());
1421 return TPDecl;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001422}
1423
1424ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1425 unsigned ID) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001426 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1427 ObjCTypeParamVariance::Invariant,
1428 SourceLocation(), 0, SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00001429 nullptr, SourceLocation(), nullptr);
1430}
1431
1432SourceRange ObjCTypeParamDecl::getSourceRange() const {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001433 SourceLocation startLoc = VarianceLoc;
1434 if (startLoc.isInvalid())
1435 startLoc = getLocation();
1436
Douglas Gregor85f3f952015-07-07 03:57:15 +00001437 if (hasExplicitBound()) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001438 return SourceRange(startLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001439 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1440 }
1441
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001442 return SourceRange(startLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001443}
1444
1445//===----------------------------------------------------------------------===//
1446// ObjCTypeParamList
1447//===----------------------------------------------------------------------===//
1448ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1449 ArrayRef<ObjCTypeParamDecl *> typeParams,
1450 SourceLocation rAngleLoc)
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001451 : NumParams(typeParams.size()) {
Douglas Gregor0a8890ff2015-07-07 06:20:46 +00001452 Brackets.Begin = lAngleLoc.getRawEncoding();
1453 Brackets.End = rAngleLoc.getRawEncoding();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001454 std::copy(typeParams.begin(), typeParams.end(), begin());
1455}
1456
Douglas Gregor85f3f952015-07-07 03:57:15 +00001457ObjCTypeParamList *ObjCTypeParamList::create(
1458 ASTContext &ctx,
1459 SourceLocation lAngleLoc,
1460 ArrayRef<ObjCTypeParamDecl *> typeParams,
1461 SourceLocation rAngleLoc) {
James Y Knight967eb202015-12-29 22:13:13 +00001462 void *mem =
1463 ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001464 alignof(ObjCTypeParamList));
Douglas Gregor85f3f952015-07-07 03:57:15 +00001465 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1466}
1467
Douglas Gregore83b9562015-07-07 03:57:53 +00001468void ObjCTypeParamList::gatherDefaultTypeArgs(
1469 SmallVectorImpl<QualType> &typeArgs) const {
1470 typeArgs.reserve(size());
1471 for (auto typeParam : *this)
1472 typeArgs.push_back(typeParam->getUnderlyingType());
1473}
1474
Douglas Gregor85f3f952015-07-07 03:57:15 +00001475//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001476// ObjCInterfaceDecl
1477//===----------------------------------------------------------------------===//
1478
Douglas Gregord53ae832012-01-17 18:09:05 +00001479ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001480 DeclContext *DC,
1481 SourceLocation atLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001482 IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001483 ObjCTypeParamList *typeParamList,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001484 ObjCInterfaceDecl *PrevDecl,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001485 SourceLocation ClassLoc,
Douglas Gregordc9166c2011-12-15 20:29:51 +00001486 bool isInternal){
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001487 auto *Result = new (C, DC)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001488 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1489 isInternal);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001490 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001491 C.getObjCInterfaceType(Result, PrevDecl);
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001492 return Result;
1493}
1494
Richard Smith053f6c62014-05-16 23:01:30 +00001495ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001496 unsigned ID) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001497 auto *Result = new (C, ID)
1498 ObjCInterfaceDecl(C, nullptr, SourceLocation(), nullptr, nullptr,
1499 SourceLocation(), nullptr, false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001500 Result->Data.setInt(!C.getLangOpts().Modules);
1501 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001502}
1503
Richard Smith053f6c62014-05-16 23:01:30 +00001504ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1505 SourceLocation AtLoc, IdentifierInfo *Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001506 ObjCTypeParamList *typeParamList,
Richard Smith053f6c62014-05-16 23:01:30 +00001507 SourceLocation CLoc,
1508 ObjCInterfaceDecl *PrevDecl,
1509 bool IsInternal)
1510 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001511 redeclarable_base(C) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001512 setPreviousDecl(PrevDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +00001513
Douglas Gregor81252352011-12-16 22:37:11 +00001514 // Copy the 'data' pointer over.
1515 if (PrevDecl)
1516 Data = PrevDecl->Data;
Fangrui Song6907ce22018-07-30 19:24:48 +00001517
Richard Smith053f6c62014-05-16 23:01:30 +00001518 setImplicit(IsInternal);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001519
Douglas Gregorab7f0b32015-07-07 06:20:12 +00001520 setTypeParamList(typeParamList);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001521}
1522
Douglas Gregor73693022010-12-01 23:49:52 +00001523void ObjCInterfaceDecl::LoadExternalDefinition() const {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001524 assert(data().ExternallyCompleted && "Class is not externally completed");
1525 data().ExternallyCompleted = false;
Douglas Gregor73693022010-12-01 23:49:52 +00001526 getASTContext().getExternalSource()->CompleteType(
1527 const_cast<ObjCInterfaceDecl *>(this));
1528}
1529
1530void ObjCInterfaceDecl::setExternallyCompleted() {
Fangrui Song6907ce22018-07-30 19:24:48 +00001531 assert(getASTContext().getExternalSource() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001532 "Class can't be externally completed without an external source");
Fangrui Song6907ce22018-07-30 19:24:48 +00001533 assert(hasDefinition() &&
Douglas Gregor73693022010-12-01 23:49:52 +00001534 "Forward declarations can't be externally completed");
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001535 data().ExternallyCompleted = true;
Douglas Gregor73693022010-12-01 23:49:52 +00001536}
1537
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001538void ObjCInterfaceDecl::setHasDesignatedInitializers() {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001539 // Check for a complete definition and recover if not so.
1540 if (!isThisDeclarationADefinition())
1541 return;
Argyrios Kyrtzidis9ed9e5f2013-12-03 21:11:30 +00001542 data().HasDesignatedInitializers = true;
1543}
1544
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001545bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
Fariborz Jahanian0c325312014-03-11 18:56:18 +00001546 // Check for a complete definition and recover if not so.
1547 if (!isThisDeclarationADefinition())
1548 return false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +00001549 if (data().ExternallyCompleted)
1550 LoadExternalDefinition();
1551
1552 return data().HasDesignatedInitializers;
1553}
1554
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001555StringRef
1556ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001557 if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001558 return ObjCRTName->getMetadataName();
1559
1560 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001561}
1562
1563StringRef
1564ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001565 if (ObjCInterfaceDecl *ID =
1566 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1567 return ID->getObjCRuntimeNameAsString();
Fangrui Song6907ce22018-07-30 19:24:48 +00001568
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001569 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001570}
1571
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001572ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001573 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1574 if (data().ExternallyCompleted)
1575 LoadExternalDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00001576
Douglas Gregordc9166c2011-12-15 20:29:51 +00001577 return getASTContext().getObjCImplementation(
1578 const_cast<ObjCInterfaceDecl*>(Def));
1579 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001580
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001581 // FIXME: Should make sure no callers ever do this.
Craig Topper36250ad2014-05-12 05:36:57 +00001582 return nullptr;
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001583}
1584
1585void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
Douglas Gregordc9166c2011-12-15 20:29:51 +00001586 getASTContext().setObjCImplementation(getDefinition(), ImplD);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001587}
1588
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001589namespace {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001590
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001591struct SynthesizeIvarChunk {
1592 uint64_t Size;
1593 ObjCIvarDecl *Ivar;
1594
1595 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1596 : Size(size), Ivar(ivar) {}
1597};
1598
1599bool operator<(const SynthesizeIvarChunk & LHS,
1600 const SynthesizeIvarChunk &RHS) {
1601 return LHS.Size < RHS.Size;
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001602}
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001603
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001604} // namespace
1605
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001606/// all_declared_ivar_begin - return first ivar declared in this class,
1607/// its extensions and its implementation. Lazily build the list on first
1608/// access.
Adrian Prantla03a85a2013-03-06 22:03:30 +00001609///
1610/// Caveat: The list returned by this method reflects the current
1611/// state of the parser. The cache will be updated for every ivar
1612/// added by an extension or the implementation when they are
1613/// encountered.
1614/// See also ObjCIvarDecl::Create().
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001615ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001616 // FIXME: Should make sure no callers ever do this.
1617 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001618 return nullptr;
1619
1620 ObjCIvarDecl *curIvar = nullptr;
Adrian Prantla03a85a2013-03-06 22:03:30 +00001621 if (!data().IvarList) {
1622 if (!ivar_empty()) {
1623 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1624 data().IvarList = *I; ++I;
1625 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
Adrian Prantl68a57502013-02-27 01:31:55 +00001626 curIvar->setNextIvar(*I);
1627 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001628
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001629 for (const auto *Ext : known_extensions()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001630 if (!Ext->ivar_empty()) {
1631 ObjCCategoryDecl::ivar_iterator
1632 I = Ext->ivar_begin(),
1633 E = Ext->ivar_end();
1634 if (!data().IvarList) {
1635 data().IvarList = *I; ++I;
1636 curIvar = data().IvarList;
1637 }
1638 for ( ;I != E; curIvar = *I, ++I)
1639 curIvar->setNextIvar(*I);
1640 }
1641 }
1642 data().IvarListMissingImplementation = true;
Adrian Prantl68a57502013-02-27 01:31:55 +00001643 }
Adrian Prantla03a85a2013-03-06 22:03:30 +00001644
1645 // cached and complete!
1646 if (!data().IvarListMissingImplementation)
1647 return data().IvarList;
Fangrui Song6907ce22018-07-30 19:24:48 +00001648
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001649 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
Adrian Prantla03a85a2013-03-06 22:03:30 +00001650 data().IvarListMissingImplementation = false;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001651 if (!ImplDecl->ivar_empty()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001652 SmallVector<SynthesizeIvarChunk, 16> layout;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001653 for (auto *IV : ImplDecl->ivars()) {
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001654 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1655 layout.push_back(SynthesizeIvarChunk(
1656 IV->getASTContext().getTypeSize(IV->getType()), IV));
1657 continue;
1658 }
1659 if (!data().IvarList)
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001660 data().IvarList = IV;
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001661 else
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001662 curIvar->setNextIvar(IV);
1663 curIvar = IV;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001664 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001665
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001666 if (!layout.empty()) {
1667 // Order synthesized ivars by their size.
Fangrui Song899d1392019-04-24 14:43:05 +00001668 llvm::stable_sort(layout);
Fariborz Jahanian3c822042013-02-13 22:50:36 +00001669 unsigned Ix = 0, EIx = layout.size();
1670 if (!data().IvarList) {
1671 data().IvarList = layout[0].Ivar; Ix++;
1672 curIvar = data().IvarList;
1673 }
1674 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1675 curIvar->setNextIvar(layout[Ix].Ivar);
1676 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001677 }
1678 }
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001679 return data().IvarList;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001680}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001681
1682/// FindCategoryDeclaration - Finds category declaration in the list of
1683/// categories for this class and returns it. Name of the category is passed
1684/// in 'CategoryId'. If category not found, return 0;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001685///
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001686ObjCCategoryDecl *
1687ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001688 // FIXME: Should make sure no callers ever do this.
1689 if (!hasDefinition())
Craig Topper36250ad2014-05-12 05:36:57 +00001690 return nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +00001691
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001692 if (data().ExternallyCompleted)
Douglas Gregor73693022010-12-01 23:49:52 +00001693 LoadExternalDefinition();
1694
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001695 for (auto *Cat : visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001696 if (Cat->getIdentifier() == CategoryId)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001697 return Cat;
Craig Topper36250ad2014-05-12 05:36:57 +00001698
1699 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00001700}
1701
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001702ObjCMethodDecl *
1703ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001704 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001705 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001706 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1707 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001708 }
1709
Craig Topper36250ad2014-05-12 05:36:57 +00001710 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001711}
1712
1713ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001714 for (const auto *Cat : visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001715 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001716 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1717 return MD;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001718 }
Craig Topper36250ad2014-05-12 05:36:57 +00001719
1720 return nullptr;
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +00001721}
1722
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001723/// ClassImplementsProtocol - Checks that 'lProto' protocol
1724/// has been implemented in IDecl class, its super class or categories (if
1725/// lookupCategory is true).
1726bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1727 bool lookupCategory,
1728 bool RHSIsQualifiedID) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001729 if (!hasDefinition())
1730 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001731
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001732 ObjCInterfaceDecl *IDecl = this;
1733 // 1st, look up the class.
Aaron Ballmana49c5062014-03-13 20:29:09 +00001734 for (auto *PI : IDecl->protocols()){
1735 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001736 return true;
1737 // This is dubious and is added to be compatible with gcc. In gcc, it is
1738 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1739 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1740 // object. This IMO, should be a bug.
1741 // FIXME: Treat this as an extension, and flag this as an error when GCC
1742 // extensions are not enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001743 if (RHSIsQualifiedID &&
Aaron Ballmana49c5062014-03-13 20:29:09 +00001744 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001745 return true;
1746 }
Mike Stump11289f42009-09-09 15:08:12 +00001747
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001748 // 2nd, look up the category.
1749 if (lookupCategory)
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001750 for (const auto *Cat : visible_categories()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00001751 for (auto *PI : Cat->protocols())
1752 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001753 return true;
1754 }
Mike Stump11289f42009-09-09 15:08:12 +00001755
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001756 // 3rd, look up the super class(s)
1757 if (IDecl->getSuperClass())
1758 return
1759 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1760 RHSIsQualifiedID);
Mike Stump11289f42009-09-09 15:08:12 +00001761
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00001762 return false;
1763}
1764
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001765//===----------------------------------------------------------------------===//
1766// ObjCIvarDecl
1767//===----------------------------------------------------------------------===//
1768
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001769void ObjCIvarDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001770
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001771ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001772 SourceLocation StartLoc,
1773 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001774 QualType T, TypeSourceInfo *TInfo,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001775 AccessControl ac, Expr *BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001776 bool synthesized) {
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001777 if (DC) {
1778 // Ivar's can only appear in interfaces, implementations (via synthesized
1779 // properties), and class extensions (via direct declaration, or synthesized
1780 // properties).
1781 //
1782 // FIXME: This should really be asserting this:
1783 // (isa<ObjCCategoryDecl>(DC) &&
1784 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1785 // but unfortunately we sometimes place ivars into non-class extension
1786 // categories on error. This breaks an AST invariant, and should not be
1787 // fixed.
1788 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1789 isa<ObjCCategoryDecl>(DC)) &&
1790 "Invalid ivar decl context!");
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001791 // Once a new ivar is created in any of class/class-extension/implementation
1792 // decl contexts, the previously built IvarList must be rebuilt.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001793 auto *ID = dyn_cast<ObjCInterfaceDecl>(DC);
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001794 if (!ID) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001795 if (auto *IM = dyn_cast<ObjCImplementationDecl>(DC))
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001796 ID = IM->getClassInterface();
Eric Christopherf8378ca2012-07-19 22:22:55 +00001797 else
1798 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001799 }
Craig Topper36250ad2014-05-12 05:36:57 +00001800 ID->setIvarList(nullptr);
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00001801 }
1802
Richard Smithf7981722013-11-22 09:01:48 +00001803 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00001804 synthesized);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001805}
1806
Douglas Gregor72172e92012-01-05 21:55:30 +00001807ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001808 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1809 nullptr, QualType(), nullptr,
1810 ObjCIvarDecl::None, nullptr, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001811}
1812
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001813const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001814 const auto *DC = cast<ObjCContainerDecl>(getDeclContext());
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001815
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001816 switch (DC->getKind()) {
1817 default:
1818 case ObjCCategoryImpl:
1819 case ObjCProtocol:
David Blaikie83d382b2011-09-23 05:06:16 +00001820 llvm_unreachable("invalid ivar container!");
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001821
1822 // Ivars can only appear in class extension categories.
1823 case ObjCCategory: {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001824 const auto *CD = cast<ObjCCategoryDecl>(DC);
Daniel Dunbar89947ea2010-04-02 21:13:59 +00001825 assert(CD->IsClassExtension() && "invalid container for ivar!");
1826 return CD->getClassInterface();
1827 }
1828
1829 case ObjCImplementation:
1830 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1831
1832 case ObjCInterface:
1833 return cast<ObjCInterfaceDecl>(DC);
1834 }
1835}
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001836
Douglas Gregore83b9562015-07-07 03:57:53 +00001837QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1838 return getType().substObjCMemberType(objectType, getDeclContext(),
1839 ObjCSubstitutionContext::Property);
1840}
1841
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001842//===----------------------------------------------------------------------===//
1843// ObjCAtDefsFieldDecl
1844//===----------------------------------------------------------------------===//
1845
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001846void ObjCAtDefsFieldDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001847
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001848ObjCAtDefsFieldDecl
Abramo Bagnaradff19302011-03-08 08:55:46 +00001849*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1850 SourceLocation StartLoc, SourceLocation IdLoc,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001851 IdentifierInfo *Id, QualType T, Expr *BW) {
Richard Smithf7981722013-11-22 09:01:48 +00001852 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001853}
1854
Richard Smithf7981722013-11-22 09:01:48 +00001855ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001856 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001857 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1858 SourceLocation(), nullptr, QualType(),
1859 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001860}
1861
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001862//===----------------------------------------------------------------------===//
1863// ObjCProtocolDecl
1864//===----------------------------------------------------------------------===//
1865
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001866void ObjCProtocolDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001867
Richard Smith053f6c62014-05-16 23:01:30 +00001868ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1869 IdentifierInfo *Id, SourceLocation nameLoc,
Douglas Gregor32c17572012-01-01 20:30:41 +00001870 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001871 ObjCProtocolDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +00001872 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001873 redeclarable_base(C) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001874 setPreviousDecl(PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +00001875 if (PrevDecl)
1876 Data = PrevDecl->Data;
1877}
1878
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001879ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001880 IdentifierInfo *Id,
1881 SourceLocation nameLoc,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +00001882 SourceLocation atStartLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +00001883 ObjCProtocolDecl *PrevDecl) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001884 auto *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001885 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001886 Result->Data.setInt(!C.getLangOpts().Modules);
Douglas Gregor32c17572012-01-01 20:30:41 +00001887 return Result;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001888}
1889
Richard Smithf7981722013-11-22 09:01:48 +00001890ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00001891 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001892 ObjCProtocolDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +00001893 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001894 SourceLocation(), nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001895 Result->Data.setInt(!C.getLangOpts().Modules);
1896 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +00001897}
1898
Steve Naroff114aecb2009-03-01 16:12:44 +00001899ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1900 ObjCProtocolDecl *PDecl = this;
1901
1902 if (Name == getIdentifier())
1903 return PDecl;
1904
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001905 for (auto *I : protocols())
1906 if ((PDecl = I->lookupProtocolNamed(Name)))
Steve Naroff114aecb2009-03-01 16:12:44 +00001907 return PDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001908
Craig Topper36250ad2014-05-12 05:36:57 +00001909 return nullptr;
Steve Naroff114aecb2009-03-01 16:12:44 +00001910}
1911
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001912// lookupMethod - Lookup a instance/class method in the protocol and protocols
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001913// it inherited.
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001914ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1915 bool isInstance) const {
Craig Topper36250ad2014-05-12 05:36:57 +00001916 ObjCMethodDecl *MethodDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001917
Douglas Gregoreed49792013-01-17 00:38:46 +00001918 // If there is no definition or the definition is hidden, we don't find
1919 // anything.
1920 const ObjCProtocolDecl *Def = getDefinition();
1921 if (!Def || Def->isHidden())
Craig Topper36250ad2014-05-12 05:36:57 +00001922 return nullptr;
Douglas Gregoreed49792013-01-17 00:38:46 +00001923
Argyrios Kyrtzidise6ed65b2009-07-25 22:15:38 +00001924 if ((MethodDecl = getMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001925 return MethodDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001926
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001927 for (const auto *I : protocols())
1928 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001929 return MethodDecl;
Craig Topper36250ad2014-05-12 05:36:57 +00001930 return nullptr;
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001931}
1932
Douglas Gregore6e48b12012-01-01 19:29:29 +00001933void ObjCProtocolDecl::allocateDefinitionData() {
Douglas Gregor7dab26b2013-02-09 01:35:03 +00001934 assert(!Data.getPointer() && "Protocol already has a definition!");
1935 Data.setPointer(new (getASTContext()) DefinitionData);
1936 Data.getPointer()->Definition = this;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001937}
1938
1939void ObjCProtocolDecl::startDefinition() {
1940 allocateDefinitionData();
Fangrui Song6907ce22018-07-30 19:24:48 +00001941
Douglas Gregora715bff2012-01-01 19:51:50 +00001942 // Update all of the declarations with a pointer to the definition.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001943 for (auto *RD : redecls())
Douglas Gregora715bff2012-01-01 19:51:50 +00001944 RD->Data = this->Data;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00001945}
1946
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001947void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1948 PropertyDeclOrder &PO) const {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001949 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001950 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001951 // Insert into PM if not there already.
Manman Ren494ee5b2016-01-28 23:36:05 +00001952 PM.insert(std::make_pair(
1953 std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
1954 Prop));
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001955 PO.push_back(Prop);
Fariborz Jahanian0a17f592013-01-07 21:31:08 +00001956 }
1957 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001958 for (const auto *PI : PDecl->protocols())
1959 PI->collectPropertiesToImplement(PM, PO);
Anna Zaks673d76b2012-10-18 19:17:53 +00001960 }
Anna Zaks673d76b2012-10-18 19:17:53 +00001961}
1962
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001963void ObjCProtocolDecl::collectInheritedProtocolProperties(
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001964 const ObjCPropertyDecl *Property, ProtocolPropertySet &PS,
1965 PropertyDeclOrder &PO) const {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001966 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001967 if (!PS.insert(PDecl).second)
1968 return;
Manman Renefe1bac2016-01-27 20:00:32 +00001969 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001970 if (Prop == Property)
1971 continue;
1972 if (Prop->getIdentifier() == Property->getIdentifier()) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001973 PO.push_back(Prop);
1974 return;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001975 }
1976 }
1977 // Scan through protocol's protocols which did not have a matching property.
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001978 for (const auto *PI : PDecl->protocols())
1979 PI->collectInheritedProtocolProperties(Property, PS, PO);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001980 }
1981}
Anna Zaks673d76b2012-10-18 19:17:53 +00001982
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001983StringRef
1984ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00001985 if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
Fariborz Jahaniana2e5deb2014-07-16 19:44:34 +00001986 return ObjCRTName->getMetadataName();
1987
1988 return getName();
Fariborz Jahanian451b92a2014-07-16 16:16:04 +00001989}
1990
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001991//===----------------------------------------------------------------------===//
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00001992// ObjCCategoryDecl
1993//===----------------------------------------------------------------------===//
1994
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00001995void ObjCCategoryDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001996
Douglas Gregor85f3f952015-07-07 03:57:15 +00001997ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
Fangrui Song6907ce22018-07-30 19:24:48 +00001998 SourceLocation ClassNameLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001999 SourceLocation CategoryNameLoc,
2000 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
2001 ObjCTypeParamList *typeParamList,
2002 SourceLocation IvarLBraceLoc,
2003 SourceLocation IvarRBraceLoc)
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002004 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
2005 ClassInterface(IDecl), CategoryNameLoc(CategoryNameLoc),
2006 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002007 setTypeParamList(typeParamList);
Douglas Gregor85f3f952015-07-07 03:57:15 +00002008}
2009
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002010ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
Richard Smithf7981722013-11-22 09:01:48 +00002011 SourceLocation AtLoc,
Douglas Gregor071676f2010-01-16 16:38:58 +00002012 SourceLocation ClassNameLoc,
2013 SourceLocation CategoryNameLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00002014 IdentifierInfo *Id,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002015 ObjCInterfaceDecl *IDecl,
Douglas Gregor85f3f952015-07-07 03:57:15 +00002016 ObjCTypeParamList *typeParamList,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002017 SourceLocation IvarLBraceLoc,
2018 SourceLocation IvarRBraceLoc) {
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002019 auto *CatDecl =
Richard Smithf7981722013-11-22 09:01:48 +00002020 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
Douglas Gregor85f3f952015-07-07 03:57:15 +00002021 IDecl, typeParamList, IvarLBraceLoc,
2022 IvarRBraceLoc);
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00002023 if (IDecl) {
2024 // Link this category into its class's category list.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002025 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00002026 if (IDecl->hasDefinition()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002027 IDecl->setCategoryListRaw(CatDecl);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00002028 if (ASTMutationListener *L = C.getASTMutationListener())
2029 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
2030 }
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00002031 }
2032
2033 return CatDecl;
2034}
2035
Richard Smithf7981722013-11-22 09:01:48 +00002036ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002037 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002038 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
2039 SourceLocation(), SourceLocation(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00002040 nullptr, nullptr, nullptr);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002041}
2042
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002043ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
2044 return getASTContext().getObjCImplementation(
2045 const_cast<ObjCCategoryDecl*>(this));
2046}
2047
2048void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
2049 getASTContext().setObjCImplementation(this, ImplD);
2050}
2051
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002052void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
2053 TypeParamList = TPL;
2054 if (!TPL)
2055 return;
2056 // Set the declaration context of each of the type parameters.
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002057 for (auto *typeParam : *TypeParamList)
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002058 typeParam->setDeclContext(this);
2059}
2060
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002061//===----------------------------------------------------------------------===//
2062// ObjCCategoryImplDecl
2063//===----------------------------------------------------------------------===//
2064
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002065void ObjCCategoryImplDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002066
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002067ObjCCategoryImplDecl *
2068ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002069 IdentifierInfo *Id,
2070 ObjCInterfaceDecl *ClassInterface,
2071 SourceLocation nameLoc,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00002072 SourceLocation atStartLoc,
2073 SourceLocation CategoryNameLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00002074 if (ClassInterface && ClassInterface->hasDefinition())
2075 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00002076 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
2077 atStartLoc, CategoryNameLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002078}
2079
Fangrui Song6907ce22018-07-30 19:24:48 +00002080ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002081 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002082 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
2083 SourceLocation(), SourceLocation(),
2084 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002085}
2086
Steve Narofff406f4d2009-10-29 21:11:04 +00002087ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
Ted Kremeneke184ac52010-03-19 20:39:03 +00002088 // The class interface might be NULL if we are working with invalid code.
2089 if (const ObjCInterfaceDecl *ID = getClassInterface())
2090 return ID->FindCategoryDeclaration(getIdentifier());
Craig Topper36250ad2014-05-12 05:36:57 +00002091 return nullptr;
Argyrios Kyrtzidisa56fa192009-07-28 05:11:05 +00002092}
2093
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002094void ObjCImplDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002095
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002096void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
Douglas Gregor9a13efd2009-04-23 02:42:49 +00002097 // FIXME: The context should be correct before we get here.
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002098 property->setLexicalDeclContext(this);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002099 addDecl(property);
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002100}
2101
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002102void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
2103 ASTContext &Ctx = getASTContext();
2104
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002105 if (auto *ImplD = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002106 if (IFace)
2107 Ctx.setObjCImplementation(IFace, ImplD);
2108
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002109 } else if (auto *ImplD = dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00002110 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
2111 Ctx.setObjCImplementation(CD, ImplD);
2112 }
2113
2114 ClassInterface = IFace;
2115}
2116
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002117/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
Fariborz Jahaniane92f54a2013-03-12 17:43:00 +00002118/// properties implemented in this \@implementation block and returns
Chris Lattneraab70d22009-02-16 19:24:31 +00002119/// the implemented property that uses it.
Chris Lattnera9ca0522009-02-28 18:42:10 +00002120ObjCPropertyImplDecl *ObjCImplDecl::
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002121FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
Aaron Ballmand85eff42014-03-14 15:02:45 +00002122 for (auto *PID : property_impls())
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002123 if (PID->getPropertyIvarDecl() &&
2124 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
2125 return PID;
Craig Topper36250ad2014-05-12 05:36:57 +00002126 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002127}
2128
2129/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
James Dennett5207a1c2012-06-15 22:30:14 +00002130/// added to the list of those properties \@synthesized/\@dynamic in this
2131/// category \@implementation block.
Chris Lattnera9ca0522009-02-28 18:42:10 +00002132ObjCPropertyImplDecl *ObjCImplDecl::
Manman Ren5b786402016-01-28 18:49:28 +00002133FindPropertyImplDecl(IdentifierInfo *Id,
2134 ObjCPropertyQueryKind QueryKind) const {
2135 ObjCPropertyImplDecl *ClassPropImpl = nullptr;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002136 for (auto *PID : property_impls())
Manman Ren5b786402016-01-28 18:49:28 +00002137 // If queryKind is unknown, we return the instance property if one
2138 // exists; otherwise we return the class property.
2139 if (PID->getPropertyDecl()->getIdentifier() == Id) {
2140 if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
2141 !PID->getPropertyDecl()->isClassProperty()) ||
2142 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
2143 PID->getPropertyDecl()->isClassProperty()) ||
2144 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
2145 !PID->getPropertyDecl()->isClassProperty()))
2146 return PID;
2147
2148 if (PID->getPropertyDecl()->isClassProperty())
2149 ClassPropImpl = PID;
2150 }
2151
2152 if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
2153 // We can't find the instance property, return the class property.
2154 return ClassPropImpl;
2155
Craig Topper36250ad2014-05-12 05:36:57 +00002156 return nullptr;
Fariborz Jahanianfbbaf6a2008-12-05 22:32:48 +00002157}
2158
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002159raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002160 const ObjCCategoryImplDecl &CID) {
2161 OS << CID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002162 return OS;
2163}
2164
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002165//===----------------------------------------------------------------------===//
2166// ObjCImplementationDecl
2167//===----------------------------------------------------------------------===//
2168
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002169void ObjCImplementationDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002170
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002171ObjCImplementationDecl *
Mike Stump11289f42009-09-09 15:08:12 +00002172ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002173 ObjCInterfaceDecl *ClassInterface,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002174 ObjCInterfaceDecl *SuperDecl,
2175 SourceLocation nameLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002176 SourceLocation atStartLoc,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00002177 SourceLocation superLoc,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002178 SourceLocation IvarLBraceLoc,
2179 SourceLocation IvarRBraceLoc) {
Fariborz Jahanian87b4ae6c2011-12-23 00:31:02 +00002180 if (ClassInterface && ClassInterface->hasDefinition())
2181 ClassInterface = ClassInterface->getDefinition();
Richard Smithf7981722013-11-22 09:01:48 +00002182 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
2183 nameLoc, atStartLoc, superLoc,
2184 IvarLBraceLoc, IvarRBraceLoc);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002185}
2186
Douglas Gregor72172e92012-01-05 21:55:30 +00002187ObjCImplementationDecl *
2188ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002189 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
2190 SourceLocation(), SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002191}
2192
John McCall0410e572011-07-22 04:15:06 +00002193void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2194 CXXCtorInitializer ** initializers,
2195 unsigned numInitializers) {
2196 if (numInitializers > 0) {
2197 NumIvarInitializers = numInitializers;
Eugene Zelenko2a1ba942018-04-09 22:14:10 +00002198 auto **ivarInitializers = new (C) CXXCtorInitializer*[NumIvarInitializers];
John McCall0410e572011-07-22 04:15:06 +00002199 memcpy(ivarInitializers, initializers,
2200 numInitializers * sizeof(CXXCtorInitializer*));
2201 IvarInitializers = ivarInitializers;
2202 }
2203}
2204
Richard Smithc2bb8182015-03-24 06:36:48 +00002205ObjCImplementationDecl::init_const_iterator
2206ObjCImplementationDecl::init_begin() const {
2207 return IvarInitializers.get(getASTContext().getExternalSource());
2208}
2209
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002210raw_ostream &clang::operator<<(raw_ostream &OS,
Benjamin Kramer2f569922012-02-07 11:57:45 +00002211 const ObjCImplementationDecl &ID) {
2212 OS << ID.getName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002213 return OS;
2214}
2215
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002216//===----------------------------------------------------------------------===//
2217// ObjCCompatibleAliasDecl
2218//===----------------------------------------------------------------------===//
2219
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002220void ObjCCompatibleAliasDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002221
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002222ObjCCompatibleAliasDecl *
2223ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2224 SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +00002225 IdentifierInfo *Id,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002226 ObjCInterfaceDecl* AliasedClass) {
Richard Smithf7981722013-11-22 09:01:48 +00002227 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002228}
2229
Douglas Gregor72172e92012-01-05 21:55:30 +00002230ObjCCompatibleAliasDecl *
2231ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002232 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2233 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002234}
2235
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002236//===----------------------------------------------------------------------===//
2237// ObjCPropertyDecl
2238//===----------------------------------------------------------------------===//
2239
Eugene Zelenko2f8e66b2017-11-22 21:32:07 +00002240void ObjCPropertyDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002241
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002242ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2243 SourceLocation L,
2244 IdentifierInfo *Id,
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00002245 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002246 SourceLocation LParenLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +00002247 QualType T,
2248 TypeSourceInfo *TSI,
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002249 PropertyControl propControl) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002250 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2251 propControl);
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002252}
2253
Richard Smithf7981722013-11-22 09:01:48 +00002254ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002255 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002256 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2257 SourceLocation(), SourceLocation(),
Douglas Gregor813a0662015-06-19 18:14:38 +00002258 QualType(), nullptr, None);
Douglas Gregor72172e92012-01-05 21:55:30 +00002259}
2260
Douglas Gregore83b9562015-07-07 03:57:53 +00002261QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2262 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2263 ObjCSubstitutionContext::Property);
2264}
2265
Chris Lattnerf1ccb0c2009-02-20 20:59:54 +00002266//===----------------------------------------------------------------------===//
2267// ObjCPropertyImplDecl
2268//===----------------------------------------------------------------------===//
2269
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002270ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
Douglas Gregorc25d7a72009-01-09 00:49:46 +00002271 DeclContext *DC,
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002272 SourceLocation atLoc,
2273 SourceLocation L,
2274 ObjCPropertyDecl *property,
Daniel Dunbar3b4fdb02008-08-26 04:47:31 +00002275 Kind PK,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002276 ObjCIvarDecl *ivar,
2277 SourceLocation ivarLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002278 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2279 ivarLoc);
Fariborz Jahanian6efdf1d2008-04-23 00:06:01 +00002280}
Chris Lattnered0e1642008-03-17 01:19:02 +00002281
Richard Smithf7981722013-11-22 09:01:48 +00002282ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002283 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002284 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2285 SourceLocation(), nullptr, Dynamic,
2286 nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002287}
2288
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002289SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2290 SourceLocation EndLoc = getLocation();
2291 if (IvarLoc.isValid())
2292 EndLoc = IvarLoc;
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002293
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002294 return SourceRange(AtLoc, EndLoc);
2295}