Skip to content

Commit dffc55e

Browse files
committed
AST: Introduce TypeBase::increasePackElementLevel()
1 parent 43eb71c commit dffc55e

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

include/swift/AST/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
780780
/// this function will wrap into a pack containing a singleton expansion.
781781
PackType *getPackSubstitutionAsPackType();
782782

783+
/// Increase the expansion level of each parameter pack appearing in this type.
784+
Type increasePackElementLevel(unsigned level);
785+
783786
/// Determines whether this type is an lvalue. This includes both straight
784787
/// lvalue types as well as tuples or optionals of lvalues.
785788
bool hasLValueType() {

lib/AST/ParameterPack.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,45 @@ PackType *TypeBase::getPackSubstitutionAsPackType() {
133133
}
134134
}
135135

136+
static Type increasePackElementLevelImpl(
137+
Type type, unsigned level, unsigned outerLevel) {
138+
assert(level > 0);
139+
140+
return type.transformRec([&](TypeBase *t) -> Optional<Type> {
141+
if (auto *elementType = dyn_cast<PackElementType>(t)) {
142+
if (elementType->getLevel() >= outerLevel) {
143+
elementType = PackElementType::get(elementType->getPackType(),
144+
elementType->getLevel() + level);
145+
}
146+
147+
return Type(elementType);
148+
}
149+
150+
if (auto *expansionType = dyn_cast<PackExpansionType>(t)) {
151+
return Type(PackExpansionType::get(
152+
increasePackElementLevelImpl(expansionType->getPatternType(),
153+
level, outerLevel + 1),
154+
expansionType->getCountType()));
155+
}
156+
157+
if (t->isParameterPack() || isa<PackArchetypeType>(t)) {
158+
if (outerLevel == 0)
159+
return Type(PackElementType::get(t, level));
160+
161+
return Type(t);
162+
}
163+
164+
return None;
165+
});
166+
}
167+
168+
Type TypeBase::increasePackElementLevel(unsigned level) {
169+
if (level == 0)
170+
return Type(this);
171+
172+
return increasePackElementLevelImpl(Type(this), level, 0);
173+
}
174+
136175
CanType PackExpansionType::getReducedShape() {
137176
auto reducedShape = countType->getReducedShape();
138177
if (reducedShape == getASTContext().TheEmptyTupleType)

0 commit comments

Comments
 (0)