@@ -1683,6 +1683,68 @@ getOperatorDesignatedNominalTypes(Constraint *bindOverload) {
1683
1683
return operatorDecl->getDesignatedNominalTypes ();
1684
1684
}
1685
1685
1686
+ void ConstraintSystem::partitionForDesignatedTypes (
1687
+ ArrayRef<Constraint *> Choices, ConstraintMatchLoop forEachChoice,
1688
+ PartitionAppendCallback appendPartition) {
1689
+
1690
+ auto designatedNominalTypes = getOperatorDesignatedNominalTypes (Choices[0 ]);
1691
+ if (designatedNominalTypes.empty ())
1692
+ return ;
1693
+
1694
+ SmallVector<SmallVector<unsigned , 4 >, 4 > definedInDesignatedType;
1695
+ SmallVector<SmallVector<unsigned , 4 >, 4 > definedInExtensionOfDesignatedType;
1696
+
1697
+ auto examineConstraint =
1698
+ [&](unsigned constraintIndex, Constraint *constraint) -> bool {
1699
+ auto *decl = constraint->getOverloadChoice ().getDecl ();
1700
+ auto *funcDecl = cast<FuncDecl>(decl);
1701
+
1702
+ auto *parentDC = funcDecl->getParent ();
1703
+ auto *parentDecl = parentDC->getAsDecl ();
1704
+
1705
+ if (parentDC->isExtensionContext ())
1706
+ parentDecl = cast<ExtensionDecl>(parentDecl)->getExtendedNominal ();
1707
+
1708
+ for (auto designatedTypeIndex : indices (designatedNominalTypes)) {
1709
+ auto *designatedNominal =
1710
+ designatedNominalTypes[designatedTypeIndex];
1711
+
1712
+ if (parentDecl != designatedNominal)
1713
+ continue ;
1714
+
1715
+ auto &constraints =
1716
+ parentDC->isExtensionContext ()
1717
+ ? definedInExtensionOfDesignatedType[designatedTypeIndex]
1718
+ : definedInDesignatedType[designatedTypeIndex];
1719
+
1720
+ constraints.push_back (constraintIndex);
1721
+ return true ;
1722
+ }
1723
+
1724
+ return false ;
1725
+ };
1726
+
1727
+ definedInDesignatedType.resize (designatedNominalTypes.size ());
1728
+ definedInExtensionOfDesignatedType.resize (designatedNominalTypes.size ());
1729
+
1730
+ forEachChoice (Choices, examineConstraint);
1731
+
1732
+ // Now collect the overload choices that are defined within the type
1733
+ // that was designated in the operator declaration.
1734
+ // Add partitions for each of the overloads we found in types that
1735
+ // were designated as part of the operator declaration.
1736
+ for (auto designatedTypeIndex : indices (designatedNominalTypes)) {
1737
+ if (designatedTypeIndex < definedInDesignatedType.size ()) {
1738
+ auto &primary = definedInDesignatedType[designatedTypeIndex];
1739
+ appendPartition (primary);
1740
+ }
1741
+ if (designatedTypeIndex < definedInExtensionOfDesignatedType.size ()) {
1742
+ auto &secondary = definedInExtensionOfDesignatedType[designatedTypeIndex];
1743
+ appendPartition (secondary);
1744
+ }
1745
+ }
1746
+ }
1747
+
1686
1748
void ConstraintSystem::partitionDisjunction (
1687
1749
ArrayRef<Constraint *> Choices, SmallVectorImpl<unsigned > &Ordering,
1688
1750
SmallVectorImpl<unsigned > &PartitionBeginning) {
@@ -1702,20 +1764,14 @@ void ConstraintSystem::partitionDisjunction(
1702
1764
return ;
1703
1765
}
1704
1766
1705
- SmallVector<unsigned , 4 > disabled;
1706
- SmallVector<unsigned , 4 > unavailable;
1707
- SmallVector<unsigned , 4 > globalScope;
1708
- SmallVector<SmallVector<unsigned , 4 >, 4 > definedInDesignatedType;
1709
- SmallVector<SmallVector<unsigned , 4 >, 4 > definedInExtensionOfDesignatedType;
1710
- SmallVector<unsigned , 4 > everythingElse;
1711
1767
SmallSet<Constraint *, 16 > taken;
1712
1768
1713
1769
// Local function used to iterate over the untaken choices from the
1714
1770
// disjunction and use a higher-order function to determine if they
1715
1771
// should be part of a partition.
1716
- auto forEachChoice =
1772
+ ConstraintMatchLoop forEachChoice =
1717
1773
[&](ArrayRef<Constraint *>,
1718
- llvm::function_ref <bool (unsigned index, Constraint *)> fn) {
1774
+ std::function <bool (unsigned index, Constraint *)> fn) {
1719
1775
for (auto index : indices (Choices)) {
1720
1776
auto *constraint = Choices[index];
1721
1777
if (taken.count (constraint))
@@ -1729,6 +1785,13 @@ void ConstraintSystem::partitionDisjunction(
1729
1785
}
1730
1786
};
1731
1787
1788
+ // First collect some things that we'll generally put near the end
1789
+ // of the partitioning.
1790
+
1791
+ SmallVector<unsigned , 4 > disabled;
1792
+ SmallVector<unsigned , 4 > unavailable;
1793
+ SmallVector<unsigned , 4 > globalScope;
1794
+
1732
1795
// First collect disabled constraints.
1733
1796
forEachChoice (Choices, [&](unsigned index, Constraint *constraint) -> bool {
1734
1797
if (!constraint->isDisabled ())
@@ -1765,77 +1828,27 @@ void ConstraintSystem::partitionDisjunction(
1765
1828
return true ;
1766
1829
});
1767
1830
1768
- // Now collect the overload choices that are defined within the type
1769
- // that was designated in the operator declaration.
1770
- auto designatedNominalTypes = getOperatorDesignatedNominalTypes (Choices[0 ]);
1771
- if (!designatedNominalTypes.empty ()) {
1772
- forEachChoice (
1773
- Choices, [&](unsigned constraintIndex, Constraint *constraint) -> bool {
1774
- auto *decl = constraint->getOverloadChoice ().getDecl ();
1775
- auto *funcDecl = cast<FuncDecl>(decl);
1776
-
1777
- auto *parentDecl = funcDecl->getParent ()->getAsDecl ();
1778
- for (auto designatedTypeIndex : indices (designatedNominalTypes)) {
1779
- auto *designatedNominal =
1780
- designatedNominalTypes[designatedTypeIndex];
1781
- if (parentDecl == designatedNominal) {
1782
- if (designatedTypeIndex >= definedInDesignatedType.size ())
1783
- definedInDesignatedType.resize (designatedTypeIndex + 1 );
1784
- auto &constraints = definedInDesignatedType[designatedTypeIndex];
1785
- constraints.push_back (constraintIndex);
1786
- return true ;
1787
- }
1788
-
1789
- if (auto *extensionDecl = dyn_cast<ExtensionDecl>(parentDecl)) {
1790
- parentDecl = extensionDecl->getExtendedNominal ();
1791
- if (parentDecl == designatedNominal) {
1792
- if (designatedTypeIndex >=
1793
- definedInExtensionOfDesignatedType.size ())
1794
- definedInExtensionOfDesignatedType.resize (
1795
- designatedTypeIndex + 1 );
1796
-
1797
- auto &constraints =
1798
- definedInExtensionOfDesignatedType[designatedTypeIndex];
1799
- constraints.push_back (constraintIndex);
1800
- return true ;
1801
- }
1802
- }
1803
- }
1831
+ // Local function to create the next partition based on the options
1832
+ // passed in.
1833
+ PartitionAppendCallback appendPartition =
1834
+ [&](SmallVectorImpl<unsigned > &options) {
1835
+ if (options.size ()) {
1836
+ PartitionBeginning.push_back (Ordering.size ());
1837
+ Ordering.insert (Ordering.end (), options.begin (), options.end ());
1838
+ }
1839
+ };
1804
1840
1805
- return false ;
1806
- });
1807
- }
1841
+ partitionForDesignatedTypes (Choices, forEachChoice, appendPartition);
1808
1842
1843
+ SmallVector<unsigned , 4 > everythingElse;
1809
1844
// Gather the remaining options.
1810
1845
forEachChoice (Choices, [&](unsigned index, Constraint *constraint) -> bool {
1811
1846
everythingElse.push_back (index);
1812
1847
return true ;
1813
1848
});
1814
-
1815
- // Local function to create the next partition based on the options
1816
- // passed in.
1817
- auto appendPartition = [&](SmallVectorImpl<unsigned > &options) {
1818
- if (options.size ()) {
1819
- PartitionBeginning.push_back (Ordering.size ());
1820
- Ordering.insert (Ordering.end (), options.begin (), options.end ());
1821
- }
1822
- };
1823
-
1824
- // Now create the partitioning based on what was collected.
1825
-
1826
- // First we'll add partitions for each of the overloads we found in
1827
- // types that were designated as part of the operator declaration.
1828
- for (auto designatedTypeIndex : indices (designatedNominalTypes)) {
1829
- if (designatedTypeIndex < definedInDesignatedType.size ()) {
1830
- auto &primary = definedInDesignatedType[designatedTypeIndex];
1831
- appendPartition (primary);
1832
- }
1833
- if (designatedTypeIndex < definedInExtensionOfDesignatedType.size ()) {
1834
- auto &secondary = definedInExtensionOfDesignatedType[designatedTypeIndex];
1835
- appendPartition (secondary);
1836
- }
1837
- }
1838
1849
appendPartition (everythingElse);
1850
+
1851
+ // Now create the remaining partitions from what we previously collected.
1839
1852
appendPartition (globalScope);
1840
1853
appendPartition (unavailable);
1841
1854
appendPartition (disabled);
0 commit comments