Skip to content

Commit 3fa6286

Browse files
committed
[NFC] Make ExistentialLayout store the presence of parameterized protocols
This is preparation for supporting parameterized protocols in protocol compositions. The `sameTypeRequirements` field will probably have to go; it's just not general enough to capture what we want. That's assuming that we keep ExistentialLayout in the long term at all, of course.
1 parent d0691e3 commit 3fa6286

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

include/swift/AST/ExistentialLayout.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct ExistentialLayout {
3232
ExistentialLayout() {
3333
hasExplicitAnyObject = false;
3434
containsNonObjCProtocol = false;
35+
containsParameterized = false;
3536
singleProtocol = nullptr;
3637
}
3738

@@ -48,6 +49,9 @@ struct ExistentialLayout {
4849
/// Whether any protocol members are non-@objc.
4950
bool containsNonObjCProtocol : 1;
5051

52+
/// Whether any protocol members are parameterized.s
53+
bool containsParameterized : 1;
54+
5155
/// Return the kind of this existential (class/error/opaque).
5256
Kind getKind() {
5357
if (requiresClass())

lib/AST/Type.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ ExistentialLayout::ExistentialLayout(ProtocolType *type) {
271271

272272
hasExplicitAnyObject = false;
273273
containsNonObjCProtocol = !protoDecl->isObjC();
274+
containsParameterized = false;
274275

275276
singleProtocol = type;
276277
}
@@ -280,16 +281,24 @@ ExistentialLayout::ExistentialLayout(ProtocolCompositionType *type) {
280281

281282
hasExplicitAnyObject = type->hasExplicitAnyObject();
282283
containsNonObjCProtocol = false;
284+
containsParameterized = false;
283285

284-
auto members = type->getMembers();
286+
auto members = type.getMembers();
285287
if (!members.empty() &&
286-
isa<ClassDecl>(members[0]->getAnyNominal())) {
288+
isa<ClassDecl>(members[0].getAnyNominal())) {
287289
explicitSuperclass = members[0];
288290
members = members.slice(1);
289291
}
290292

291293
for (auto member : members) {
292-
auto *protoDecl = member->castTo<ProtocolType>()->getDecl();
294+
ProtocolDecl *protoDecl;
295+
if (auto protocolType = dyn_cast<ProtocolType>(member)) {
296+
protoDecl = protocolType->getDecl();
297+
} else {
298+
auto parameterized = cast<ParameterizedProtocolType>(member);
299+
protoDecl = parameterized->getProtocol();
300+
containsParameterized = true;
301+
}
293302
containsNonObjCProtocol |= !protoDecl->isObjC();
294303
}
295304

@@ -302,6 +311,7 @@ ExistentialLayout::ExistentialLayout(ParameterizedProtocolType *type) {
302311

303312
*this = ExistentialLayout(type->getBaseType());
304313
sameTypeRequirements = type->getArgs();
314+
containsParameterized = true;
305315
}
306316

307317
ExistentialLayout TypeBase::getExistentialLayout() {

0 commit comments

Comments
 (0)