File: SILGenBridging.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (2318 lines) | stat: -rw-r--r-- 95,308 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
//===--- SILGenBridging.cpp - SILGen for bridging to Clang ASTs -----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "ArgumentScope.h"
#include "Callee.h"
#include "ExecutorBreadcrumb.h"
#include "RValue.h"
#include "ResultPlan.h"
#include "SILGenFunction.h"
#include "SILGenFunctionBuilder.h"
#include "Scope.h"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/ForeignErrorConvention.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/ModuleLoader.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILUndef.h"
#include "swift/SIL/TypeLowering.h"
#include "clang/AST/DeclObjC.h"

using namespace swift;
using namespace Lowering;

/// Convert to the given formal type, assuming that the lowered type of
/// the source type is the same as its formal type.  This is a reasonable
/// assumption for a wide variety of types.
static ManagedValue emitUnabstractedCast(SILGenFunction &SGF, SILLocation loc,
                                         ManagedValue value,
                                         CanType sourceFormalType,
                                         CanType targetFormalType) {
  SILType loweredResultTy = SGF.getLoweredType(targetFormalType);
  if (value.getType() == loweredResultTy)
    return value;

  return SGF.emitTransformedValue(loc, value,
                                  AbstractionPattern(sourceFormalType),
                                  sourceFormalType,
                                  AbstractionPattern(targetFormalType),
                                  targetFormalType,
                                  loweredResultTy);
}

static bool shouldBridgeThroughError(SILGenModule &SGM, CanType type,
                                     CanType targetType) {
  // Never use this logic if the target type is AnyObject.
  if (targetType->isEqual(SGM.getASTContext().getAnyObjectType()))
    return false;

  auto errorProtocol = SGM.getASTContext().getErrorDecl();
  if (!errorProtocol) return false;

  // Existential types are convertible to Error if they are, or imply, Error.
  if (type.isExistentialType()) {
    auto layout = type->getExistentialLayout();
    for (auto proto : layout.getProtocols()) {
      if (proto == errorProtocol ||
          proto->inheritsFrom(errorProtocol)) {
        return true;
      }
    }

    // They're also convertible to Error if they have a class bound that
    // conforms to Error.
    if (auto superclass = layout.getSuperclass()) {
      type = superclass->getCanonicalType();

    // Otherwise, they are not convertible to Error.
    } else {
      return false;
    }
  }

  return (bool)SGM.SwiftModule->lookupConformance(type, errorProtocol);
}

/// Bridge the given Swift value to its corresponding Objective-C
/// object, using the appropriate witness for the
/// _ObjectiveCBridgeable._bridgeToObjectiveC requirement.
static std::optional<ManagedValue>
emitBridgeNativeToObjectiveC(SILGenFunction &SGF, SILLocation loc,
                             ManagedValue swiftValue, CanType swiftValueType,
                             CanType bridgedType,
                             ProtocolConformance *conformance) {
  // Find the _bridgeToObjectiveC requirement.
  auto requirement = SGF.SGM.getBridgeToObjectiveCRequirement(loc);
  if (!requirement)
    return std::nullopt;

  // Retrieve the _bridgeToObjectiveC witness.
  auto witness = conformance->getWitnessDecl(requirement);
  assert(witness);

  // Determine the type we're bridging to.
  auto objcTypeReq = SGF.SGM.getBridgedObjectiveCTypeRequirement(loc);
  if (!objcTypeReq)
    return std::nullopt;

  Type objcType = conformance->getTypeWitness(objcTypeReq);

  // Create a reference to the witness.
  SILDeclRef witnessConstant(witness);
  auto witnessRef = SGF.emitGlobalFunctionRef(loc, witnessConstant);

  // Determine the substitutions.
  auto witnessFnTy = witnessRef->getType();

  // Compute the substitutions.

  // FIXME: Figure out the right SubstitutionMap stuff if the witness
  // has generic parameters of its own.
  assert(!cast<FuncDecl>(witness)->isGeneric() &&
         "Generic witnesses not supported");

  auto *dc = cast<FuncDecl>(witness)->getDeclContext();
  auto typeSubMap = swiftValueType->getContextSubstitutionMap(
      SGF.SGM.SwiftModule, dc);

  // Substitute into the witness function type.
  witnessFnTy = witnessFnTy.substGenericArgs(SGF.SGM.M, typeSubMap,
                                             SGF.getTypeExpansionContext());

  // We might have to re-abstract the 'self' value if it is an
  // Optional.
  AbstractionPattern origSelfType(witness->getInterfaceType());
  origSelfType = origSelfType.getFunctionParamType(0);

  ArgumentScope scope(SGF, loc);

  swiftValue = SGF.emitSubstToOrigValue(loc, swiftValue,
                                        origSelfType,
                                        swiftValueType,
                                        SGFContext());

  // The witness may be more abstract than the concrete value we're bridging,
  // for instance, if the value is a concrete instantiation of a generic type.
  //
  // Note that we assume that we don't ever have to reabstract the parameter.
  // This is safe for now, since only nominal types currently can conform to
  // protocols.
  SILFunctionConventions witnessConv(witnessFnTy.castTo<SILFunctionType>(),
                                     SGF.SGM.M);
  if (witnessConv.isSILIndirect(witnessConv.getParameters()[0])
      && !swiftValue.getType().isAddress()) {
    auto tmp = SGF.emitTemporaryAllocation(loc, swiftValue.getType());
    swiftValue = SGF.emitManagedStoreBorrow(
        loc, swiftValue.borrow(SGF, loc).getValue(), tmp);
  }

  // Call the witness.
  SILValue bridgedValue =
      SGF.B.createApply(loc, witnessRef, typeSubMap,
                        swiftValue.borrow(SGF, loc).getValue());

  auto bridgedMV = SGF.emitManagedRValueWithCleanup(bridgedValue);
  bridgedMV = scope.popPreservingValue(bridgedMV);

  // The Objective-C value doesn't necessarily match the desired type.
  bridgedMV = emitUnabstractedCast(SGF, loc, bridgedMV,
                                   objcType->getCanonicalType(), bridgedType);

  return bridgedMV;
}

/// Bridge the given Objective-C object to its corresponding Swift
/// value, using the appropriate witness for the
/// _ObjectiveCBridgeable._unconditionallyBridgeFromObjectiveC requirement.
static std::optional<ManagedValue>
emitBridgeObjectiveCToNative(SILGenFunction &SGF, SILLocation loc,
                             ManagedValue objcValue, CanType bridgedType,
                             ProtocolConformance *conformance) {
  // Find the _unconditionallyBridgeFromObjectiveC requirement.
  auto requirement =
    SGF.SGM.getUnconditionallyBridgeFromObjectiveCRequirement(loc);
  if (!requirement)
    return std::nullopt;

  // Find the _ObjectiveCType requirement.
  auto objcTypeRequirement = SGF.SGM.getBridgedObjectiveCTypeRequirement(loc);
  if (!objcTypeRequirement)
    return std::nullopt;

  // Retrieve the _unconditionallyBridgeFromObjectiveC witness.
  auto witness = conformance->getWitnessDeclRef(requirement);
  assert(witness);

  // Retrieve the _ObjectiveCType witness.
  auto objcType = conformance->getTypeWitness(objcTypeRequirement);

  // Create a reference to the witness.
  SILDeclRef witnessConstant(witness.getDecl());
  auto witnessRef = SGF.emitGlobalFunctionRef(loc, witnessConstant);

  // Determine the substitutions.
  auto witnessFnTy = witnessRef->getType().castTo<SILFunctionType>();

  CanType swiftValueType = conformance->getType()->getCanonicalType();
  auto genericSig = witnessFnTy->getInvocationGenericSignature();
  SubstitutionMap typeSubMap = witness.getSubstitutions();

  // Substitute into the witness function type.
  witnessFnTy = witnessFnTy->substGenericArgs(SGF.SGM.M, typeSubMap,
                                              SGF.getTypeExpansionContext());

  // The witness takes an _ObjectiveCType?, so convert to that type.
  CanType desiredValueType = OptionalType::get(objcType)->getCanonicalType();
  objcValue = emitUnabstractedCast(SGF, loc, objcValue, bridgedType,
                                   desiredValueType);

  // Call the witness.
  auto metatypeParam = witnessFnTy->getParameters()[1];
  assert(isa<MetatypeType>(metatypeParam.getInterfaceType()) &&
         cast<MetatypeType>(metatypeParam.getInterfaceType()).getInstanceType()
           == swiftValueType);
  SILValue metatypeValue = SGF.B.createMetatype(
      loc, metatypeParam.getSILStorageType(SGF.SGM.M, witnessFnTy,
                                           SGF.getTypeExpansionContext()));

  auto witnessCI =
      SGF.getConstantInfo(SGF.getTypeExpansionContext(), witnessConstant);
  CanType formalResultTy = witnessCI.LoweredType.getResult();

  auto subs = witness.getSubstitutions();

  // Set up the generic signature, since formalResultTy is an interface type.
  CalleeTypeInfo calleeTypeInfo(
      witnessFnTy,
      AbstractionPattern(genericSig, formalResultTy),
      swiftValueType);
  SGFContext context;
  ResultPlanPtr resultPlan =
      ResultPlanBuilder::computeResultPlan(SGF, calleeTypeInfo, loc, context);
  ArgumentScope argScope(SGF, loc);
  RValue result = SGF.emitApply(
      std::move(resultPlan), std::move(argScope), loc,
      ManagedValue::forObjectRValueWithoutOwnership(witnessRef), subs,
      {objcValue, ManagedValue::forObjectRValueWithoutOwnership(metatypeValue)},
      calleeTypeInfo, ApplyOptions(), context, std::nullopt);
  return std::move(result).getAsSingleValue(SGF, loc);
}

static ManagedValue emitBridgeBoolToObjCBool(SILGenFunction &SGF,
                                             SILLocation loc,
                                             ManagedValue swiftBool) {
  // func _convertBoolToObjCBool(Bool) -> ObjCBool
  SILValue boolToObjCBoolFn
    = SGF.emitGlobalFunctionRef(loc, SGF.SGM.getBoolToObjCBoolFn());

  SILValue result = SGF.B.createApply(loc, boolToObjCBoolFn,
                                      {}, swiftBool.forward(SGF));
  return SGF.emitManagedRValueWithCleanup(result);
}

static ManagedValue emitBridgeBoolToDarwinBoolean(SILGenFunction &SGF,
                                                  SILLocation loc,
                                                  ManagedValue swiftBool) {
  // func _convertBoolToDarwinBoolean(Bool) -> DarwinBoolean
  SILValue boolToDarwinBooleanFn
    = SGF.emitGlobalFunctionRef(loc, SGF.SGM.getBoolToDarwinBooleanFn());

  SILValue result = SGF.B.createApply(loc, boolToDarwinBooleanFn,
                                      {}, swiftBool.forward(SGF));
  return SGF.emitManagedRValueWithCleanup(result);
}

static ManagedValue emitBridgeBoolToWindowsBool(SILGenFunction &SGF,
                                                SILLocation L, ManagedValue b) {
  // func _convertToWindowsBool(Bool) -> WindowsBool
  SILValue F = SGF.emitGlobalFunctionRef(L, SGF.SGM.getBoolToWindowsBoolFn());
  SILValue R = SGF.B.createApply(L, F, {}, b.forward(SGF));
  return SGF.emitManagedRValueWithCleanup(R);
}

static ManagedValue emitBridgeForeignBoolToBool(SILGenFunction &SGF,
                                                SILLocation loc,
                                                ManagedValue foreignBool,
                                                SILDeclRef bridgingFnRef) {
  // func _convertObjCBoolToBool(ObjCBool) -> Bool
  SILValue bridgingFn = SGF.emitGlobalFunctionRef(loc, bridgingFnRef);

  SILValue result = SGF.B.createApply(loc, bridgingFn, {},
                                      foreignBool.forward(SGF));
  return SGF.emitManagedRValueWithCleanup(result);
}

