Skip to content

Commit 987552d

Browse files
committed
Mangler: Add support for same-shape requirements
1 parent 532d097 commit 987552d

File tree

7 files changed

+53
-7
lines changed

7 files changed

+53
-7
lines changed

docs/ABI/Mangling.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,8 @@ now codified into the ABI; the index 0 is therefore reserved.
887887
requirement ::= type assoc-type-list 'RM' GENERIC-PARAM-INDEX LAYOUT-CONSTRAINT // layout requirement on associated type at depth
888888
requirement ::= type substitution 'RM' LAYOUT-CONSTRAINT // layout requirement with substitution
889889

890+
requirement ::= type 'Rh' GENERIC-PARAM-INDEX // same-shape requirement (only supported on a generic parameter)
891+
890892
GENERIC-PARAM-INDEX ::= 'z' // depth = 0, idx = 0
891893
GENERIC-PARAM-INDEX ::= INDEX // depth = 0, idx = N+1
892894
GENERIC-PARAM-INDEX ::= 'd' INDEX INDEX // depth = M+1, idx = N

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ NODE(DependentGenericConformanceRequirement)
5959
NODE(DependentGenericParamCount)
6060
NODE(DependentGenericParamType)
6161
NODE(DependentGenericSameTypeRequirement)
62+
NODE(DependentGenericSameShapeRequirement)
6263
NODE(DependentGenericLayoutRequirement)
6364
NODE(DependentGenericSignature)
6465
NODE(DependentGenericType)

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,8 +2912,6 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
29122912
Type FirstTy = reqt.getFirstType()->getCanonicalType();
29132913

