@@ -1537,11 +1537,57 @@ void ASTMangler::appendTypeList(Type listTy) {
1537
1537
}
1538
1538
}
1539
1539
1540
- void ASTMangler::appendGenericSignature (const GenericSignature *sig) {
1540
+ bool ASTMangler::appendGenericSignature (const GenericSignature *sig,
1541
+ GenericSignature *contextSig) {
1541
1542
auto canSig = sig->getCanonicalSignature ();
1542
1543
CurGenericSignature = canSig;
1543
- appendGenericSignatureParts (canSig->getGenericParams (), 0 ,
1544
- canSig->getRequirements ());
1544
+
1545
+ unsigned initialParamDepth;
1546
+ ArrayRef<GenericTypeParamType *> genericParams;
1547
+ ArrayRef<Requirement> requirements;
1548
+ SmallVector<Requirement, 4 > requirementsBuffer;
1549
+ if (contextSig) {
1550
+ // If the signature is the same as the context signature, there's nothing
1551
+ // to do.
1552
+ if (contextSig->getCanonicalSignature () == canSig) {
1553
+ return false ;
1554
+ }
1555
+
1556
+ // The signature depth starts above the depth of the context signature.
1557
+ if (!contextSig->getGenericParams ().empty ()) {
1558
+ initialParamDepth = contextSig->getGenericParams ().back ()->getDepth () + 1 ;
1559
+ }
1560
+
1561
+ // Find the parameters at this depth (or greater).
1562
+ genericParams = canSig->getGenericParams ();
1563
+ unsigned firstParam = genericParams.size ();
1564
+ while (firstParam > 1 &&
1565
+ genericParams[firstParam-1 ]->getDepth () >= initialParamDepth)
1566
+ --firstParam;
1567
+ genericParams = genericParams.slice (firstParam);
1568
+
1569
+ for (auto &reqt : canSig->getRequirements ()) {
1570
+ // If the requirement is satisfied by the context signature,
1571
+ // we don't need to mangle it here.
1572
+ if (contextSig->isRequirementSatisfied (reqt))
1573
+ continue ;
1574
+
1575
+ // Mangle the requirement.
1576
+ requirementsBuffer.push_back (reqt);
1577
+ }
1578
+ requirements = requirementsBuffer;
1579
+ } else {
1580
+ // Use the complete canonical signature.
1581
+ initialParamDepth = 0 ;
1582
+ genericParams = canSig->getGenericParams ();
1583
+ requirements = canSig->getRequirements ();
1584
+ }
1585
+
1586
+ if (genericParams.empty () && requirements.empty ())
1587
+ return false ;
1588
+
1589
+ appendGenericSignatureParts (genericParams, initialParamDepth, requirements);
1590
+ return true ;
1545
1591
}
1546
1592
1547
1593
void ASTMangler::appendRequirement (const Requirement &reqt) {
@@ -1736,23 +1782,13 @@ static bool isMethodDecl(const Decl *decl) {
1736
1782
&& decl->getDeclContext ()->isTypeContext ();
1737
1783
}
1738
1784
1739
- static bool genericParamIsBelowDepth (Type type, unsigned methodDepth) {
1740
- if (!type->hasTypeParameter ())
1741
- return true ;
1742
-
1743
- return !type.findIf ([methodDepth](Type t) -> bool {
1744
- if (auto *gp = t->getAs <GenericTypeParamType>())
1745
- return gp->getDepth () >= methodDepth;
1746
- return false ;
1747
- });
1748
- }
1749
-
1750
1785
CanType ASTMangler::getDeclTypeForMangling (
1751
- const ValueDecl *decl,
1752
- ArrayRef<GenericTypeParamType *> &genericParams,
1753
- unsigned &initialParamDepth,
1754
- ArrayRef<Requirement> &requirements,
1755
- SmallVectorImpl<Requirement> &requirementsBuf) {
1786
+ const ValueDecl *decl,
1787
+ GenericSignature *&genericSig,
1788
+ GenericSignature *&parentGenericSig) {
1789
+ genericSig = nullptr ;
1790
+ parentGenericSig = nullptr ;
1791
+
1756
1792
auto &C = decl->getASTContext ();
1757
1793
if (!decl->hasInterfaceType () || decl->getInterfaceType ()->is <ErrorType>()) {
1758
1794
if (isa<AbstractFunctionDecl>(decl))
@@ -1761,21 +1797,14 @@ CanType ASTMangler::getDeclTypeForMangling(
1761
1797
return C.TheErrorType ;
1762
1798
}
1763
1799
1764
- auto type = decl->getInterfaceType ()->getCanonicalType ();
1765
1800
1766
- initialParamDepth = 0 ;
1767
- CanGenericSignature sig;
1801
+ CanType type = decl->getInterfaceType ()->getCanonicalType ();
1768
1802
if (auto gft = dyn_cast<GenericFunctionType>(type)) {
1769
- sig = gft.getGenericSignature ();
1770
- CurGenericSignature = sig;
1771
- genericParams = sig->getGenericParams ();
1772
- requirements = sig->getRequirements ();
1803
+ genericSig = gft.getGenericSignature ();
1804
+ CurGenericSignature = gft.getGenericSignature ();
1773
1805
1774
1806
type = CanFunctionType::get (gft->getParams (), gft.getResult (),
1775
1807
gft->getExtInfo ());
1776
- } else {
1777
- genericParams = {};
1778
- requirements = {};
1779
1808
}
1780
1809
1781
1810
if (!type->hasError ()) {
@@ -1785,62 +1814,19 @@ CanType ASTMangler::getDeclTypeForMangling(
1785
1814
type = cast<AnyFunctionType>(type).getResult ();
1786
1815
}
1787
1816
1788
- if (isMethodDecl (decl) || isa<SubscriptDecl>(decl)) {
1789
- // Drop generic parameters and requirements from the method's context.
1790
- auto parentGenericSig =
1791
- decl->getDeclContext ()->getGenericSignatureOfContext ();
1792
- if (parentGenericSig && sig) {
1793
- // The method's depth starts above the depth of the context.
1794
- if (!parentGenericSig->getGenericParams ().empty ())
1795
- initialParamDepth =
1796
- parentGenericSig->getGenericParams ().back ()->getDepth () + 1 ;
1797
-
1798
- while (!genericParams.empty ()) {
1799
- if (genericParams.front ()->getDepth () >= initialParamDepth)
1800
- break ;
1801
- genericParams = genericParams.slice (1 );
1802
- }
1803
-
1804
- requirementsBuf.clear ();
1805
- for (auto &reqt : sig->getRequirements ()) {
1806
- switch (reqt.getKind ()) {
1807
- case RequirementKind::Conformance:
1808
- case RequirementKind::Layout:
1809
- case RequirementKind::Superclass:
1810
- // We don't need the requirement if the constrained type is below the
1811
- // method depth.
1812
- if (genericParamIsBelowDepth (reqt.getFirstType (), initialParamDepth))
1813
- continue ;
1814
- break ;
1815
- case RequirementKind::SameType:
1816
- // We don't need the requirement if both types are below the method
1817
- // depth, or non-dependent.
1818
- if (genericParamIsBelowDepth (reqt.getFirstType (), initialParamDepth) &&
1819
- genericParamIsBelowDepth (reqt.getSecondType (), initialParamDepth))
1820
- continue ;
1821
- break ;
1822
- }
1823
-
1824
- // If we fell through the switch, mangle the requirement.
1825
- requirementsBuf.push_back (reqt);
1826
- }
1827
- requirements = requirementsBuf;
1828
- }
1829
- }
1817
+ if (isMethodDecl (decl) || isa<SubscriptDecl>(decl))
1818
+ parentGenericSig = decl->getDeclContext ()->getGenericSignatureOfContext ();
1830
1819
}
1831
- return type->getCanonicalType ();
1820
+
1821
+ return type;
1832
1822
}
1833
1823
1834
1824
void ASTMangler::appendDeclType (const ValueDecl *decl, bool isFunctionMangling) {
1835
- ArrayRef<GenericTypeParamType *> genericParams;
1836
- unsigned initialParamDepth;
1837
- ArrayRef<Requirement> requirements;
1838
- SmallVector<Requirement, 4 > requirementsBuf;
1839
1825
Mod = decl->getModuleContext ();
1840
- auto type = getDeclTypeForMangling (decl,
1841
- genericParams, initialParamDepth,
1842
- requirements, requirementsBuf );
1843
-
1826
+ GenericSignature *genericSig = nullptr ;
1827
+ GenericSignature *parentGenericSig = nullptr ;
1828
+ auto type = getDeclTypeForMangling (decl, genericSig, parentGenericSig );
1829
+
1844
1830
if (AnyFunctionType *FuncTy = type->getAs <AnyFunctionType>()) {
1845
1831
1846
1832
const ParameterList *Params = nullptr ;
@@ -1865,9 +1851,7 @@ void ASTMangler::appendDeclType(const ValueDecl *decl, bool isFunctionMangling)
1865
1851
}
1866
1852
1867
1853
// Mangle the generic signature, if any.
1868
- if (!genericParams.empty () || !requirements.empty ()) {
1869
- appendGenericSignatureParts (genericParams, initialParamDepth,
1870
- requirements);
1854
+ if (genericSig && appendGenericSignature (genericSig, parentGenericSig)) {
1871
1855
// The 'F' function mangling doesn't need a 'u' for its generic signature.
1872
1856
if (!isFunctionMangling)
1873
1857
appendOperator (" u" );
0 commit comments