static ManagedValue emitManagedParameter(SILGenFunction &SGF, SILLocation loc,
                                         SILParameterInfo param,
                                         SILValue value) {
  const TypeLowering &valueTL = SGF.getTypeLowering(value->getType());

  switch (param.getConvention()) {
  case ParameterConvention::Direct_Owned:
    // Consume owned parameters at +1.
    return SGF.emitManagedRValueWithCleanup(value, valueTL);

  case ParameterConvention::Direct_Guaranteed:
    // If we have a guaranteed parameter, the object should not need to be
    // retained or have a cleanup.
    return ManagedValue::forBorrowedObjectRValue(value);

  case ParameterConvention::Direct_Unowned:
    // We need to independently retain the value.
    return SGF.emitManagedCopy(loc, value, valueTL);

  case ParameterConvention::Indirect_Inout:
    return ManagedValue::forLValue(value);

  case ParameterConvention::Indirect_In_Guaranteed:
    if (valueTL.isLoadable()) {
      return SGF.B.createLoadBorrow(
          loc, ManagedValue::forBorrowedAddressRValue(value));
    } else {
      return ManagedValue::forBorrowedAddressRValue(value);
    }

  case ParameterConvention::Indirect_In:
    if (valueTL.isLoadable()) {
      return SGF.emitLoad(loc, value, valueTL, SGFContext(), IsTake);
    } else {
      return SGF.emitManagedRValueWithCleanup(value, valueTL);
    }

  case ParameterConvention::Indirect_InoutAliasable:
  case ParameterConvention::Pack_Guaranteed:
  case ParameterConvention::Pack_Owned:
  case ParameterConvention::Pack_Inout:
    llvm_unreachable("unexpected convention");
  }
  llvm_unreachable("bad convention");
}

/// Get the type of each parameter, filtering out empty tuples.
static SmallVector<CanType, 8>
getParameterTypes(AnyFunctionType::CanParamArrayRef params,
                  bool hasSelfParam=false) {
  SmallVector<CanType, 8> results;
  for (auto n : indices(params)) {
    bool isSelf = (hasSelfParam ? n == params.size() - 1 : false);

    const auto &param = params[n];
    assert(isSelf || !param.isInOut() &&
           "Only the 'self' parameter can be inout in a bridging thunk");
    assert(!param.isVariadic());

    if (param.getPlainType()->isVoid())
      continue;
    results.push_back(param.getPlainType());
  }
  return results;
}

static CanAnyFunctionType
getBridgedBlockType(SILGenModule &SGM, CanAnyFunctionType blockType,
                    SILFunctionTypeRepresentation silRep) {
  return SGM.Types.getBridgedFunctionType(
      AbstractionPattern(blockType), blockType, Bridgeability::Full, silRep);
}

static void buildFuncToBlockInvokeBody(SILGenFunction &SGF,
                                       SILLocation loc,
                                       CanAnyFunctionType formalFuncType,
                                       CanAnyFunctionType formalBlockType,
                                       CanSILFunctionType funcTy,
                                       CanSILFunctionType blockTy,
                                       CanSILBlockStorageType blockStorageTy,
                                       bool isUnretainedClosureSafe) {
  Scope scope(SGF.Cleanups, CleanupLocation(loc));
  SILBasicBlock *entry = &*SGF.F.begin();
  SILFunctionConventions blockConv(blockTy, SGF.SGM.M);
  SILFunctionConventions funcConv(funcTy, SGF.SGM.M);

  // Make sure we lower the component types of the formal block type.
  formalBlockType = getBridgedBlockType(SGF.SGM, formalBlockType,
                                        blockTy->getRepresentation());

  // Set up the indirect result.
  SILType blockResultTy =
      blockTy->getAllResultsSubstType(SGF.SGM.M, SGF.getTypeExpansionContext());
  SILValue indirectResult;
  if (blockTy->getNumResults() != 0) {
    auto result = blockTy->getSingleResult();
    if (result.getConvention() == ResultConvention::Indirect) {
      indirectResult =
          entry->createFunctionArgument(blockResultTy.getAddressType());
    }
  }

  // Get the captured native function value out of the block.
  auto storageAddrTy = SILType::getPrimitiveAddressType(blockStorageTy);
  auto storage = entry->createFunctionArgument(storageAddrTy);
  auto capture = SGF.B.createProjectBlockStorage(loc, storage);
  auto &funcTL = SGF.getTypeLowering(funcTy);
  auto fn = isUnretainedClosureSafe
                ? SGF.emitManagedLoadBorrow(loc, capture)
                : SGF.emitLoad(loc, capture, funcTL, SGFContext(), IsNotTake);

  // Collect the block arguments, which may have nonstandard conventions.
  assert(blockTy->getParameters().size() == funcTy->getParameters().size()
         && "block and function types don't match");

  auto nativeParamTypes = getParameterTypes(formalFuncType.getParams());
  auto bridgedParamTypes = getParameterTypes(formalBlockType.getParams());

  SmallVector<ManagedValue, 4> args;
  for (unsigned i : indices(funcTy->getParameters())) {
    auto &param = blockTy->getParameters()[i];
    SILType paramTy =
        blockConv.getSILType(param, SGF.getTypeExpansionContext());
    SILValue v = entry->createFunctionArgument(paramTy);
    ManagedValue mv;
    
    // If the parameter is a block, we need to copy it to ensure it lives on
    // the heap. The adapted closure value might outlive the block's original
    // scope.
    if (SGF.getSILType(param, blockTy).isBlockPointerCompatible()) {
      // We still need to consume the original block if it was owned.
      switch (param.getConvention()) {
      case ParameterConvention::Direct_Owned:
        SGF.emitManagedRValueWithCleanup(v);
        break;

      case ParameterConvention::Direct_Guaranteed:
      case ParameterConvention::Direct_Unowned:
        break;
        
      case ParameterConvention::Indirect_In:
      case ParameterConvention::Indirect_In_Guaranteed:
      case ParameterConvention::Indirect_Inout:
      case ParameterConvention::Indirect_InoutAliasable:
      case ParameterConvention::Pack_Guaranteed:
      case ParameterConvention::Pack_Owned:
      case ParameterConvention::Pack_Inout:
        llvm_unreachable("indirect params to blocks not supported");
      }
      
      SILValue blockCopy = SGF.B.createCopyBlock(loc, v);
      mv = SGF.emitManagedRValueWithCleanup(blockCopy);
    } else {
      mv = emitManagedParameter(SGF, loc, param, v);
    }

    CanType formalBridgedType = bridgedParamTypes[i];
    CanType formalNativeType = nativeParamTypes[i];
    SILType loweredNativeTy = funcTy->getParameters()[i].getSILStorageType(
        SGF.SGM.M, funcTy, SGF.getTypeExpansionContext());

    args.push_back(SGF.emitBridgedToNativeValue(loc, mv, formalBridgedType,
                                                formalNativeType,
                                                loweredNativeTy));
  }

  auto init = indirectResult
                ? SGF.useBufferAsTemporary(indirectResult,
                                SGF.getTypeLowering(indirectResult->getType()))
                : nullptr;

  CanType formalNativeResultType = formalFuncType.getResult();
  CanType formalBridgedResultType = formalBlockType.getResult();

  bool canEmitIntoInit =
    (indirectResult &&
     indirectResult->getType()
       == SGF.getLoweredType(formalNativeResultType).getAddressType());

  // Call the native function.
  SGFContext C(canEmitIntoInit ? init.get() : nullptr);
  ManagedValue result =
      SGF.emitMonomorphicApply(loc, fn, args, formalNativeResultType,
                               formalNativeResultType, ApplyOptions(),
                               std::nullopt, std::nullopt, C)
          .getAsSingleValue(SGF, loc);

  // Bridge the result back to ObjC.
  if (!canEmitIntoInit) {
    result = SGF.emitNativeToBridgedValue(loc, result,
                                          formalNativeResultType,
                                          formalBridgedResultType,
                                          blockResultTy,
                                          SGFContext(init.get()));
  }

  SILValue resultVal;

  // If we have an indirect result, make sure the result is there.
  if (indirectResult) {
    if (!result.isInContext()) {
      init->copyOrInitValueInto(SGF, loc, result, /*isInit*/ true);
      init->finishInitialization(SGF);
    }
    init->getManagedAddress().forward(SGF);
    resultVal = SGF.B.createTuple(loc, {});
  } else {
    // Otherwise, return the result at +1.
    resultVal = result.forward(SGF);
  }

  scope.pop();

  SGF.B.createReturn(loc, resultVal);
}

/// Bridge a native function to a block with a thunk.
ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
                                             ManagedValue fn,
                                             CanAnyFunctionType funcType,
                                             CanAnyFunctionType blockType,
                                             CanSILFunctionType loweredBlockTy){
  auto loweredFuncTy = fn.getType().castTo<SILFunctionType>();

  // If we store a @noescape closure in a block verify that the block has not
  // escaped by storing a withoutActuallyEscaping closure in the block and after
  // the block is ultimately destroyed checking that the closure is uniquely
  // referenced.
  bool useWithoutEscapingVerification = false;
  ManagedValue escaping;
  if (loweredFuncTy->isNoEscape()) {
    auto escapingTy = loweredFuncTy->getWithExtInfo(
        loweredFuncTy->getExtInfo().withNoEscape(false));

    escaping = createWithoutActuallyEscapingClosure(
        loc, fn, SILType::getPrimitiveObjectType(escapingTy));
    loweredFuncTy = escapingTy;
    auto escapingAnyTy =
        funcType.withExtInfo(funcType->getExtInfo().withNoEscape(false));
    funcType = escapingAnyTy;
    fn = escaping.copy(*this, loc);
    useWithoutEscapingVerification = true;
  } else {
    // Since we are going to be storing this into memory, we need fn at +1.
    fn = fn.ensurePlusOne(*this, loc);
  }
  
  // All different substitutions of a function type can share a thunk.
  auto loweredFuncUnsubstTy = loweredFuncTy->getUnsubstitutedType(SGM.M);
  if (loweredFuncUnsubstTy != loweredFuncTy) {
    fn = B.createConvertFunction(loc, fn,
                         SILType::getPrimitiveObjectType(loweredFuncUnsubstTy));
  }

  // Build the invoke function signature. The block will capture the original
  // function value.
  auto fnInterfaceTy = cast<SILFunctionType>(
    loweredFuncUnsubstTy->mapTypeOutOfContext()->getCanonicalType());
  auto blockInterfaceTy = cast<SILFunctionType>(
    loweredBlockTy->mapTypeOutOfContext()->getCanonicalType());

  assert(!blockInterfaceTy->isCoroutine());

  auto storageTy = SILBlockStorageType::get(loweredFuncUnsubstTy);
  auto storageInterfaceTy = SILBlockStorageType::get(fnInterfaceTy);

  // Build the invoke function type.
  SmallVector<SILParameterInfo, 4> params;
  params.push_back(SILParameterInfo(storageInterfaceTy,
                                 ParameterConvention::Indirect_InoutAliasable));
  std::copy(blockInterfaceTy->getParameters().begin(),
            blockInterfaceTy->getParameters().end(),
            std::back_inserter(params));

  auto results = blockInterfaceTy->getResults();
  auto representation = SILFunctionType::Representation::CFunctionPointer;
  auto *clangFnType = getASTContext().getCanonicalClangFunctionType(
      params, results.empty() ? std::optional<SILResultInfo>() : results[0],
      representation);

  auto extInfo = SILFunctionType::ExtInfoBuilder()
                     .withRepresentation(representation)
                     .withClangFunctionType(clangFnType)
                     .build();

  CanGenericSignature genericSig;
  GenericEnvironment *genericEnv = nullptr;
  SubstitutionMap subs;
  if (funcType->hasArchetype() || blockType->hasArchetype()) {
    genericSig = F.getLoweredFunctionType()->getInvocationGenericSignature();
    genericEnv = F.getGenericEnvironment();

    subs = F.getForwardingSubstitutionMap();

    // The block invoke function must be pseudogeneric. This should be OK for now
    // since a bridgeable function's parameters and returns should all be
    // trivially representable in ObjC so not need to exercise the type metadata.
    //
    // Ultimately we may need to capture generic parameters in block storage, but
    // that will require a redesign of the interface to support dependent-layout
    // context. Currently we don't capture anything directly into a block but a
    // Swift closure, but that's totally dumb.
    if (genericSig)
      extInfo = extInfo.intoBuilder().withIsPseudogeneric().build();
  }

  auto invokeTy = SILFunctionType::get(
      genericSig, extInfo, SILCoroutineKind::None,
      ParameterConvention::Direct_Unowned, params, 
      /*yields*/ {}, blockInterfaceTy->getResults(),
      blockInterfaceTy->getOptionalErrorResult(),
      SubstitutionMap(), SubstitutionMap(),
      getASTContext());

  // Create the invoke function. Borrow the mangling scheme from reabstraction
  // thunks, which is what we are in spirit.
  auto thunk = SGM.getOrCreateReabstractionThunk(invokeTy,
                                                 loweredFuncUnsubstTy,
                                                 loweredBlockTy,
                                                 /*dynamicSelfType=*/CanType(),
                                                 /*global actor=*/CanType());

  // Build it if necessary.
  if (thunk->empty()) {
    thunk->setGenericEnvironment(genericEnv);
    SILGenFunction thunkSGF(SGM, *thunk, FunctionDC);
    auto loc = RegularLocation::getAutoGeneratedLocation();
    // Not retaining the closure in the reabstraction thunk is safe if we hold
    // another reference for the is_escaping sentinel.
    buildFuncToBlockInvokeBody(thunkSGF, loc, funcType, blockType,
                               loweredFuncUnsubstTy, loweredBlockTy, storageTy,
                               useWithoutEscapingVerification);
    SGM.emitLazyConformancesForFunction(thunk);
  }

  // Form the block on the stack.
  auto storageAddrTy = SILType::getPrimitiveAddressType(storageTy);
  auto storage = emitTemporaryAllocation(loc, storageAddrTy);
  auto capture = B.createProjectBlockStorage(loc, storage);
  B.createStore(loc, fn, capture, StoreOwnershipQualifier::Init);
  auto invokeFn = B.createFunctionRefFor(loc, thunk);

  auto stackBlock = B.createInitBlockStorageHeader(loc, storage, invokeFn,
                              SILType::getPrimitiveObjectType(loweredBlockTy),
                                                   subs);

  // Copy the block so we have an independent heap object we can hand off.

  // If withoutActuallyEscaping verification is requested we emit a
  // copy_block_without_escaping %block withoutEscaping %closure instruction.
  // A mandatory SIL pass will replace this instruction by the required
  // verification instruction sequence.
  auto heapBlock = useWithoutEscapingVerification
                       ? SILValue(B.createCopyBlockWithoutEscaping(
                             loc, stackBlock, escaping.forward(*this)))
                       : SILValue(B.createCopyBlock(loc, stackBlock));
  return emitManagedRValueWithCleanup(heapBlock);
}

