Skip to content

Commit fbf082b

Browse files
committed
[Demangling] Make Demangle::getUnspecialized() return errors too.
This lets us completely remove the unreachable() function from Remangler.cpp. rdar://79725187
1 parent 145f281 commit fbf082b

File tree

7 files changed

+48
-20
lines changed

7 files changed

+48
-20
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ bool nodeConsumesGenericArgs(Node *node);
691691

692692
bool isSpecialized(Node *node);
693693

694-
NodePointer getUnspecialized(Node *node, NodeFactory &Factory);
694+
ManglingErrorOr<NodePointer> getUnspecialized(Node *node, NodeFactory &Factory);
695695

696696
/// Returns true if the node \p kind refers to a context node, e.g. a nominal
697697
/// type or a function.

include/swift/Demangling/TypeDecoder.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,10 @@ return {}; // Not Implemented!
13781378
parent = decodeMangledType(parentContext, depth + 1).getType();
13791379
// Remove any generic arguments from the context node, producing a
13801380
// node that references the nominal type declaration.
1381-
declNode = Demangle::getUnspecialized(node, Builder.getNodeFactory());
1381+
auto unspec = Demangle::getUnspecialized(node, Builder.getNodeFactory());
1382+
if (!unspec.isSuccess())
1383+
return TypeLookupError("Failed to unspecialize type");
1384+
declNode = unspec.result();
13821385
break;
13831386
}
13841387
}

