Skip to content

Commit

Permalink
Merge pull request swiftlang#8346 from eeckstein/vararg-mangling
Browse files Browse the repository at this point in the history
  • Loading branch information
swift-ci authored Mar 26, 2017
2 parents 5127bcb + e427ded commit 41ab3fe
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 60 deletions.
4 changes: 2 additions & 2 deletions docs/ABI.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1031,10 +1031,10 @@ Types

throws ::= 'K' // 'throws' annotation on function types

type-list ::= list-type '_' list-type* 'd'? // list of types with optional variadic specifier
type-list ::= list-type '_' list-type* // list of types
type-list ::= empty-list

list-type ::= type identifier? 'z'? // type with optional label and inout convention
list-type ::= type identifier? 'z'? 'd'? // type with optional label, inout convention and variadic specifier

METATYPE-REPR ::= 't' // Thin metatype representation
METATYPE-REPR ::= 'T' // Thick metatype representation
Expand Down
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
4 changes: 2 additions & 2 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1333,10 +1333,10 @@ void ASTMangler::appendTypeList(Type listTy) {
appendType(field.getType());
if (field.hasName())
appendIdentifier(field.getName().str());
if (field.isVararg())
appendOperator("d");
appendListSeparator(firstField);
}
if (tuple->getElements().back().isVararg())
return appendOperator("d");
} else {
appendType(listTy);
appendListSeparator();
Expand Down
7 changes: 3 additions & 4 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,23 +805,22 @@ 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;
do {
firstElem = (popNode(Node::Kind::FirstElementMarker) != nullptr);
NodePointer TupleElmt = createNode(Node::Kind::TupleElement);
addChild(TupleElmt, popNode(Node::Kind::VariadicMarker));
if (NodePointer Ident = popNode(Node::Kind::Identifier)) {
TupleElmt->addChild(createNodeWithAllocatedText(
Node::Kind::TupleElementName, Ident->getText()),
Expand Down
33 changes: 19 additions & 14 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,26 +954,33 @@ 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;
}
case Node::Kind::TupleElement:
if (pointer->getNumChildren() == 1) {
NodePointer type = pointer->getChild(0);
case Node::Kind::TupleElement: {
unsigned Idx = 0;
bool isVariadic = false;
if (pointer->getNumChildren() >= 1 &&
pointer->getFirstChild()->getKind() == Node::Kind::VariadicMarker) {
isVariadic = true;
Idx++;
}
if (pointer->getNumChildren() == Idx + 1) {
NodePointer type = pointer->getChild(Idx);
print(type);
} else if (pointer->getNumChildren() == 2) {
NodePointer id = pointer->getChild(0);
NodePointer type = pointer->getChild(1);
} else if (pointer->getNumChildren() == Idx + 2) {
NodePointer id = pointer->getChild(Idx);
NodePointer type = pointer->getChild(Idx + 1);
print(id);
print(type);
}
if (isVariadic)
Printer << "...";
return;
}
case Node::Kind::TupleElementName:
Printer << pointer->getText() << " : ";
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
3 changes: 3 additions & 0 deletions test/Demangle/Inputs/manglings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,6 @@ _T04main5innerys5Int32Vz_yADctF25closure_with_box_argumentxz_Bi32__lXXTf1nc_n --
_T03foo6testityyyc_yyctF1a1bTf3pfpf_n ---> function signature specialization <Arg[0] = [Constant Propagated Function : a], Arg[1] = [Constant Propagated Function : b]> of foo.testit (() -> (), () -> ()) -> ()
_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]...) -> ()

8 changes: 4 additions & 4 deletions test/SIL/Serialization/deserialize_generic.sil
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import Builtin
import Swift

// CHECK-LABEL: sil @top_level_code
// CHECK: function_ref @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxG_dtF
// CHECK: function_ref @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxGd_tF
sil @top_level_code : $@convention(thin) () -> () {
bb0:
%3 = function_ref @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxG_dtF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T>
%3 = function_ref @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxGd_tF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T>
%0 = tuple () // user: %1
return %0 : $() // id: %1
}

// Make sure the function body is deserialized.
// CHECK-LABEL: @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxG_dtF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T> {
sil @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxG_dtF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T>
// CHECK-LABEL: @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxGd_tF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T> {
sil @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxGd_tF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T>
2 changes: 1 addition & 1 deletion test/SILGen/default_arguments_imported.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func testGizmo(gizmo: Gizmo) {
func testNonnullDictionary(gizmo: Gizmo) {
// CHECK: class_method [volatile] [[SELF:%[0-9]+]] : $Gizmo, #Gizmo.doTheThing!1.foreign
// CHECK-NOT: nilLiteral
// CHECK: function_ref @_T0s10DictionaryVAByxq_GSayx_q_tG17dictionaryLiteral_dtcfC
// CHECK: function_ref @_T0s10DictionaryVAByxq_GSayx_q_tG17dictionaryLiterald_tcfC
gizmo.doTheThing()
} // CHECK: } // end sil function '_T026default_arguments_imported21testNonnullDictionaryySo5GizmoC5gizmo_tF'

Expand Down
2 changes: 1 addition & 1 deletion test/SILGen/errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func test_variadic(_ cat: Cat) throws {
// CHECK-LABEL: sil hidden @_T06errors13test_variadicyAA3CatCKF : $@convention(thin) (@owned Cat) -> @error Error {
// CHECK: bb0([[ARG:%.*]] : $Cat):
// CHECK: debug_value undef : $Error, var, name "$error", argno 2
// CHECK: [[TAKE_FN:%.*]] = function_ref @_T06errors14take_many_catsySayAA3CatCG_dtKF : $@convention(thin) (@owned Array<Cat>) -> @error Error
// CHECK: [[TAKE_FN:%.*]] = function_ref @_T06errors14take_many_catsySayAA3CatCGd_tKF : $@convention(thin) (@owned Array<Cat>) -> @error Error
// CHECK: [[N:%.*]] = integer_literal $Builtin.Word, 4
// CHECK: [[T0:%.*]] = function_ref @_T0s27_allocateUninitializedArray{{.*}}F
// CHECK: [[T1:%.*]] = apply [[T0]]<Cat>([[N]])
Expand Down
8 changes: 7 additions & 1 deletion test/SILGen/mangling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Pročprostěnemluvíčesky() { }

// CHECK-LABEL: sil hidden @_T08mangling9r13757744ySaySiG1x_tF
func r13757744(x x: [Int]) {}
// CHECK-LABEL: sil hidden @_T08mangling9r13757744ySaySiG1x_dtF
// CHECK-LABEL: sil hidden @_T08mangling9r13757744ySaySiG1xd_tF
func r13757744(x x: Int...) {}

// <rdar://problem/13757750> Prefix, postfix, and infix operators need
Expand Down Expand Up @@ -179,3 +179,9 @@ func curry3() -> () throws -> () {
func curry3Throws() throws -> () throws -> () {
return curry1Throws
}

// CHECK-LABEL: sil hidden @_T08mangling14varargsVsArrayySaySiG3arrd_SS1ntF : $@convention(thin) (@owned Array<Int>, @owned String) -> ()
func varargsVsArray(arr: Int..., n: String) { }

// CHECK-LABEL: sil hidden @_T08mangling14varargsVsArrayySaySiG3arr_SS1ntF : $@convention(thin) (@owned Array<Int>, @owned String) -> ()
func varargsVsArray(arr: [Int], n: String) { }
2 changes: 1 addition & 1 deletion test/SILGen/retaining_globals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
print(arr as Any)
print(constArr as Any)

// CHECK: [[PRINT_FUN:%.*]] = function_ref @_T0s5printySayypG_SS9separatorSS10terminatortF : $@convention(thin) (@owned Array<Any>, @owned String, @owned String) -> ()
// CHECK: [[PRINT_FUN:%.*]] = function_ref @_T0s5printySayypGd_SS9separatorSS10terminatortF : $@convention(thin) (@owned Array<Any>, @owned String, @owned String) -> ()
// CHECK: apply [[PRINT_FUN]]({{.*}})
// CHECK: destroy_value [[load_4]]
// CHECK: destroy_value [[load_3]]
Expand Down
2 changes: 1 addition & 1 deletion test/SILGen/scalar_to_tuple_args.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ scalarWithCallerSideDefaults(x)
// CHECK: apply [[TUPLE_WITH_DEFAULTS]]([[X1]], [[X2]], [[DEFAULT_Y]], [[DEFAULT_Z]])
tupleWithDefaults(x: (x,x))

// CHECK: [[VARIADIC_FIRST:%.*]] = function_ref @_T020scalar_to_tuple_args13variadicFirstySaySiG_dtF
// CHECK: [[VARIADIC_FIRST:%.*]] = function_ref @_T020scalar_to_tuple_args13variadicFirstySaySiGd_tF
// CHECK: [[ALLOC_ARRAY:%.*]] = apply {{.*}} -> (@owned Array<τ_0_0>, Builtin.RawPointer)
// CHECK: [[BORROWED_ALLOC_ARRAY:%.*]] = begin_borrow [[ALLOC_ARRAY]]
// CHECK: [[BORROWED_ARRAY:%.*]] = tuple_extract [[BORROWED_ALLOC_ARRAY]] {{.*}}, 0
Expand Down
2 changes: 1 addition & 1 deletion test/Serialization/function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ useNested((raw, raw2), n: raw3)
// SIL: {{%.+}} = apply [[VARIADIC]]({{%.+}}, {{%.+}}) : $@convention(thin) (Double, @owned Array<Int>) -> ()
variadic(x: 2.5, 4, 5)

// SIL: [[VARIADIC:%.+]] = function_ref @_T08def_func9variadic2ySaySiG_Sd1xtF : $@convention(thin) (@owned Array<Int>, Double) -> ()
// SIL: [[VARIADIC:%.+]] = function_ref @_T08def_func9variadic2ySaySiGd_Sd1xtF : $@convention(thin) (@owned Array<Int>, Double) -> ()
variadic2(1, 2, 3, x: 5.0)

// SIL: [[SLICE:%.+]] = function_ref @_T08def_func5sliceySaySiG1x_tF : $@convention(thin) (@owned Array<Int>) -> ()
Expand Down
Loading

0 comments on commit 41ab3fe

Please sign in to comment.