Skip to content

Commit ebee60d

Browse files
authored
Merge pull request #8323 from eeckstein/arg-tuple-mangling
2 parents d73f047 + 11f66f8 commit ebee60d

File tree

6 files changed

+101
-51
lines changed

6 files changed

+101
-51
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ class ASTMangler : public Mangler {
169169

170170
void appendNominalType(const NominalTypeDecl *decl);
171171

172-
void appendFunctionType(AnyFunctionType *fn);
172+
void appendFunctionType(AnyFunctionType *fn, bool forceSingleParam);
173173

174-
void appendFunctionSignature(AnyFunctionType *fn);
174+
void appendFunctionSignature(AnyFunctionType *fn, bool forceSingleParam);
175175

176-
void appendParams(Type ParamsTy);
176+
void appendParams(Type ParamsTy, bool forceSingleParam);
177177

178178
void appendTypeList(Type listTy);
179179

@@ -205,7 +205,7 @@ class ASTMangler : public Mangler {
205205
ArrayRef<Requirement> &requirements,
206206
SmallVectorImpl<Requirement> &requirementsBuf);
207207

208-
void appendDeclType(const ValueDecl *decl);
208+
void appendDeclType(const ValueDecl *decl, bool isFunctionMangling = false);
209209

210210
bool tryAppendStandardSubstitution(const NominalTypeDecl *type);
211211

lib/AST/ASTMangler.cpp

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ void ASTMangler::appendType(Type type) {
708708

709709
case TypeKind::GenericFunction: {
710710
auto genFunc = cast<GenericFunctionType>(tybase);
711-
appendFunctionType(genFunc);
711+
appendFunctionType(genFunc, /*forceSingleParam*/ false);
712712
appendGenericSignature(genFunc->getGenericSignature());
713713
appendOperator("u");
714714
return;
@@ -751,7 +751,7 @@ void ASTMangler::appendType(Type type) {
751751
}
752752

753753
case TypeKind::Function:
754-
appendFunctionType(cast<FunctionType>(tybase));
754+
appendFunctionType(cast<FunctionType>(tybase), /*forceSingleParam*/ false);
755755
return;
756756

757757
case TypeKind::SILBox: {
@@ -1243,11 +1243,12 @@ void ASTMangler::appendNominalType(const NominalTypeDecl *decl) {
12431243
addSubstitution(key);
12441244
}
12451245

1246-
void ASTMangler::appendFunctionType(AnyFunctionType *fn) {
1246+
void ASTMangler::appendFunctionType(AnyFunctionType *fn,
1247+
bool forceSingleParam) {
12471248
assert((DWARFMangling || fn->isCanonical()) &&
12481249
"expecting canonical types when not mangling for the debugger");
12491250

1250-
appendFunctionSignature(fn);
1251+
appendFunctionSignature(fn, forceSingleParam);
12511252

12521253
// Note that we do not currently use thin representations in the AST
12531254
// for the types of function decls. This may need to change at some
@@ -1274,20 +1275,34 @@ void ASTMangler::appendFunctionType(AnyFunctionType *fn) {
12741275
}
12751276
}
12761277

1277-
void ASTMangler::appendFunctionSignature(AnyFunctionType *fn) {
1278-
appendParams(fn->getResult());
1279-
appendParams(fn->getInput());
1278+
void ASTMangler::appendFunctionSignature(AnyFunctionType *fn,
1279+
bool forceSingleParam) {
1280+
appendParams(fn->getResult(), /*forceSingleParam*/ false);
1281+
appendParams(fn->getInput(), forceSingleParam);
12801282
if (fn->throws())
12811283
appendOperator("K");
12821284
}
12831285

1284-
void ASTMangler::appendParams(Type ParamsTy) {
1285-
TupleType *Tuple = ParamsTy->getAs<TupleType>();
1286-
if (Tuple && Tuple->getNumElements() == 0) {
1287-
appendOperator("y");
1288-
} else {
1289-
appendType(ParamsTy);
1286+
void ASTMangler::appendParams(Type ParamsTy, bool forceSingleParam) {
1287+
if (TupleType *Tuple = ParamsTy->getAs<TupleType>()) {
1288+
if (Tuple->getNumElements() == 0) {
1289+
if (forceSingleParam) {
1290+
// A tuple containing a single empty tuple.
1291+
appendOperator("t");
1292+
appendListSeparator();
1293+
appendOperator("t");
1294+
}
1295+
appendOperator("y");
1296+
return;
1297+
}
1298+
if (forceSingleParam && Tuple->getNumElements() > 1) {
1299+
appendType(ParamsTy);
1300+
appendListSeparator();
1301+
appendOperator("t");
1302+
return;
1303+
}
12901304
}
1305+
appendType(ParamsTy);
12911306
}
12921307

12931308
void ASTMangler::appendTypeList(Type listTy) {
@@ -1599,7 +1614,7 @@ CanType ASTMangler::getDeclTypeForMangling(
15991614
return type->getCanonicalType();
16001615
}
16011616

1602-
void ASTMangler::appendDeclType(const ValueDecl *decl) {
1617+
void ASTMangler::appendDeclType(const ValueDecl *decl, bool isFunctionMangling) {
16031618
ArrayRef<GenericTypeParamType *> genericParams;
16041619
unsigned initialParamDepth;
16051620
ArrayRef<Requirement> requirements;
@@ -1608,13 +1623,32 @@ void ASTMangler::appendDeclType(const ValueDecl *decl) {
16081623
auto type = getDeclTypeForMangling(decl,
16091624
genericParams, initialParamDepth,
16101625
requirements, requirementsBuf);
1611-
appendType(type);
1626+
1627+
if (AnyFunctionType *FuncTy = type->getAs<AnyFunctionType>()) {
1628+
bool forceSingleParam = false;
1629+
if (const auto *FDecl = dyn_cast<AbstractFunctionDecl>(decl)) {
1630+
unsigned PListIdx = isMethodDecl(decl) ? 1 : 0;
1631+
if (PListIdx < FDecl->getNumParameterLists()) {
1632+
const ParameterList *Params = FDecl->getParameterList(PListIdx);
1633+
forceSingleParam = (Params->size() == 1);
1634+
}
1635+
}
1636+
if (isFunctionMangling) {
1637+
appendFunctionSignature(FuncTy, forceSingleParam);
1638+
} else {
1639+
appendFunctionType(FuncTy, forceSingleParam);
1640+
}
1641+
} else {
1642+
appendType(type);
1643+
}
16121644

16131645
// Mangle the generic signature, if any.
16141646
if (!genericParams.empty() || !requirements.empty()) {
16151647
appendGenericSignatureParts(genericParams, initialParamDepth,
16161648
requirements);
1617-
appendOperator("u");
1649+
// The 'F' function mangling doesn't need a 'u' for its generic signature.
1650+
if (!isFunctionMangling)
1651+
appendOperator("u");
16181652
}
16191653
}
16201654

@@ -1728,28 +1762,7 @@ void ASTMangler::appendEntity(const ValueDecl *decl) {
17281762

17291763
appendContextOf(decl);
17301764
appendDeclName(decl);
1731-
1732-
ArrayRef<GenericTypeParamType *> genericParams;
1733-
unsigned initialParamDepth = 0;
1734-
ArrayRef<Requirement> requirements;
1735-
SmallVector<Requirement, 4> requirementsBuf;
1736-
Mod = decl->getModuleContext();
1737-
auto type = getDeclTypeForMangling(decl,
1738-
genericParams, initialParamDepth,
1739-
requirements, requirementsBuf);
1740-
1741-
if (AnyFunctionType *FuncTy = type->getAs<AnyFunctionType>()) {
1742-
appendFunctionSignature(FuncTy);
1743-
} else {
1744-
// In case SourceKit comes up with an invalid (Error) function type.
1745-
appendType(type);
1746-
}
1747-
1748-
// Mangle the generic signature, if any.
1749-
if (!genericParams.empty() || !requirements.empty()) {
1750-
appendGenericSignatureParts(genericParams, initialParamDepth,
1751-
requirements);
1752-
}
1765+
appendDeclType(decl, /*isFunctionMangling*/ true);
17531766
appendOperator("F");
17541767
if (decl->isStatic())
17551768
appendOperator("Z");

test/IRGen/generic_tuples.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ func lump3<T>(_ x: T) -> (T, T, T) { return (x,x,x) }
8383
// CHECK: define hidden swiftcc i64 @_T014generic_tuples5lump4x_SixtxlF(%swift.opaque* noalias nocapture, %swift.opaque* noalias nocapture, %swift.opaque* noalias nocapture, %swift.type* %T)
8484
func lump4<T>(_ x: T) -> (T, Int, T) { return (x,0,x) }
8585

86-
// CHECK: define hidden swiftcc i64 @_T014generic_tuples6unlumpS2i_xxtlF(i64, %swift.opaque* noalias nocapture, %swift.opaque* noalias nocapture, %swift.type* %T)
86+
// CHECK: define hidden swiftcc i64 @_T014generic_tuples6unlumpS2i_xxt_tlF(i64, %swift.opaque* noalias nocapture, %swift.opaque* noalias nocapture, %swift.type* %T)
8787
func unlump<T>(_ x: (Int, T, T)) -> Int { return x.0 }
88-
// CHECK: define hidden swiftcc void @_T014generic_tuples7unlump1xSi_xxtlF(%swift.opaque* noalias nocapture sret, i64, %swift.opaque* noalias nocapture, %swift.opaque* noalias nocapture, %swift.type* %T)
88+
// CHECK: define hidden swiftcc void @_T014generic_tuples7unlump1xSi_xxt_tlF(%swift.opaque* noalias nocapture sret, i64, %swift.opaque* noalias nocapture, %swift.opaque* noalias nocapture, %swift.type* %T)
8989
func unlump1<T>(_ x: (Int, T, T)) -> T { return x.1 }
90-
// CHECK: define hidden swiftcc void @_T014generic_tuples7unlump2xx_SixtlF(%swift.opaque* noalias nocapture sret, %swift.opaque* noalias nocapture, i64, %swift.opaque* noalias nocapture, %swift.type* %T)
90+
// CHECK: define hidden swiftcc void @_T014generic_tuples7unlump2xx_Sixt_tlF(%swift.opaque* noalias nocapture sret, %swift.opaque* noalias nocapture, i64, %swift.opaque* noalias nocapture, %swift.type* %T)
9191
func unlump2<T>(_ x: (T, Int, T)) -> T { return x.0 }
92-
// CHECK: define hidden swiftcc i64 @_T014generic_tuples7unlump3Six_SixtlF(%swift.opaque* noalias nocapture, i64, %swift.opaque* noalias nocapture, %swift.type* %T)
92+
// CHECK: define hidden swiftcc i64 @_T014generic_tuples7unlump3Six_Sixt_tlF(%swift.opaque* noalias nocapture, i64, %swift.opaque* noalias nocapture, %swift.type* %T)
9393
func unlump3<T>(_ x: (T, Int, T)) -> Int { return x.1 }
9494

9595

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-swift-frontend -parse-as-library -module-name=test -emit-silgen -primary-file %s | %FileCheck %s
2+
3+
// Check if we mangle the following constructors and functions correctly.
4+
5+
public struct Pair {
6+
// CHECK: sil @_T04test4PairVACSi_SitcfC :
7+
public init(_ a: Int, _ b: Int) {
8+
}
9+
10+
// CHECK: sil @_T04test4PairVACSi_Sit_tcfC :
11+
public init(_ t: (Int, Int)) {
12+
}
13+
14+
// CHECK: sil @_T04test4PairVAAySi_SitF :
15+
public func test(_ a: Int, _ b: Int) {
16+
}
17+
18+
// CHECK: sil @_T04test4PairVAAySi_Sit_tF :
19+
public func test(_ t: (Int, Int)) {
20+
}
21+
}
22+
23+
// CHECK: sil @_T04testAAySi_SitF :
24+
public func test(_ a: Int, _ b: Int) {
25+
}
26+
27+
// CHECK: sil @_T04testAAySi_Sit_tF :
28+
public func test(_ t: (Int, Int)) {
29+
}
30+
31+
// CHECK: sil @_T04test3fooyt_tyF :
32+
public func foo(_: ()) {
33+
}
34+
35+
// CHECK: sil @_T04test3fooyyF :
36+
public func foo() {
37+
}

test/SILGen/mangling.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ postfix func +- <T>(a: T) {}
4040
// CHECK-LABEL: sil hidden @_T08mangling2psoiyx_xtlF
4141
func +- <T>(a: T, b: T) {}
4242

43-
// CHECK-LABEL: sil hidden @_T08mangling2psopyx1a_x1btlF
43+
// CHECK-LABEL: sil hidden @_T08mangling2psopyx1a_x1bt_tlF
4444
prefix func +- <T>(_: (a: T, b: T)) {}
45-
// CHECK-LABEL: sil hidden @_T08mangling2psoPyx1a_x1btlF
45+
// CHECK-LABEL: sil hidden @_T08mangling2psoPyx1a_x1bt_tlF
4646
postfix func +- <T>(_: (a: T, b: T)) {}
4747

4848
infix operator «+» {}

test/SILGen/opaque_values_silgen.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func s030______assigninout<T>(_ a: inout T, _ b: T) {
195195

196196
// Test that we no longer use copy_addr or tuple_element_addr when copy by value is possible
197197
// ---
198-
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s040___tupleReturnIntS2i_xtlF : $@convention(thin) <T> (Int, @in T) -> Int {
198+
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s040___tupleReturnIntS2i_xt_tlF : $@convention(thin) <T> (Int, @in T) -> Int {
199199
// CHECK: bb0([[ARG0:%.*]] : $Int, [[ARG1:%.*]] : $T):
200200
// CHECK: [[TPL:%.*]] = tuple ([[ARG0]] : $Int, [[ARG1]] : $T)
201201
// CHECK: [[BORROWED_ARG1:%.*]] = begin_borrow [[TPL]] : $(Int, T)
@@ -210,7 +210,7 @@ func s030______assigninout<T>(_ a: inout T, _ b: T) {
210210
// CHECK: end_borrow [[BORROWED_ARG1]] from [[TPL]] : $(Int, T), $(Int, T)
211211
// CHECK: destroy_value [[TPL]] : $(Int, T)
212212
// CHECK: return [[INT]]
213-
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s040___tupleReturnIntS2i_xtlF'
213+
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s040___tupleReturnIntS2i_xt_tlF'
214214
func s040___tupleReturnInt<T>(_ x: (Int, T)) -> Int {
215215
let y = x.0
216216
return y
@@ -1104,4 +1104,4 @@ extension Dictionary {
11041104
// CHECK: [[APPLYARG:%.*]] = apply [[ARG2]]([[INIT_OPAQUE]]) : $@callee_owned (@in Any) -> ()
11051105
// CHECK: dealloc_stack [[ASTACK]] : $*Any
11061106
// CHECK: return %{{.*}} : $()
1107-
// CHECK-LABEL: } // end sil function '_T0ypIxi_S2iIxyy_TR'
1107+
// CHECK-LABEL: } // end sil function '_T0ypIxi_S2iIxyy_TR'

0 commit comments

Comments
 (0)