Skip to content

Commit 6220bc9

Browse files
committed
AST: Introduce PackType::unwrapSingletonPackExpansion()
1 parent 6f043e0 commit 6220bc9

File tree

5 files changed

+38
-28
lines changed

5 files changed

+38
-28
lines changed

include/swift/AST/Types.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6845,6 +6845,8 @@ class PackType final : public TypeBase, public llvm::FoldingSetNode,
68456845

68466846
bool containsPackExpansionType() const;
68476847

6848+
PackExpansionType *unwrapSingletonPackExpansion() const;
6849+
68486850
CanTypeWrapper<PackType> getReducedShape();
68496851

68506852
public:
@@ -6883,6 +6885,8 @@ BEGIN_CAN_TYPE_WRAPPER(PackType, Type)
68836885
CanTypeArrayRef getElementTypes() const {
68846886
return CanTypeArrayRef(getPointer()->getElementTypes());
68856887
}
6888+
6889+
CanTypeWrapper<PackExpansionType> unwrapSingletonPackExpansion() const;
68866890
END_CAN_TYPE_WRAPPER(PackType, Type)
68876891

68886892
inline CanPackType CanTupleType::getInducedPackType() const {
@@ -6971,6 +6975,13 @@ BEGIN_CAN_TYPE_WRAPPER(PackExpansionType, Type)
69716975
}
69726976
END_CAN_TYPE_WRAPPER(PackExpansionType, Type)
69736977

6978+
6979+
inline CanTypeWrapper<PackExpansionType>
6980+
CanPackType::unwrapSingletonPackExpansion() const {
6981+
return CanPackExpansionType(
6982+
getPointer()->unwrapSingletonPackExpansion());
6983+
}
6984+
69746985
/// getASTContext - Return the ASTContext that this type belongs to.
69756986
inline ASTContext &TypeBase::getASTContext() const {
69766987
// If this type is canonical, it has the ASTContext in it.

lib/AST/ParameterPack.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,18 @@ CanPackType CanPackType::getSingletonPackExpansion(CanType param) {
300300
return CanPackType(PackType::getSingletonPackExpansion(param));
301301
}
302302

303+
PackExpansionType *PackType::unwrapSingletonPackExpansion() const {
304+
if (getNumElements() == 1) {
305+
if (auto expansion = getElementTypes()[0]->getAs<PackExpansionType>()) {
306+
auto pattern = expansion->getPatternType();
307+
if (pattern->isParameterPack() || pattern->is<PackArchetypeType>())
308+
return expansion;
309+
}
310+
}
311+
312+
return nullptr;
313+
}
314+
303315
bool SILPackType::containsPackExpansionType() const {
304316
for (auto type : getElementTypes()) {
305317
if (isa<PackExpansionType>(type))

lib/IRGen/Fulfillment.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,12 @@ bool FulfillmentMap::searchTypeMetadata(IRGenModule &IGM, CanType type,
182182
static CanType getSingletonPackExpansionParameter(CanPackType packType,
183183
const FulfillmentMap::InterestingKeysCallback &keys,
184184
Optional<unsigned> &packExpansionComponent) {
185-
if (packType->getNumElements() != 1)
186-
return CanType();
187-
auto expansion = dyn_cast<PackExpansionType>(packType.getElementType(0));
188-
if (!expansion || !keys.isInterestingPackExpansion(expansion))
189-
return CanType();
190-
191-
packExpansionComponent = 0;
192-
return expansion.getPatternType();
185+
if (auto expansion = packType.unwrapSingletonPackExpansion()) {
186+
if (keys.isInterestingPackExpansion(expansion)) {
187+
packExpansionComponent = 0;
188+
return expansion.getPatternType();
189+
}
190+
}
193191
}
194192

195193
bool FulfillmentMap::searchTypeMetadataPack(IRGenModule &IGM,
@@ -405,10 +403,7 @@ bool FulfillmentMap::searchShapeRequirement(IRGenModule &IGM, CanType argType,
405403
// there aren't expansions over pack parameters with different shapes,
406404
// we should always be able to turn this into the equation
407405
// `ax + b = <fulfilled count>` and then solve that.
408-
if (packType->getNumElements() != 1)
409-
return false;
410-
auto expansion =
411-
dyn_cast<PackExpansionType>(packType.getElementType(0));
406+
auto expansion = packType.unwrapSingletonPackExpansion();
412407
if (!expansion)
413408
return false;
414409

lib/IRGen/GenPack.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,9 @@ using namespace irgen;
3737

3838
static CanPackArchetypeType
3939
getForwardedPackArchetypeType(CanPackType packType) {
40-
if (packType->getNumElements() != 1)
41-
return CanPackArchetypeType();
42-
auto uncastElement = packType.getElementType(0);
43-
auto element = dyn_cast<PackExpansionType>(uncastElement);
44-
if (!element)
45-
return CanPackArchetypeType();
46-
auto patternType = element.getPatternType();
47-
auto packArchetype = dyn_cast<PackArchetypeType>(patternType);
48-
return packArchetype;
40+
if (auto expansion = packType.unwrapSingletonPackExpansion())
41+
return dyn_cast<PackArchetypeType>(expansion.getPatternType());
42+
return CanPackArchetypeType();
4943
}
5044

5145
static MetadataResponse

lib/IRGen/GenProto.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3730,13 +3730,11 @@ void irgen::bindGenericRequirement(IRGenFunction &IGF,
37303730
// FIXME: Remove this
37313731
bool wasUnwrappedPack = false;
37323732
if (auto packType = dyn_cast<PackType>(type)) {
3733-
if (packType->getNumElements() == 1) {
3734-
auto eltType = packType.getElementType(0);
3735-
if (auto expansionType = dyn_cast<PackExpansionType>(eltType)) {
3736-
if (auto archetypeType = dyn_cast<PackArchetypeType>(expansionType.getPatternType())) {
3737-
type = archetypeType;
3738-
wasUnwrappedPack = true;
3739-
}
3733+
if (auto expansionType = packType.unwrapSingletonPackExpansion()) {
3734+
if (auto archetypeType = dyn_cast_or_null<PackArchetypeType>(
3735+
expansionType.getPatternType())) {
3736+
type = archetypeType;
3737+
wasUnwrappedPack = true;
37403738
}
37413739
}
37423740
}

0 commit comments

Comments
 (0)