Skip to content

Commit 8a45d5a

Browse files
committed
Mangler: Support substituted SIL function types in thunk manglings
1 parent 5dab3cb commit 8a45d5a

File tree

8 files changed

+114
-20
lines changed

8 files changed

+114
-20
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ class ASTMangler : public Mangler {
245245
unsigned appendBoundGenericArgs(DeclContext *dc,
246246
SubstitutionMap subs,
247247
bool &isFirstArgList);
248+
249+
/// Append the bound generic arguments as a flat list, disregarding depth.
250+
void appendFlatGenericArgs(SubstitutionMap subs);
248251

249252
/// Append any retroactive conformances.
250253
void appendRetroactiveConformances(Type type);

include/swift/Demangling/DemangleNodes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ NODE(ImplEscaping)
113113
NODE(ImplConvention)
114114
NODE(ImplFunctionAttribute)
115115
NODE(ImplFunctionType)
116+
NODE(ImplImpliedSubstitutions)
117+
NODE(ImplSubstitutions)
116118
CONTEXT_NODE(ImplicitClosure)
117119
NODE(ImplParameter)
118120
NODE(ImplResult)

lib/AST/ASTMangler.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,16 @@ void ASTMangler::bindGenericParameters(const DeclContext *DC) {
12011201
bindGenericParameters(sig->getCanonicalSignature());
12021202
}
12031203

1204+
void ASTMangler::appendFlatGenericArgs(SubstitutionMap subs) {
1205+
appendOperator("y");
1206+
1207+
for (auto replacement : subs.getReplacementTypes()) {
1208+
if (replacement->hasArchetype())
1209+
replacement = replacement->mapTypeOutOfContext();
1210+
appendType(replacement);
1211+
}
1212+
}
1213+
12041214
unsigned ASTMangler::appendBoundGenericArgs(DeclContext *dc,
12051215
SubstitutionMap subs,
12061216
bool &isFirstArgList) {
@@ -1429,6 +1439,13 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn) {
14291439

14301440
llvm::SmallVector<char, 32> OpArgs;
14311441

1442+
if (fn->getSubstitutions()) {
1443+
OpArgs.push_back('s');
1444+
if (!fn->isGenericSignatureImplied()) {
1445+
OpArgs.push_back('i');
1446+
}
1447+
}
1448+
14321449
if (fn->isPolymorphic() && fn->isPseudogeneric())
14331450
OpArgs.push_back('P');
14341451

@@ -1465,7 +1482,7 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn) {
14651482
OpArgs.push_back('W');
14661483
break;
14671484
}
1468-
1485+
14691486
// Mangle the parameters.
14701487
for (auto param : fn->getParameters()) {
14711488
OpArgs.push_back(getParamConvention(param.getConvention()));
@@ -1485,8 +1502,13 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn) {
14851502
OpArgs.push_back(getResultConvention(error.getConvention()));
14861503
appendType(error.getInterfaceType());
14871504
}
1488-
if (fn->isPolymorphic())
1489-
appendGenericSignature(fn->getSubstGenericSignature());
1505+
if (auto sig = fn->getSubstGenericSignature()) {
1506+
appendGenericSignature(sig);
1507+
}
1508+
if (auto subs = fn->getSubstitutions()) {
1509+
appendFlatGenericArgs(subs);
1510+
appendRetroactiveConformances(subs, Mod);
1511+
}
14901512

14911513
OpArgs.push_back('_');
14921514

lib/Demangling/Demangler.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,24 @@ NodePointer Demangler::demangleImplResultConvention(Node::Kind ConvKind) {
17331733
NodePointer Demangler::demangleImplFunctionType() {
17341734
NodePointer type = createNode(Node::Kind::ImplFunctionType);
17351735

1736+
if (nextIf('s')) {
1737+
Vector<NodePointer> Substitutions;
1738+
NodePointer SubstitutionRetroConformances;
1739+
if (!demangleBoundGenerics(Substitutions, SubstitutionRetroConformances))
1740+
return nullptr;
1741+
1742+
bool isImplied = !nextIf('i');
1743+
1744+
auto subsNode = createNode(isImplied
1745+
? Node::Kind::ImplImpliedSubstitutions
1746+
: Node::Kind::ImplSubstitutions);
1747+
assert(Substitutions.size() == 1);
1748+
subsNode->addChild(Substitutions[0], *this);
1749+
if (SubstitutionRetroConformances)
1750+
subsNode->addChild(SubstitutionRetroConformances, *this);
1751+
type->addChild(subsNode, *this);
1752+
}
1753+
17361754
NodePointer GenSig = popNode(Node::Kind::DependentGenericSignature);
17371755
if (GenSig && nextIf('P'))
17381756
GenSig = changeKind(GenSig, Node::Kind::DependentPseudogenericSignature);
@@ -1794,6 +1812,7 @@ NodePointer Demangler::demangleImplFunctionType() {
17941812
return nullptr;
17951813
type->getChild(type->getNumChildren() - Idx - 1)->addChild(ConvTy, *this);
17961814
}
1815+
17971816
return createType(type);
17981817
}
17991818

lib/Demangling/NodePrinter.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ class NodePrinter {
390390
case Node::Kind::ImplConvention:
391391
case Node::Kind::ImplFunctionAttribute:
392392
case Node::Kind::ImplFunctionType:
393+
case Node::Kind::ImplImpliedSubstitutions:
394+
case Node::Kind::ImplSubstitutions:
393395
case Node::Kind::ImplicitClosure:
394396
case Node::Kind::ImplParameter:
395397
case Node::Kind::ImplResult:
@@ -979,7 +981,7 @@ static bool needSpaceBeforeType(NodePointer Type) {
979981
}
980982

981983
NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
982-
switch (Node->getKind()) {
984+
switch (auto kind = Node->getKind()) {
983985
case Node::Kind::Static:
984986
Printer << "static ";
985987
print(Node->getChild(0));
@@ -2016,6 +2018,14 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
20162018
case Node::Kind::ImplFunctionType:
20172019
printImplFunctionType(Node);
20182020
return nullptr;
2021+
case Node::Kind::ImplSubstitutions:
2022+
case Node::Kind::ImplImpliedSubstitutions:
2023+
Printer << "for <";
2024+
printChildren(Node->getChild(0), ", ");
2025+
Printer << '>';
2026+
if (kind == Node::Kind::ImplImpliedSubstitutions)
2027+
Printer << " in";
2028+
return nullptr;
20192029
case Node::Kind::ErrorType:
20202030
Printer << "<ERROR TYPE>";
20212031
return nullptr;

lib/Demangling/OldRemangler.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,14 @@ void Remangler::mangleImplEscaping(Node *node) {
12521252
// The old mangler does not encode escaping.
12531253
}
12541254

1255+
void Remangler::mangleImplSubstitutions(Node *node) {
1256+
// The old mangler does not encode substituted function types.
1257+
}
1258+
1259+
void Remangler::mangleImplImpliedSubstitutions(Node *node) {
1260+
// The old mangler does not encode substituted function types.
1261+
}
1262+
12551263
void Remangler::mangleImplConvention(Node *node) {
12561264
assert(node->getKind() == Node::Kind::ImplConvention);
12571265
StringRef text = node->getText();

lib/Demangling/Remangler.cpp

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,30 +1396,61 @@ void Remangler::mangleImplFunctionAttribute(Node *node) {
13961396
unreachable("handled inline");
13971397
}
13981398

1399+
void Remangler::mangleImplSubstitutions(Node *node) {
1400+
unreachable("handled inline");
1401+
}
1402+
1403+
void Remangler::mangleImplImpliedSubstitutions(Node *node) {
1404+
unreachable("handled inline");
1405+
}
1406+
13991407
void Remangler::mangleImplFunctionType(Node *node) {
14001408
const char *PseudoGeneric = "";
14011409
Node *GenSig = nullptr;
1410+
Node *GenSubs = nullptr;
1411+
bool isImplied;
14021412
for (NodePointer Child : *node) {
1403-
switch (Child->getKind()) {
1404-
case Node::Kind::ImplParameter:
1405-
case Node::Kind::ImplResult:
1406-
case Node::Kind::ImplErrorResult:
1407-
mangleChildNode(Child, 1);
1408-
break;
1409-
case Node::Kind::DependentPseudogenericSignature:
1410-
PseudoGeneric = "P";
1411-
LLVM_FALLTHROUGH;
1412-
case Node::Kind::DependentGenericSignature:
1413-
GenSig = Child;
1414-
break;
1415-
default:
1416-
break;
1413+
switch (auto kind = Child->getKind()) {
1414+
case Node::Kind::ImplParameter:
1415+
case Node::Kind::ImplResult:
1416+
case Node::Kind::ImplErrorResult:
1417+
mangleChildNode(Child, 1);
1418+
break;
1419+
case Node::Kind::DependentPseudogenericSignature:
1420+
PseudoGeneric = "P";
1421+
LLVM_FALLTHROUGH;
1422+
case Node::Kind::DependentGenericSignature:
1423+
GenSig = Child;
1424+
break;
1425+
case Node::Kind::ImplSubstitutions:
1426+
case Node::Kind::ImplImpliedSubstitutions:
1427+
GenSubs = Child;
1428+
isImplied = kind == Node::Kind::ImplImpliedSubstitutions;
1429+
break;
1430+
default:
1431+
break;
14171432
}
14181433
}
14191434
if (GenSig)
14201435
mangle(GenSig);
1436+
if (GenSubs) {
1437+
GenSubs->dump();
1438+
1439+
Buffer << 'y';
1440+
mangleChildNodes(GenSubs->getChild(0));
1441+
if (GenSubs->getNumChildren() >= 2)
1442+
mangleRetroactiveConformance(GenSubs->getChild(1));
1443+
}
1444+
1445+
Buffer << 'I';
1446+
1447+
if (GenSubs) {
1448+
Buffer << 's';
1449+
if (!isImplied)
1450+
Buffer << 'i';
1451+
}
14211452

1422-
Buffer << 'I' << PseudoGeneric;
1453+
Buffer << PseudoGeneric;
14231454
for (NodePointer Child : *node) {
14241455
switch (Child->getKind()) {
14251456
case Node::Kind::ImplEscaping:

lib/SIL/SILType.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ ValueOwnershipKind
421421
SILResultInfo::getOwnershipKind(SILFunction &F) const {
422422
auto &M = F.getModule();
423423
auto FTy = F.getLoweredFunctionType();
424-
auto sig = FTy->getInvocationGenericSignature();
425424

426425
bool IsTrivial = getSILStorageType(M, FTy).isTrivial(F);
427426
switch (getConvention()) {

0 commit comments

Comments
 (0)