29142914
switch (reqt.getKind()) {
2915-
case RequirementKind::SameShape:
2916-
llvm_unreachable("Same-shape requirement not supported here");
29172915
case RequirementKind::Layout:
29182916
break;
29192917
case RequirementKind::Conformance: {
@@ -2925,7 +2923,8 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
29252923
appendProtocolName(reqt.getProtocolDecl());
29262924
} break;
29272925
case RequirementKind::Superclass:
2928-
case RequirementKind::SameType: {
2926+
case RequirementKind::SameType:
2927+
case RequirementKind::SameShape: {
29292928
Type SecondTy = reqt.getSecondType();
29302929
appendType(SecondTy->getCanonicalType(), sig);
29312930
break;
@@ -2957,7 +2956,7 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
29572956
assert(gpBase);
29582957
switch (reqt.getKind()) {
29592958
case RequirementKind::SameShape:
2960-
llvm_unreachable("Same-shape requirement not supported here");
2959+
llvm_unreachable("Same-shape requirement with a dependent member type?");
29612960
case RequirementKind::Conformance:
29622961
return appendOpWithGenericParamIndex(isAssocTypeAtDepth ? "RP" : "Rp",
29632962
gpBase, lhsBaseIsProtocolSelf);
@@ -2977,8 +2976,6 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
29772976
}
29782977
GenericTypeParamType *gpBase = FirstTy->castTo<GenericTypeParamType>();
29792978
switch (reqt.getKind()) {
2980-
case RequirementKind::SameShape:
2981-
llvm_unreachable("Same-shape requirement not supported here");
29822979
case RequirementKind::Conformance:
29832980
return appendOpWithGenericParamIndex("R", gpBase);
29842981
case RequirementKind::Layout:
@@ -2989,6 +2986,8 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
29892986
return appendOpWithGenericParamIndex("Rb", gpBase);
29902987
case RequirementKind::SameType:
29912988
return appendOpWithGenericParamIndex("Rs", gpBase);
2989+
case RequirementKind::SameShape:
2990+
return appendOpWithGenericParamIndex("Rh", gpBase);
29922991
}
29932992
llvm_unreachable("bad requirement type");
29942993
}

lib/Demangling/Demangler.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static bool isEntity(Node::Kind kind) {
7777
static bool isRequirement(Node::Kind kind) {
7878
switch (kind) {
7979
case Node::Kind::DependentGenericSameTypeRequirement:
80+
case Node::Kind::DependentGenericSameShapeRequirement:
8081
case Node::Kind::DependentGenericLayoutRequirement:
8182
case Node::Kind::DependentGenericConformanceRequirement:
8283
return true;
@@ -3691,7 +3692,7 @@ NodePointer Demangler::demangleGenericSignature(bool hasParamCounts) {
36913692
NodePointer Demangler::demangleGenericRequirement() {
36923693

36933694
enum { Generic, Assoc, CompoundAssoc, Substitution } TypeKind;
3694-
enum { Protocol, BaseClass, SameType, Layout } ConstraintKind;
3695+
enum { Protocol, BaseClass, SameType, SameShape, Layout } ConstraintKind;
36953696

36963697
switch (nextChar()) {
36973698
case 'c': ConstraintKind = BaseClass; TypeKind = Assoc; break;
@@ -3709,6 +3710,7 @@ NodePointer Demangler::demangleGenericRequirement() {
37093710
case 'p': ConstraintKind = Protocol; TypeKind = Assoc; break;
37103711
case 'P': ConstraintKind = Protocol; TypeKind = CompoundAssoc; break;
37113712
case 'Q': ConstraintKind = Protocol; TypeKind = Substitution; break;
3713+
case 'h': ConstraintKind = SameShape; TypeKind = Generic; break;
37123714
default: ConstraintKind = Protocol; TypeKind = Generic; pushBack(); break;
37133715
}
37143716

@@ -3743,6 +3745,9 @@ NodePointer Demangler::demangleGenericRequirement() {
37433745
case SameType:
37443746
return createWithChildren(Node::Kind::DependentGenericSameTypeRequirement,
37453747
ConstrTy, popNode(Node::Kind::Type));
3748+
case SameShape:
3749+
return createWithChildren(Node::Kind::DependentGenericSameShapeRequirement,
3750+
ConstrTy, popNode(Node::Kind::Type));
37463751
case Layout: {
37473752
auto c = nextChar();
37483753
NodePointer size = nullptr;

lib/Demangling/NodePrinter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ class NodePrinter {
368368
case Node::Kind::DependentGenericConformanceRequirement:
369369
case Node::Kind::DependentGenericLayoutRequirement:
370370
case Node::Kind::DependentGenericSameTypeRequirement:
371+
case Node::Kind::DependentGenericSameShapeRequirement:
371372
case Node::Kind::DependentPseudogenericSignature:
372373
case Node::Kind::Destructor:
373374
case Node::Kind::DidSet:
@@ -2674,6 +2675,16 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
26742675
print(snd, depth + 1);
26752676
return nullptr;
26762677
}
2678+
case Node::Kind::DependentGenericSameShapeRequirement: {
2679+
NodePointer fst = Node->getChild(0);
2680+
NodePointer snd = Node->getChild(1);
2681+
2682+
print(fst, depth + 1);
2683+
Printer << ".shape == ";
2684+
print(snd, depth + 1);
2685+
Printer << ".shape";
2686+
return nullptr;
2687+
}
26772688
case Node::Kind::DependentGenericParamType: {
26782689
unsigned index = Node->getChild(1)->getIndex();
26792690
unsigned depth = Node->getChild(0)->getIndex();

lib/Demangling/OldRemangler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,12 @@ Remangler::mangleDependentGenericSameTypeRequirement(Node *node,
19461946
return mangle(node->getChild(1), depth + 1);
19471947
}
19481948

1949+
ManglingError
1950+
Remangler::mangleDependentGenericSameShapeRequirement(Node *node,
1951+
unsigned depth) {
1952+
return MANGLING_ERROR(ManglingError::UnsupportedNodeKind, node);
1953+
}
1954+
19491955
ManglingError
19501956
Remangler::mangleDependentGenericLayoutRequirement(Node *node, unsigned depth) {
19511957
RETURN_IF_ERROR(mangleConstrainedType(node->getChild(0), depth + 1));

lib/Demangling/Remangler.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,28 @@ Remangler::mangleDependentGenericSameTypeRequirement(Node *node,
10761076
return ManglingError::Success;
10771077
}
10781078

1079+
ManglingError
1080+
Remangler::mangleDependentGenericSameShapeRequirement(Node *node,
1081+
unsigned depth) {
1082+
RETURN_IF_ERROR(mangleChildNode(node, 1, depth + 1));
1083+
auto Mangling = mangleConstrainedType(node->getChild(0), depth + 1);
1084+
if (!Mangling.isSuccess())
1085+
return Mangling.error();
1086+
auto NumMembersAndParamIdx = Mangling.result();
1087+
DEMANGLER_ASSERT(
1088+
NumMembersAndParamIdx.first < 0 || NumMembersAndParamIdx.second, node);
1089+
switch (NumMembersAndParamIdx.first) {
1090+
case 0:
1091+
Buffer << "Rh";
1092+
break;
1093+
default:
1094+
assert(false && "Invalid same-shape requirement");
1095+
return ManglingError::AssertionFailed;
1096+
}
1097+
mangleDependentGenericParamIndex(NumMembersAndParamIdx.second);
1098+
return ManglingError::Success;
1099+
}
1100+
10791101
ManglingError
10801102
Remangler::mangleDependentGenericLayoutRequirement(Node *node, unsigned depth) {
10811103
auto Mangling = mangleConstrainedType(node->getChild(0), depth + 1);

0 commit comments

Comments
 (0)