Skip to content

[6.2] ASTDemangler: Round-trip @isolated @sil_implicit_leading_param parameter attributes #82543

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
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
9 changes: 9 additions & 0 deletions docs/ABI/Mangling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,15 @@ mangled in to disambiguate.
PARAM-CONVENTION ::= 'p' // pack guaranteed
PARAM-CONVENTION ::= 'm' // pack inout

#if SWIFT_RUNTIME_VERSION >= 6.0
SENDING-PARAM ::= 'T' // sending parameter
#endif

#if SWIFT_RUNTIME_VERSION >= 6.2
ISOLATED-PARAM ::= 'I' // @isolated parameter
IMPLICIT-LEADING-PARAM ::= 'L' // @implicit_leading parameter
#endif

PARAM-DIFFERENTIABILITY ::= 'w' // @noDerivative

RESULT-CONVENTION ::= 'r' // indirect
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Demangling/Demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ struct [[nodiscard]] ManglingError {
InvalidImplCoroutineKind,
InvalidImplFunctionAttribute,
InvalidImplParameterConvention,
InvalidImplParameterSending,
InvalidImplParameterAttr,
InvalidMetatypeRepresentation,
MultiByteRelatedEntity,
BadValueWitnessKind,
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ NODE(ImplErasedIsolation)
NODE(ImplSendingResult)
NODE(ImplParameterResultDifferentiability)
NODE(ImplParameterSending)
NODE(ImplParameterIsolated)
NODE(ImplParameterImplicitLeading)
NODE(ImplFunctionAttribute)
NODE(ImplFunctionConvention)
NODE(ImplFunctionConventionName)
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Demangling/Demangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,8 @@ class Demangler : public NodeFactory {
NodePointer demangleImplParamConvention(Node::Kind ConvKind);
NodePointer demangleImplResultConvention(Node::Kind ConvKind);
NodePointer demangleImplParameterSending();
NodePointer demangleImplParameterIsolated();
NodePointer demangleImplParameterImplicitLeading();
NodePointer demangleImplParameterResultDifferentiability();
NodePointer demangleImplFunctionType();
NodePointer demangleClangType();
Expand Down
119 changes: 84 additions & 35 deletions include/swift/Demangling/TypeDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ enum class ImplParameterConvention {
enum class ImplParameterInfoFlags : uint8_t {
NotDifferentiable = 0x1,
Sending = 0x2,
Isolated = 0x4,
ImplicitLeading = 0x8
};

using ImplParameterInfoOptions = OptionSet<ImplParameterInfoFlags>;
Expand Down Expand Up @@ -192,6 +194,22 @@ class ImplFunctionParam {
return result;
}

static OptionsType getIsolated() {
OptionsType result;

result |= ImplParameterInfoFlags::Isolated;

return result;
}

static OptionsType getImplicitLeading() {
OptionsType result;

result |= ImplParameterInfoFlags::ImplicitLeading;

return result;
}

ImplFunctionParam(BuiltType type, ImplParameterConvention convention,
OptionsType options)
: Type(type), Convention(convention), Options(options) {}
Expand Down Expand Up @@ -1143,11 +1161,11 @@ class TypeDecoder {
return MAKE_NODE_TYPE_ERROR0(child,
"failed to decode function yields");
} else if (child->getKind() == NodeKind::ImplResult) {
if (decodeImplFunctionParam(child, depth + 1, results))
if (decodeImplFunctionResult(child, depth + 1, results))
return MAKE_NODE_TYPE_ERROR0(child,
"failed to decode function results");
} else if (child->getKind() == NodeKind::ImplErrorResult) {
if (decodeImplFunctionPart(child, depth + 1, errorResults))
if (decodeImplFunctionResult(child, depth + 1, errorResults))
return MAKE_NODE_TYPE_ERROR0(child,
"failed to decode function part");
} else {
Expand Down Expand Up @@ -1638,40 +1656,70 @@ class TypeDecoder {
}

template <typename T>
bool decodeImplFunctionPart(Demangle::NodePointer node, unsigned depth,
llvm::SmallVectorImpl<T> &results) {
bool decodeImplFunctionParam(Demangle::NodePointer node, unsigned depth,
llvm::SmallVectorImpl<T> &results) {
if (depth > TypeDecoder::MaxDepth)
return true;

if (node->getNumChildren() != 2)
// Children: `convention, attrs, type`
// attrs: `differentiability?, sending?, isolated?, implicit_leading?`
if (node->getNumChildren() < 2)
return true;

if (node->getChild(0)->getKind() != Node::Kind::ImplConvention ||
node->getChild(1)->getKind() != Node::Kind::Type)

auto *conventionNode = node->getChild(0);
auto *typeNode = node->getLastChild();
if (conventionNode->getKind() != Node::Kind::ImplConvention ||
typeNode->getKind() != Node::Kind::Type)
return true;

StringRef conventionString = node->getChild(0)->getText();
std::optional<typename T::ConventionType> convention =
T::getConventionFromString(conventionString);
StringRef conventionString = conventionNode->getText();
auto convention = T::getConventionFromString(conventionString);
if (!convention)
return true;
auto type = decodeMangledType(node->getChild(1), depth + 1);
if (type.isError())
auto result = decodeMangledType(typeNode, depth + 1);
if (result.isError())
return true;

results.emplace_back(type.getType(), *convention);
typename T::OptionsType options;
for (unsigned i = 1; i < node->getNumChildren() - 1; ++i) {
auto child = node->getChild(i);
switch (child->getKind()) {
case Node::Kind::ImplParameterResultDifferentiability: {
auto optDiffOptions =
T::getDifferentiabilityFromString(child->getText());
if (!optDiffOptions)
return true;
options |= *optDiffOptions;
break;
}
case Node::Kind::ImplParameterSending:
options |= T::getSending();
break;
case Node::Kind::ImplParameterIsolated:
options |= T::getIsolated();
break;
case Node::Kind::ImplParameterImplicitLeading:
options |= T::getImplicitLeading();
break;
default:
return true;
}
}

results.emplace_back(result.getType(), *convention, options);

return false;
}

template <typename T>
bool decodeImplFunctionParam(Demangle::NodePointer node, unsigned depth,
llvm::SmallVectorImpl<T> &results) {
bool decodeImplFunctionResult(Demangle::NodePointer node, unsigned depth,
llvm::SmallVectorImpl<T> &results) {
if (depth > TypeDecoder::MaxDepth)
return true;

// Children: `convention, differentiability?, sending?, type`
if (node->getNumChildren() != 2 && node->getNumChildren() != 3 &&
node->getNumChildren() != 4)
// Children: `convention, attrs, type`
// attrs: `differentiability?, sending?, isolated?, implicit_leading?`
if (node->getNumChildren() < 2)
return true;

auto *conventionNode = node->getChild(0);
Expand All @@ -1689,23 +1737,24 @@ class TypeDecoder {
return true;

typename T::OptionsType options;
if (node->getNumChildren() == 3 || node->getNumChildren() == 4) {
auto diffKindNode = node->getChild(1);
if (diffKindNode->getKind() !=
Node::Kind::ImplParameterResultDifferentiability)
return true;
auto optDiffOptions =
T::getDifferentiabilityFromString(diffKindNode->getText());
if (!optDiffOptions)
return true;
options |= *optDiffOptions;
}

if (node->getNumChildren() == 4) {
auto sendingKindNode = node->getChild(2);
if (sendingKindNode->getKind() != Node::Kind::ImplParameterSending)
for (unsigned i = 1; i < node->getNumChildren() - 1; ++i) {
auto child = node->getChild(i);
switch (child->getKind()) {
case Node::Kind::ImplParameterResultDifferentiability: {
auto optDiffOptions =
T::getDifferentiabilityFromString(child->getText());
if (!optDiffOptions)
return true;
options |= *optDiffOptions;
break;
}
case Node::Kind::ImplParameterSending:
options |= T::getSending();
break;
break;
default:
return true;
options |= T::getSending();
}
}

results.emplace_back(result.getType(), *convention, options);
Expand Down
10 changes: 10 additions & 0 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,16 @@ getParameterOptions(ImplParameterInfoOptions implOptions) {
result |= SILParameterInfo::Sending;
}

if (implOptions.contains(ImplParameterInfoFlags::Isolated)) {
implOptions -= ImplParameterInfoFlags::Isolated;
result |= SILParameterInfo::Isolated;
}

if (implOptions.contains(ImplParameterInfoFlags::ImplicitLeading)) {
implOptions -= ImplParameterInfoFlags::ImplicitLeading;
result |= SILParameterInfo::ImplicitLeading;
}

// If we did not handle all flags in implOptions, this code was not updated
// appropriately. Return None to signal error.
if (bool(implOptions))
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2411,6 +2411,10 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
OpArgs.push_back(getParamConvention(param.getConvention()));
if (param.hasOption(SILParameterInfo::Sending))
OpArgs.push_back('T');
if (param.hasOption(SILParameterInfo::Isolated))
OpArgs.push_back('I');
if (param.hasOption(SILParameterInfo::ImplicitLeading))
OpArgs.push_back('L');
if (auto diffKind = getParamDifferentiability(param.getOptions()))
OpArgs.push_back(*diffKind);
appendType(param.getInterfaceType(), sig, forDecl);
Expand Down
20 changes: 20 additions & 0 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,22 @@ NodePointer Demangler::demangleImplParameterSending() {
return createNode(Node::Kind::ImplParameterSending, attr);
}

NodePointer Demangler::demangleImplParameterIsolated() {
// Empty string represents default differentiability.
if (!nextIf('I'))
return nullptr;
const char *attr = "isolated";
return createNode(Node::Kind::ImplParameterIsolated, attr);
}

NodePointer Demangler::demangleImplParameterImplicitLeading() {
// Empty string represents default differentiability.
if (!nextIf('L'))
return nullptr;
const char *attr = "sil_implicit_leading_param";
return createNode(Node::Kind::ImplParameterImplicitLeading, attr);
}

NodePointer Demangler::demangleImplParameterResultDifferentiability() {
// Empty string represents default differentiability.
const char *attr = "";
Expand Down Expand Up @@ -2452,6 +2468,10 @@ NodePointer Demangler::demangleImplFunctionType() {
Param = addChild(Param, Diff);
if (auto Sending = demangleImplParameterSending())
Param = addChild(Param, Sending);
if (auto Sending = demangleImplParameterIsolated())
Param = addChild(Param, Sending);
if (auto Sending = demangleImplParameterImplicitLeading())
Param = addChild(Param, Sending);
++NumTypesToAdd;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ bool NodePrinter::isSimpleType(NodePointer Node) {
case Node::Kind::ImplConvention:
case Node::Kind::ImplParameterResultDifferentiability:
case Node::Kind::ImplParameterSending:
case Node::Kind::ImplParameterIsolated:
case Node::Kind::ImplParameterImplicitLeading:
case Node::Kind::ImplFunctionAttribute:
case Node::Kind::ImplFunctionConvention:
case Node::Kind::ImplFunctionConventionName:
Expand Down Expand Up @@ -2803,6 +2805,8 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
Printer << Node->getText() << ' ';
return nullptr;
case Node::Kind::ImplParameterSending:
case Node::Kind::ImplParameterIsolated:
case Node::Kind::ImplParameterImplicitLeading:
// Skip if text is empty.
if (Node->getText().empty())
return nullptr;
Expand Down
12 changes: 11 additions & 1 deletion lib/Demangling/OldRemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,17 @@ ManglingError Remangler::mangleImplParameterSending(Node *node,
Buffer << 'T';
return ManglingError::Success;
}
return MANGLING_ERROR(ManglingError::InvalidImplParameterSending, node);
return MANGLING_ERROR(ManglingError::InvalidImplParameterAttr, node);
}

ManglingError Remangler::mangleImplParameterIsolated(Node *node,
unsigned depth) {
return ManglingError::Success;
}

ManglingError Remangler::mangleImplParameterImplicitLeading(Node *node,
unsigned depth) {
return ManglingError::Success;
}

ManglingError Remangler::mangleDynamicSelf(Node *node, unsigned depth) {
Expand Down
Loading