@@ -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) {
@@ -1737,11 +1783,12 @@ static bool isMethodDecl(const Decl *decl) {
1737
1783
}
1738
1784
1739
1785
CanType ASTMangler::getDeclTypeForMangling (
1740
- const ValueDecl *decl,
1741
- ArrayRef<GenericTypeParamType *> &genericParams,
1742
- unsigned &initialParamDepth,
1743
- ArrayRef<Requirement> &requirements,
1744
- SmallVectorImpl<Requirement> &requirementsBuf) {
1786
+ const ValueDecl *decl,
1787
+ GenericSignature *&genericSig,
1788
+ GenericSignature *&parentGenericSig) {
1789
+ genericSig = nullptr ;
1790
+ parentGenericSig = nullptr ;
1791
+
1745
1792
auto &C = decl->getASTContext ();
1746
1793
if (!decl->hasInterfaceType () || decl->getInterfaceType ()->is <ErrorType>()) {
1747
1794
if (isa<AbstractFunctionDecl>(decl))
@@ -1750,21 +1797,14 @@ CanType ASTMangler::getDeclTypeForMangling(
1750
1797
return C.TheErrorType ;
1751
1798
}
1752
1799
1753
- auto type = decl->getInterfaceType ()->getCanonicalType ();
1754
1800
1755
- initialParamDepth = 0 ;
1756
- CanGenericSignature sig;
1801
+ CanType type = decl->getInterfaceType ()->getCanonicalType ();
1757
1802
if (auto gft = dyn_cast<GenericFunctionType>(type)) {
1758
- sig = gft.getGenericSignature ();
1759
- CurGenericSignature = sig;
1760
- genericParams = sig->getGenericParams ();
1761
- requirements = sig->getRequirements ();
1803
+ genericSig = gft.getGenericSignature ();
1804
+ CurGenericSignature = gft.getGenericSignature ();
1762
1805
1763
1806
type = CanFunctionType::get (gft->getParams (), gft.getResult (),
1764
1807
gft->getExtInfo ());
1765
- } else {
1766
- genericParams = {};
1767
- requirements = {};
1768
1808
}
1769
1809
1770
1810
if (!type->hasError ()) {
@@ -1774,49 +1814,19 @@ CanType ASTMangler::getDeclTypeForMangling(
1774
1814
type = cast<AnyFunctionType>(type).getResult ();
1775
1815
}
1776
1816
1777
- if (isMethodDecl (decl) || isa<SubscriptDecl>(decl)) {
1778
- // Drop generic parameters and requirements from the method's context.
1779
- auto parentGenericSig =
1780
- decl->getDeclContext ()->getGenericSignatureOfContext ();
1781
- if (parentGenericSig && sig) {
1782
- // The method's depth starts above the depth of the context.
1783
- if (!parentGenericSig->getGenericParams ().empty ())
1784
- initialParamDepth =
1785
- parentGenericSig->getGenericParams ().back ()->getDepth () + 1 ;
1786
-
1787
- while (!genericParams.empty ()) {
1788
- if (genericParams.front ()->getDepth () >= initialParamDepth)
1789
- break ;
1790
- genericParams = genericParams.slice (1 );
1791
- }
1792
-
1793
- requirementsBuf.clear ();
1794
- for (auto &reqt : sig->getRequirements ()) {
1795
- // If the requirement is satisfied by the enclosing context,
1796
- // we don't need to mangle it here.
1797
- if (parentGenericSig->isRequirementSatisfied (reqt))
1798
- continue ;
1799
-
1800
- // Mangle the requirement.
1801
- requirementsBuf.push_back (reqt);
1802
- }
1803
- requirements = requirementsBuf;
1804
- }
1805
- }
1817
+ if (isMethodDecl (decl) || isa<SubscriptDecl>(decl))
1818
+ parentGenericSig = decl->getDeclContext ()->getGenericSignatureOfContext ();
1806
1819
}
1807
- return type->getCanonicalType ();
1820
+
1821
+ return type;
1808
1822
}
1809
1823
1810
1824
void ASTMangler::appendDeclType (const ValueDecl *decl, bool isFunctionMangling) {
1811
- ArrayRef<GenericTypeParamType *> genericParams;
1812
- unsigned initialParamDepth;
1813
- ArrayRef<Requirement> requirements;
1814
- SmallVector<Requirement, 4 > requirementsBuf;
1815
1825
Mod = decl->getModuleContext ();
1816
- auto type = getDeclTypeForMangling (decl,
1817
- genericParams, initialParamDepth,
1818
- requirements, requirementsBuf );
1819
-
1826
+ GenericSignature *genericSig = nullptr ;
1827
+ GenericSignature *parentGenericSig = nullptr ;
1828
+ auto type = getDeclTypeForMangling (decl, genericSig, parentGenericSig );
1829
+
1820
1830
if (AnyFunctionType *FuncTy = type->getAs <AnyFunctionType>()) {
1821
1831
1822
1832
const ParameterList *Params = nullptr ;
@@ -1841,9 +1851,7 @@ void ASTMangler::appendDeclType(const ValueDecl *decl, bool isFunctionMangling)
1841
1851
}
1842
1852
1843
1853
// Mangle the generic signature, if any.
1844
- if (!genericParams.empty () || !requirements.empty ()) {
1845
- appendGenericSignatureParts (genericParams, initialParamDepth,
1846
- requirements);
1854
+ if (genericSig && appendGenericSignature (genericSig, parentGenericSig)) {
1847
1855
// The 'F' function mangling doesn't need a 'u' for its generic signature.
1848
1856
if (!isFunctionMangling)
1849
1857
appendOperator (" u" );
0 commit comments