Skip to content

Commit 4da1032

Browse files
committed
Add name mangling support for functions with a thrown error type
1 parent 29acda5 commit 4da1032

File tree

20 files changed

+152
-67
lines changed

20 files changed

+152
-67
lines changed

docs/ABI/Mangling.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,9 @@ Types
646646
global-actor :: = type 'Yc' // Global actor on function type
647647
#endif
648648
throws ::= 'K' // 'throws' annotation on function types
649+
#if SWIFT_RUNTIME_VERSION >= 5.11
650+
throws ::= type 'YK' // 'throws(type)' annotation on function types
651+
#endif
649652
differentiable ::= 'Yjf' // @differentiable(_forward) on function type
650653
differentiable ::= 'Yjr' // @differentiable(reverse) on function type
651654
differentiable ::= 'Yjd' // @differentiable on function type

include/swift/AST/ASTDemangler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ class ASTBuilder {
144144
Type createFunctionType(
145145
ArrayRef<Demangle::FunctionParam<Type>> params,
146146
Type output, FunctionTypeFlags flags,
147-
FunctionMetadataDifferentiabilityKind diffKind, Type globalActor);
147+
FunctionMetadataDifferentiabilityKind diffKind, Type globalActor,
148+
Type thrownError);
148149

149150
Type createImplFunctionType(
150151
Demangle::ImplParameterConvention calleeConvention,

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ NODE(BaseConformanceDescriptor)
286286
NODE(AssociatedTypeDescriptor)
287287
NODE(AsyncAnnotation)
288288
NODE(ThrowsAnnotation)
289+
NODE(TypedThrowsAnnotation)
289290
NODE(EmptyList)
290291
NODE(FirstElementMarker)
291292
NODE(VariadicMarker)

include/swift/Demangling/TypeDecoder.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,11 +855,29 @@ class TypeDecoder {
855855
++firstChildIdx;
856856
}
857857

858+
BuiltType thrownErrorType = BuiltType();
858859
bool isThrow = false;
859860
if (Node->getChild(firstChildIdx)->getKind()
860861
== NodeKind::ThrowsAnnotation) {
861862
isThrow = true;
862863
++firstChildIdx;
864+
} else if (Node->getChild(firstChildIdx)->getKind()
865+
== NodeKind::TypedThrowsAnnotation) {
866+
isThrow = true;
867+
868+
auto child = Node->getChild(firstChildIdx);
869+
if (child->getNumChildren() < 1) {
870+
return MAKE_NODE_TYPE_ERROR0(child,
871+
"Thrown error node is missing child");
872+
}
873+
874+
auto thrownErrorResult =
875+
decodeMangledType(child->getChild(0), depth + 1);
876+
if (thrownErrorResult.isError())
877+
return thrownErrorResult;
878+
879+
thrownErrorType = thrownErrorResult.getType();
880+
++firstChildIdx;
863881
}
864882

865883
bool isSendable = false;
@@ -905,8 +923,10 @@ class TypeDecoder {
905923
/*forRequirement=*/false);
906924
if (result.isError())
907925
return result;
926+
908927
return Builder.createFunctionType(
909-
parameters, result.getType(), flags, diffKind, globalActorType);
928+
parameters, result.getType(), flags, diffKind, globalActorType,
929+
thrownErrorType);
910930
}
911931
case NodeKind::ImplFunctionType: {
912932
auto calleeConvention = ImplParameterConvention::Direct_Unowned;

include/swift/Remote/MetadataReader.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,11 @@ class MetadataReader {
10011001
#undef CASE
10021002
}
10031003

1004+
BuiltType thrownError = BuiltType();
1005+
// FIXME: Read from metadata.
1006+
10041007
auto BuiltFunction = Builder.createFunctionType(
1005-
Parameters, Result, flags, diffKind, globalActor);
1008+
Parameters, Result, flags, diffKind, globalActor, thrownError);
10061009
TypeCache[TypeCacheKey] = BuiltFunction;
10071010
return BuiltFunction;
10081011
}