static ManagedValue emitNativeToCBridgedNonoptionalValue(SILGenFunction &SGF,
                                                         SILLocation loc,
                                                         ManagedValue v,
                                                         CanType nativeType,
                                                         CanType bridgedType,
                                                      SILType loweredBridgedTy,
                                                         SGFContext C) {
  assert(loweredBridgedTy.isObject());
  if (v.getType().getObjectType() == loweredBridgedTy)
    return v;

  // If the input is a native type with a bridged mapping, convert it.
#define BRIDGE_TYPE(BridgedModule,BridgedType, NativeModule,NativeType,Opt) \
  if (nativeType == SGF.SGM.Types.get##NativeType##Type()                   \
      && bridgedType == SGF.SGM.Types.get##BridgedType##Type()) {           \
    return emitBridge##NativeType##To##BridgedType(SGF, loc, v);            \
  }
#include "swift/SIL/BridgedTypes.def"

  // Bridge thick to Objective-C metatypes.
  if (auto bridgedMetaTy = dyn_cast<AnyMetatypeType>(bridgedType)) {
    if (bridgedMetaTy->hasRepresentation() &&
        bridgedMetaTy->getRepresentation() == MetatypeRepresentation::ObjC) {
      SILValue native = SGF.B.emitThickToObjCMetatype(loc, v.getValue(),
                                                      loweredBridgedTy);
      // *NOTE*: ObjCMetatypes are trivial types. They only gain ARC semantics
      // when they are converted to an object via objc_metatype_to_object.
      assert(!v.hasCleanup() &&
             "Metatypes are trivial and thus should not have cleanups");
      return ManagedValue::forObjectRValueWithoutOwnership(native);
    }
  }

  // Bridge native functions to blocks.
  auto bridgedFTy = dyn_cast<AnyFunctionType>(bridgedType);
  if (bridgedFTy && bridgedFTy->getRepresentation()
                      == AnyFunctionType::Representation::Block) {
    auto nativeFTy = cast<AnyFunctionType>(nativeType);

    if (nativeFTy->getRepresentation()
          != AnyFunctionType::Representation::Block)
      return SGF.emitFuncToBlock(loc, v, nativeFTy, bridgedFTy,
                                 loweredBridgedTy.castTo<SILFunctionType>());
  }

  // If the native type conforms to _ObjectiveCBridgeable, use its
  // _bridgeToObjectiveC witness.
  if (auto conformance =
          SGF.SGM.getConformanceToObjectiveCBridgeable(loc, nativeType)) {
    if (auto result = emitBridgeNativeToObjectiveC(SGF, loc, v, nativeType,
                                                   bridgedType, conformance))
      return *result;

    assert(SGF.SGM.getASTContext().Diags.hadAnyError() &&
           "Bridging code should have complained");
    return SGF.emitUndef(bridgedType);
  }

  // Bridge Error, or types that conform to it, to NSError.
  if (shouldBridgeThroughError(SGF.SGM, nativeType, bridgedType)) {
    auto errorTy = SGF.SGM.Types.getNSErrorType();
    auto error = SGF.emitNativeToBridgedError(loc, v, nativeType, errorTy);
    if (errorTy != bridgedType) {
      error = emitUnabstractedCast(SGF, loc, error, errorTy, bridgedType);
    }
    return error;
  }

  // Fall back to dynamic Any-to-id bridging.
  // The destination type should be AnyObject in this case.
  assert(bridgedType->isEqual(SGF.getASTContext().getAnyObjectType()));

  // Blocks bridge to id with a cast under ObjCInterop.
  if (auto nativeFnType = dyn_cast<AnyFunctionType>(nativeType)) {
    if (nativeFnType->getRepresentation() ==
          FunctionTypeRepresentation::Block &&
        SGF.getASTContext().LangOpts.EnableObjCInterop) {
      return SGF.B.createBlockToAnyObject(loc, v, loweredBridgedTy);
    }
  }

  // If the input argument is known to be an existential, save the runtime
  // some work by opening it.
  if (nativeType->isExistentialType()) {
    auto openedType = OpenedArchetypeType::get(nativeType,
                                               SGF.F.getGenericSignature());

    FormalEvaluationScope scope(SGF);

    v = SGF.emitOpenExistential(
        loc, v, SGF.getLoweredType(openedType),
        AccessKind::Read);
    v = v.ensurePlusOne(SGF, loc);

    nativeType = openedType;
  }

  // Call into the stdlib intrinsic.
  if (auto bridgeAnything =
        SGF.getASTContext().getBridgeAnythingToObjectiveC()) {
    auto genericSig = bridgeAnything->getGenericSignature();
    auto subMap = SubstitutionMap::get(
      genericSig,
      [&](SubstitutableType *t) -> Type {
        return nativeType;
      },
      LookUpConformanceInModule(SGF.SGM.SwiftModule));

    // The intrinsic takes a T; reabstract to the generic abstraction
    // pattern.
    v = SGF.emitSubstToOrigValue(loc, v, AbstractionPattern::getOpaque(),
                                 nativeType);

    // Put the value into memory if necessary.
    assert(v.getOwnershipKind() == OwnershipKind::None || v.hasCleanup());
    SILModuleConventions silConv(SGF.SGM.M);
    // bridgeAnything always takes an indirect argument as @in.
    // Since we don't have the SIL type here, check the current SIL stage/mode
    // to determine the convention.
    if (v.getType().isObject() && silConv.useLoweredAddresses()) {
      auto tmp = SGF.emitTemporaryAllocation(loc, v.getType());
      v.forwardInto(SGF, loc, tmp);
      v = SGF.emitManagedBufferWithCleanup(tmp);
    }
    return SGF.emitApplyOfLibraryIntrinsic(loc, bridgeAnything, subMap, v, C)
              .getAsSingleValue(SGF, loc);
  }
  
  // Shouldn't get here unless the standard library is busted.
  return SGF.emitUndef(loweredBridgedTy);
}

static ManagedValue emitNativeToCBridgedValue(SILGenFunction &SGF,
                                              SILLocation loc,
                                              ManagedValue v,
                                              CanType nativeType,
                                              CanType bridgedType,
                                              SILType loweredBridgedTy,
                                              SGFContext C = SGFContext()) {
  SILType loweredNativeTy = v.getType();
  if (loweredNativeTy.getObjectType() == loweredBridgedTy.getObjectType())
    return v;

  CanType bridgedObjectType = bridgedType.getOptionalObjectType();
  CanType nativeObjectType = nativeType.getOptionalObjectType();

  // Check for optional-to-optional conversions.
  if (bridgedObjectType && nativeObjectType) {
    auto helper = [&](SILGenFunction &SGF, SILLocation loc,
                      ManagedValue v, SILType loweredBridgedObjectTy,
                      SGFContext C) {
      return emitNativeToCBridgedValue(SGF, loc, v, nativeObjectType,
                                       bridgedObjectType,
                                       loweredBridgedObjectTy, C);
    };
    return SGF.emitOptionalToOptional(loc, v, loweredBridgedTy, helper, C);
  }
  
  // Check if we need to wrap the bridged result in an optional.
  if (bridgedObjectType) {
    auto helper = [&](SILGenFunction &SGF, SILLocation loc, SGFContext C) {
      auto loweredBridgedObjectTy = loweredBridgedTy.getOptionalObjectType();
      return emitNativeToCBridgedValue(SGF, loc, v, nativeType,
                                       bridgedObjectType,
                                       loweredBridgedObjectTy, C);
    };
    return SGF.emitOptionalSome(loc, loweredBridgedTy, helper, C);
  }
  
  return emitNativeToCBridgedNonoptionalValue(SGF, loc, v, nativeType,
                                              bridgedType, loweredBridgedTy, C);
}

ManagedValue SILGenFunction::emitNativeToBridgedValue(SILLocation loc,
                                                      ManagedValue v,
                                                      CanType nativeTy,
                                                      CanType bridgedTy,
                                                      SILType loweredBridgedTy,
                                                      SGFContext C) {
  loweredBridgedTy = loweredBridgedTy.getObjectType();
  return emitNativeToCBridgedValue(*this, loc, v, nativeTy, bridgedTy,
                                   loweredBridgedTy, C);
}

