Skip to content

Commit

Permalink
Mangling: refactoring: clean up tuple nodes
Browse files Browse the repository at this point in the history
Replace VariadicTuple and NonVariadicTuple with a single Tuple node.
The variadic property is now part of the tuple element and not of the whole tuple.
  • Loading branch information
eeckstein committed Mar 26, 2017
1 parent 327017a commit e427ded
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 37 deletions.
3 changes: 1 addition & 2 deletions include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ CONTEXT_NODE(NativePinningAddressor)
CONTEXT_NODE(NativePinningMutableAddressor)
NODE(NominalTypeDescriptor)
NODE(NonObjCAttribute)
NODE(NonVariadicTuple)
NODE(Number)
NODE(ObjCAttribute)
NODE(ObjCBlock)
Expand Down Expand Up @@ -144,6 +143,7 @@ CONTEXT_NODE(Structure)
CONTEXT_NODE(Subscript)
NODE(Suffix)
NODE(ThinFunctionType)
NODE(Tuple)
NODE(TupleElement)
NODE(TupleElementName)
NODE(Type)
Expand All @@ -161,7 +161,6 @@ CONTEXT_NODE(UnsafeMutableAddressor)
NODE(ValueWitness)
NODE(ValueWitnessTable)
CONTEXT_NODE(Variable)
NODE(VariadicTuple)
NODE(VTableThunk)
NODE(VTableAttribute) // note: old mangling only
NODE(Weak)
Expand Down
18 changes: 11 additions & 7 deletions include/swift/Remote/MetadataReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,25 +233,31 @@ class TypeDecoder {
return decodeMangledType(Node->getChild(0));
case NodeKind::ReturnType:
return decodeMangledType(Node->getChild(0));
case NodeKind::NonVariadicTuple:
case NodeKind::VariadicTuple: {
case NodeKind::Tuple: {
std::vector<BuiltType> elements;
std::string labels;
bool variadic = false;
for (auto &element : *Node) {
if (element->getKind() != NodeKind::TupleElement)
return BuiltType();

// If the tuple element is labeled, add its label to 'labels'.
unsigned typeChildIndex = 0;
if (element->getChild(0)->getKind() == NodeKind::TupleElementName) {
unsigned nameIdx = 0;
if (element->getChild(nameIdx)->getKind() == NodeKind::VariadicMarker) {
variadic = true;
nameIdx = 1;
typeChildIndex = 1;
}
if (element->getChild(nameIdx)->getKind() == NodeKind::TupleElementName) {
// Add spaces to terminate all the previous labels if this
// is the first we've seen.
if (labels.empty()) labels.append(elements.size(), ' ');

// Add the label and its terminator.
labels += element->getChild(0)->getText();
labels += ' ';
typeChildIndex = 1;
typeChildIndex++;

// Otherwise, add a space if a previous element had a label.
} else if (!labels.empty()) {
Expand All @@ -266,7 +272,6 @@ class TypeDecoder {

elements.push_back(elementType);
}
bool variadic = (Node->getKind() == NodeKind::VariadicTuple);
return Builder.createTupleType(elements, std::move(labels), variadic);
}
case NodeKind::TupleElement:
Expand Down Expand Up @@ -378,8 +383,7 @@ class TypeDecoder {
};

// Expand a single level of tuple.
if (node->getKind() == NodeKind::VariadicTuple ||
node->getKind() == NodeKind::NonVariadicTuple) {
if (node->getKind() == NodeKind::Tuple) {
// TODO: preserve variadic somewhere?

// Decode all the elements as separate arguments.
Expand Down
6 changes: 2 additions & 4 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,17 +805,15 @@ NodePointer Demangler::popFunctionType(Node::Kind kind) {
NodePointer Demangler::popFunctionParams(Node::Kind kind) {
NodePointer ParamsType = nullptr;
if (popNode(Node::Kind::EmptyList)) {
ParamsType = createType(createNode(Node::Kind::NonVariadicTuple));
ParamsType = createType(createNode(Node::Kind::Tuple));
} else {
ParamsType = popNode(Node::Kind::Type);
}
return createWithChild(kind, ParamsType);
}

NodePointer Demangler::popTuple() {
NodePointer Root = createNode(popNode(Node::Kind::VariadicMarker) ?
Node::Kind::VariadicTuple :
Node::Kind::NonVariadicTuple);
NodePointer Root = createNode(Node::Kind::Tuple);

if (!popNode(Node::Kind::EmptyList)) {
bool firstElem = false;
Expand Down
11 changes: 3 additions & 8 deletions lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class NodePrinter {
case Node::Kind::Metatype:
case Node::Kind::MetatypeRepresentation:
case Node::Kind::Module:
case Node::Kind::NonVariadicTuple:
case Node::Kind::Tuple:
case Node::Kind::Protocol:
case Node::Kind::QualifiedArchetype:
case Node::Kind::ReturnType:
Expand All @@ -260,7 +260,6 @@ class NodePrinter {
case Node::Kind::Type:
case Node::Kind::TypeAlias:
case Node::Kind::TypeList:
case Node::Kind::VariadicTuple:
return true;

case Node::Kind::ProtocolList:
Expand Down Expand Up @@ -944,8 +943,7 @@ void NodePrinter::print(NodePointer pointer, bool asContext, bool suppressType)
if (child0_kind == Node::Kind::Type)
child0_kind = pointer->getChild(0)->getChild(0)->getKind();

if (child0_kind != Node::Kind::VariadicTuple &&
child0_kind != Node::Kind::NonVariadicTuple)
if (child0_kind != Node::Kind::Tuple)
need_parens = true;
}
}
Expand All @@ -956,12 +954,9 @@ void NodePrinter::print(NodePointer pointer, bool asContext, bool suppressType)
Printer << ")";
return;
}
case Node::Kind::NonVariadicTuple:
case Node::Kind::VariadicTuple: {
case Node::Kind::Tuple: {
Printer << "(";
printChildren(pointer, ", ");
if (pointer->getKind() == Node::Kind::VariadicTuple)
Printer << "...";
Printer << ")";
return;
}
Expand Down
13 changes: 9 additions & 4 deletions lib/Demangling/OldDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1654,13 +1654,12 @@ class OldDemangler {
}

NodePointer demangleTuple(IsVariadic isV) {
NodePointer tuple = Factory.createNode(
isV == IsVariadic::yes ? Node::Kind::VariadicTuple
: Node::Kind::NonVariadicTuple);
NodePointer tuple = Factory.createNode(Node::Kind::Tuple);
NodePointer elt = nullptr;
while (!Mangled.nextIf('_')) {
if (!Mangled)
return nullptr;
NodePointer elt = Factory.createNode(Node::Kind::TupleElement);
elt = Factory.createNode(Node::Kind::TupleElement);

if (isStartOfIdentifier(Mangled.peek())) {
NodePointer label = demangleIdentifier(Node::Kind::TupleElementName);
Expand All @@ -1676,6 +1675,12 @@ class OldDemangler {

tuple->addChild(elt, Factory);
}
if (isV == IsVariadic::yes && elt) {
elt->reverseChildren();
NodePointer marker = Factory.createNode(Node::Kind::VariadicMarker);
elt->addChild(marker, Factory);
elt->reverseChildren();
}
return tuple;
}

Expand Down
9 changes: 2 additions & 7 deletions lib/Demangling/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ void Remangler::mangleAllocator(Node *node) {

void Remangler::mangleArgumentTuple(Node *node) {
Node *Child = skipType(getSingleChild(node));
if (Child->getKind() == Node::Kind::NonVariadicTuple &&
if (Child->getKind() == Node::Kind::Tuple &&
Child->getNumChildren() == 0) {
Buffer << 'y';
return;
Expand Down Expand Up @@ -1327,7 +1327,7 @@ void Remangler::mangleNonObjCAttribute(Node *node) {
Buffer << "TO";
}

void Remangler::mangleNonVariadicTuple(Node *node) {
void Remangler::mangleTuple(Node *node) {
mangleTypeList(node);
Buffer << 't';
}
Expand Down Expand Up @@ -1599,11 +1599,6 @@ void Remangler::mangleVariable(Node *node) {
Buffer << 'v';
}

void Remangler::mangleVariadicTuple(Node *node) {
mangleTypeList(node);
Buffer << "dt";
}

void Remangler::mangleVTableAttribute(Node *node) {
unreachable("Old-fashioned vtable thunk in new mangling format");
}
Expand Down
6 changes: 3 additions & 3 deletions lib/IDE/TypeReconstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1716,7 +1716,7 @@ static void VisitNodeModule(
}
}

static void VisitNodeNonVariadicTuple(
static void VisitNodeTuple(
ASTContext *ast, std::vector<Demangle::NodePointer> &nodes,
Demangle::NodePointer &cur_node, VisitNodeResult &result,
const VisitNodeResult &generic_context) { // set by GenericType case
Expand Down Expand Up @@ -2060,8 +2060,8 @@ static void visitNodeImpl(
VisitNodeModule(ast, nodes, node, result, genericContext);
break;

case Demangle::Node::Kind::NonVariadicTuple:
VisitNodeNonVariadicTuple(ast, nodes, node, result, genericContext);
case Demangle::Node::Kind::Tuple:
VisitNodeTuple(ast, nodes, node, result, genericContext);
break;

case Demangle::Node::Kind::PrivateDeclName:
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/runtime/Demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,

NodePointer totalInput = nullptr;
if (inputs.size() > 1) {
auto tuple = Dem.createNode(Node::Kind::NonVariadicTuple);
auto tuple = Dem.createNode(Node::Kind::Tuple);
for (auto &input : inputs)
tuple->addChild(input, Dem);
totalInput = tuple;
Expand Down Expand Up @@ -280,7 +280,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,
case MetadataKind::Tuple: {
auto tuple = static_cast<const TupleTypeMetadata *>(type);
const char *labels = tuple->Labels;
auto tupleNode = Dem.createNode(Node::Kind::NonVariadicTuple);
auto tupleNode = Dem.createNode(Node::Kind::Tuple);
for (unsigned i = 0, e = tuple->NumElements; i < e; ++i) {
auto elt = Dem.createNode(Node::Kind::TupleElement);

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 @@ -247,3 +247,5 @@ _T03foo6testityyyc_yyctF1a1bTf3pfpf_n ---> function signature specialization <Ar
_SocketJoinOrLeaveMulticast ---> _SocketJoinOrLeaveMulticast
_T0s10DictionaryV3t17E6Index2V1loiSbAEyxq__G_AGtFZ ---> static (extension in t17):Swift.Dictionary.Index2.< infix ((extension in t17):[A : B].Index2, (extension in t17):[A : B].Index2) -> Swift.Bool
_T08mangling14varargsVsArrayySaySiG3arrd_SS1ntF ---> mangling.varargsVsArray (arr : [Swift.Int]..., n : Swift.String) -> ()
_T08mangling14varargsVsArrayySaySiG3arrd_tF ---> mangling.varargsVsArray (arr : [Swift.Int]...) -> ()

0 comments on commit e427ded

Please sign in to comment.