include/swift/RemoteInspection/TypeRef.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,13 @@ class FunctionTypeRef final : public TypeRef {
485485
FunctionTypeFlags Flags;
486486
FunctionMetadataDifferentiabilityKind DifferentiabilityKind;
487487
const TypeRef *GlobalActor;
488+
const TypeRef *ThrownError;
488489

489490
static TypeRefID Profile(const std::vector<Param> &Parameters,
490491
const TypeRef *Result, FunctionTypeFlags Flags,
491492
FunctionMetadataDifferentiabilityKind DiffKind,
492-
const TypeRef *GlobalActor) {
493+
const TypeRef *GlobalActor,
494+
const TypeRef *ThrownError) {
493495
TypeRefID ID;
494496
for (const auto &Param : Parameters) {
495497
ID.addString(Param.getLabel().str());
@@ -500,6 +502,7 @@ class FunctionTypeRef final : public TypeRef {
500502
ID.addInteger(static_cast<uint64_t>(Flags.getIntValue()));
501503
ID.addInteger(static_cast<uint64_t>(DiffKind.getIntValue()));
502504
ID.addPointer(GlobalActor);
505+
ID.addPointer(ThrownError);
503506

504507
return ID;
505508
}
@@ -508,18 +511,20 @@ class FunctionTypeRef final : public TypeRef {
508511
FunctionTypeRef(std::vector<Param> Params, const TypeRef *Result,
509512
FunctionTypeFlags Flags,
510513
FunctionMetadataDifferentiabilityKind DiffKind,
511-
const TypeRef *GlobalActor)
514+
const TypeRef *GlobalActor,
515+
const TypeRef *ThrownError)
512516
: TypeRef(TypeRefKind::Function), Parameters(Params), Result(Result),
513517
Flags(Flags), DifferentiabilityKind(DiffKind),
514-
GlobalActor(GlobalActor) {}
518+
GlobalActor(GlobalActor), ThrownError(ThrownError) {}
515519

516520
template <typename Allocator>
517521
static const FunctionTypeRef *create(
518522
Allocator &A, std::vector<Param> Params, const TypeRef *Result,
519523
FunctionTypeFlags Flags, FunctionMetadataDifferentiabilityKind DiffKind,
520-
const TypeRef *GlobalActor) {
524+
const TypeRef *GlobalActor, const TypeRef *ThrownError) {
521525
FIND_OR_CREATE_TYPEREF(
522-
A, FunctionTypeRef, Params, Result, Flags, DiffKind, GlobalActor);
526+
A, FunctionTypeRef, Params, Result, Flags, DiffKind, GlobalActor,
527+
ThrownError);
523528
}
524529

525530
const std::vector<Param> &getParameters() const { return Parameters; };
@@ -540,6 +545,10 @@ class FunctionTypeRef final : public TypeRef {
540545
return GlobalActor;
541546
}
542547

548+
const TypeRef *getThrownError() const {
549+
return ThrownError;
550+
}
551+
543552
static bool classof(const TypeRef *TR) {
544553
return TR->getKind() == TypeRefKind::Function;
545554
}

include/swift/RemoteInspection/TypeRefBuilder.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,9 @@ class TypeRefBuilder {
793793
llvm::ArrayRef<remote::FunctionParam<const TypeRef *>> params,
794794
const TypeRef *result, FunctionTypeFlags flags,
795795
FunctionMetadataDifferentiabilityKind diffKind,
796-
const TypeRef *globalActor) {
796+
const TypeRef *globalActor, const TypeRef *thrownError) {
797797
return FunctionTypeRef::create(
798-
*this, params, result, flags, diffKind, globalActor);
798+
*this, params, result, flags, diffKind, globalActor, thrownError);
799799
}
800800

801801
const FunctionTypeRef *createImplFunctionType(
@@ -852,7 +852,7 @@ class TypeRefBuilder {
852852

853853
auto result = createTupleType({}, llvm::ArrayRef<llvm::StringRef>());
854854
return FunctionTypeRef::create(
855-
*this, {}, result, funcFlags, diffKind, nullptr);
855+
*this, {}, result, funcFlags, diffKind, nullptr, nullptr);
856856
}
857857

858858
BuiltType createProtocolTypeFromDecl(BuiltProtocolDecl protocol) {

lib/AST/ASTDemangler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ void ASTBuilder::endPackExpansion() {
390390
Type ASTBuilder::createFunctionType(
391391
ArrayRef<Demangle::FunctionParam<Type>> params,
392392
Type output, FunctionTypeFlags flags,
393-
FunctionMetadataDifferentiabilityKind diffKind, Type globalActor) {
393+
FunctionMetadataDifferentiabilityKind diffKind, Type globalActor,
394+
Type thrownError) {
394395
// The result type must be materializable.
395396
if (!output->isMaterializable()) return Type();
396397

@@ -454,9 +455,6 @@ Type ASTBuilder::createFunctionType(
454455
clangFunctionType = Ctx.getClangFunctionType(funcParams, output,
455456
representation);
456457

457-
// FIXME: Populate thrownError
458-
Type thrownError;
459-
460458
auto einfo =
461459
FunctionType::ExtInfoBuilder(representation, noescape, flags.isThrowing(),
462460
thrownError, resultDiffKind,

lib/AST/ASTMangler.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2828,8 +2828,14 @@ void ASTMangler::appendFunctionSignature(AnyFunctionType *fn,
28282828
appendOperator("Ya");
28292829
if (fn->isSendable())
28302830
appendOperator("Yb");
2831-
if (fn->isThrowing())
2832-
appendOperator("K");
2831+
if (auto thrownError = fn->getEffectiveThrownErrorType()) {
2832+
if ((*thrownError)->isEqual(fn->getASTContext().getErrorExistentialType())){
2833+
appendOperator("K");
2834+
} else {
2835+
appendType(*thrownError, sig);
2836+
appendOperator("YK");
2837+
}
2838+
}
28332839
switch (auto diffKind = fn->getDifferentiabilityKind()) {
28342840
case DifferentiabilityKind::NonDifferentiable:
28352841
break;

lib/Demangling/Demangler.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,8 @@ NodePointer Demangler::demangleTypeAnnotation() {
926926
case 'k':
927927
return createType(
928928
createWithChild(Node::Kind::NoDerivative, popTypeAndGetChild()));
929+
case 'K':
930+
return createWithChild(Node::Kind::TypedThrowsAnnotation, popTypeAndGetChild());
929931
case 't':
930932
return createType(
931933
createWithChild(Node::Kind::CompileTimeConst, popTypeAndGetChild()));
@@ -1530,7 +1532,10 @@ NodePointer Demangler::popFunctionType(Node::Kind kind, bool hasClangType) {
15301532
addChild(FuncType, ClangType);
15311533
addChild(FuncType, popNode(Node::Kind::GlobalActorFunctionType));
15321534
addChild(FuncType, popNode(Node::Kind::DifferentiableFunctionType));
1533-
addChild(FuncType, popNode(Node::Kind::ThrowsAnnotation));
1535+
addChild(FuncType, popNode([](Node::Kind kind) {
1536+
return kind == Node::Kind::ThrowsAnnotation ||
1537+
kind == Node::Kind::TypedThrowsAnnotation;
1538+
}));
15341539
addChild(FuncType, popNode(Node::Kind::ConcurrentFunctionType));
15351540
addChild(FuncType, popNode(Node::Kind::AsyncAnnotation));
15361541

@@ -1572,7 +1577,9 @@ NodePointer Demangler::popFunctionParamLabels(NodePointer Type) {
15721577
== Node::Kind::DifferentiableFunctionType)
15731578
++FirstChildIdx;
15741579
if (FuncType->getChild(FirstChildIdx)->getKind()
1575-
== Node::Kind::ThrowsAnnotation)
1580+
== Node::Kind::ThrowsAnnotation ||
1581+
FuncType->getChild(FirstChildIdx)->getKind()
1582+
== Node::Kind::TypedThrowsAnnotation)
15761583
++FirstChildIdx;
15771584
if (FuncType->getChild(FirstChildIdx)->getKind()
15781585
== Node::Kind::ConcurrentFunctionType)

0 commit comments

Comments
 (0)