static void buildBlockToFuncThunkBody(SILGenFunction &SGF,
                                      SILLocation loc,
                                      CanAnyFunctionType formalBlockTy,
                                      CanAnyFunctionType formalFuncTy,
                                      CanSILFunctionType blockTy,
                                      CanSILFunctionType funcTy) {
  // Collect the native arguments, which should all be +1.
  Scope scope(SGF.Cleanups, CleanupLocation(loc));

  // Make sure we lower the component types of the formal block type.
  formalBlockTy =
      getBridgedBlockType(SGF.SGM, formalBlockTy, blockTy->getRepresentation());

  assert(blockTy->getNumParameters() == funcTy->getNumParameters()
         && "block and function types don't match");

  SmallVector<ManagedValue, 4> args;
  SILBasicBlock *entry = &*SGF.F.begin();

  SILFunctionConventions fnConv(funcTy, SGF.SGM.M);

  // Set up the indirect result slot.
  SILValue indirectResult;
  if (funcTy->getNumResults() != 0) {
    auto result = funcTy->getSingleResult();
    if (result.getConvention() == ResultConvention::Indirect) {
      SILType resultTy =
          fnConv.getSILType(result, SGF.getTypeExpansionContext());
      indirectResult = entry->createFunctionArgument(resultTy);
    }
  }

  auto formalBlockParams = getParameterTypes(formalBlockTy.getParams());
  auto formalFuncParams = getParameterTypes(formalFuncTy.getParams());
  assert(formalBlockParams.size() == blockTy->getNumParameters());
  assert(formalFuncParams.size() == funcTy->getNumParameters());

  // Create the arguments for the call.
  for (unsigned i : indices(funcTy->getParameters())) {
    auto &param = funcTy->getParameters()[i];
    CanType formalBlockParamTy = formalBlockParams[i];
    CanType formalFuncParamTy = formalFuncParams[i];

    auto paramTy = fnConv.getSILType(param, SGF.getTypeExpansionContext());
    SILValue v = entry->createFunctionArgument(paramTy);

    // First get the managed parameter for this function.
    auto mv = emitManagedParameter(SGF, loc, param, v);

    SILType loweredBlockArgTy = blockTy->getParameters()[i].getSILStorageType(
        SGF.SGM.M, blockTy, SGF.getTypeExpansionContext());

    // Then bridge the native value to its bridged variant.
    mv = SGF.emitNativeToBridgedValue(loc, mv, formalFuncParamTy,
                                      formalBlockParamTy, loweredBlockArgTy);

    // Finally change ownership if we need to. We do not need to care about the
    // case of a +1 parameter being passed to a +0 function since +1 parameters
    // can be "instantaneously" borrowed at the call site.
    if (blockTy->getParameters()[i].isConsumed()) {
      mv = mv.ensurePlusOne(SGF, loc);
    }
    args.push_back(mv);
  }

  // Add the block argument.
  SILValue blockV =
      entry->createFunctionArgument(SILType::getPrimitiveObjectType(blockTy));
  ManagedValue block = ManagedValue::forBorrowedObjectRValue(blockV);

  CanType formalResultType = formalFuncTy.getResult();

  auto init = indirectResult
                ? SGF.useBufferAsTemporary(indirectResult,
                                SGF.getTypeLowering(indirectResult->getType()))
                : nullptr;

  // Call the block.
  ManagedValue result =
      SGF.emitMonomorphicApply(
             loc, block, args, formalBlockTy.getResult(), formalResultType,
             ApplyOptions(),
             /*override CC*/ SILFunctionTypeRepresentation::Block,
             /*foreign error*/ std::nullopt, SGFContext(init.get()))
          .getAsSingleValue(SGF, loc);

  SILValue r;

  // If we have an indirect result, make sure the result is there.
  if (indirectResult) {
    if (!result.isInContext()) {
      init->copyOrInitValueInto(SGF, loc, result, /*isInit*/ true);
      init->finishInitialization(SGF);
    }
    init->getManagedAddress().forward(SGF);
    r = SGF.B.createTuple(
        loc, fnConv.getSILResultType(SGF.getTypeExpansionContext()),
        ArrayRef<SILValue>());

    // Otherwise, return the result at +1.
  } else {
    r = result.forward(SGF);
  }

  scope.pop();

  SGF.B.createReturn(loc, r);

  // Finally, verify the thunk for SIL invariants.
  SGF.F.verifyIncompleteOSSA();
}

/// Bridge a native function to a block with a thunk.
ManagedValue
SILGenFunction::emitBlockToFunc(SILLocation loc,
                                ManagedValue block,
                                CanAnyFunctionType blockType,
                                CanAnyFunctionType funcType,
                                CanSILFunctionType loweredFuncTy) {
  // Declare the thunk.
  auto loweredBlockTy = block.getType().castTo<SILFunctionType>();

  SubstitutionMap contextSubs, interfaceSubs;
  GenericEnvironment *genericEnv = nullptr;

  // These two are not used here -- but really, bridging thunks
  // should be emitted using the formal AST type, not the lowered
  // type
  CanType inputSubstType, outputSubstType;

  auto loweredFuncTyWithoutNoEscape = adjustFunctionType(
      loweredFuncTy, loweredFuncTy->getExtInfo().withNoEscape(false),
      loweredFuncTy->getWitnessMethodConformanceOrInvalid());
  
  auto loweredFuncUnsubstTy =
    loweredFuncTyWithoutNoEscape->getUnsubstitutedType(SGM.M);

  CanType dynamicSelfType;
  auto thunkTy = buildThunkType(loweredBlockTy, loweredFuncUnsubstTy,
                                inputSubstType, outputSubstType,
                                genericEnv, interfaceSubs, dynamicSelfType);
  assert(!dynamicSelfType && "Not implemented");

  auto thunk = SGM.getOrCreateReabstractionThunk(thunkTy,
                                                 loweredBlockTy,
                                                 loweredFuncUnsubstTy,
                                                 /*dynamicSelfType=*/CanType(),
                                                 /*global actor=*/CanType());

  // Build it if necessary.
  if (thunk->empty()) {
    SILGenFunction thunkSGF(SGM, *thunk, FunctionDC);
    thunk->setGenericEnvironment(genericEnv);
    auto loc = RegularLocation::getAutoGeneratedLocation();
    buildBlockToFuncThunkBody(thunkSGF, loc, blockType, funcType,
                              loweredBlockTy, loweredFuncUnsubstTy);
    SGM.emitLazyConformancesForFunction(thunk);
  }

  CanSILFunctionType substFnTy = thunkTy;

  if (thunkTy->getInvocationGenericSignature()) {
    substFnTy = thunkTy->substGenericArgs(F.getModule(),
                                          interfaceSubs,
                                          getTypeExpansionContext());
  }

  // Create it in the current function.
  auto thunkValue = B.createFunctionRefFor(loc, thunk);
  ManagedValue thunkedFn = B.createPartialApply(
      loc, thunkValue, interfaceSubs, block,
      loweredFuncTy->getCalleeConvention());

  if (loweredFuncUnsubstTy != loweredFuncTyWithoutNoEscape) {
    thunkedFn = B.createConvertFunction(loc, thunkedFn,
                SILType::getPrimitiveObjectType(loweredFuncTyWithoutNoEscape));
  }
  
  if (!loweredFuncTy->isNoEscape()) {
    return thunkedFn;
  }

  // Handle the escaping to noescape conversion.
  assert(loweredFuncTy->isNoEscape());
  return B.createConvertEscapeToNoEscape(
      loc, thunkedFn, SILType::getPrimitiveObjectType(loweredFuncTy));
}

static ManagedValue emitCBridgedToNativeValue(
    SILGenFunction &SGF, SILLocation loc, ManagedValue v, CanType bridgedType,
    SILType loweredBridgedTy, CanType nativeType, SILType loweredNativeTy,
    int bridgedOptionalsToUnwrap, bool isCallResult, SGFContext C) {
  assert(loweredNativeTy.isObject());
  if (loweredNativeTy == loweredBridgedTy.getObjectType())
    return v;

  if (auto nativeObjectType = nativeType.getOptionalObjectType()) {
    auto bridgedObjectType = bridgedType.getOptionalObjectType();

    // Optional injection.
    if (!bridgedObjectType) {
      auto helper = [&](SILGenFunction &SGF, SILLocation loc, SGFContext C) {
        auto loweredNativeObjectTy = loweredNativeTy.getOptionalObjectType();
        return emitCBridgedToNativeValue(
            SGF, loc, v, bridgedType, loweredBridgedTy, nativeObjectType,
            loweredNativeObjectTy, bridgedOptionalsToUnwrap, isCallResult, C);
      };
      return SGF.emitOptionalSome(loc, loweredNativeTy, helper, C);
    }

    // Optional-to-optional.
    auto helper = [=](SILGenFunction &SGF, SILLocation loc, ManagedValue v,
                      SILType loweredNativeObjectTy, SGFContext C) {
      return emitCBridgedToNativeValue(
          SGF, loc, v, bridgedObjectType,
          loweredBridgedTy.getOptionalObjectType(), nativeObjectType,
          loweredNativeObjectTy, bridgedOptionalsToUnwrap, isCallResult, C);
    };
    return SGF.emitOptionalToOptional(loc, v, loweredNativeTy, helper, C);
  }
  if (auto bridgedObjectType = bridgedType.getOptionalObjectType()) {
    return emitCBridgedToNativeValue(
        SGF, loc, v, bridgedObjectType,
        loweredBridgedTy.getOptionalObjectType(), nativeType, loweredNativeTy,
        bridgedOptionalsToUnwrap + 1, isCallResult, C);
  }

  auto unwrapBridgedOptionals = [&](ManagedValue v) {
    for (int i = 0; i < bridgedOptionalsToUnwrap; ++i) {
      v = SGF.emitPreconditionOptionalHasValue(loc, v,
                                               /*implicit*/ true);
    };
    return v;
  };

  // Bridge ObjCBool, DarwinBoolean, WindowsBool to Bool when requested.
  if (nativeType == SGF.SGM.Types.getBoolType()) {
    if (bridgedType == SGF.SGM.Types.getObjCBoolType()) {
      return emitBridgeForeignBoolToBool(SGF, loc, unwrapBridgedOptionals(v),
                                         SGF.SGM.getObjCBoolToBoolFn());
    }
    if (bridgedType == SGF.SGM.Types.getDarwinBooleanType()) {
      return emitBridgeForeignBoolToBool(SGF, loc, unwrapBridgedOptionals(v),
                                         SGF.SGM.getDarwinBooleanToBoolFn());
    }
    if (bridgedType == SGF.SGM.Types.getWindowsBoolType()) {
      return emitBridgeForeignBoolToBool(SGF, loc, unwrapBridgedOptionals(v),
                                         SGF.SGM.getWindowsBoolToBoolFn());
    }
  }

  // Bridge Objective-C to thick metatypes.
  if (isa<AnyMetatypeType>(nativeType)) {
    auto bridgedMetaTy = cast<AnyMetatypeType>(bridgedType);
    if (bridgedMetaTy->hasRepresentation() &&
        bridgedMetaTy->getRepresentation() == MetatypeRepresentation::ObjC) {
      SILValue native = SGF.B.emitObjCToThickMetatype(
          loc, unwrapBridgedOptionals(v).getValue(), loweredNativeTy);
      // *NOTE*: ObjCMetatypes are trivial types. They only gain ARC semantics
      // when they are converted to an object via objc_metatype_to_object.
      assert(!v.hasCleanup() && "Metatypes are trivial and should not have "
                                "cleanups");
      return ManagedValue::forUnmanagedOwnedValue(native);
    }
  }

  // Bridge blocks back into native function types.
  if (auto nativeFTy = dyn_cast<AnyFunctionType>(nativeType)) {
    auto bridgedFTy = cast<AnyFunctionType>(bridgedType);
    if (bridgedFTy->getRepresentation()
          == AnyFunctionType::Representation::Block
        && nativeFTy->getRepresentation()
          != AnyFunctionType::Representation::Block) {
      return SGF.emitBlockToFunc(loc, unwrapBridgedOptionals(v), bridgedFTy,
                                 nativeFTy,
                                 loweredNativeTy.castTo<SILFunctionType>());
    }
  }

  // Bridge via _ObjectiveCBridgeable.
  if (auto conformance =
        SGF.SGM.getConformanceToObjectiveCBridgeable(loc, nativeType)) {
    if (auto result = emitBridgeObjectiveCToNative(SGF, loc, v, bridgedType,
                                                   conformance)) {
      --bridgedOptionalsToUnwrap;
      return unwrapBridgedOptionals(*result);
    }

    assert(SGF.SGM.getASTContext().Diags.hadAnyError() &&
           "Bridging code should have complained");
    return SGF.emitUndef(nativeType);
  }

  // id-to-Any bridging.
  if (nativeType->isMarkerExistential()) {
    // If this is not a call result, use the normal erasure logic.
    if (!isCallResult) {
      return SGF.emitTransformedValue(loc, unwrapBridgedOptionals(v),
                                      bridgedType, nativeType, C);
    }

    // Otherwise, we use more complicated logic that handles results that
    // were unexpectedly null.

    assert(bridgedType.isAnyClassReferenceType());

    // Convert to AnyObject if necessary.
    CanType anyObjectTy =
      SGF.getASTContext().getAnyObjectType()->getCanonicalType();
    if (bridgedType != anyObjectTy) {
      v = SGF.emitTransformedValue(loc, unwrapBridgedOptionals(v), bridgedType,
                                   anyObjectTy);
    }

    // TODO: Ever need to handle +0 values here?
    assert(v.hasCleanup());

    // Use a runtime call to bridge the AnyObject to Any. We do this instead of
    // a simple AnyObject-to-Any upcast because the ObjC API may have returned
    // a null object in spite of its annotation.
    
    // Bitcast to Optional. This provides a barrier to the optimizer to prevent
    // it from attempting to eliminate null checks.
    auto optionalBridgedTy = SILType::getOptionalType(loweredBridgedTy);
    auto optionalMV = SGF.B.createUncheckedBitCast(
        loc, unwrapBridgedOptionals(v), optionalBridgedTy);
    v = SGF.emitApplyOfLibraryIntrinsic(loc,
                           SGF.getASTContext().getBridgeAnyObjectToAny(),
                           SubstitutionMap(), optionalMV, C)
              .getAsSingleValue(SGF, loc);
    
    // Convert to the marker existential if necessary.
    if (!v.isInContext()) {
      auto anyType = SGF.getASTContext().getAnyExistentialType();
      v = SGF.emitTransformedValue(loc, v, anyType, nativeType);
    }

    return v;
  }

  // Bridge NSError to Error.
  if (bridgedType == SGF.SGM.Types.getNSErrorType())
    return SGF.emitBridgedToNativeError(loc, unwrapBridgedOptionals(v));

  return unwrapBridgedOptionals(v);
}

