Skip to content

Commit 8f8b02c

Browse files
committed
AST: Move compareDependentTypes() to GenericSignature.cpp
1 parent b3deddb commit 8f8b02c

File tree

4 files changed

+40
-41
lines changed

4 files changed

+40
-41
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ inline bool CanGenericSignature::isActuallyCanonicalOrNull() const {
475475
int compareAssociatedTypes(AssociatedTypeDecl *assocType1,
476476
AssociatedTypeDecl *assocType2);
477477

478+
int compareDependentTypes(Type type1, Type type2);
479+
478480
} // end namespace swift
479481

480482
namespace llvm {

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,9 +1677,6 @@ inline bool isErrorResult(GenericSignatureBuilder::ConstraintResult result) {
16771677
llvm_unreachable("unhandled result");
16781678
}
16791679

1680-
/// Canonical ordering for dependent types.
1681-
int compareDependentTypes(Type type1, Type type2);
1682-
16831680
template<typename T>
16841681
Type GenericSignatureBuilder::Constraint<T>::getSubjectDependentType(
16851682
TypeArrayView<GenericTypeParamType> genericParams) const {

lib/AST/GenericSignature.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,5 +1457,43 @@ int swift::compareAssociatedTypes(AssociatedTypeDecl *assocType1,
14571457
if (assocType1 != assocType2)
14581458
return assocType1 < assocType2 ? -1 : +1;
14591459

1460+
return 0;
1461+
}
1462+
1463+
/// Canonical ordering for type parameters.
1464+
int swift::compareDependentTypes(Type type1, Type type2) {
1465+
// Fast-path check for equality.
1466+
if (type1->isEqual(type2)) return 0;
1467+
1468+
// Ordering is as follows:
1469+
// - Generic params
1470+
auto gp1 = type1->getAs<GenericTypeParamType>();
1471+
auto gp2 = type2->getAs<GenericTypeParamType>();
1472+
if (gp1 && gp2)
1473+
return GenericParamKey(gp1) < GenericParamKey(gp2) ? -1 : +1;
1474+
1475+
// A generic parameter is always ordered before a nested type.
1476+
if (static_cast<bool>(gp1) != static_cast<bool>(gp2))
1477+
return gp1 ? -1 : +1;
1478+
1479+
// - Dependent members
1480+
auto depMemTy1 = type1->castTo<DependentMemberType>();
1481+
auto depMemTy2 = type2->castTo<DependentMemberType>();
1482+
1483+
// - by base, so t_0_n.`P.T` < t_1_m.`P.T`
1484+
if (int compareBases =
1485+
compareDependentTypes(depMemTy1->getBase(), depMemTy2->getBase()))
1486+
return compareBases;
1487+
1488+
// - by name, so t_n_m.`P.T` < t_n_m.`P.U`
1489+
if (int compareNames = depMemTy1->getName().str().compare(
1490+
depMemTy2->getName().str()))
1491+
return compareNames;
1492+
1493+
auto *assocType1 = depMemTy1->getAssocType();
1494+
auto *assocType2 = depMemTy2->getAssocType();
1495+
if (int result = compareAssociatedTypes(assocType1, assocType2))
1496+
return result;
1497+
14601498
return 0;
14611499
}

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,44 +2482,6 @@ auto PotentialArchetype::getRepresentative() const -> PotentialArchetype * {
24822482
return result;
24832483
}
24842484

2485-
/// Canonical ordering for dependent types.
2486-
int swift::compareDependentTypes(Type type1, Type type2) {
2487-
// Fast-path check for equality.
2488-
if (type1->isEqual(type2)) return 0;
2489-
2490-
// Ordering is as follows:
2491-
// - Generic params
2492-
auto gp1 = type1->getAs<GenericTypeParamType>();
2493-
auto gp2 = type2->getAs<GenericTypeParamType>();
2494-
if (gp1 && gp2)
2495-
return GenericParamKey(gp1) < GenericParamKey(gp2) ? -1 : +1;
2496-
2497-
// A generic parameter is always ordered before a nested type.
2498-
if (static_cast<bool>(gp1) != static_cast<bool>(gp2))
2499-
return gp1 ? -1 : +1;
2500-
2501-
// - Dependent members
2502-
auto depMemTy1 = type1->castTo<DependentMemberType>();
2503-
auto depMemTy2 = type2->castTo<DependentMemberType>();
2504-
2505-
// - by base, so t_0_n.`P.T` < t_1_m.`P.T`
2506-
if (int compareBases =
2507-
compareDependentTypes(depMemTy1->getBase(), depMemTy2->getBase()))
2508-
return compareBases;
2509-
2510-
// - by name, so t_n_m.`P.T` < t_n_m.`P.U`
2511-
if (int compareNames = depMemTy1->getName().str().compare(
2512-
depMemTy2->getName().str()))
2513-
return compareNames;
2514-
2515-
auto *assocType1 = depMemTy1->getAssocType();
2516-
auto *assocType2 = depMemTy2->getAssocType();
2517-
if (int result = compareAssociatedTypes(assocType1, assocType2))
2518-
return result;
2519-
2520-
return 0;
2521-
}
2522-
25232485
/// Compare two dependent paths to determine which is better.
25242486
static int compareDependentPaths(ArrayRef<AssociatedTypeDecl *> path1,
25252487
ArrayRef<AssociatedTypeDecl *> path2) {

0 commit comments

Comments
 (0)