Skip to content

Commit 1ab9f8f

Browse files
committed
[AST] Add helper methods to GenericTypeParamType for converting a given type
parameter to and from a pack type parameter.
1 parent e80ee92 commit 1ab9f8f

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

include/swift/AST/Types.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6172,6 +6172,14 @@ class GenericTypeParamType : public SubstitutableType {
61726172
/// \endcode
61736173
bool isParameterPack() const;
61746174

6175+
/// Returns a new GenericTypeParamType with the same depth and index
6176+
/// as this one, with the type parameter pack bit set.
6177+
GenericTypeParamType *asParameterPack(ASTContext &ctx) const;
6178+
6179+
/// Returns a new GenericTypeParamType with the same depth and index
6180+
/// as this one, removing the type parameter pack bit.
6181+
GenericTypeParamType *asScalar(ASTContext &ctx) const;
6182+
61756183
// Implement isa/cast/dyncast/etc.
61766184
static bool classof(const TypeBase *T) {
61776185
return T->getKind() == TypeKind::GenericTypeParam;

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5600,20 +5600,14 @@ ASTContext::getOpenedElementSignature(CanGenericSignature baseGenericSig) {
56005600
SmallVector<GenericTypeParamType *, 2> genericParams;
56015601
SmallVector<Requirement, 2> requirements;
56025602

5603-
auto eraseParameterPack = [&](GenericTypeParamType *paramType) {
5604-
return GenericTypeParamType::get(
5605-
/*isParameterPack=*/false, paramType->getDepth(),
5606-
paramType->getIndex(), *this);
5607-
};
5608-
56095603
for (auto paramType : baseGenericSig.getGenericParams()) {
5610-
genericParams.push_back(eraseParameterPack(paramType));
5604+
genericParams.push_back(paramType->asScalar(*this));
56115605
}
56125606

56135607
auto eraseParameterPackRec = [&](Type type) -> Type {
56145608
return type.transformRec([&](Type t) -> Optional<Type> {
56155609
if (auto *paramType = t->getAs<GenericTypeParamType>())
5616-
return Type(eraseParameterPack(paramType));
5610+
return Type(paramType->asScalar(*this));
56175611
return None;
56185612
});
56195613
};

lib/AST/ParameterPack.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ bool GenericTypeParamType::isParameterPack() const {
5555
GenericTypeParamType::TYPE_SEQUENCE_BIT;
5656
}
5757

58+
GenericTypeParamType *
59+
GenericTypeParamType::asParameterPack(ASTContext &ctx) const {
60+
return GenericTypeParamType::get(/*isParameterPack*/true,
61+
getDepth(), getIndex(),
62+
ctx);
63+
}
64+
65+
GenericTypeParamType *
66+
GenericTypeParamType::asScalar(ASTContext &ctx) const {
67+
return GenericTypeParamType::get(/*isParameterPack*/false,
68+
getDepth(), getIndex(),
69+
ctx);
70+
}
71+
5872
/// G<{X1, ..., Xn}, {Y1, ..., Yn}>... => {G<X1, Y1>, ..., G<Xn, Yn>}...
5973
PackExpansionType *PackExpansionType::expand() {
6074
auto countType = getCountType();
@@ -278,4 +292,4 @@ unsigned ParameterList::getOrigParamIndex(SubstitutionMap subMap,
278292
subMap.dump(llvm::errs());
279293
dump(llvm::errs());
280294
abort();
281-
}
295+
}

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7103,9 +7103,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
71037103
if (!genericParam || !genericParam->isParameterPack())
71047104
return type;
71057105

7106-
auto param = GenericTypeParamType::get(/*isParameterPack*/false,
7107-
genericParam->getDepth(),
7108-
genericParam->getIndex(), ctx);
7106+
auto param = genericParam->asScalar(ctx);
71097107
return expansion->getGenericEnvironment()->mapTypeIntoContext(param);
71107108
});
71117109

@@ -7120,10 +7118,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
71207118
return type;
71217119

71227120
auto *elementParam = element->mapTypeOutOfContext()->getAs<GenericTypeParamType>();
7123-
auto *pack = GenericTypeParamType::get(/*isParameterPack*/true,
7124-
elementParam->getDepth(),
7125-
elementParam->getIndex(),
7126-
ctx);
7121+
auto *pack = elementParam->asParameterPack(ctx);;
71277122
return cs.DC->mapTypeIntoContext(pack);
71287123
});
71297124
auto shapeType = toExpansionType->getCountType();

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8432,6 +8432,7 @@ ConstraintSystem::SolutionKind
84328432
ConstraintSystem::simplifyPackElementOfConstraint(Type first, Type second,
84338433
TypeMatchOptions flags,
84348434
ConstraintLocatorBuilder locator) {
8435+
ASTContext &ctx = getASTContext();
84358436
auto elementType = simplifyType(first, flags);
84368437
auto *loc = getConstraintLocator(locator);
84378438

@@ -8454,10 +8455,7 @@ ConstraintSystem::simplifyPackElementOfConstraint(Type first, Type second,
84548455
return type;
84558456

84568457
auto *elementParam = element->mapTypeOutOfContext()->getAs<GenericTypeParamType>();
8457-
auto *pack = GenericTypeParamType::get(/*isParameterPack*/true,
8458-
elementParam->getDepth(),
8459-
elementParam->getIndex(),
8460-
this->getASTContext());
8458+
auto *pack = elementParam->asParameterPack(ctx);
84618459
return this->DC->mapTypeIntoContext(pack);
84628460
});
84638461

lib/Sema/PreCheckExpr.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,7 @@ static Expr *getPackExpansion(DeclContext *dc, Expr *expr, SourceLoc opLoc) {
445445
if (!genericParam || !genericParam->isParameterPack())
446446
return type;
447447

448-
return GenericTypeParamType::get(/*isParameterPack*/false,
449-
genericParam->getDepth(),
450-
genericParam->getIndex(), ctx);
448+
return genericParam->asScalar(ctx);
451449
});
452450

453451
// Map the element interface type into the context of the opened

0 commit comments

Comments
 (0)