ManagedValue SILGenFunction::emitBridgedToNativeValue(SILLocation loc,
                                                      ManagedValue v,
                                                      CanType bridgedType,
                                                      CanType nativeType,
                                                      SILType loweredNativeTy,
                                                      SGFContext C,
                                                      bool isCallResult) {
  loweredNativeTy = loweredNativeTy.getObjectType();
  SILType loweredBridgedTy = v.getType();
  return emitCBridgedToNativeValue(
      *this, loc, v, bridgedType, loweredBridgedTy, nativeType, loweredNativeTy,
      /*bridgedOptionalsToUnwrap=*/0, isCallResult, C);
}

/// Bridge a possibly-optional foreign error type to Error.
ManagedValue SILGenFunction::emitBridgedToNativeError(SILLocation loc,
                                                  ManagedValue bridgedError) {
  // If the incoming error is non-optional, just do an existential erasure.
  auto bridgedErrorTy = bridgedError.getType().getASTType();
  if (!bridgedErrorTy.getOptionalObjectType()) {
    auto nativeErrorTy = SILType::getExceptionType(getASTContext());

    auto conformance = SGM.getNSErrorConformanceToError();
    if (!conformance)
      return emitUndef(nativeErrorTy);
    ProtocolConformanceRef conformanceArray[] = {
      ProtocolConformanceRef(conformance)
    };
    auto conformances = getASTContext().AllocateCopy(conformanceArray);

    return B.createInitExistentialRef(loc, nativeErrorTy, bridgedErrorTy,
                                      bridgedError, conformances);
  }

  // Otherwise, we need to call a runtime function to potential substitute
  // a standard error for a nil NSError.
  auto bridgeFn = emitGlobalFunctionRef(loc, SGM.getNSErrorToErrorFn());
  auto bridgeFnType = bridgeFn->getType().castTo<SILFunctionType>();
  assert(bridgeFnType->getNumResults() == 1);
  assert(bridgeFnType->getResults()[0].getConvention()
         == ResultConvention::Owned);

  assert(bridgeFnType->getParameters()[0].getConvention()
	       == ParameterConvention::Direct_Guaranteed);
  (void) bridgeFnType;

  SILValue arg = bridgedError.getValue();

  SILValue nativeError = B.createApply(loc, bridgeFn, {}, arg);
  return emitManagedRValueWithCleanup(nativeError);
}

/// Bridge Error to a foreign error type.
ManagedValue SILGenFunction::emitNativeToBridgedError(SILLocation loc,
                                                      ManagedValue nativeError,
                                                      CanType nativeType,
                                                      CanType bridgedErrorType){
  // Handle injections into optional.
  if (auto bridgedObjectType = bridgedErrorType.getOptionalObjectType()) {
    auto loweredBridgedOptionalTy =
      SILType::getPrimitiveObjectType(bridgedErrorType);
    return emitOptionalSome(
        loc, loweredBridgedOptionalTy,
        [&](SILGenFunction &SGF, SILLocation loc, SGFContext C) {
          SILType loweredBridgedObjectTy =
              loweredBridgedOptionalTy.getOptionalObjectType();
          return emitNativeToBridgedValue(loc, nativeError, nativeType,
                                          bridgedObjectType,
                                          loweredBridgedObjectTy);
        });
  }

  assert(bridgedErrorType == SGM.Types.getNSErrorType() &&
         "only handling NSError for now");

  // The native error might just be a value of a type that conforms to
  // Error.  This should be a subtyping or erasure conversion of the sort
  // that we can do automatically.
  // FIXME: maybe we should use a different entrypoint for this case, to
  // avoid the code size and performance overhead of forming the box?
  nativeError = emitUnabstractedCast(*this, loc, nativeError, nativeType,
                                     getASTContext().getErrorExistentialType());

  auto bridgeFn = emitGlobalFunctionRef(loc, SGM.getErrorToNSErrorFn());
  auto bridgeFnType = bridgeFn->getType().castTo<SILFunctionType>();
  assert(bridgeFnType->getNumResults() == 1);
  assert(bridgeFnType->getResults()[0].getConvention()
         == ResultConvention::Owned);
  assert(bridgeFnType->getParameters()[0].getConvention()
         == ParameterConvention::Direct_Guaranteed);
  (void) bridgeFnType;

  SILValue arg = nativeError.getValue();

  SILValue bridgedError = B.createApply(loc, bridgeFn, {}, arg);
  return emitManagedRValueWithCleanup(bridgedError);
}

//===----------------------------------------------------------------------===//
// ObjC method thunks
//===----------------------------------------------------------------------===//

static SILValue emitBridgeReturnValue(SILGenFunction &SGF,
                                      SILLocation loc,
                                      SILValue result,
                                      CanType formalNativeTy,
                                      CanType formalBridgedTy,
                                      SILType loweredBridgedTy) {
  Scope scope(SGF.Cleanups, CleanupLocation(loc));

  ManagedValue native = SGF.emitManagedRValueWithCleanup(result);
  ManagedValue bridged =
    SGF.emitNativeToBridgedValue(loc, native, formalNativeTy, formalBridgedTy,
                                 loweredBridgedTy);
  return bridged.forward(SGF);
}

/// Take an argument at +0 and bring it to +1.
static SILValue emitObjCUnconsumedArgument(SILGenFunction &SGF,
                                           SILLocation loc,
                                           SILValue arg) {
  auto &lowering = SGF.getTypeLowering(arg->getType());
  // If address-only, make a +1 copy and operate on that.
  if (lowering.isAddressOnly()) {
    auto tmp = SGF.emitTemporaryAllocation(loc, arg->getType().getObjectType());
    SGF.B.createCopyAddr(loc, arg, tmp, IsNotTake, IsInitialization);
    return tmp;
  }

  return lowering.emitCopyValue(SGF.B, loc, arg);
}

static CanAnyFunctionType substGenericArgs(CanAnyFunctionType fnType,
                                           SubstitutionMap subs) {
  if (auto genericFnType = dyn_cast<GenericFunctionType>(fnType)) {
    return cast<FunctionType>(genericFnType->substGenericArgs(subs)
                                           ->getCanonicalType());
  }
  return fnType;
}

/// Bridge argument types and adjust retain count conventions for an ObjC thunk.
static SILFunctionType *
emitObjCThunkArguments(SILGenFunction &SGF, SILLocation loc, SILDeclRef thunk,
                       SmallVectorImpl<SILValue> &args,
                       SILValue &foreignErrorSlot, SILValue &foreignAsyncSlot,
                       std::optional<ForeignErrorConvention> foreignError,
                       std::optional<ForeignAsyncConvention> foreignAsync,
                       CanType &nativeFormalResultTy,
                       CanType &bridgedFormalResultTy) {
  SILDeclRef native = thunk.asForeign(false);

  auto subs = SGF.F.getForwardingSubstitutionMap();

  auto objcInfo =
      SGF.SGM.Types.getConstantInfo(SGF.getTypeExpansionContext(), thunk);
  auto objcFnTy = objcInfo.SILFnType->substGenericArgs(
      SGF.SGM.M, subs, SGF.getTypeExpansionContext());
  auto objcFormalFnTy = substGenericArgs(objcInfo.LoweredType, subs);

  auto swiftInfo =
      SGF.SGM.Types.getConstantInfo(SGF.getTypeExpansionContext(), native);
  auto swiftFnTy = swiftInfo.SILFnType->substGenericArgs(
      SGF.SGM.M, subs, SGF.getTypeExpansionContext());
  auto swiftFormalFnTy = substGenericArgs(swiftInfo.LoweredType, subs);
  SILFunctionConventions swiftConv(swiftFnTy, SGF.SGM.M);

  SmallVector<ManagedValue, 8> bridgedArgs;
  bridgedArgs.reserve(objcFnTy->getParameters().size());

  auto bridgedFormalTypes = getParameterTypes(objcFormalFnTy.getParams());
  bridgedFormalResultTy = objcFormalFnTy.getResult();

  auto nativeFormalTypes = getParameterTypes(swiftFormalFnTy.getParams());
  nativeFormalResultTy = swiftFormalFnTy.getResult();

  // Emit the other arguments, taking ownership of arguments if necessary.
  auto inputs = objcFnTy->getParameters();
  auto nativeInputs = swiftFnTy->getParameters();
  auto fnConv = SGF.silConv.getFunctionConventions(swiftFnTy);
  assert(nativeInputs.size() == bridgedFormalTypes.size());
  assert(nativeInputs.size() == nativeFormalTypes.size());
  assert(inputs.size() ==
           nativeInputs.size() + unsigned(foreignError.has_value())
                               + unsigned(foreignAsync.has_value()));
  for (unsigned i = 0, e = inputs.size(); i < e; ++i) {
    SILType argTy = SGF.getSILType(inputs[i], objcFnTy);
    SILValue arg = SGF.F.begin()->createFunctionArgument(argTy);

    // If this parameter is the foreign error or completion slot, pull it out.
    // It does not correspond to a native argument.
    if (foreignError && i == foreignError->getErrorParameterIndex()) {
      foreignErrorSlot = arg;
      continue;
    }

    if (foreignAsync && i == foreignAsync->completionHandlerParamIndex()) {
      // Copy the block.
      foreignAsyncSlot = SGF.B.createCopyBlock(loc, arg);
      // If the argument is consumed, we're still responsible for releasing the
      // original.
      if (inputs[i].isConsumed())
        SGF.emitManagedRValueWithCleanup(arg);
      continue;
    }

    // If the argument is a block, copy it.
    if (argTy.isBlockPointerCompatible()) {
      auto copy = SGF.B.createCopyBlock(loc, arg);
      // If the argument is consumed, we're still responsible for releasing the
      // original.
      if (inputs[i].isConsumed())
        SGF.emitManagedRValueWithCleanup(arg);
      arg = copy;
    }
    // Convert the argument to +1 if necessary.
    else if (!inputs[i].isConsumed()) {
      arg = emitObjCUnconsumedArgument(SGF, loc, arg);
    }

    auto managedArg = SGF.emitManagedRValueWithCleanup(arg);

    bridgedArgs.push_back(managedArg);
  }

  assert(bridgedArgs.size()
           + unsigned(foreignError.has_value())
           + unsigned(foreignAsync.has_value())
        == objcFnTy->getParameters().size() &&
         "objc inputs don't match number of arguments?!");
  assert(bridgedArgs.size() == swiftFnTy->getParameters().size() &&
         "swift inputs don't match number of arguments?!");
  assert((foreignErrorSlot || !foreignError) &&
         "didn't find foreign error slot");

  // Bridge the input types.
  assert(bridgedArgs.size() == nativeInputs.size());
  for (unsigned i = 0, size = bridgedArgs.size(); i < size; ++i) {
    // Consider the bridged values to be "call results" since they're coming
    // from potentially nil-unsound ObjC callers.
    ManagedValue native = SGF.emitBridgedToNativeValue(
        loc, bridgedArgs[i], bridgedFormalTypes[i], nativeFormalTypes[i],
        swiftFnTy->getParameters()[i].getSILStorageType(
            SGF.SGM.M, swiftFnTy, SGF.getTypeExpansionContext()),
        SGFContext(),
        /*isCallResult*/ true);
    SILValue argValue;

    // This can happen if the value is resilient in the calling convention
    // but not resilient locally.
    if (fnConv.isSILIndirect(nativeInputs[i]) &&
        !native.getType().isAddress()) {
      auto buf = SGF.emitTemporaryAllocation(loc, native.getType());
      native.forwardInto(SGF, loc, buf);
      native = SGF.emitManagedBufferWithCleanup(buf);
    }

    if (nativeInputs[i].isConsumed()) {
      argValue = native.forward(SGF);
    } else if (nativeInputs[i].isGuaranteed()) {
      argValue = native.borrow(SGF, loc).getUnmanagedValue();
    } else {
      argValue = native.getValue();
    }

    args.push_back(argValue);
  }

  return objcFnTy;
}