lib/Demangling/Context.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ std::string Context::getModuleName(llvm::StringRef mangledName) {
216216
}
217217
default:
218218
if (isSpecialized(node)) {
219-
node = getUnspecialized(node, *D);
219+
auto unspec = getUnspecialized(node, *D);
220+
if (!unspec.isSuccess())
221+
node = nullptr;
222+
else
223+
node = unspec.result();
220224
break;
221225
}
222226
if (isContext(node->getKind())) {

lib/Demangling/OldRemangler.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,10 @@ void Remangler::mangleAnyNominalType(Node *node, EntityContext &ctx,
19371937
if (isSpecialized(node)) {
19381938
Buffer << 'G';
19391939

1940-
NodePointer unboundType = getUnspecialized(node, Factory);
1940+
auto unspec = getUnspecialized(node, Factory);
1941+
if (!unspec.isSuccess())
1942+
unreachable("unable to unspecialize");
1943+
NodePointer unboundType = unspec.result();
19411944

19421945
mangleAnyNominalType(unboundType, ctx, depth + 1);
19431946
mangleGenericArgs(node, ctx, depth + 1);

lib/Demangling/Remangler.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ using namespace Mangle;
3939
return err; \
4040
} while (0)
4141

42-
[[noreturn]]
43-
static void unreachable(const char *Message) {
44-
fprintf(stderr, "fatal error: %s\n", Message);
45-
std::abort();
46-
}
47-
4842
static char getCharOfNodeText(Node *node, unsigned idx) {
4943
switch (node->getKind()) {
5044
case Node::Kind::InfixOperator:
@@ -500,7 +494,10 @@ ManglingError Remangler::mangleAnyNominalType(Node *node, unsigned depth) {
500494
if (trySubstitution(node, entry))
501495
return ManglingError::Success;
502496

503-
NodePointer unboundType = getUnspecialized(node, Factory);
497+
auto unspec = getUnspecialized(node, Factory);
498+
if (!unspec.isSuccess())
499+
return unspec.error();
500+
NodePointer unboundType = unspec.result();
504501
RETURN_IF_ERROR(mangleAnyNominalType(unboundType, depth + 1));
505502
char Separator = 'y';
506503
RETURN_IF_ERROR(mangleGenericArgs(node, Separator, depth + 1));
@@ -642,7 +639,8 @@ ManglingError Remangler::mangleAbstractStorage(Node *node,
642639
switch (node->getKind()) {
643640
case Node::Kind::Subscript: Buffer << "i"; break;
644641
case Node::Kind::Variable: Buffer << "v"; break;
645-
default: unreachable("Not a storage node");
642+
default:
643+
return ManglingError(ManglingError::NotAStorageNode, node);
646644
}
647645
Buffer << accessorCode;
648646
return ManglingError::Success;
@@ -815,7 +813,10 @@ ManglingError Remangler::mangleBoundGenericFunction(Node *node,
815813
if (trySubstitution(node, entry))
816814
return ManglingError::Success;
817815

818-
NodePointer unboundFunction = getUnspecialized(node, Factory);
816+
auto unspec = getUnspecialized(node, Factory);
817+
if (!unspec.isSuccess())
818+
return unspec.error();
819+
NodePointer unboundFunction = unspec.result();
819820
RETURN_IF_ERROR(mangleFunction(unboundFunction, depth + 1));
820821
char Separator = 'y';
821822
RETURN_IF_ERROR(mangleGenericArgs(node, Separator, depth + 1));
@@ -3418,7 +3419,8 @@ bool Demangle::isSpecialized(Node *node) {
34183419
}
34193420
}
34203421

3421-
NodePointer Demangle::getUnspecialized(Node *node, NodeFactory &Factory) {
3422+
ManglingErrorOr<NodePointer> Demangle::getUnspecialized(Node *node,
3423+
NodeFactory &Factory) {
34223424
unsigned NumToCopy = 2;
34233425
switch (node->getKind()) {
34243426
case Node::Kind::Function:
@@ -3450,8 +3452,12 @@ NodePointer Demangle::getUnspecialized(Node *node, NodeFactory &Factory) {
34503452
case Node::Kind::OtherNominalType: {
34513453
NodePointer result = Factory.createNode(node->getKind());
34523454
NodePointer parentOrModule = node->getChild(0);
3453-
if (isSpecialized(parentOrModule))
3454-
parentOrModule = getUnspecialized(parentOrModule, Factory);
3455+
if (isSpecialized(parentOrModule)) {
3456+
auto unspec = getUnspecialized(parentOrModule, Factory);
3457+
if (!unspec.isSuccess())
3458+
return unspec;
3459+
parentOrModule = unspec.result();
3460+
}
34553461
result->addChild(parentOrModule, Factory);
34563462
for (unsigned Idx = 1; Idx < NumToCopy; ++Idx) {
34573463
result->addChild(node->getChild(Idx), Factory);
@@ -3486,16 +3492,19 @@ NodePointer Demangle::getUnspecialized(Node *node, NodeFactory &Factory) {
34863492
NodePointer parent = node->getChild(1);
34873493
if (!isSpecialized(parent))
34883494
return node;
3495+
auto unspec = getUnspecialized(parent, Factory);
3496+
if (!unspec.isSuccess())
3497+
return unspec.error();
34893498
NodePointer result = Factory.createNode(Node::Kind::Extension);
34903499
result->addChild(node->getFirstChild(), Factory);
3491-
result->addChild(getUnspecialized(parent, Factory), Factory);
3500+
result->addChild(unspec.result(), Factory);
34923501
if (node->getNumChildren() == 3) {
34933502
// Add the generic signature of the extension.
34943503
result->addChild(node->getChild(2), Factory);
34953504
}
34963505
return result;
34973506
}
34983507
default:
3499-
unreachable("bad nominal type kind");
3508+
return ManglingError(ManglingError::BadNominalTypeKind, node);
35003509
}
35013510
}

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,10 @@ _findExtendedTypeContextDescriptor(const ContextDescriptor *maybeExtension,
325325
node = node->getChild(0);
326326
}
327327
if (Demangle::isSpecialized(node)) {
328-
node = Demangle::getUnspecialized(node, demangler);
328+
auto unspec = Demangle::getUnspecialized(node, demangler);
329+
if (!unspec.isSuccess())
330+
return nullptr;
331+
node = unspec.result();
329332
}
330333

331334
return _findContextDescriptor(node, demangler);

test/TypeRoundTrip/Inputs/RoundTrip/RoundTrip.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ extern "C" SWIFT_CC(swift) void roundTripType(const Metadata *md) {
3030
auto nodeTree = _swift_buildDemanglingForMetadata(md, dem);
3131

3232
// Mangle that
33-
std::string mangledName = Demangle::__runtime::mangleNode(nodeTree);
33+
auto mangling = Demangle::__runtime::mangleNode(nodeTree);
34+
if (!mangling.isSuccess()) {
35+
printf("FAIL: %s (%p) -> mangling error %d\n",
36+
mdName.c_str(), md, mangling.errorCode());
37+
return;
38+
}
39+
std::string mangledName = mangling.result();
3440

3541
// Look up the result
3642
auto result = swift_getTypeByMangledName(MetadataState::Abstract,

0 commit comments

Comments
 (0)