Skip to content

Commit b3b52bb

Browse files
committed
AST: Factor out duplication between PackTypeParameterCollector and PackTypeVariableCollector
1 parent 75e21e6 commit b3b52bb

File tree

3 files changed

+42
-45
lines changed

3 files changed

+42
-45
lines changed

include/swift/AST/Types.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,8 +724,10 @@ class alignas(1 << TypeAlignInBits) TypeBase
724724
SmallVectorImpl<OpenedArchetypeType *> &rootOpenedArchetypes) const;
725725

726726
/// Retrieve the set of type parameter packs that occur within this type.
727-
void getTypeParameterPacks(
728-
SmallVectorImpl<Type> &rootParameterPacks);
727+
void getTypeParameterPacks(SmallVectorImpl<Type> &rootParameterPacks);
728+
729+
/// Retrieve the set of type parameter packs that occur within this type.
730+
void walkPackReferences(llvm::function_ref<bool (Type)> fn);
729731

730732
/// Replace opened archetypes with the given root with their most
731733
/// specific non-dependent upper bounds throughout this type.

lib/AST/ParameterPack.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ namespace {
2929

3030
/// Collects all unique pack type parameters referenced from the pattern type,
3131
/// skipping those captured by nested pack expansion types.
32-
struct PackTypeParameterCollector: TypeWalker {
33-
llvm::SetVector<Type> typeParams;
32+
struct PackReferenceCollector: TypeWalker {
33+
llvm::function_ref<bool (Type)> fn;
3434
unsigned expansionLevel;
3535
SmallVector<unsigned, 2> elementLevel;
3636

37-
PackTypeParameterCollector() : expansionLevel(0) {
37+
PackReferenceCollector(llvm::function_ref<bool (Type)> fn)
38+
: fn(fn), expansionLevel(0) {
3839
elementLevel.push_back(0);
3940
}
4041

@@ -72,12 +73,8 @@ struct PackTypeParameterCollector: TypeWalker {
7273
}
7374

7475
if (elementLevel.back() == expansionLevel) {
75-
if (auto *paramTy = t->getAs<GenericTypeParamType>()) {
76-
if (paramTy->isParameterPack())
77-
typeParams.insert(paramTy);
78-
} else if (auto *archetypeTy = t->getAs<PackArchetypeType>()) {
79-
typeParams.insert(archetypeTy->getRoot());
80-
}
76+
if (fn(t))
77+
return Action::Stop;
8178
}
8279

8380
return Action::Continue;
@@ -96,13 +93,28 @@ struct PackTypeParameterCollector: TypeWalker {
9693

9794
}
9895

96+
void TypeBase::walkPackReferences(
97+
llvm::function_ref<bool (Type)> fn) {
98+
Type(this).walk(PackReferenceCollector(fn));
99+
}
100+
99101
void TypeBase::getTypeParameterPacks(
100102
SmallVectorImpl<Type> &rootParameterPacks) {
101-
PackTypeParameterCollector collector;
102-
Type(this).walk(collector);
103+
llvm::SmallSetVector<Type, 2> rootParameterPackSet;
104+
105+
walkPackReferences([&](Type t) {
106+
if (auto *paramTy = t->getAs<GenericTypeParamType>()) {
107+
if (paramTy->isParameterPack())
108+
rootParameterPackSet.insert(paramTy);
109+
} else if (auto *archetypeTy = t->getAs<PackArchetypeType>()) {
110+
rootParameterPackSet.insert(archetypeTy->getRoot());
111+
}
112+
113+
return false;
114+
});
103115

104-
rootParameterPacks.append(collector.typeParams.begin(),
105-
collector.typeParams.end());
116+
rootParameterPacks.append(rootParameterPackSet.begin(),
117+
rootParameterPackSet.end());
106118
}
107119

108120
bool GenericTypeParamType::isParameterPack() const {

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,30 +2343,6 @@ ConstraintSystem::matchPackTypes(PackType *pack1, PackType *pack2,
23432343
return getTypeMatchSuccess();
23442344
}
23452345

2346-
namespace {
2347-
2348-
/// Collects all unique pack type variables referenced from the pattern type,
2349-
/// skipping those captured by nested pack expansion types.
2350-
///
2351-
/// FIXME: This could probably be a common utility method somewhere.
2352-
struct PackTypeVariableCollector: TypeWalker {
2353-
llvm::SetVector<TypeVariableType *> typeVars;
2354-
2355-
Action walkToTypePre(Type t) override {
2356-
if (t->is<PackExpansionType>())
2357-
return Action::SkipChildren;
2358-
2359-
if (auto *typeVar = t->getAs<TypeVariableType>()) {
2360-
if (typeVar->getImpl().canBindToPack())
2361-
typeVars.insert(typeVar);
2362-
}
2363-
2364-
return Action::Continue;
2365-
}
2366-
};
2367-
2368-
}
2369-
23702346
/// Utility function used when matching a pack expansion type against a
23712347
/// pack type.
23722348
///
@@ -2399,21 +2375,26 @@ static PackType *replaceTypeVariablesWithFreshPacks(ConstraintSystem &cs,
23992375
Type pattern,
24002376
PackType *pack,
24012377
ConstraintLocatorBuilder locator) {
2402-
SmallVector<Type, 2> elts;
2403-
2378+
llvm::SmallSetVector<TypeVariableType *, 2> typeVarSet;
24042379
llvm::MapVector<TypeVariableType *, SmallVector<Type, 2>> typeVars;
24052380

2406-
PackTypeVariableCollector collector;
2407-
pattern.walk(collector);
2381+
pattern->walkPackReferences([&](Type t) {
2382+
if (auto *typeVar = t->getAs<TypeVariableType>()) {
2383+
if (typeVar->getImpl().canBindToPack())
2384+
typeVarSet.insert(typeVar);
2385+
}
2386+
2387+
return false;
2388+
});
24082389

2409-
if (collector.typeVars.empty())
2390+
if (typeVarSet.empty())
24102391
return nullptr;
24112392

24122393
auto *loc = cs.getConstraintLocator(locator);
24132394

24142395
// For each pack type variable occurring in the pattern type, compute a
24152396
// binding pack type comprised of fresh type variables.
2416-
for (auto *typeVar : collector.typeVars) {
2397+
for (auto *typeVar : typeVarSet) {
24172398
auto &freshTypeVars = typeVars[typeVar];
24182399
for (unsigned i = 0, e = pack->getNumElements(); i < e; ++i) {
24192400
auto *packExpansionElt = pack->getElementType(i)->getAs<PackExpansionType>();
@@ -2441,6 +2422,8 @@ static PackType *replaceTypeVariablesWithFreshPacks(ConstraintSystem &cs,
24412422
}
24422423
}
24432424

2425+
SmallVector<Type, 2> elts;
2426+
24442427
// For each element of the original pack type, instantiate the pattern type by
24452428
// replacing each pack type variable with the corresponding element of the
24462429
// pack type variable's binding pack.

0 commit comments

Comments
 (0)