SILFunction *SILGenFunction::emitNativeAsyncToForeignThunk(SILDeclRef thunk) {
  assert(thunk.isForeign);
  assert(thunk.hasAsync());
  SILDeclRef native = thunk.asForeign(false);

  // Use the same generic environment as the native entry point.
  F.setGenericEnvironment(SGM.Types.getConstantGenericEnvironment(native));

  // Collect the arguments and make copies of them we can absorb into the
  // closure.
  auto subs = F.getForwardingSubstitutionMap();
  SmallVector<SILValue, 4> closureArgs;
  auto objcInfo =
      SGM.Types.getConstantInfo(getTypeExpansionContext(), thunk);
  auto objcFnTy = objcInfo.SILFnType->substGenericArgs(
      SGM.M, subs, getTypeExpansionContext());
  auto loc = thunk.getAsRegularLocation();
  loc.markAutoGenerated();
  
  Scope scope(*this, loc);

  for (auto input : objcFnTy->getParameters()) {
    SILType argTy = getSILType(input, objcFnTy);
    SILValue arg = F.begin()->createFunctionArgument(argTy);
    // Copy block arguments.
    if (argTy.isBlockPointerCompatible()) {
      auto argCopy = B.createCopyBlock(loc, arg);
      // If the argument is consumed, we're still responsible for releasing the
      // original.
      if (input.isConsumed())
        emitManagedRValueWithCleanup(arg);
      arg = argCopy;
    } else if (!input.isConsumed()) {
      arg = emitObjCUnconsumedArgument(*this, loc, arg);
    }
    auto managedArg = emitManagedRValueWithCleanup(arg);
    closureArgs.push_back(managedArg.forward(*this));
  }
  
  // Create the closure implementation function. It has the same signature,
  // but is swiftcc and async.
  auto closureExtInfo = objcFnTy->getExtInfo().intoBuilder()
    .withRepresentation(SILFunctionTypeRepresentation::Thin)
    .withAsync()
    .withSendable()
    .build();
  auto closureTy = objcInfo.SILFnType->getWithExtInfo(closureExtInfo);
  
  SmallString<64> closureName(F.getName().begin(), F.getName().end());
  // Trim off the thunk suffix and mangle this like a closure nested inside the
  // thunk (which it sorta is)
  char thunkSuffix[2] = {closureName.pop_back_val(),
                         closureName.pop_back_val()};
  assert(thunkSuffix[1] == 'T'
         && thunkSuffix[0] == 'o'
         && "not an objc thunk?");
  closureName += "yyYacfU_"; // closure with type () async -> ()
  closureName.push_back(thunkSuffix[1]);
  closureName.push_back(thunkSuffix[0]);

  SILGenFunctionBuilder fb(SGM);
  auto closure = fb.getOrCreateSharedFunction(
      loc, closureName, closureTy, IsBare, IsNotTransparent,
      F.getSerializedKind(), ProfileCounter(), IsThunk, IsNotDynamic,
      IsNotDistributed, IsNotRuntimeAccessible);

  auto closureRef = B.createFunctionRef(loc, closure);

  auto closureVal = B.createPartialApply(loc, closureRef, subs,
                                      closureArgs,
                                      ParameterConvention::Direct_Guaranteed);
  auto closureMV = emitManagedRValueWithCleanup(closureVal);
  // Pass the closure on to the intrinsic to spawn it on a task.
  auto spawnTask = SGM.getRunTaskForBridgedAsyncMethod();
  emitApplyOfLibraryIntrinsic(loc, spawnTask, {}, closureMV, SGFContext());
  
  scope.pop();
  
  // Return void to the immediate caller.
  B.createReturn(loc, SILUndef::get(&F, SGM.Types.getEmptyTupleType()));

  return closure;
}

