@@ -900,7 +900,7 @@ void ASTMangler::appendType(Type type) {
900
900
901
901
case TypeKind::GenericFunction: {
902
902
auto genFunc = cast<GenericFunctionType>(tybase);
903
- appendFunctionType (genFunc, /* forceSingleParam */ false );
903
+ appendFunctionType (genFunc);
904
904
appendGenericSignature (genFunc->getGenericSignature ());
905
905
appendOperator (" u" );
906
906
return ;
@@ -943,7 +943,7 @@ void ASTMangler::appendType(Type type) {
943
943
}
944
944
945
945
case TypeKind::Function:
946
- appendFunctionType (cast<FunctionType>(tybase), /* forceSingleParam */ false );
946
+ appendFunctionType (cast<FunctionType>(tybase));
947
947
return ;
948
948
949
949
case TypeKind::SILBox: {
@@ -1438,12 +1438,11 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
1438
1438
addSubstitution (key.getPointer ());
1439
1439
}
1440
1440
1441
- void ASTMangler::appendFunctionType (AnyFunctionType *fn,
1442
- bool forceSingleParam) {
1441
+ void ASTMangler::appendFunctionType (AnyFunctionType *fn) {
1443
1442
assert ((DWARFMangling || fn->isCanonical ()) &&
1444
1443
" expecting canonical types when not mangling for the debugger" );
1445
1444
1446
- appendFunctionSignature (fn, forceSingleParam );
1445
+ appendFunctionSignature (fn);
1447
1446
1448
1447
// Note that we do not currently use thin representations in the AST
1449
1448
// for the types of function decls. This may need to change at some
@@ -1470,48 +1469,66 @@ void ASTMangler::appendFunctionType(AnyFunctionType *fn,
1470
1469
}
1471
1470
}
1472
1471
1473
- void ASTMangler::appendFunctionSignature (AnyFunctionType *fn,
1474
- bool forceSingleParam) {
1475
- appendParams (fn->getResult (), /* forceSingleParam*/ false );
1476
- appendParams (fn->getInput (), forceSingleParam);
1472
+ void ASTMangler::appendFunctionSignature (AnyFunctionType *fn) {
1473
+ appendFunctionResultType (fn->getResult ());
1474
+ appendFunctionInputType (fn->getParams ());
1477
1475
if (fn->throws ())
1478
1476
appendOperator (" K" );
1479
1477
}
1480
1478
1481
- void ASTMangler::appendParams (Type ParamsTy, bool forceSingleParam) {
1482
- if (TupleType *Tuple = ParamsTy->getAs <TupleType>()) {
1483
- if (Tuple->getNumElements () == 0 ) {
1484
- if (forceSingleParam) {
1485
- // A tuple containing a single empty tuple.
1486
- appendOperator (" y" );
1487
- appendOperator (" t" );
1488
- appendListSeparator ();
1489
- appendOperator (" t" );
1490
- } else {
1491
- appendOperator (" y" );
1492
- }
1493
- return ;
1479
+ void ASTMangler::appendFunctionInputType (
1480
+ ArrayRef<AnyFunctionType::Param> params) {
1481
+ auto getParamType = [](const AnyFunctionType::Param ¶m) -> Type {
1482
+ auto type = param.getType ();
1483
+
1484
+ // FIXME: Change mangling for variadic parameters so
1485
+ // the enclosing array type is not required.
1486
+ if (param.isVariadic ()) {
1487
+ auto *arrayDecl = type->getASTContext ().getArrayDecl ();
1488
+ assert (arrayDecl);
1489
+ return BoundGenericType::get (arrayDecl, Type (), {type});
1494
1490
}
1495
- if (forceSingleParam && Tuple->getNumElements () > 1 ) {
1496
- auto flags = ParameterTypeFlags ();
1497
- if (ParenType *Paren = dyn_cast<ParenType>(ParamsTy.getPointer ())) {
1498
- ParamsTy = Paren->getUnderlyingType ();
1499
- flags = Paren->getParameterFlags ();
1500
- }
1501
1491
1502
- appendType (ParamsTy);
1503
- if (flags.isShared ())
1504
- appendOperator (" h" );
1505
- appendListSeparator ();
1506
- appendOperator (" t" );
1507
- return ;
1492
+ return type;
1493
+ };
1494
+
1495
+ switch (params.size ()) {
1496
+ case 0 :
1497
+ appendOperator (" y" );
1498
+ break ;
1499
+
1500
+ case 1 : {
1501
+ const auto ¶m = params.front ();
1502
+ auto type = param.getType ();
1503
+
1504
+ // If this is just a single parenthesized type,
1505
+ // to save space in the mangled name, let's encode
1506
+ // it as a single type dropping sugar.
1507
+ if (!param.hasLabel () && !param.isVariadic () &&
1508
+ !isa<TupleType>(type.getPointer ())) {
1509
+ appendType (type);
1510
+ break ;
1508
1511
}
1512
+
1513
+ // If this is a tuple type with a single labeled element
1514
+ // let's handle it as a general case.
1515
+ LLVM_FALLTHROUGH;
1509
1516
}
1510
1517
1511
- if (ParenType *Paren = dyn_cast<ParenType>(ParamsTy.getPointer ()))
1512
- ParamsTy = Paren->getUnderlyingType ();
1518
+ default :
1519
+ bool isFirstParam = true ;
1520
+ for (auto ¶m : params) {
1521
+ appendTypeListElement (param.getLabel (), getParamType (param),
1522
+ param.getParameterFlags ());
1523
+ appendListSeparator (isFirstParam);
1524
+ }
1525
+ appendOperator (" t" );
1526
+ break ;
1527
+ }
1528
+ }
1513
1529
1514
- appendType (ParamsTy);
1530
+ void ASTMangler::appendFunctionResultType (Type resultType) {
1531
+ return resultType->isVoid () ? appendOperator (" y" ) : appendType (resultType);
1515
1532
}
1516
1533
1517
1534
void ASTMangler::appendTypeList (Type listTy) {
@@ -1520,15 +1537,8 @@ void ASTMangler::appendTypeList(Type listTy) {
1520
1537
return appendOperator (" y" );
1521
1538
bool firstField = true ;
1522
1539
for (auto &field : tuple->getElements ()) {
1523
- appendType (field.getType ()->getInOutObjectType ());
1524
- if (field.isInOut ())
1525
- appendOperator (" z" );
1526
- if (field.getParameterFlags ().isShared ())
1527
- appendOperator (" h" );
1528
- if (field.hasName ())
1529
- appendIdentifier (field.getName ().str ());
1530
- if (field.isVararg ())
1531
- appendOperator (" d" );
1540
+ appendTypeListElement (field.getName (), field.getType (),
1541
+ field.getParameterFlags ());
1532
1542
appendListSeparator (firstField);
1533
1543
}
1534
1544
} else {
@@ -1537,6 +1547,19 @@ void ASTMangler::appendTypeList(Type listTy) {
1537
1547
}
1538
1548
}
1539
1549
1550
+ void ASTMangler::appendTypeListElement (Identifier name, Type elementType,
1551
+ ParameterTypeFlags flags) {
1552
+ appendType (elementType->getInOutObjectType ());
1553
+ if (flags.isInOut ())
1554
+ appendOperator (" z" );
1555
+ if (flags.isShared ())
1556
+ appendOperator (" h" );
1557
+ if (!name.empty ())
1558
+ appendIdentifier (name.str ());
1559
+ if (flags.isVariadic ())
1560
+ appendOperator (" d" );
1561
+ }
1562
+
1540
1563
bool ASTMangler::appendGenericSignature (const GenericSignature *sig,
1541
1564
GenericSignature *contextSig) {
1542
1565
auto canSig = sig->getCanonicalSignature ();
@@ -1828,23 +1851,10 @@ void ASTMangler::appendDeclType(const ValueDecl *decl, bool isFunctionMangling)
1828
1851
auto type = getDeclTypeForMangling (decl, genericSig, parentGenericSig);
1829
1852
1830
1853
if (AnyFunctionType *FuncTy = type->getAs <AnyFunctionType>()) {
1831
-
1832
- const ParameterList *Params = nullptr ;
1833
- if (const auto *FDecl = dyn_cast<AbstractFunctionDecl>(decl)) {
1834
- unsigned PListIdx = isMethodDecl (decl) ? 1 : 0 ;
1835
- if (PListIdx < FDecl->getNumParameterLists ()) {
1836
- Params = FDecl->getParameterList (PListIdx);
1837
- }
1838
- } else if (const auto *SDecl = dyn_cast<SubscriptDecl>(decl)) {
1839
- Params = SDecl->getIndices ();
1840
- }
1841
- bool forceSingleParam = Params && (Params->size () == 1 );
1842
-
1843
-
1844
1854
if (isFunctionMangling) {
1845
- appendFunctionSignature (FuncTy, forceSingleParam );
1855
+ appendFunctionSignature (FuncTy);
1846
1856
} else {
1847
- appendFunctionType (FuncTy, forceSingleParam );
1857
+ appendFunctionType (FuncTy);
1848
1858
}
1849
1859
} else {
1850
1860
appendType (type);
0 commit comments