@@ -1331,8 +1331,8 @@ static SILFunctionType *
1331
1331
emitObjCThunkArguments (SILGenFunction &SGF, SILLocation loc, SILDeclRef thunk,
1332
1332
SmallVectorImpl<SILValue> &args,
1333
1333
SILValue &foreignErrorSlot, SILValue &foreignAsyncSlot,
1334
- std::optional<ForeignErrorConvention> & foreignError,
1335
- std::optional<ForeignAsyncConvention> & foreignAsync,
1334
+ std::optional<ForeignErrorConvention> foreignError,
1335
+ std::optional<ForeignAsyncConvention> foreignAsync,
1336
1336
CanType &nativeFormalResultTy,
1337
1337
CanType &bridgedFormalResultTy) {
1338
1338
SILDeclRef native = thunk.asForeign (false );
@@ -1355,20 +1355,6 @@ emitObjCThunkArguments(SILGenFunction &SGF, SILLocation loc, SILDeclRef thunk,
1355
1355
SmallVector<ManagedValue, 8 > bridgedArgs;
1356
1356
bridgedArgs.reserve (objcFnTy->getParameters ().size ());
1357
1357
1358
- // Find the foreign error and async conventions if we have one.
1359
- if (thunk.hasDecl ()) {
1360
- if (auto func = dyn_cast<AbstractFunctionDecl>(thunk.getDecl ())) {
1361
- foreignError = func->getForeignErrorConvention ();
1362
- foreignAsync = func->getForeignAsyncConvention ();
1363
- }
1364
- }
1365
-
1366
- // We don't know what to do with indirect results from the Objective-C side.
1367
- assert ((SGF.F .getRepresentation () ==
1368
- SILFunctionType::Representation::CFunctionPointer ||
1369
- objcFnTy->getNumIndirectFormalResults () == 0 ) &&
1370
- " Objective-C methods cannot have indirect results" );
1371
-
1372
1358
auto bridgedFormalTypes = getParameterTypes (objcFormalFnTy.getParams ());
1373
1359
bridgedFormalResultTy = objcFormalFnTy.getResult ();
1374
1360
@@ -1618,27 +1604,40 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
1618
1604
}
1619
1605
}
1620
1606
1607
+ std::optional<ForeignErrorConvention> foreignError;
1608
+ std::optional<ForeignAsyncConvention> foreignAsync;
1609
+
1610
+ // Find the foreign error and async conventions if we have one.
1611
+ if (thunk.hasDecl ()) {
1612
+ if (auto func = dyn_cast<AbstractFunctionDecl>(thunk.getDecl ())) {
1613
+ foreignError = func->getForeignErrorConvention ();
1614
+ foreignAsync = func->getForeignAsyncConvention ();
1615
+ }
1616
+ }
1617
+
1618
+ // If we are bridging a Swift method with Any return value(s), create a
1619
+ // stack allocation to hold the result(s), since Any is address-only.
1621
1620
SmallVector<SILValue, 4 > args;
1621
+ SILFunctionConventions funcConv = F.getConventions ();
1622
+ bool needsBridging = true ;
1622
1623
if (substConv.hasIndirectSILResults ()) {
1623
- if (F.getRepresentation () ==
1624
- SILFunctionType::Representation::CFunctionPointer) {
1625
- // Pass the result address of the thunk to the native function.
1626
- auto resultTy =
1627
- F.getConventions ().getSingleSILResultType (getTypeExpansionContext ());
1628
- assert (resultTy ==
1629
- substConv.getSingleSILResultType (getTypeExpansionContext ()) &&
1630
- " result type mismatch" );
1631
- args.push_back (F.begin ()->createFunctionArgument (resultTy));
1632
- } else {
1633
- // If we are bridging a Swift method with Any return value(s), create a
1634
- // stack allocation to hold the result(s), since Any is address-only.
1635
- for (auto result : substConv.getResults ()) {
1636
- if (!substConv.isSILIndirect (result)) {
1637
- continue ;
1638
- }
1639
- args.push_back (emitTemporaryAllocation (
1640
- loc, substConv.getSILType (result, getTypeExpansionContext ())));
1624
+ for (auto result : substConv.getResults ()) {
1625
+ if (!substConv.isSILIndirect (result)) {
1626
+ continue ;
1627
+ }
1628
+
1629
+ if (!foreignAsync && funcConv.hasIndirectSILResults ()) {
1630
+ auto resultTy =
1631
+ funcConv.getSingleSILResultType (getTypeExpansionContext ());
1632
+ assert (substConv.getSingleSILResultType (getTypeExpansionContext ()) ==
1633
+ resultTy);
1634
+ args.push_back (F.begin ()->createFunctionArgument (resultTy));
1635
+ needsBridging = false ;
1636
+ break ;
1641
1637
}
1638
+
1639
+ args.push_back (emitTemporaryAllocation (
1640
+ loc, substConv.getSILType (result, getTypeExpansionContext ())));
1642
1641
}
1643
1642
}
1644
1643
@@ -1648,8 +1647,6 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
1648
1647
Scope argScope (Cleanups, CleanupLocation (loc));
1649
1648
1650
1649
// Bridge the arguments.
1651
- std::optional<ForeignErrorConvention> foreignError;
1652
- std::optional<ForeignAsyncConvention> foreignAsync;
1653
1650
SILValue foreignErrorSlot;
1654
1651
SILValue foreignAsyncSlot;
1655
1652
CanType nativeFormalResultType, bridgedFormalResultType;
@@ -1876,7 +1873,7 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
1876
1873
if (foreignAsync) {
1877
1874
result = passResultToCompletionHandler (result);
1878
1875
} else {
1879
- if (F. getRepresentation () != SILFunctionType::Representation::CFunctionPointer ) {
1876
+ if (needsBridging ) {
1880
1877
if (substConv.hasIndirectSILResults ()) {
1881
1878
assert (substTy->getNumResults () == 1 );
1882
1879
result = args[0 ];
0 commit comments