void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
  assert(thunk.isForeign);
  SILDeclRef native = thunk.asForeign(false);

  if (thunk.hasDecl()) {
    if (thunk.getDecl()->requiresUnavailableDeclABICompatibilityStubs())
      emitApplyOfUnavailableCodeReached();
  }

  // If we're calling a native non-designated class initializer, we have to
  // discard the `self` object we were given, since
  // Swift convenience initializers only have allocating entry points that
  // create whole new objects.
  bool isInitializingToAllocatingInitThunk = false;
  if (native.kind == SILDeclRef::Kind::Initializer) {
    if (auto ctor = dyn_cast<ConstructorDecl>(native.getDecl())) {
      if (!ctor->isDesignatedInit() && !ctor->isObjC()) {
        isInitializingToAllocatingInitThunk = true;
        native = SILDeclRef(ctor, SILDeclRef::Kind::Allocator);
      }
    }
  }

  // Use the same generic environment as the native entry point.
  // We need to set this before we can call things like
  // F.getForwardingSubstitutionMap().
  F.setGenericEnvironment(SGM.Types.getConstantGenericEnvironment(native));

  auto nativeInfo = getConstantInfo(getTypeExpansionContext(), native);
  auto subs = F.getForwardingSubstitutionMap();
  auto substTy = nativeInfo.SILFnType->substGenericArgs(
      SGM.M, subs, getTypeExpansionContext());
  SILFunctionConventions substConv(substTy, SGM.M);

  auto loc = thunk.getAsRegularLocation();
  loc.markAutoGenerated();
  Scope scope(Cleanups, CleanupLocation(loc));

  std::optional<ActorIsolation> isolation;
  if (F.isAsync()) {
    if (thunk.hasDecl())
      isolation = getActorIsolation(thunk.getDecl());
  } else if (getASTContext()
                 .LangOpts.isDynamicActorIsolationCheckingEnabled()) {
    if (thunk.hasDecl()) {
      isolation = getActorIsolation(thunk.getDecl());
    } else if (auto globalActor = nativeInfo.FormalType->getGlobalActor()) {
      isolation = ActorIsolation::forGlobalActor(globalActor);
    }
  }

  // A hop/check is only needed in the thunk if it is global-actor isolated.
  // Native, instance-isolated async methods will hop in the prologue.
  if (isolation && isolation->isGlobalActor()) {
    if (F.isAsync()) {
      // Hop to the actor for the method's actor constraint.
      // Note that, since an async native-to-foreign thunk only ever runs in a
      // task purpose-built for running the Swift async code triggering the
      // completion handler, there is no need for us to hop back to the existing
      // executor, since the task will end after we invoke the completion handler.
      emitPrologGlobalActorHop(loc, isolation->getGlobalActor());
    } else {
      emitPreconditionCheckExpectedExecutor(loc, *isolation, std::nullopt);
    }
  }

  std::optional<ForeignErrorConvention> foreignError;
  std::optional<ForeignAsyncConvention> foreignAsync;

  // Find the foreign error and async conventions if we have one.
  if (thunk.hasDecl()) {
    if (auto func = dyn_cast<AbstractFunctionDecl>(thunk.getDecl())) {
      foreignError = func->getForeignErrorConvention();
      foreignAsync = func->getForeignAsyncConvention();
    }
  }

  // If we are bridging a Swift method with Any return value(s), create a
  // stack allocation to hold the result(s), since Any is address-only.
  SmallVector<SILValue, 4> args;
  SILFunctionConventions funcConv = F.getConventions();
  bool needsBridging = true;
  if (substConv.hasIndirectSILResults()) {
    for (auto result : substConv.getResults()) {
      if (!substConv.isSILIndirect(result)) {
        continue;
      }

      if (!foreignAsync && funcConv.hasIndirectSILResults()) {
        auto resultTy =
            funcConv.getSingleSILResultType(getTypeExpansionContext());
        assert(substConv.getSingleSILResultType(getTypeExpansionContext()) ==
               resultTy);
        args.push_back(F.begin()->createFunctionArgument(resultTy));
        needsBridging = false;
        break;
      }

      args.push_back(emitTemporaryAllocation(
          loc, substConv.getSILType(result, getTypeExpansionContext())));
    }
  }

  // Now, enter a cleanup used for bridging the arguments. Note that if we
  // have an indirect result, it must be outside of this scope, otherwise
  // we will deallocate it too early.
  Scope argScope(Cleanups, CleanupLocation(loc));

  // Bridge the arguments.
  SILValue foreignErrorSlot;
  SILValue foreignAsyncSlot;
  CanType nativeFormalResultType, bridgedFormalResultType;
  auto objcFnTy = emitObjCThunkArguments(*this, loc, thunk, args,
                                         foreignErrorSlot, foreignAsyncSlot,
                                         foreignError, foreignAsync,
                                         nativeFormalResultType,
                                         bridgedFormalResultType);

  // Throw away the partially-initialized `self` value we were given if we're
  // bridging from an initializing to allocating entry point.
  if (isInitializingToAllocatingInitThunk) {
    auto oldSelf = args.pop_back_val();
    auto oldSelfTy = B.createValueMetatype(loc,
         SILType::getPrimitiveObjectType(
           CanMetatypeType::get(oldSelf->getType().getASTType(),
                                MetatypeRepresentation::Thick)),
         oldSelf);
    
    B.createDeallocPartialRef(loc, oldSelf, oldSelfTy);
    
    // Pass the dynamic type on to the native allocating initializer.
    args.push_back(oldSelfTy);
    native = SILDeclRef(native.getDecl(), SILDeclRef::Kind::Allocator);
  }

  SILFunctionConventions objcConv(CanSILFunctionType(objcFnTy), SGM.M);
  SILFunctionConventions nativeConv(CanSILFunctionType(nativeInfo.SILFnType),
                                    SGM.M);
  auto swiftResultTy = F.mapTypeIntoContext(
      nativeConv.getSILResultType(getTypeExpansionContext()));
  auto objcResultTy = objcConv.getSILResultType(getTypeExpansionContext());

  // Call the native entry point.
  SILValue nativeFn = emitGlobalFunctionRef(loc, native, nativeInfo);

  SILValue result;
  
  CanSILFunctionType completionTy;
  bool completionIsOptional = false;
  if (foreignAsyncSlot) {
    completionTy = foreignAsyncSlot->getType().getAs<SILFunctionType>();
    if (!completionTy) {
      completionTy = foreignAsyncSlot->getType().getOptionalObjectType()
        .castTo<SILFunctionType>();
      completionIsOptional = true;
    }
  }
  
  // Helper function to take ownership of the completion handler from the
  // foreign async slot, and unwrap it if it's in an optional.
  auto consumeAndUnwrapCompletionBlock = [&](SILValue &completionBlock,
                                             SILBasicBlock *&doneBBOrNull) {
    auto completionBlockMV = emitManagedRValueWithCleanup(foreignAsyncSlot);
    
    // If the completion handler argument is nullable, and the caller gave us
    // no completion handler, discard the result.
    completionBlock = completionBlockMV.borrow(*this, loc).getValue();
    doneBBOrNull = nullptr;
    if (completionIsOptional) {
      doneBBOrNull = createBasicBlock();
      auto hasCompletionBB = createBasicBlock();
      auto noCompletionBB = createBasicBlock();

      std::pair<EnumElementDecl *, SILBasicBlock *> dests[] = {
        {getASTContext().getOptionalSomeDecl(), hasCompletionBB},
        {getASTContext().getOptionalNoneDecl(), noCompletionBB},
      };

      auto *switchEnum =
          B.createSwitchEnum(loc, completionBlock, nullptr, dests);

      B.emitBlock(noCompletionBB);
      B.createBranch(loc, doneBBOrNull);

      B.emitBlock(hasCompletionBB);
      completionBlock = switchEnum->createOptionalSomeResult();
    }
  };
  
  auto pushErrorFlag = [&](bool hasError,
                           SmallVectorImpl<SILValue> &completionHandlerArgs) {
    bool errorFlagIsZeroOnError = foreignAsync->completionHandlerFlagIsErrorOnZero();
    auto errorFlagIndex = foreignAsync->completionHandlerFlagParamIndex();
    auto errorFlagTy = completionTy->getParameters()[*errorFlagIndex]
      .getSILStorageInterfaceType();
    
    auto errorFlag = emitWrapIntegerLiteral(loc, errorFlagTy,
                                            hasError ^ errorFlagIsZeroOnError);
    
    completionHandlerArgs.push_back(errorFlag);
  };
  
  // Helper function to pass a native async function's result as arguments to
  // the ObjC completion handler block.
  auto passResultToCompletionHandler = [&](SILValue result) -> SILValue {
    Scope completionArgScope(*this, loc);
    
    SmallVector<SILValue, 2> completionHandlerArgs;
    
    auto asyncResult = emitManagedRValueWithCleanup(result);
    
    SILValue completionBlock;
    SILBasicBlock *doneBB;
    consumeAndUnwrapCompletionBlock(completionBlock, doneBB);
    
    auto pushArg = [&](ManagedValue arg,
                       CanType nativeFormalTy,
                       SILParameterInfo param) {
      auto bridgedTy = param.getInterfaceType();
      auto bridgedArg = emitNativeToBridgedValue(loc,
                                 arg.borrow(*this, loc), nativeFormalTy,
                                 bridgedTy,
                                 SILType::getPrimitiveObjectType(bridgedTy));
      completionHandlerArgs.push_back(bridgedArg.getValue());
    };
 
    Scope completionArgDestructureScope(*this, loc);

    auto errorParamIndex = foreignAsync->completionHandlerErrorParamIndex();
    auto errorFlagIndex = foreignAsync->completionHandlerFlagParamIndex();
    auto pushErrorPlaceholder = [&]{
      auto errorArgTy = completionTy->getParameters()[*errorParamIndex]
        .getSILStorageInterfaceType();
      
      // Error type must be optional. We pass nil for a successful return
      auto none = B.createOptionalNone(loc, errorArgTy);
      completionHandlerArgs.push_back(none);
    };
    
    unsigned numResults
      = completionTy->getParameters().size() - errorParamIndex.has_value()
                                             - errorFlagIndex.has_value();
    
    if (numResults == 1) {
      for (unsigned i = 0; i < completionTy->getNumParameters(); ++i) {
        if (errorParamIndex && *errorParamIndex == i) {
          pushErrorPlaceholder();
          continue;
        }
        if (errorFlagIndex && *errorFlagIndex == i) {
          pushErrorFlag(/*has error*/ false, completionHandlerArgs);
          continue;
        }
        
        // Use the indirect return argument if the result is indirect.
        if (substConv.hasIndirectSILResults()) {
          pushArg(emitManagedRValueWithCleanup(args[0]),
                  nativeFormalResultType,
                  completionTy->getParameters()[i]);
        } else {
          pushArg(asyncResult,
                  nativeFormalResultType,
                  completionTy->getParameters()[i]);
        }
      }
    } else {
      // A tuple return maps to multiple completion handler parameters.
      auto formalTuple = cast<TupleType>(nativeFormalResultType);
      
      unsigned indirectResultI = 0;
      unsigned directResultI = 0;
      
      auto directResults = substConv.getDirectSILResults();
      auto hasMultipleDirectResults
        = !directResults.empty() &&
          std::next(directResults.begin()) != directResults.end();
      
      for (unsigned paramI : indices(completionTy->getParameters())) {
        if (errorParamIndex && paramI == *errorParamIndex) {
          pushErrorPlaceholder();
          continue;
        }
        if (errorFlagIndex && paramI == *errorFlagIndex) {
          pushErrorFlag(/*has error*/ false, completionHandlerArgs);
          continue;
        }
        auto elementI = paramI - (errorParamIndex && paramI > *errorParamIndex)
                               - (errorFlagIndex && paramI > *errorFlagIndex);
        auto param = completionTy->getParameters()[paramI];
        auto formalTy = formalTuple.getElementType(elementI);
        ManagedValue argPiece;
        
        auto result = substConv.getResults()[elementI];
        if (substConv.isSILIndirect(result)) {
          // Take the arg piece from the indirect return arguments.
          argPiece = emitManagedRValueWithCleanup(args[indirectResultI++]);
        } else if (hasMultipleDirectResults) {
          // Take the arg piece from one of the tuple elements of the direct
          // result tuple from the apply.
          argPiece = B.createTupleExtract(loc, asyncResult, directResultI++);
        } else {
          // Take the entire direct result from the apply as the arg piece.
          argPiece = asyncResult;
        }
        
        pushArg(argPiece, formalTy, param);
      }
    }
    // Pass the bridged results on to the completion handler.
    B.createApply(loc, completionBlock, {}, completionHandlerArgs);
    completionArgDestructureScope.pop();
    
    if (doneBB) {
      B.createBranch(loc, doneBB);
      B.emitBlock(doneBB);
    }
    
    // The immediate function result is an empty tuple.
    return SILUndef::get(&F, SGM.Types.getEmptyTupleType());
  };

  // If the function we're calling isn't actually polymorphic, drop the
  // substitutions.  This should only happen in concrete specializations.
  if (subs && !nativeFn->getType().castTo<SILFunctionType>()->isPolymorphic()) {
    assert(subs.getGenericSignature()->areAllParamsConcrete());
    subs = SubstitutionMap();
  }
    
  if (!substTy->hasErrorResult()) {
    // Create the apply.
    result = B.createApply(loc, nativeFn, subs, args);
  
    // Leave the argument cleanup scope immediately.  This isn't really
    // necessary; it just limits lifetimes a little bit more.
    argScope.pop();

    // Now bridge the return value.
    // If this is an async method, we forward the results of the async call to
    // the completion handler.
    if (foreignAsync) {
      result = passResultToCompletionHandler(result);
    } else {
      if (needsBridging) {
        if (substConv.hasIndirectSILResults()) {
          assert(substTy->getNumResults() == 1);
          result = args[0];
        }
        result = emitBridgeReturnValue(*this, loc, result, nativeFormalResultType,
                                       bridgedFormalResultType, objcResultTy);
      }
    }
  } else {
    SILBasicBlock *contBB = createBasicBlock();
    SILBasicBlock *errorBB = createBasicBlock();
    SILBasicBlock *normalBB = createBasicBlock();
    B.createTryApply(loc, nativeFn, subs, args, normalBB, errorBB);

    // Emit the non-error destination.
    {
      B.emitBlock(normalBB);
      SILValue nativeResult =
          normalBB->createPhiArgument(swiftResultTy, OwnershipKind::Owned);

      if (foreignAsync) {
        // If the function is async, pass the results as the success argument(s)
        // to the completion handler, with a nil error.
        passResultToCompletionHandler(nativeResult);
        B.createBranch(loc, contBB);
      } else {
        if (substConv.hasIndirectSILResults()) {
          assert(substTy->getNumResults() == 1);
          nativeResult = args[0];
        }
        // In this branch, the eventual return value is mostly created
        // by bridging the native return value, but we may need to
        // adjust it slightly.
        SILValue bridgedResult =
          emitBridgeReturnValueForForeignError(loc, nativeResult,
                                               nativeFormalResultType,
                                               bridgedFormalResultType,
                                               objcResultTy,
                                               foreignErrorSlot, *foreignError);
        B.createBranch(loc, contBB, bridgedResult);
      }
    }

    // Emit the error destination.
    {
      B.emitBlock(errorBB);
      
      SILValue nativeError = errorBB->createPhiArgument(
          substConv.getSILErrorType(getTypeExpansionContext()),
          OwnershipKind::Owned);

      if (foreignAsync) {
        // If the function is async, pass the bridged error along to the
        // completion handler, with dummy values for the other argument(s).
        Scope completionArgScope(*this, loc);
        
        auto nativeErrorMV = emitManagedRValueWithCleanup(nativeError);
        
        SILValue completionBlock;
        SILBasicBlock *doneBB;
        consumeAndUnwrapCompletionBlock(completionBlock, doneBB);

        Scope completionErrorScope(*this, loc);

        SmallVector<SILValue, 2> completionHandlerArgs;
        auto completionTy = completionBlock->getType().castTo<SILFunctionType>();
        auto errorParamIndex = *foreignAsync->completionHandlerErrorParamIndex();
        auto errorFlagIndex = foreignAsync->completionHandlerFlagParamIndex();
        auto completionErrorTy = completionTy->getParameters()[errorParamIndex]
          .getInterfaceType();
        auto bridgedError = emitNativeToBridgedError(loc,
                                           nativeErrorMV,
                                           nativeError->getType().getASTType(),
                                           completionErrorTy);
        
        // Fill in placeholder arguments, and put the bridged error in its
        // rightful place.
        for (unsigned i : indices(completionTy->getParameters())) {
          if (i == errorParamIndex) {
            completionHandlerArgs.push_back(bridgedError.borrow(*this, loc).getValue());
            continue;
          }
          
          if (errorFlagIndex && i == *errorFlagIndex) {
            pushErrorFlag(/*has error*/ true, completionHandlerArgs);
            continue;
          }
          
          // For non-error arguments, pass a placeholder.
          // If the argument type is non-trivial, it must be Optional, and
          // we pass nil.
          auto param = completionTy->getParameters()[i];
          auto paramTy = param.getSILStorageInterfaceType();
          if (paramTy.isTrivial(F)) {
            // If it's trivial, the value passed doesn't matter.
            completionHandlerArgs.push_back(SILUndef::get(&F, paramTy));
          } else {
            // If it's not trivial, it must be a nullable class type. Pass
            // nil.
            auto none = B.createOptionalNone(loc, paramTy);
            completionHandlerArgs.push_back(none);
          }
        }
        // Pass the bridged error on to the completion handler.
        B.createApply(loc, completionBlock, {}, completionHandlerArgs);

        completionErrorScope.pop();
        
        if (doneBB) {
          B.createBranch(loc, doneBB);
          B.emitBlock(doneBB);
        }
        completionArgScope.pop();

        B.createBranch(loc, contBB);
      } else {
        // In this branch, the eventual return value is mostly invented.
        // Store the native error in the appropriate location and return.
        SILValue bridgedResult =
          emitBridgeErrorForForeignError(loc, nativeError, objcResultTy,
                                         foreignErrorSlot, *foreignError);
        B.createBranch(loc, contBB, bridgedResult);
      }
    }

    // Emit the join block.
    B.emitBlock(contBB);
    
    if (foreignAsync) {
      // After invoking the completion handler, our immediate return value is
      // void.
      result = SILUndef::get(&F, SGM.Types.getEmptyTupleType());
    } else {
      result = contBB->createPhiArgument(objcResultTy, OwnershipKind::Owned);
    }

    // Leave the scope now.
    argScope.pop();
  }

  scope.pop();
  B.createReturn(loc, result);
}

