@@ -943,7 +943,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
943
943
InnermostOnly = 4 ,
944
944
SwapSelfAndDependentMemberType = 8 ,
945
945
PrintInherited = 16 ,
946
- CollapseDefaultReqs = 32 ,
946
+ PrintInverseRequirements = 32 ,
947
947
};
948
948
949
949
void printInheritedFromRequirementSignature (ProtocolDecl *proto,
@@ -953,11 +953,9 @@ class PrintAST : public ASTVisitor<PrintAST> {
953
953
void printInherited (const Decl *decl);
954
954
955
955
void printGenericSignature (GenericSignature genericSig,
956
- ArrayRef<InverseRequirement> inverses,
957
956
unsigned flags);
958
957
void
959
958
printGenericSignature (GenericSignature genericSig,
960
- ArrayRef<InverseRequirement> inverses,
961
959
unsigned flags,
962
960
llvm::function_ref<bool (const Requirement &)> filter);
963
961
void printSingleDepthOfGenericSignature (
@@ -1457,44 +1455,6 @@ static unsigned getDepthOfType(Type ty) {
1457
1455
return depth;
1458
1456
}
1459
1457
1460
- // / Recomputes which inverses must have been written for the given generic
1461
- // / signature.
1462
- static void reconstituteInverses (GenericSignature genericSig,
1463
- ArrayRef<Type> typeParams,
1464
- SmallVectorImpl<InverseRequirement> &inverses) {
1465
- auto &ctx = genericSig->getASTContext ();
1466
-
1467
- if (!ctx.LangOpts .hasFeature (swift::Feature::NoncopyableGenerics))
1468
- return ;
1469
-
1470
- for (auto tp : typeParams) {
1471
- assert (tp);
1472
-
1473
- // Any generic parameter with a superclass bound could not have an inverse.
1474
- if (genericSig->getSuperclassBound (tp))
1475
- continue ;
1476
-
1477
- auto defaults = InverseRequirement::expandDefault (tp);
1478
- for (auto ip : defaults) {
1479
- auto *proto = ctx.getProtocol (getKnownProtocolKind (ip));
1480
-
1481
- // If the generic signature reflects the default requirement,
1482
- // then there was no inverse for this generic parameter.
1483
- if (genericSig->requiresProtocol (tp, proto))
1484
- continue ;
1485
-
1486
- inverses.push_back ({tp, proto, SourceLoc ()});
1487
- }
1488
- }
1489
- }
1490
-
1491
- static void reconstituteInverses (GenericSignature genericSig,
1492
- ArrayRef<GenericTypeParamType *> genericParams,
1493
- SmallVectorImpl<InverseRequirement> &inverses) {
1494
- SmallVector<Type, 4 > asType (genericParams);
1495
- reconstituteInverses (genericSig, asType, inverses);
1496
- }
1497
-
1498
1458
namespace {
1499
1459
struct RequirementPrintLocation {
1500
1460
// / The Decl where the requirement should be attached (whether inherited or in
@@ -1615,13 +1575,9 @@ void PrintAST::printInheritedFromRequirementSignature(ProtocolDecl *proto,
1615
1575
else
1616
1576
llvm_unreachable (" nonexhaustive" );
1617
1577
1618
- SmallVector<InverseRequirement, 2 > inverses;
1619
- reconstituteInverses (proto->getGenericSignature (), attachedGP, inverses);
1620
-
1621
1578
printGenericSignature (
1622
1579
proto->getRequirementSignatureAsGenericSignature (),
1623
- inverses,
1624
- PrintInherited | CollapseDefaultReqs,
1580
+ PrintInherited,
1625
1581
[&](const Requirement &req) {
1626
1582
// Skip the inferred 'Self : AnyObject' constraint if this is an
1627
1583
// @objc protocol.
@@ -1640,12 +1596,11 @@ void PrintAST::printInheritedFromRequirementSignature(ProtocolDecl *proto,
1640
1596
1641
1597
void PrintAST::printWhereClauseFromRequirementSignature (ProtocolDecl *proto,
1642
1598
TypeDecl *attachingTo) {
1643
- unsigned flags = PrintRequirements | CollapseDefaultReqs ;
1599
+ unsigned flags = PrintRequirements;
1644
1600
if (isa<AssociatedTypeDecl>(attachingTo))
1645
1601
flags |= SwapSelfAndDependentMemberType;
1646
1602
printGenericSignature (
1647
1603
proto->getRequirementSignatureAsGenericSignature (),
1648
- {}, // NOTE: a protocol's inverses are only printed in inheritance clause!
1649
1604
flags,
1650
1605
[&](const Requirement &req) {
1651
1606
auto location = bestRequirementPrintLocation (proto, req);
@@ -1681,31 +1636,27 @@ static unsigned getDepthOfRequirement(const Requirement &req) {
1681
1636
llvm_unreachable (" bad RequirementKind" );
1682
1637
}
1683
1638
1684
- static void getRequirementsAtDepth (GenericSignature genericSig,
1685
- unsigned depth,
1686
- SmallVectorImpl<Requirement> &result) {
1687
- for (auto reqt : genericSig.getRequirements ()) {
1688
- unsigned currentDepth = getDepthOfRequirement (reqt);
1689
- assert (currentDepth != ErrorDepth);
1690
- if (currentDepth == depth)
1691
- result.push_back (reqt);
1692
- }
1693
- }
1694
-
1695
1639
void PrintAST::printGenericSignature (GenericSignature genericSig,
1696
- ArrayRef<InverseRequirement> inverses,
1697
1640
unsigned flags) {
1698
- printGenericSignature (genericSig, inverses, flags,
1641
+ printGenericSignature (genericSig, flags,
1699
1642
// print everything
1700
1643
[&](const Requirement &) { return true ; });
1701
1644
}
1702
1645
1703
1646
void PrintAST::printGenericSignature (
1704
1647
GenericSignature genericSig,
1705
- ArrayRef<InverseRequirement> inverses,
1706
1648
unsigned flags,
1707
1649
llvm::function_ref<bool (const Requirement &)> filter) {
1708
- auto requirements = genericSig.getRequirements ();
1650
+
1651
+ SmallVector<Requirement, 2 > requirements;
1652
+ SmallVector<InverseRequirement, 2 > inverses;
1653
+
1654
+ if (flags & PrintInverseRequirements) {
1655
+ genericSig->getRequirementsWithInverses (requirements, inverses);
1656
+ } else {
1657
+ requirements.append (genericSig.getRequirements ().begin (),
1658
+ genericSig.getRequirements ().end ());
1659
+ }
1709
1660
1710
1661
if (flags & InnermostOnly) {
1711
1662
auto genericParams = genericSig.getInnermostGenericParams ();
@@ -1741,34 +1692,27 @@ void PrintAST::printGenericSignature(
1741
1692
genericParams.slice (paramIdx, lastParamIdx - paramIdx);
1742
1693
1743
1694
SmallVector<InverseRequirement, 2 > inversesAtDepth;
1744
- if (flags & CollapseDefaultReqs)
1745
- reconstituteInverses (genericSig, genericParamsAtDepth, inversesAtDepth);
1695
+ for (auto inverseReq : inverses) {
1696
+ if (inverseReq.subject ->castTo <GenericTypeParamType>()->getDepth () == depth)
1697
+ inversesAtDepth.push_back (inverseReq);
1698
+ }
1746
1699
1747
1700
SmallVector<Requirement, 2 > requirementsAtDepth;
1748
- getRequirementsAtDepth (genericSig, depth, requirementsAtDepth);
1701
+ for (auto reqt : requirements) {
1702
+ unsigned currentDepth = getDepthOfRequirement (reqt);
1703
+ assert (currentDepth != ErrorDepth);
1704
+ if (currentDepth == depth)
1705
+ requirementsAtDepth.push_back (reqt);
1706
+ }
1749
1707
1750
1708
printSingleDepthOfGenericSignature (
1751
- genericParamsAtDepth,
1752
- requirementsAtDepth, inversesAtDepth, flags, filter);
1709
+ genericParamsAtDepth, requirementsAtDepth, inversesAtDepth,
1710
+ flags, filter);
1753
1711
1754
1712
paramIdx = lastParamIdx;
1755
1713
}
1756
1714
}
1757
1715
1758
- // / \returns the invertible protocol kind iff this requirement is a conformance
1759
- // / to an invertible protocol.
1760
- static llvm::Optional<InvertibleProtocolKind>
1761
- getInvertibleProtocolKind (const Requirement &req) {
1762
- if (req.getKind () != RequirementKind::Conformance)
1763
- return llvm::None;
1764
-
1765
- auto constraintTy = req.getSecondType ();
1766
- if (auto kp = constraintTy->getKnownProtocol ())
1767
- return getInvertibleProtocolKind (*kp);
1768
-
1769
- return llvm::None;
1770
- }
1771
-
1772
1716
void PrintAST::printSingleDepthOfGenericSignature (
1773
1717
ArrayRef<GenericTypeParamType *> genericParams,
1774
1718
ArrayRef<Requirement> requirements,
@@ -1792,7 +1736,6 @@ void PrintAST::printSingleDepthOfGenericSignature(
1792
1736
bool printInherited = (flags & PrintInherited);
1793
1737
bool swapSelfAndDependentMemberType =
1794
1738
(flags & SwapSelfAndDependentMemberType);
1795
- const bool collapseDefaults = (flags & CollapseDefaultReqs);
1796
1739
1797
1740
unsigned typeContextDepth = 0 ;
1798
1741
SubstitutionMap subMap;
@@ -1884,17 +1827,10 @@ void PrintAST::printSingleDepthOfGenericSignature(
1884
1827
}
1885
1828
1886
1829
if (printRequirements || printInherited) {
1887
- // If we're not collapsing defaults, we shouldn't also print inverses!
1888
- assert (collapseDefaults || inverses.empty ());
1889
-
1890
1830
for (const auto &req : requirements) {
1891
1831
if (!filter (req))
1892
1832
continue ;
1893
1833
1894
- // / Skip all requirements for a conformance to an invertible protocol.
1895
- if (collapseDefaults && getInvertibleProtocolKind (req))
1896
- continue ;
1897
-
1898
1834
auto first = req.getFirstType ();
1899
1835
1900
1836
if (dependsOnOpaque (first))
@@ -2678,7 +2614,7 @@ void PrintAST::printGenericDeclGenericParams(GenericContext *decl) {
2678
2614
if (decl->isGeneric ())
2679
2615
if (auto GenericSig = decl->getGenericSignature ()) {
2680
2616
Printer.printStructurePre (PrintStructureKind::DeclGenericParameterClause);
2681
- printGenericSignature (GenericSig, {}, PrintParams | InnermostOnly);
2617
+ printGenericSignature (GenericSig, PrintParams | InnermostOnly);
2682
2618
Printer.printStructurePost (PrintStructureKind::DeclGenericParameterClause);
2683
2619
}
2684
2620
}
@@ -2697,13 +2633,9 @@ void PrintAST::printDeclGenericRequirements(GenericContext *decl) {
2697
2633
SmallVector<Type, 2 > genericParams;
2698
2634
InverseRequirement::enumerateDefaultedParams (decl, genericParams);
2699
2635
2700
- SmallVector<InverseRequirement, 2 > inverses;
2701
- reconstituteInverses (genericSig, genericParams, inverses);
2702
-
2703
2636
Printer.printStructurePre (PrintStructureKind::DeclGenericParameterClause);
2704
2637
printGenericSignature (genericSig,
2705
- inverses,
2706
- PrintRequirements | CollapseDefaultReqs,
2638
+ PrintRequirements | PrintInverseRequirements,
2707
2639
[parentSig](const Requirement &req) {
2708
2640
if (parentSig)
2709
2641
return !parentSig->isRequirementSatisfied (req);
@@ -2961,12 +2893,8 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
2961
2893
auto baseGenericSig = decl->getExtendedNominal ()->getGenericSignature ();
2962
2894
assert (baseGenericSig &&
2963
2895
" an extension can't be generic if the base type isn't" );
2964
- // NOTE: We do _not_ CollapseDefaultReqs for the where-clause of an
2965
- // extension, because conditions involving Copyable here are not
2966
- // automatically inferred based on the extension itself.
2967
2896
printGenericSignature (genericSig,
2968
- {},
2969
- PrintRequirements,
2897
+ PrintRequirements | PrintInverseRequirements,
2970
2898
[baseGenericSig](const Requirement &req) -> bool {
2971
2899
// Only include constraints that are not satisfied by the base type.
2972
2900
return !baseGenericSig->isRequirementSatisfied (req);
@@ -7216,12 +7144,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7216
7144
Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
7217
7145
}
7218
7146
7219
- void printGenericSignature (GenericSignature genericSig,
7220
- ArrayRef<InverseRequirement> inverses,
7221
- unsigned flags) {
7222
- PrintAST (Printer, Options).printGenericSignature (genericSig,
7223
- inverses,
7224
- flags);
7147
+ void printGenericSignature (GenericSignature genericSig, unsigned flags) {
7148
+ PrintAST (Printer, Options).printGenericSignature (genericSig, flags);
7225
7149
}
7226
7150
7227
7151
void printSubstitutions (SubstitutionMap subs) {
@@ -7241,17 +7165,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7241
7165
Printer.printStructurePost (PrintStructureKind::FunctionType);
7242
7166
};
7243
7167
7244
- SmallVector<InverseRequirement, 2 > inverses;
7245
- reconstituteInverses (T->getGenericSignature (),
7246
- T->getGenericParams (),
7247
- inverses);
7248
-
7249
7168
printFunctionExtInfo (T);
7250
7169
printGenericSignature (T->getGenericSignature (),
7251
- inverses,
7252
7170
PrintAST::PrintParams |
7253
7171
PrintAST::PrintRequirements |
7254
- PrintAST::CollapseDefaultReqs );
7172
+ PrintAST::PrintInverseRequirements );
7255
7173
Printer << " " ;
7256
7174
7257
7175
visitAnyFunctionTypeParams (T->getParams (), /* printLabels*/ true );
@@ -7327,11 +7245,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7327
7245
printFunctionExtInfo (T);
7328
7246
printCalleeConvention (T->getCalleeConvention ());
7329
7247
7330
- if (auto sig = T->getInvocationGenericSignature ()) {
7248
+ if (GenericSignature sig = T->getInvocationGenericSignature ()) {
7331
7249
printGenericSignature (sig,
7332
- /* inverses=*/ {},
7333
7250
PrintAST::PrintParams |
7334
- PrintAST::PrintRequirements);
7251
+ PrintAST::PrintRequirements |
7252
+ PrintAST::PrintInverseRequirements);
7335
7253
Printer << " " ;
7336
7254
}
7337
7255
@@ -7351,11 +7269,13 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7351
7269
subBuffer.emplace (Printer, subOptions);
7352
7270
sub = &*subBuffer;
7353
7271
7272
+ GenericSignature sig = substitutions.getGenericSignature ();
7273
+
7354
7274
sub->Printer << " @substituted " ;
7355
- sub->printGenericSignature (substitutions.getGenericSignature (),
7356
- /* inverses=*/ {},
7275
+ sub->printGenericSignature (sig,
7357
7276
PrintAST::PrintParams |
7358
- PrintAST::PrintRequirements);
7277
+ PrintAST::PrintRequirements |
7278
+ PrintAST::PrintInverseRequirements);
7359
7279
sub->Printer << " " ;
7360
7280
}
7361
7281
@@ -7465,8 +7385,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7465
7385
// printer, but only the sub-Printer.
7466
7386
[&sub, T]{
7467
7387
if (auto sig = T->getLayout ()->getGenericSignature ()) {
7468
- sub.printGenericSignature (sig, /* inverses=*/ {},
7469
- PrintAST::PrintParams | PrintAST::PrintRequirements);
7388
+ sub.printGenericSignature (sig,
7389
+ PrintAST::PrintParams |
7390
+ PrintAST::PrintRequirements |
7391
+ PrintAST::PrintInverseRequirements);
7470
7392
sub.Printer << " " ;
7471
7393
}
7472
7394
sub.Printer << " {" ;
@@ -8020,10 +7942,11 @@ void GenericSignature::print(ASTPrinter &Printer,
8020
7942
Printer << " <null>" ;
8021
7943
return ;
8022
7944
}
8023
- PrintAST (Printer, Opts).printGenericSignature (*this ,
8024
- /* inverses=*/ {},
8025
- PrintAST::PrintParams |
8026
- PrintAST::PrintRequirements);
7945
+
7946
+ auto flags = PrintAST::PrintParams | PrintAST::PrintRequirements;
7947
+ if (Opts.PrintInverseRequirements )
7948
+ flags |= PrintAST::PrintInverseRequirements;
7949
+ PrintAST (Printer, Opts).printGenericSignature (*this , flags);
8027
7950
}
8028
7951
8029
7952
void Requirement::print (raw_ostream &os, const PrintOptions &opts) const {
@@ -8221,9 +8144,10 @@ void ProtocolConformance::printName(llvm::raw_ostream &os,
8221
8144
StreamPrinter sPrinter (os);
8222
8145
TypePrinter typePrinter (sPrinter , PO);
8223
8146
typePrinter
8224
- .printGenericSignature (genericSig, /* inverses= */ {},
8147
+ .printGenericSignature (genericSig,
8225
8148
PrintAST::PrintParams |
8226
- PrintAST::PrintRequirements);
8149
+ PrintAST::PrintRequirements |
8150
+ PrintAST::PrintInverseRequirements);
8227
8151
os << ' ' ;
8228
8152
}
8229
8153
}
0 commit comments