Skip to content

Commit 5e1e184

Browse files
committed
AST: Clean up ExistentialLayout for ParameterizedProtocolType
1 parent 3400022 commit 5e1e184

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

include/swift/AST/ExistentialLayout.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
// This file defines the ExistentialLayout struct.
13+
// The ExistentialLayout struct describes the in-memory layout of an existential
14+
// type.
15+
//
16+
// It flattens and canonicalizes protocol compositions, and also expands defaults
17+
// for invertible protocols.
1418
//
1519
//===----------------------------------------------------------------------===//
1620

@@ -33,7 +37,6 @@ struct ExistentialLayout {
3337
hasExplicitAnyObject = false;
3438
containsObjCProtocol = false;
3539
containsSwiftProtocol = false;
36-
containsParameterized = false;
3740
representsAnyObject = false;
3841
}
3942

@@ -53,9 +56,6 @@ struct ExistentialLayout {
5356
/// Whether any protocol members require a witness table.
5457
bool containsSwiftProtocol : 1;
5558

56-
/// Whether any protocol members are parameterized.s
57-
bool containsParameterized : 1;
58-
5959
/// Whether this layout is the canonical layout for plain-old 'AnyObject'.
6060
bool representsAnyObject : 1;
6161

@@ -105,14 +105,18 @@ struct ExistentialLayout {
105105
/// calling this on a temporary is likely to be incorrect.
106106
ArrayRef<ProtocolDecl*> getProtocols() const && = delete;
107107

108+
ArrayRef<ParameterizedProtocolType *> getParameterizedProtocols() const & {
109+
return parameterized;
110+
}
111+
/// The returned ArrayRef points to internal storage, so
112+
/// calling this on a temporary is likely to be incorrect.
113+
ArrayRef<ProtocolDecl*> getParameterizedProtocols() const && = delete;
114+
108115
LayoutConstraint getLayoutConstraint() const;
109116

110117
private:
111118
SmallVector<ProtocolDecl *, 4> protocols;
112-
113-
/// Zero or more primary associated type requirements from a
114-
/// ParameterizedProtocolType
115-
ArrayRef<Type> sameTypeRequirements;
119+
SmallVector<ParameterizedProtocolType *, 4> parameterized;
116120
};
117121

118122
}

lib/AST/Type.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ ExistentialLayout::ExistentialLayout(CanProtocolType type) {
305305
containsObjCProtocol = protoDecl->isObjC();
306306
containsSwiftProtocol = (!protoDecl->isObjC() &&
307307
!protoDecl->isMarkerProtocol());
308-
containsParameterized = false;
309308
representsAnyObject = false;
310309

311310
protocols.push_back(protoDecl);
@@ -316,7 +315,6 @@ ExistentialLayout::ExistentialLayout(CanProtocolCompositionType type) {
316315
hasExplicitAnyObject = type->hasExplicitAnyObject();
317316
containsObjCProtocol = false;
318317
containsSwiftProtocol = false;
319-
containsParameterized = false;
320318

321319
auto members = type.getMembers();
322320
if (!members.empty() &&
@@ -331,9 +329,9 @@ ExistentialLayout::ExistentialLayout(CanProtocolCompositionType type) {
331329
if (auto protocolType = dyn_cast<ProtocolType>(member)) {
332330
protoDecl = protocolType->getDecl();
333331
} else {
334-
auto parameterized = cast<ParameterizedProtocolType>(member);
335-
protoDecl = parameterized->getProtocol();
336-
containsParameterized = true;
332+
auto *parameterizedType = member->castTo<ParameterizedProtocolType>();
333+
protoDecl = parameterizedType->getProtocol();
334+
parameterized.push_back(parameterizedType);
337335
}
338336
if (protoDecl->isObjC())
339337
containsObjCProtocol = true;
@@ -366,8 +364,7 @@ ExistentialLayout::ExistentialLayout(CanProtocolCompositionType type) {
366364

367365
ExistentialLayout::ExistentialLayout(CanParameterizedProtocolType type)
368366
: ExistentialLayout(type.getBaseType()) {
369-
sameTypeRequirements = type->getArgs();
370-
containsParameterized = true;
367+
parameterized.push_back(type);
371368
}
372369

373370
ExistentialLayout TypeBase::getExistentialLayout() {

lib/IRGen/MetadataRequest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static bool usesExtendedExistentialMetadata(CanType type) {
272272
// should turn them into unparameterized protocol types. If the
273273
// structure makes it to IRGen, we have to honor that decision that
274274
// they represent different types.
275-
return layout.containsParameterized;
275+
return !layout.getParameterizedProtocols().empty();
276276
}
277277

278278
static std::optional<std::pair<CanExistentialType, /*depth*/ unsigned>>
@@ -2026,7 +2026,7 @@ namespace {
20262026
llvm::Value *emitExistentialTypeMetadata(CanExistentialType type) {
20272027
auto layout = type.getExistentialLayout();
20282028

2029-
if (layout.containsParameterized) {
2029+
if (!layout.getParameterizedProtocols().empty()) {
20302030
return emitExtendedExistentialTypeMetadata(type);
20312031
}
20322032

0 commit comments

Comments
 (0)