Skip to content

Commit d942e5d

Browse files
authored
Merge pull request #16566 from dcci/mangle42
[ASTMangler] Add support for mangling generic typealiases.
2 parents 046010d + 410940c commit d942e5d

File tree

7 files changed

+42
-6
lines changed

7 files changed

+42
-6
lines changed

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ NODE(BoundGenericClass)
3636
NODE(BoundGenericEnum)
3737
NODE(BoundGenericStructure)
3838
NODE(BoundGenericOtherNominalType)
39+
NODE(BoundGenericTypeAlias)
3940
NODE(BuiltinTypeName)
4041
NODE(CFunctionPointer)
4142
CONTEXT_NODE(Class)

lib/AST/ASTMangler.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,15 +739,31 @@ void ASTMangler::appendType(Type type) {
739739
auto aliasTy = cast<NameAliasType>(tybase);
740740

741741
// It's not possible to mangle the context of the builtin module.
742-
// FIXME: We also cannot yet mangle references to typealiases that
743-
// involve generics.
742+
// For the DWARF output we want to mangle the type alias + context,
743+
// unless the type alias references a builtin type.
744744
TypeAliasDecl *decl = aliasTy->getDecl();
745745
if (decl->getModuleContext() == decl->getASTContext().TheBuiltinModule) {
746746
return appendType(aliasTy->getSinglyDesugaredType());
747747
}
748748

749-
// For the DWARF output we want to mangle the type alias + context,
750-
// unless the type alias references a builtin type.
749+
if (type->isSpecialized()) {
750+
// Try to mangle the entire name as a substitution.
751+
if (tryMangleSubstitution(tybase))
752+
return;
753+
754+
appendAnyGenericType(decl);
755+
bool isFirstArgList = true;
756+
if (auto *nominalType = type->getAs<NominalType>()) {
757+
if (nominalType->getParent())
758+
type = nominalType->getParent();
759+
}
760+
appendBoundGenericArgs(type, isFirstArgList);
761+
appendRetroactiveConformances(type);
762+
appendOperator("G");
763+
addSubstitution(type.getPointer());
764+
return;
765+
}
766+
751767
return appendAnyGenericType(decl);
752768
}
753769

lib/Demangling/Demangler.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,9 @@ NodePointer Demangler::demangleBoundGenericArgs(NodePointer Nominal,
13241324
case Node::Kind::OtherNominalType:
13251325
kind = Node::Kind::BoundGenericOtherNominalType;
13261326
break;
1327+
case Node::Kind::TypeAlias:
1328+
kind = Node::Kind::BoundGenericTypeAlias;
1329+
break;
13271330
default:
13281331
return nullptr;
13291332
}

lib/Demangling/NodePrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ class NodePrinter {
267267
case Node::Kind::BoundGenericEnum:
268268
case Node::Kind::BoundGenericStructure:
269269
case Node::Kind::BoundGenericOtherNominalType:
270+
case Node::Kind::BoundGenericTypeAlias:
270271
case Node::Kind::BuiltinTypeName:
271272
case Node::Kind::Class:
272273
case Node::Kind::DependentGenericType:
@@ -1540,6 +1541,7 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
15401541
case Node::Kind::BoundGenericStructure:
15411542
case Node::Kind::BoundGenericEnum:
15421543
case Node::Kind::BoundGenericOtherNominalType:
1544+
case Node::Kind::BoundGenericTypeAlias:
15431545
printBoundGeneric(Node);
15441546
return nullptr;
15451547
case Node::Kind::DynamicSelf:

lib/Demangling/OldRemangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,11 @@ void Remangler::mangleBoundGenericOtherNominalType(Node *node) {
18721872
mangleAnyNominalType(node, ctx);
18731873
}
18741874

1875+
void Remangler::mangleBoundGenericTypeAlias(Node *node) {
1876+
EntityContext ctx;
1877+
mangleAnyNominalType(node, ctx);
1878+
}
1879+
18751880
void Remangler::mangleTypeList(Node *node) {
18761881
mangleChildNodes(node); // all types
18771882
Out << '_';

lib/Demangling/Remangler.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ void Remangler::mangleAnyNominalType(Node *node) {
459459
case Node::Kind::Enum: return mangleAnyGenericType(node, "O");
460460
case Node::Kind::Class: return mangleAnyGenericType(node, "C");
461461
case Node::Kind::OtherNominalType: return mangleAnyGenericType(node, "XY");
462+
case Node::Kind::TypeAlias: return mangleAnyGenericType(node, "a");
462463
default:
463464
unreachable("bad nominal type kind");
464465
}
@@ -477,7 +478,8 @@ void Remangler::mangleGenericArgs(Node *node, char &Separator) {
477478
case Node::Kind::BoundGenericOtherNominalType:
478479
case Node::Kind::BoundGenericStructure:
479480
case Node::Kind::BoundGenericEnum:
480-
case Node::Kind::BoundGenericClass: {
481+
case Node::Kind::BoundGenericClass:
482+
case Node::Kind::BoundGenericTypeAlias: {
481483
NodePointer unboundType = node->getChild(0);
482484
assert(unboundType->getKind() == Node::Kind::Type);
483485
NodePointer nominalType = unboundType->getChild(0);
@@ -589,6 +591,10 @@ void Remangler::mangleBoundGenericOtherNominalType(Node *node) {
589591
mangleAnyNominalType(node);
590592
}
591593

594+
void Remangler::mangleBoundGenericTypeAlias(Node *node) {
595+
mangleAnyNominalType(node);
596+
}
597+
592598
void Remangler::mangleBuiltinTypeName(Node *node) {
593599
Buffer << 'B';
594600
StringRef text = node->getText();
@@ -2027,6 +2033,7 @@ bool Demangle::isSpecialized(Node *node) {
20272033
case Node::Kind::BoundGenericEnum:
20282034
case Node::Kind::BoundGenericClass:
20292035
case Node::Kind::BoundGenericOtherNominalType:
2036+
case Node::Kind::BoundGenericTypeAlias:
20302037
return true;
20312038

20322039
case Node::Kind::Structure:
@@ -2062,7 +2069,8 @@ NodePointer Demangle::getUnspecialized(Node *node, NodeFactory &Factory) {
20622069
case Node::Kind::BoundGenericStructure:
20632070
case Node::Kind::BoundGenericEnum:
20642071
case Node::Kind::BoundGenericClass:
2065-
case Node::Kind::BoundGenericOtherNominalType: {
2072+
case Node::Kind::BoundGenericOtherNominalType:
2073+
case Node::Kind::BoundGenericTypeAlias: {
20662074
NodePointer unboundType = node->getChild(0);
20672075
assert(unboundType->getKind() == Node::Kind::Type);
20682076
NodePointer nominalType = unboundType->getChild(0);

test/Demangle/Inputs/manglings.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,5 @@ _$S3BBBBf0602365061_ ---> _$S3BBBBf0602365061_
315315
_$S3BBBBi0602365061_ ---> _$S3BBBBi0602365061_
316316
_$S3BBBBv0602365061_ ---> _$S3BBBBv0602365061_
317317
_T0lxxxmmmTk ---> _T0lxxxmmmTk
318+
$S4blah8PatatinoaySiGD -> blah.Patatino<Swift.Int>
318319

0 commit comments

Comments
 (0)