Skip to content

Commit 8f2e11a

Browse files
committed
AST: ExistentialLayout support for ParametrizedProtocolType
1 parent e99e2b3 commit 8f2e11a

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

include/swift/AST/ExistentialLayout.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ namespace swift {
2626
class ProtocolType;
2727
class ProtocolCompositionType;
2828

29+
struct PrimaryAssociatedTypeRequirement {
30+
AssociatedTypeDecl *AssocType;
31+
Type Argument;
32+
};
33+
2934
struct ExistentialLayout {
3035
enum Kind { Class, Error, Opaque };
3136

@@ -37,6 +42,7 @@ struct ExistentialLayout {
3742

3843
ExistentialLayout(ProtocolType *type);
3944
ExistentialLayout(ProtocolCompositionType *type);
45+
ExistentialLayout(ParametrizedProtocolType *type);
4046

4147
/// The explicit superclass constraint, if any.
4248
Type explicitSuperclass;
@@ -108,6 +114,10 @@ struct ExistentialLayout {
108114

109115
/// Zero or more protocol constraints from a ProtocolCompositionType
110116
ArrayRef<Type> protocols;
117+
118+
/// Zero or more primary associated type requirements from a
119+
/// ParametrizedProtocolType
120+
ArrayRef<PrimaryAssociatedTypeRequirement> sameTypeRequirements;
111121
};
112122

113123
}

include/swift/AST/Types.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5261,7 +5261,10 @@ END_CAN_TYPE_WRAPPER(ProtocolCompositionType, Type)
52615261
/// \endcode
52625262
class ParametrizedProtocolType final : public TypeBase,
52635263
public llvm::FoldingSetNode {
5264+
friend struct ExistentialLayout;
5265+
52645266
ProtocolType *Base;
5267+
AssociatedTypeDecl *AssocType;
52655268
Type Arg;
52665269

52675270
public:
@@ -5274,6 +5277,10 @@ class ParametrizedProtocolType final : public TypeBase,
52745277
return Base;
52755278
}
52765279

5280+
AssociatedTypeDecl *getAssocType() const {
5281+
return AssocType;
5282+
}
5283+
52775284
Type getArgumentType() const {
52785285
return Arg;
52795286
}
@@ -5293,9 +5300,7 @@ class ParametrizedProtocolType final : public TypeBase,
52935300
private:
52945301
ParametrizedProtocolType(const ASTContext *ctx,
52955302
ProtocolType *base, Type arg,
5296-
RecursiveTypeProperties properties)
5297-
: TypeBase(TypeKind::ParametrizedProtocol, /*Context=*/ctx, properties),
5298-
Base(base), Arg(arg) { }
5303+
RecursiveTypeProperties properties);
52995304
};
53005305
BEGIN_CAN_TYPE_WRAPPER(ParametrizedProtocolType, Type)
53015306
PROXY_CAN_TYPE_SIMPLE_GETTER(getBaseType)

lib/AST/Type.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,15 @@ ExistentialLayout::ExistentialLayout(ProtocolCompositionType *type) {
295295
protocols = { members.data(), members.size() };
296296
}
297297

298+
ExistentialLayout::ExistentialLayout(ParametrizedProtocolType *type) {
299+
assert(type->isCanonical());
300+
301+
*this = ExistentialLayout(type->getBaseType());
302+
sameTypeRequirements = {
303+
reinterpret_cast<PrimaryAssociatedTypeRequirement *>(&type->AssocType),
304+
1
305+
};
306+
}
298307

299308
ExistentialLayout TypeBase::getExistentialLayout() {
300309
return getCanonicalType().getExistentialLayout();
@@ -310,6 +319,9 @@ ExistentialLayout CanType::getExistentialLayout() {
310319
if (auto proto = dyn_cast<ProtocolType>(*this))
311320
return ExistentialLayout(proto);
312321

322+
if (auto param = dyn_cast<ParametrizedProtocolType>(*this))
323+
return ExistentialLayout(param);
324+
313325
auto comp = cast<ProtocolCompositionType>(*this);
314326
return ExistentialLayout(comp);
315327
}
@@ -3808,6 +3820,17 @@ void ProtocolCompositionType::Profile(llvm::FoldingSetNodeID &ID,
38083820
ID.AddPointer(T.getPointer());
38093821
}
38103822

3823+
ParametrizedProtocolType::ParametrizedProtocolType(
3824+
const ASTContext *ctx,
3825+
ProtocolType *base, Type arg,
3826+
RecursiveTypeProperties properties)
3827+
: TypeBase(TypeKind::ParametrizedProtocol, /*Context=*/ctx, properties),
3828+
Base(base), AssocType(base->getDecl()->getPrimaryAssociatedType()),
3829+
Arg(arg) {
3830+
assert(AssocType != nullptr &&
3831+
"Protocol doesn't have a primary associated type");
3832+
}
3833+
38113834
void ParametrizedProtocolType::Profile(llvm::FoldingSetNodeID &ID,
38123835
ProtocolType *baseTy,
38133836
Type argTy) {

0 commit comments

Comments
 (0)