Skip to content

[Demangler] Print convention attributes consistently. #34122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 36 additions & 49 deletions lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "swift/Demangling/Demangle.h"
#include "swift/AST/Ownership.h"
#include "swift/Strings.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>

Expand Down Expand Up @@ -753,6 +754,35 @@ class NodePrinter {
setInvalid();
return;
}

switch (node->getKind()) {
case Node::Kind::FunctionType:
case Node::Kind::UncurriedFunctionType:
case Node::Kind::NoEscapeFunctionType:
break;
case Node::Kind::AutoClosureType:
case Node::Kind::EscapingAutoClosureType:
Printer << "@autoclosure "; break;
case Node::Kind::ThinFunctionType:
Printer << "@convention(thin) "; break;
case Node::Kind::CFunctionPointer:
Printer << "@convention(c) "; break;
case Node::Kind::ObjCBlock:
Printer << "@convention(block) "; break;
case Node::Kind::EscapingObjCBlock:
Printer << "@escaping @convention(block) "; break;
case Node::Kind::DifferentiableFunctionType:
Printer << "@differentiable "; break;
case Node::Kind::EscapingDifferentiableFunctionType:
Printer << "@escaping @differentiable "; break;
case Node::Kind::LinearFunctionType:
Printer << "@differentiable(linear) "; break;
case Node::Kind::EscapingLinearFunctionType:
Printer << "@escaping @differentiable(linear) "; break;
default:
assert(false && "Unhandled function type in printFunctionType!");
}

unsigned startIndex = 0;
bool isAsync = false, isThrows = false;
if (node->getChild(startIndex)->getKind() == Node::Kind::ThrowsAnnotation) {
Expand Down Expand Up @@ -1256,39 +1286,19 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
case Node::Kind::UnknownIndex:
Printer << "unknown index";
return nullptr;
case Node::Kind::FunctionType:
case Node::Kind::UncurriedFunctionType:
case Node::Kind::NoEscapeFunctionType:
printFunctionType(nullptr, Node);
return nullptr;
case Node::Kind::EscapingAutoClosureType:
Printer << "@autoclosure ";
printFunctionType(nullptr, Node);
return nullptr;
case Node::Kind::AutoClosureType:
Printer << "@autoclosure ";
printFunctionType(nullptr, Node);
return nullptr;
case Node::Kind::EscapingAutoClosureType:
case Node::Kind::ThinFunctionType:
Printer << "@convention(thin) ";
printFunctionType(nullptr, Node);
return nullptr;
case Node::Kind::CFunctionPointer:
case Node::Kind::ObjCBlock:
case Node::Kind::EscapingObjCBlock:
case Node::Kind::DifferentiableFunctionType:
Printer << "@differentiable ";
printFunctionType(nullptr, Node);
return nullptr;
case Node::Kind::EscapingDifferentiableFunctionType:
Printer << "@escaping @differentiable ";
printFunctionType(nullptr, Node);
return nullptr;
case Node::Kind::LinearFunctionType:
Printer << "@differentiable(linear) ";
printFunctionType(nullptr, Node);
return nullptr;
case Node::Kind::EscapingLinearFunctionType:
Printer << "@escaping @differentiable(linear) ";
printFunctionType(nullptr, Node);
return nullptr;
case Node::Kind::FunctionType:
case Node::Kind::UncurriedFunctionType:
printFunctionType(nullptr, Node);
return nullptr;
case Node::Kind::ArgumentTuple:
Expand Down Expand Up @@ -1881,21 +1891,6 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
case Node::Kind::DynamicSelf:
Printer << "Self";
return nullptr;
case Node::Kind::CFunctionPointer: {
Printer << "@convention(c) ";
printFunctionType(nullptr, Node);
return nullptr;
}
case Node::Kind::ObjCBlock: {
Printer << "@convention(block) ";
printFunctionType(nullptr, Node);
return nullptr;
}
case Node::Kind::EscapingObjCBlock: {
Printer << "@escaping @convention(block) ";
printFunctionType(nullptr, Node);
return nullptr;
}
case Node::Kind::SILBoxType: {
Printer << "@box ";
NodePointer type = Node->getChild(0);
Expand Down Expand Up @@ -2648,14 +2643,6 @@ void NodePrinter::printEntityType(NodePointer Entity, NodePointer type,
Printer << ' ';
type = dependentType->getFirstChild();
}
if (type->getKind() == Node::Kind::DifferentiableFunctionType)
Printer << "@differentiable ";
else if (type->getKind() == Node::Kind::EscapingDifferentiableFunctionType)
Printer << "@escaping @differentiable ";
else if (type->getKind() == Node::Kind::LinearFunctionType)
Printer << "@differentiable(linear) ";
else if (type->getKind() == Node::Kind::EscapingLinearFunctionType)
Printer << "@escaping @differentiable(linear) ";
printFunctionType(labelList, type);
} else {
print(type);
Expand Down
2 changes: 2 additions & 0 deletions test/Demangle/Inputs/manglings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ $s3red4testyAA3ResOyxSayq_GAEs5ErrorAAq_sAFHD1__HCg_GADyxq_GsAFR_r0_lF ---> red.
$s3red4testyAA7OurTypeOy4them05TheirD0Vy5AssocQzGAjE0F8ProtocolAAxAA0c7DerivedH0HD1_AA0c4BaseH0HI1_AieKHA2__HCg_GxmAaLRzlF ---> red.test<A where A: red.OurDerivedProtocol>(A.Type) -> red.OurType<them.TheirType<A.Assoc>>
$s17property_wrappers10WithTuplesV9fractionsSd_S2dtvpfP ---> property wrapper backing initializer of property_wrappers.WithTuples.fractions : (Swift.Double, Swift.Double, Swift.Double)
$sSo17OS_dispatch_queueC4sync7executeyyyXE_tFTOTA ---> {T:$sSo17OS_dispatch_queueC4sync7executeyyyXE_tFTO} partial apply forwarder for @nonobjc __C.OS_dispatch_queue.sync(execute: () -> ()) -> ()
$s4main1gyySiXCvp ---> main.g : @convention(c) (Swift.Int) -> ()
$s4main1gyySiXBvp ---> main.g : @convention(block) (Swift.Int) -> ()
$sxq_Idgnr_D ---> @differentiable @callee_guaranteed (@in_guaranteed A) -> (@out B)
$sxq_Ilgnr_D ---> @differentiable(linear) @callee_guaranteed (@in_guaranteed A) -> (@out B)
$sS3fIedgyywd_D ---> @escaping @differentiable @callee_guaranteed (@unowned Swift.Float, @unowned @noDerivative Swift.Float) -> (@unowned Swift.Float)
Expand Down