static SILValue getThunkedForeignFunctionRef(SILGenFunction &SGF,
                                             AbstractFunctionDecl *fd,
                                             SILDeclRef foreign,
                                             ArrayRef<ManagedValue> args,
                                             const SILConstantInfo &foreignCI) {
  assert(foreign.isForeign);

  // Produce an objc_method when thunking ObjC methods.
  if (foreignCI.SILFnType->getRepresentation() ==
      SILFunctionTypeRepresentation::ObjCMethod) {
    auto *objcDecl =
        dyn_cast_or_null<clang::ObjCMethodDecl>(fd->getClangDecl());
    const bool isObjCDirect = objcDecl && objcDecl->isDirectMethod();
    if (isObjCDirect) {
      auto *fn = SGF.SGM.getFunction(foreign, NotForDefinition);
      return SGF.B.createFunctionRef(fd, fn);
    }

    SILValue thisArg = args.back().getValue();
    return SGF.B.createObjCMethod(fd, thisArg, foreign, foreignCI.getSILType());
  }

  // Otherwise, emit a function_ref.
  return SGF.emitGlobalFunctionRef(fd, foreign);
}

/// Generate code to emit a thunk with native conventions that calls a
/// function with foreign conventions.
void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
  assert(!thunk.isForeign && "foreign-to-native thunks only");

  // Wrap the function in its original form.

  auto fd = cast<AbstractFunctionDecl>(thunk.getDecl());
  auto nativeCI = getConstantInfo(getTypeExpansionContext(), thunk);
  auto nativeFnTy = F.getLoweredFunctionType();
  assert(nativeFnTy == nativeCI.SILFnType);

  if (fd->requiresUnavailableDeclABICompatibilityStubs())
    emitApplyOfUnavailableCodeReached();

  // Use the same generic environment as the native entry point.
  F.setGenericEnvironment(SGM.Types.getConstantGenericEnvironment(thunk));
  
  SILDeclRef foreignDeclRef = thunk.asForeign(true);
  SILConstantInfo foreignCI =
      getConstantInfo(getTypeExpansionContext(), foreignDeclRef);
  auto foreignFnTy = foreignCI.SILFnType;

  // Find the foreign error/async convention and 'self' parameter index.
  std::optional<Type> thrownErrorType;
  std::optional<ForeignAsyncConvention> foreignAsync;
  if (nativeFnTy->isAsync()) {
    foreignAsync = fd->getForeignAsyncConvention();
    assert(foreignAsync && "couldn't find foreign async convention?!");
  }
  std::optional<ForeignErrorConvention> foreignError;
  if (nativeFnTy->hasErrorResult()) {
    thrownErrorType = nativeFnTy->getErrorResult().getInterfaceType();
    foreignError = fd->getForeignErrorConvention();
    assert((foreignError || foreignAsync)
           && "couldn't find foreign error or async convention for foreign error!");
  }
  ImportAsMemberStatus memberStatus = fd->getImportAsMemberStatus();

  // Introduce indirect returns if necessary.
  // TODO: Handle exploded results? We don't currently need to since the only
  // bridged indirect type is Any.
  SILValue indirectResult;
  SILFunctionConventions nativeConv(nativeFnTy, SGM.M);
  if (nativeConv.hasIndirectSILResults()) {
    assert(nativeConv.getNumIndirectSILResults() == 1
           && "bridged exploded result?!");
    indirectResult = F.begin()->createFunctionArgument(
        nativeConv.getSingleSILResultType(F.getTypeExpansionContext()));
  }
  
  // Forward the arguments.
  SmallVector<SILValue, 8> params;

  bindParametersForForwarding(fd->getParameters(), params);
  if (thunk.kind != SILDeclRef::Kind::Allocator)
    if (auto *selfDecl = fd->getImplicitSelfDecl())
      bindParameterForForwarding(selfDecl, params);

  // For allocating constructors, 'self' is a metatype, not the 'self' value
  // formally present in the constructor body.
  Type allocatorSelfType;
  if (thunk.kind == SILDeclRef::Kind::Allocator) {
    auto *selfDecl = fd->getImplicitSelfDecl();
    allocatorSelfType = F.mapTypeIntoContext(
      fd->getDeclContext()->getSelfInterfaceType());

    auto selfMetatype =
      CanMetatypeType::get(allocatorSelfType->getCanonicalType());
    auto selfArg = F.begin()->createFunctionArgument(
        getLoweredLoadableType(selfMetatype), selfDecl);
    params.push_back(selfArg);
  }

  // Set up the throw destination if necessary.
  CleanupLocation cleanupLoc(fd);
  if (thrownErrorType) {
    prepareRethrowEpilog(fd,
                         AbstractionPattern(*thrownErrorType),
                         *thrownErrorType,
                         cleanupLoc);
  }

  SILValue result;
  {
    Scope scope(Cleanups, fd);

    // Bridge all the arguments.
    SmallVector<ManagedValue, 8> args;
    unsigned foreignArgIndex = 0;

    // A helper function to add a placeholder for a foreign argument in the
    // appropriate position.
    auto maybeAddForeignArg = [&]() -> bool {
      if ((foreignError
           && foreignArgIndex == foreignError->getErrorParameterIndex())
          || (foreignAsync
              && foreignArgIndex == foreignAsync->completionHandlerParamIndex()))
      {
        args.push_back(ManagedValue());
        ++foreignArgIndex;
        return true;
      }
      
      return false;
    };

    {
      bool hasSelfParam = fd->hasImplicitSelfDecl();
      auto foreignFormalParams =
        getParameterTypes(foreignCI.LoweredType.getParams(), hasSelfParam);
      auto nativeFormalParams =
        getParameterTypes(nativeCI.LoweredType.getParams(), hasSelfParam);

      for (unsigned nativeParamIndex : indices(params)) {
        // Bring the parameter to +1.
        auto paramValue = params[nativeParamIndex];
        auto thunkParam = nativeFnTy->getParameters()[nativeParamIndex];
        // TODO: Could avoid a retain if the bridged parameter is also +0 and
        // doesn't require a bridging conversion.
        ManagedValue param;
        switch (thunkParam.getConvention()) {
        case ParameterConvention::Direct_Owned:
          param = emitManagedRValueWithCleanup(paramValue);
          break;
        case ParameterConvention::Direct_Guaranteed:
        case ParameterConvention::Direct_Unowned:
          param = emitManagedCopy(fd, paramValue);
          break;
        case ParameterConvention::Indirect_Inout:
        case ParameterConvention::Indirect_InoutAliasable:
          param = ManagedValue::forLValue(paramValue);
          break;
        case ParameterConvention::Indirect_In:
          param = emitManagedRValueWithCleanup(paramValue);
          break;
        case ParameterConvention::Indirect_In_Guaranteed: {
          auto tmp = emitTemporaryAllocation(fd, paramValue->getType());
          B.createCopyAddr(fd, paramValue, tmp, IsNotTake, IsInitialization);
          param = emitManagedRValueWithCleanup(tmp);
          break;
        }
        case ParameterConvention::Pack_Guaranteed:
        case ParameterConvention::Pack_Owned:
        case ParameterConvention::Pack_Inout:
          llvm_unreachable("bridging a parameter pack?");
        }

        while (maybeAddForeignArg());

        bool isSelf = (hasSelfParam && nativeParamIndex == params.size() - 1);

        if (memberStatus.isInstance()) {
          // Leave space for `self` to be filled in later.
          if (foreignArgIndex == memberStatus.getSelfIndex()) {
            args.push_back({});
            ++foreignArgIndex;
          }
          
          // Use the `self` space we skipped earlier if it's time.
          if (isSelf) {
            foreignArgIndex = memberStatus.getSelfIndex();
          }
        } else if (memberStatus.isStatic() && isSelf) {
          // Lose a static `self` parameter.
          break;
        }

        CanType nativeFormalType =
          F.mapTypeIntoContext(nativeFormalParams[nativeParamIndex])
            ->getCanonicalType();
        CanType foreignFormalType =
          F.mapTypeIntoContext(foreignFormalParams[nativeParamIndex])
            ->getCanonicalType();

        if (isSelf) {
          assert(!nativeCI.LoweredType.getParams()[nativeParamIndex].isInOut() ||
                 nativeFormalType == foreignFormalType &&
                 "Cannot bridge 'self' parameter if passed inout");
        }

        auto foreignParam = foreignFnTy->getParameters()[foreignArgIndex++];
        SILType foreignLoweredTy =
            F.mapTypeIntoContext(foreignParam.getSILStorageType(
                F.getModule(), foreignFnTy, F.getTypeExpansionContext()));

        auto bridged = emitNativeToBridgedValue(fd, param, nativeFormalType,
                                                foreignFormalType,
                                                foreignLoweredTy);
        if (useLoweredAddresses() &&
            (foreignParam.getConvention() == ParameterConvention::Indirect_In ||
             foreignParam.getConvention() ==
                 ParameterConvention::Indirect_In_Guaranteed)) {
          auto temp = emitTemporaryAllocation(fd, bridged.getType());
          bridged.forwardInto(*this, fd, temp);
          bridged = emitManagedBufferWithCleanup(temp);
        }

        if (memberStatus.isInstance() && isSelf) {
          // Fill in the `self` space.
          args[memberStatus.getSelfIndex()] = bridged;
        } else {
          args.push_back(bridged);
        }
      }
    }

    while (maybeAddForeignArg());
    
    // Call the original.
    auto subs = getForwardingSubstitutionMap();
    auto fn = getThunkedForeignFunctionRef(*this, fd, foreignDeclRef, args,
                                           foreignCI);

    auto fnType = fn->getType().castTo<SILFunctionType>();
    fnType = fnType->substGenericArgs(SGM.M, subs, getTypeExpansionContext());

    CanType nativeFormalResultType =
        fd->mapTypeIntoContext(nativeCI.LoweredType.getResult())
            ->getCanonicalType();
    CanType bridgedFormalResultType =
        fd->mapTypeIntoContext(foreignCI.LoweredType.getResult())
            ->getCanonicalType();
    CalleeTypeInfo calleeTypeInfo(
        fnType, AbstractionPattern(nativeFnTy->getInvocationGenericSignature(),
                                   bridgedFormalResultType),
        nativeFormalResultType,
        foreignError,
        foreignAsync,
        ImportAsMemberStatus());
    calleeTypeInfo.origFormalType =
        foreignCI.FormalPattern.getFunctionResultType();

    auto init = indirectResult
                ? useBufferAsTemporary(indirectResult,
                                    getTypeLowering(indirectResult->getType()))
                : nullptr;

    SGFContext context(init.get());
    ResultPlanPtr resultPlan = ResultPlanBuilder::computeResultPlan(
        *this, calleeTypeInfo, fd, context);
    ArgumentScope argScope(*this, fd);
    ManagedValue resultMV =
        emitApply(std::move(resultPlan), std::move(argScope), fd,
                  ManagedValue::forObjectRValueWithoutOwnership(fn), subs, args,
                  calleeTypeInfo, ApplyOptions(), context, std::nullopt)
            .getAsSingleValue(*this, fd);

    if (indirectResult) {
      if (!resultMV.isInContext()) {
        init->copyOrInitValueInto(*this, fd, resultMV, /*isInit*/ true);
        init->finishInitialization(*this);
      }
      init->getManagedAddress().forward(*this);
      result = emitEmptyTuple(fd);
    } else {
      result = resultMV.forward(*this);
    }
  }
  B.createReturn(ImplicitReturnLocation(fd), result);
  // Emit the throw destination.
  emitRethrowEpilog(fd);
}