Skip to content

Commit b3deddb

Browse files
committed
AST: Factor out duplicated definition of compareAssociatedTypes()
1 parent 7cf32d2 commit b3deddb

File tree

4 files changed

+32
-58
lines changed

4 files changed

+32
-58
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ inline bool CanGenericSignature::isActuallyCanonicalOrNull() const {
472472
getPointer()->isCanonical();
473473
}
474474

475+
int compareAssociatedTypes(AssociatedTypeDecl *assocType1,
476+
AssociatedTypeDecl *assocType2);
477+
475478
} // end namespace swift
476479

477480
namespace llvm {

lib/AST/GenericSignature.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,3 +1430,32 @@ bool Requirement::canBeSatisfied() const {
14301430

14311431
llvm_unreachable("Bad requirement kind");
14321432
}
1433+
1434+
/// Compare two associated types.
1435+
int swift::compareAssociatedTypes(AssociatedTypeDecl *assocType1,
1436+
AssociatedTypeDecl *assocType2) {
1437+
// - by name.
1438+
if (int result = assocType1->getName().str().compare(
1439+
assocType2->getName().str()))
1440+
return result;
1441+
1442+
// Prefer an associated type with no overrides (i.e., an anchor) to one
1443+
// that has overrides.
1444+
bool hasOverridden1 = !assocType1->getOverriddenDecls().empty();
1445+
bool hasOverridden2 = !assocType2->getOverriddenDecls().empty();
1446+
if (hasOverridden1 != hasOverridden2)
1447+
return hasOverridden1 ? +1 : -1;
1448+
1449+
// - by protocol, so t_n_m.`P.T` < t_n_m.`Q.T` (given P < Q)
1450+
auto proto1 = assocType1->getProtocol();
1451+
auto proto2 = assocType2->getProtocol();
1452+
if (int compareProtocols = TypeDecl::compare(proto1, proto2))
1453+
return compareProtocols;
1454+
1455+
// Error case: if we have two associated types with the same name in the
1456+
// same protocol, just tie-break based on address.
1457+
if (assocType1 != assocType2)
1458+
return assocType1 < assocType2 ? -1 : +1;
1459+
1460+
return 0;
1461+
}

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,35 +1979,6 @@ bool EquivalenceClass::isConformanceSatisfiedBySuperclass(
19791979
return false;
19801980
}
19811981

1982-
/// Compare two associated types.
1983-
static int compareAssociatedTypes(AssociatedTypeDecl *assocType1,
1984-
AssociatedTypeDecl *assocType2) {
1985-
// - by name.
1986-
if (int result = assocType1->getName().str().compare(
1987-
assocType2->getName().str()))
1988-
return result;
1989-
1990-
// Prefer an associated type with no overrides (i.e., an anchor) to one
1991-
// that has overrides.
1992-
bool hasOverridden1 = !assocType1->getOverriddenDecls().empty();
1993-
bool hasOverridden2 = !assocType2->getOverriddenDecls().empty();
1994-
if (hasOverridden1 != hasOverridden2)
1995-
return hasOverridden1 ? +1 : -1;
1996-
1997-
// - by protocol, so t_n_m.`P.T` < t_n_m.`Q.T` (given P < Q)
1998-
auto proto1 = assocType1->getProtocol();
1999-
auto proto2 = assocType2->getProtocol();
2000-
if (int compareProtocols = TypeDecl::compare(proto1, proto2))
2001-
return compareProtocols;
2002-
2003-
// Error case: if we have two associated types with the same name in the
2004-
// same protocol, just tie-break based on address.
2005-
if (assocType1 != assocType2)
2006-
return assocType1 < assocType2 ? -1 : +1;
2007-
2008-
return 0;
2009-
}
2010-
20111982
static void lookupConcreteNestedType(NominalTypeDecl *decl,
20121983
Identifier name,
20131984
SmallVectorImpl<TypeDecl *> &concreteDecls) {

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -575,35 +575,6 @@ RequirementMachine::getConformanceAccessPath(Type type,
575575
}
576576
}
577577

578-
/// Compare two associated types.
579-
static int compareAssociatedTypes(AssociatedTypeDecl *assocType1,
580-
AssociatedTypeDecl *assocType2) {
581-
// - by name.
582-
if (int result = assocType1->getName().str().compare(
583-
assocType2->getName().str()))
584-
return result;
585-
586-
// Prefer an associated type with no overrides (i.e., an anchor) to one
587-
// that has overrides.
588-
bool hasOverridden1 = !assocType1->getOverriddenDecls().empty();
589-
bool hasOverridden2 = !assocType2->getOverriddenDecls().empty();
590-
if (hasOverridden1 != hasOverridden2)
591-
return hasOverridden1 ? +1 : -1;
592-
593-
// - by protocol, so t_n_m.`P.T` < t_n_m.`Q.T` (given P < Q)
594-
auto proto1 = assocType1->getProtocol();
595-
auto proto2 = assocType2->getProtocol();
596-
if (int compareProtocols = TypeDecl::compare(proto1, proto2))
597-
return compareProtocols;
598-
599-
// Error case: if we have two associated types with the same name in the
600-
// same protocol, just tie-break based on address.
601-
if (assocType1 != assocType2)
602-
return assocType1 < assocType2 ? -1 : +1;
603-
604-
return 0;
605-
}
606-
607578
static void lookupConcreteNestedType(NominalTypeDecl *decl,
608579
Identifier name,
609580
SmallVectorImpl<TypeDecl *> &concreteDecls) {

0 commit comments

Comments
 (0)