Skip to content

Commit 327017a

Browse files
committed
Mangling: support mangling of varargs even if it's not the last argument.
Example: func a(arr: Int..., n: String) fixes SR-1076
1 parent f951330 commit 327017a

File tree

13 files changed

+41
-23
lines changed

13 files changed

+41
-23
lines changed

docs/ABI.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,10 +1031,10 @@ Types
10311031

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

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

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

10391039
METATYPE-REPR ::= 't' // Thin metatype representation
10401040
METATYPE-REPR ::= 'T' // Thick metatype representation

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,10 +1333,10 @@ void ASTMangler::appendTypeList(Type listTy) {
13331333
appendType(field.getType());
13341334
if (field.hasName())
13351335
appendIdentifier(field.getName().str());
1336+
if (field.isVararg())
1337+
appendOperator("d");
13361338
appendListSeparator(firstField);
13371339
}
1338-
if (tuple->getElements().back().isVararg())
1339-
return appendOperator("d");
13401340
} else {
13411341
appendType(listTy);
13421342
appendListSeparator();

lib/Demangling/Demangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ NodePointer Demangler::popTuple() {
822822
do {
823823
firstElem = (popNode(Node::Kind::FirstElementMarker) != nullptr);
824824
NodePointer TupleElmt = createNode(Node::Kind::TupleElement);
825+
addChild(TupleElmt, popNode(Node::Kind::VariadicMarker));
825826
if (NodePointer Ident = popNode(Node::Kind::Identifier)) {
826827
TupleElmt->addChild(createNodeWithAllocatedText(
827828
Node::Kind::TupleElementName, Ident->getText()),

lib/Demangling/NodePrinter.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -965,17 +965,27 @@ void NodePrinter::print(NodePointer pointer, bool asContext, bool suppressType)
965965
Printer << ")";
966966
return;
967967
}
968-
case Node::Kind::TupleElement:
969-
if (pointer->getNumChildren() == 1) {
970-
NodePointer type = pointer->getChild(0);
968+
case Node::Kind::TupleElement: {
969+
unsigned Idx = 0;
970+
bool isVariadic = false;
971+
if (pointer->getNumChildren() >= 1 &&
972+
pointer->getFirstChild()->getKind() == Node::Kind::VariadicMarker) {
973+
isVariadic = true;
974+
Idx++;
975+
}
976+
if (pointer->getNumChildren() == Idx + 1) {
977+
NodePointer type = pointer->getChild(Idx);
971978
print(type);
972-
} else if (pointer->getNumChildren() == 2) {
973-
NodePointer id = pointer->getChild(0);
974-
NodePointer type = pointer->getChild(1);
979+
} else if (pointer->getNumChildren() == Idx + 2) {
980+
NodePointer id = pointer->getChild(Idx);
981+
NodePointer type = pointer->getChild(Idx + 1);
975982
print(id);
976983
print(type);
977984
}
985+
if (isVariadic)
986+
Printer << "...";
978987
return;
988+
}
979989
case Node::Kind::TupleElementName:
980990
Printer << pointer->getText() << " : ";
981991
return;

test/Demangle/Inputs/manglings.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,4 @@ _T04main5innerys5Int32Vz_yADctF25closure_with_box_argumentxz_Bi32__lXXTf1nc_n --
246246
_T03foo6testityyyc_yyctF1a1bTf3pfpf_n ---> function signature specialization <Arg[0] = [Constant Propagated Function : a], Arg[1] = [Constant Propagated Function : b]> of foo.testit (() -> (), () -> ()) -> ()
247247
_SocketJoinOrLeaveMulticast ---> _SocketJoinOrLeaveMulticast
248248
_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
249+
_T08mangling14varargsVsArrayySaySiG3arrd_SS1ntF ---> mangling.varargsVsArray (arr : [Swift.Int]..., n : Swift.String) -> ()

test/SIL/Serialization/deserialize_generic.sil

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import Builtin
1313
import Swift
1414

1515
// CHECK-LABEL: sil @top_level_code
16-
// CHECK: function_ref @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxG_dtF
16+
// CHECK: function_ref @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxGd_tF
1717
sil @top_level_code : $@convention(thin) () -> () {
1818
bb0:
19-
%3 = function_ref @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxG_dtF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T>
19+
%3 = function_ref @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxGd_tF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T>
2020
%0 = tuple () // user: %1
2121
return %0 : $() // id: %1
2222
}
2323

2424
// Make sure the function body is deserialized.
25-
// CHECK-LABEL: @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxG_dtF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T> {
26-
sil @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxG_dtF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T>
25+
// CHECK-LABEL: @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxGd_tF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T> {
26+
sil @_T011def_generic1AC23convertFromArrayLiteralACyxGSayxGd_tF : $@convention(method) <T> (@owned Array<T>, @guaranteed A<T>) -> @owned A<T>

test/SILGen/default_arguments_imported.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func testGizmo(gizmo: Gizmo) {
1717
func testNonnullDictionary(gizmo: Gizmo) {
1818
// CHECK: class_method [volatile] [[SELF:%[0-9]+]] : $Gizmo, #Gizmo.doTheThing!1.foreign
1919
// CHECK-NOT: nilLiteral
20-
// CHECK: function_ref @_T0s10DictionaryVAByxq_GSayx_q_tG17dictionaryLiteral_dtcfC
20+
// CHECK: function_ref @_T0s10DictionaryVAByxq_GSayx_q_tG17dictionaryLiterald_tcfC
2121
gizmo.doTheThing()
2222
} // CHECK: } // end sil function '_T026default_arguments_imported21testNonnullDictionaryySo5GizmoC5gizmo_tF'
2323

test/SILGen/errors.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ func test_variadic(_ cat: Cat) throws {
410410
// CHECK-LABEL: sil hidden @_T06errors13test_variadicyAA3CatCKF : $@convention(thin) (@owned Cat) -> @error Error {
411411
// CHECK: bb0([[ARG:%.*]] : $Cat):
412412
// CHECK: debug_value undef : $Error, var, name "$error", argno 2
413-
// CHECK: [[TAKE_FN:%.*]] = function_ref @_T06errors14take_many_catsySayAA3CatCG_dtKF : $@convention(thin) (@owned Array<Cat>) -> @error Error
413+
// CHECK: [[TAKE_FN:%.*]] = function_ref @_T06errors14take_many_catsySayAA3CatCGd_tKF : $@convention(thin) (@owned Array<Cat>) -> @error Error
414414
// CHECK: [[N:%.*]] = integer_literal $Builtin.Word, 4
415415
// CHECK: [[T0:%.*]] = function_ref @_T0s27_allocateUninitializedArray{{.*}}F
416416
// CHECK: [[T1:%.*]] = apply [[T0]]<Cat>([[N]])

test/SILGen/mangling.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Pročprostěnemluvíčesky() { }
2222

2323
// CHECK-LABEL: sil hidden @_T08mangling9r13757744ySaySiG1x_tF
2424
func r13757744(x x: [Int]) {}
25-
// CHECK-LABEL: sil hidden @_T08mangling9r13757744ySaySiG1x_dtF
25+
// CHECK-LABEL: sil hidden @_T08mangling9r13757744ySaySiG1xd_tF
2626
func r13757744(x x: Int...) {}
2727

2828
// <rdar://problem/13757750> Prefix, postfix, and infix operators need
@@ -179,3 +179,9 @@ func curry3() -> () throws -> () {
179179
func curry3Throws() throws -> () throws -> () {
180180
return curry1Throws
181181
}
182+
183+
// CHECK-LABEL: sil hidden @_T08mangling14varargsVsArrayySaySiG3arrd_SS1ntF : $@convention(thin) (@owned Array<Int>, @owned String) -> ()
184+
func varargsVsArray(arr: Int..., n: String) { }
185+
186+
// CHECK-LABEL: sil hidden @_T08mangling14varargsVsArrayySaySiG3arr_SS1ntF : $@convention(thin) (@owned Array<Int>, @owned String) -> ()
187+
func varargsVsArray(arr: [Int], n: String) { }

test/SILGen/retaining_globals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252
print(arr as Any)
5353
print(constArr as Any)
5454

55-
// CHECK: [[PRINT_FUN:%.*]] = function_ref @_T0s5printySayypG_SS9separatorSS10terminatortF : $@convention(thin) (@owned Array<Any>, @owned String, @owned String) -> ()
55+
// CHECK: [[PRINT_FUN:%.*]] = function_ref @_T0s5printySayypGd_SS9separatorSS10terminatortF : $@convention(thin) (@owned Array<Any>, @owned String, @owned String) -> ()
5656
// CHECK: apply [[PRINT_FUN]]({{.*}})
5757
// CHECK: destroy_value [[load_4]]
5858
// CHECK: destroy_value [[load_3]]

0 commit comments

Comments
 (0)