Skip to content

Commit be619c6

Browse files
committed
[RequirementMachine] Add a generic signature query that determines whether
two type parameter packs have the same shape.
1 parent c7c1a20 commit be619c6

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
426426
/// whose root generic parameter is a pack.
427427
Type getReducedShape(Type type) const;
428428

429+
/// Returns \c true if the given type parameter packs are in
430+
/// the same shape equivalence class.
431+
bool haveSameShape(Type type1, Type type2) const;
432+
429433
/// Get the ordinal of a generic parameter in this generic signature.
430434
///
431435
/// For example, if you have a generic signature for a nested context like:

lib/AST/GenericSignature.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@ GenericSignatureImpl::getReducedShape(Type type) const {
539539
return getRequirementMachine()->getReducedShape(type);
540540
}
541541

542+
bool
543+
GenericSignatureImpl::haveSameShape(Type type1, Type type2) const {
544+
return getRequirementMachine()->haveSameShape(type1, type2);
545+
}
546+
542547
unsigned GenericParamKey::findIndexIn(
543548
TypeArrayView<GenericTypeParamType> genericParams) const {
544549
// For depth 0, we have random access. We perform the extra checking so that

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,9 @@ RequirementMachine::lookupNestedType(Type depType, Identifier name) const {
696696
return nullptr;
697697
}
698698

699-
Type RequirementMachine::getReducedShape(Type type) const {
700-
if (!type->isTypeSequenceParameter())
701-
return Type();
699+
MutableTerm
700+
RequirementMachine::getReducedShapeTerm(Type type) const {
701+
assert(type->isTypeSequenceParameter());
702702

703703
auto rootType = type->getRootGenericParam();
704704
auto term = Context.getMutableTermForType(rootType->getCanonicalType(),
@@ -714,7 +714,22 @@ Type RequirementMachine::getReducedShape(Type type) const {
714714
assert(term.back().getKind() == Symbol::Kind::Shape);
715715
MutableTerm reducedTerm(term.begin(), term.end() - 1);
716716

717-
return Map.getTypeForTerm(reducedTerm, getGenericParams());
717+
return reducedTerm;
718+
}
719+
720+
Type RequirementMachine::getReducedShape(Type type) const {
721+
if (!type->isTypeSequenceParameter())
722+
return Type();
723+
724+
return Map.getTypeForTerm(getReducedShapeTerm(type),
725+
getGenericParams());
726+
}
727+
728+
bool RequirementMachine::haveSameShape(Type type1, Type type2) const {
729+
auto term1 = getReducedShapeTerm(type1);
730+
auto term2 = getReducedShapeTerm(type2);
731+
732+
return term1 == term2;
718733
}
719734

720735
void RequirementMachine::verify(const MutableTerm &term) const {

lib/AST/RequirementMachine/RequirementMachine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,14 @@ class RequirementMachine final {
160160
ConformancePath getConformancePath(Type type, ProtocolDecl *protocol);
161161
TypeDecl *lookupNestedType(Type depType, Identifier name) const;
162162

163+
private:
164+
MutableTerm getReducedShapeTerm(Type type) const;
165+
166+
public:
163167
Type getReducedShape(Type type) const;
164168

169+
bool haveSameShape(Type type1, Type type2) const;
170+
165171
llvm::DenseMap<const ProtocolDecl *, RequirementSignature>
166172
computeMinimalProtocolRequirements();
167173

0 commit comments

Comments
 (0)