File: ModuleInterfaceLoader.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 (2844 lines) | stat: -rw-r--r-- 119,847 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
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
//===------ ModuleInterfaceLoader.cpp - Loads .swiftinterface files -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 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
//
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "textual-module-interface"

#include "swift/Frontend/ModuleInterfaceLoader.h"
#include "ModuleInterfaceBuilder.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/FileSystem.h"
#include "swift/AST/Module.h"
#include "swift/AST/SearchPathOptions.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/StringExtras.h"
#include "swift/Frontend/CachingUtils.h"
#include "swift/Frontend/CompileJobCacheResult.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/ModuleInterfaceSupport.h"
#include "swift/Parse/ParseVersion.h"
#include "swift/Serialization/SerializationOptions.h"
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/Serialization/Validation.h"
#include "swift/Strings.h"
#include "clang/Basic/Module.h"
#include "clang/Frontend/CompileJobCacheResult.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Twine.h"
#include "llvm/CAS/ActionCache.h"
#include "llvm/CAS/ObjectStore.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VirtualOutputBackend.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/xxhash.h"

using namespace swift;
using FileDependency = SerializationOptions::FileDependency;

#pragma mark - Forwarding Modules

namespace {

/// Describes a "forwarding module", that is, a .swiftmodule that's actually
/// a YAML file inside, pointing to a the original .swiftmodule but describing
/// a different dependency resolution strategy.
struct ForwardingModule {
  /// The path to the original .swiftmodule in the prebuilt cache.
  std::string underlyingModulePath;

  /// Describes a set of file-based dependencies with their size and
  /// modification time stored. This is slightly different from
  /// \c SerializationOptions::FileDependency, because this type needs to be
  /// serializable to and from YAML.
  struct Dependency {
    std::string path;
    uint64_t size;
    uint64_t lastModificationTime;
    bool isSDKRelative;
  };
  std::vector<Dependency> dependencies;
  unsigned version = 1;

  ForwardingModule() = default;
  ForwardingModule(StringRef underlyingModulePath)
  : underlyingModulePath(underlyingModulePath) {}

  /// Loads the contents of the forwarding module whose contents lie in
  /// the provided buffer, and returns a new \c ForwardingModule, or an error
  /// if the YAML could not be parsed.
  static llvm::ErrorOr<ForwardingModule> load(const llvm::MemoryBuffer &buf);

  /// Adds a given dependency to the dependencies list.
  void addDependency(StringRef path, bool isSDKRelative, uint64_t size,
                     uint64_t modTime) {
    dependencies.push_back({path.str(), size, modTime, isSDKRelative});
  }
};

} // end anonymous namespace

#pragma mark - YAML Serialization

namespace llvm {
  namespace yaml {
    template <>
    struct MappingTraits<ForwardingModule::Dependency> {
      static void mapping(IO &io, ForwardingModule::Dependency &dep) {
        io.mapRequired("mtime", dep.lastModificationTime);
        io.mapRequired("path", dep.path);
        io.mapRequired("size", dep.size);
        io.mapOptional("sdk_relative", dep.isSDKRelative, /*default*/false);
      }
    };

    template <>
    struct SequenceElementTraits<ForwardingModule::Dependency> {
      static const bool flow = false;
    };

    template <>
    struct MappingTraits<ForwardingModule> {
      static void mapping(IO &io, ForwardingModule &module) {
        io.mapRequired("path", module.underlyingModulePath);
        io.mapRequired("dependencies", module.dependencies);
        io.mapRequired("version", module.version);
      }
    };
  }
} // end namespace llvm

llvm::ErrorOr<ForwardingModule>
ForwardingModule::load(const llvm::MemoryBuffer &buf) {
  llvm::yaml::Input yamlIn(buf.getBuffer());
  ForwardingModule fwd;
  yamlIn >> fwd;
  if (yamlIn.error())
    return yamlIn.error();

  // We only currently support Version 1 of the forwarding module format.
  if (fwd.version != 1)
    return std::make_error_code(std::errc::not_supported);
  return std::move(fwd);
}

#pragma mark - Module Discovery

namespace {

/// The result of a search for a module either alongside an interface, in the
/// module cache, or in the prebuilt module cache.
class DiscoveredModule {
  /// The kind of module we've found.
  enum class Kind {
    /// A module that's either alongside the swiftinterface or in the
    /// module cache.
    Normal,

    /// A module that resides in the prebuilt cache, and has hash-based
    /// dependencies.
    Prebuilt,

    /// A 'forwarded' module. This is a module in the prebuilt cache, but whose
    /// dependencies live in a forwarding module.
    /// \sa ForwardingModule.
    Forwarded
  };

  /// The kind of module that's been discovered.
  const Kind kind;

  DiscoveredModule(StringRef path, Kind kind,
    std::unique_ptr<llvm::MemoryBuffer> moduleBuffer)
    : kind(kind), moduleBuffer(std::move(moduleBuffer)), path(path) {}

public:
  /// The contents of the .swiftmodule, if we've read it while validating
  /// dependencies.
  std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;

  /// The path to the discovered serialized .swiftmodule on disk.
  const std::string path;

  /// Creates a \c Normal discovered module.
  static DiscoveredModule normal(StringRef path,
      std::unique_ptr<llvm::MemoryBuffer> moduleBuffer) {
    return { path, Kind::Normal, std::move(moduleBuffer) };
  }

  /// Creates a \c Prebuilt discovered module.
  static DiscoveredModule prebuilt(
      StringRef path, std::unique_ptr<llvm::MemoryBuffer> moduleBuffer) {
    return { path, Kind::Prebuilt, std::move(moduleBuffer) };
  }

  /// Creates a \c Forwarded discovered module, whose dependencies have been
  /// externally validated by a \c ForwardingModule.
  static DiscoveredModule forwarded(
      StringRef path, std::unique_ptr<llvm::MemoryBuffer> moduleBuffer) {
    return { path, Kind::Forwarded, std::move(moduleBuffer) };
  }

  bool isNormal() const { return kind == Kind::Normal; }
  bool isPrebuilt() const { return kind == Kind::Prebuilt; }
  bool isForwarded() const { return kind == Kind::Forwarded; }
};

} // end anonymous namespace

#pragma mark - Common utilities

namespace path = llvm::sys::path;

static bool serializedASTLooksValid(const llvm::MemoryBuffer &buf,
                                    bool requiresOSSAModules,
                                    StringRef requiredSDK) {
  auto VI = serialization::validateSerializedAST(buf.getBuffer(),
                                                 requiresOSSAModules,
                                                 requiredSDK);
  return VI.status == serialization::Status::Valid;
}

#pragma mark - Module Loading

namespace {

/// Keeps track of the various reasons the module interface loader needed to
/// fall back and rebuild a module from its interface.
struct ModuleRebuildInfo {
  enum class ModuleKind {
    Normal,
    Cached,
    Forwarding,
    Prebuilt
  };
  enum class ReasonIgnored {
    NotIgnored,
    PublicFramework,
    InterfacePreferred,
    CompilerHostModule,
    Blocklisted,
    DistributedInterfaceByDefault,
  };
  // Keep aligned with diag::module_interface_ignored_reason.
  enum class ReasonModuleInterfaceIgnored {
    NotIgnored,
    LocalModule,
    Blocklisted,
    Debugger,
  };
  struct CandidateModule {
    std::string path;
    std::optional<serialization::Status> serializationStatus;
    ModuleKind kind;
    ReasonIgnored reasonIgnored;
    ReasonModuleInterfaceIgnored reasonModuleInterfaceIgnored;
    SmallVector<std::string, 10> outOfDateDependencies;
    SmallVector<std::string, 10> missingDependencies;
  };
  SmallVector<CandidateModule, 3> candidateModules;

  CandidateModule &getOrInsertCandidateModule(StringRef path) {
    for (auto &mod : candidateModules) {
      if (mod.path == path) return mod;
    }
    candidateModules.push_back({path.str(),
                                std::nullopt,
                                ModuleKind::Normal,
                                ReasonIgnored::NotIgnored,
                                ReasonModuleInterfaceIgnored::NotIgnored,
                                {},
                                {}});
    return candidateModules.back();
  }

  /// Sets the kind of a module that failed to load.
  void setModuleKind(StringRef path, ModuleKind kind) {
    getOrInsertCandidateModule(path).kind = kind;
  }

  /// Sets the serialization status of the module at \c path. If this is
  /// anything other than \c Valid, a note will be added stating why the module
  /// was invalid.
  void setSerializationStatus(StringRef path, serialization::Status status) {
    getOrInsertCandidateModule(path).serializationStatus = status;
  }

  /// Registers an out-of-date dependency at \c depPath for the module
  /// at \c modulePath.
  void addOutOfDateDependency(StringRef modulePath, StringRef depPath) {
    getOrInsertCandidateModule(modulePath)
        .outOfDateDependencies.push_back(depPath.str());
  }

  /// Registers a missing dependency at \c depPath for the module
  /// at \c modulePath.
  void addMissingDependency(StringRef modulePath, StringRef depPath) {
    getOrInsertCandidateModule(modulePath)
        .missingDependencies.push_back(depPath.str());
  }

  /// Sets the reason that the module at \c modulePath was ignored. If this is
  /// anything besides \c NotIgnored a note will be added stating why the module
  /// was ignored.
  void addIgnoredModule(StringRef modulePath, ReasonIgnored reasonIgnored) {
    getOrInsertCandidateModule(modulePath).reasonIgnored = reasonIgnored;
  }

  /// Record why no swiftinterfaces were preferred over the binary swiftmodule
  /// at \c modulePath.
  void addIgnoredModuleInterface(StringRef modulePath,
                                 ReasonModuleInterfaceIgnored reasonIgnored) {
    getOrInsertCandidateModule(modulePath).reasonModuleInterfaceIgnored =
                                                                 reasonIgnored;
  }

  /// Determines if we saw the given module path and registered is as out of
  /// date.
  bool sawOutOfDateModule(StringRef modulePath) {
    for (auto &mod : candidateModules)
      if (mod.path == modulePath &&
          mod.reasonIgnored == ReasonIgnored::NotIgnored)
        return true;
    return false;
  }

  const char *invalidModuleReason(serialization::Status status) {
    using namespace serialization;
    switch (status) {
    case Status::FormatTooOld:
      return "compiled with an older version of the compiler";
    case Status::FormatTooNew:
      return "compiled with a newer version of the compiler";
    case Status::RevisionIncompatible:
      return "compiled with a different version of the compiler";
    case Status::ChannelIncompatible:
      return "compiled for a different distribution channel";
    case Status::NotInOSSA:
      return "module was not built with OSSA";
    case Status::MissingDependency:
      return "missing dependency";
    case Status::MissingUnderlyingModule:
      return "missing underlying module";
    case Status::CircularDependency:
      return "circular dependency";
    case Status::FailedToLoadBridgingHeader:
      return "failed to load bridging header";
    case Status::Malformed:
      return "malformed";
    case Status::MalformedDocumentation:
      return "malformed documentation";
    case Status::NameMismatch:
      return "name mismatch";
    case Status::TargetIncompatible:
      return "compiled for a different target platform";
    case Status::TargetTooNew:
      return "target platform newer than current platform";
    case Status::SDKMismatch:
      return "SDK does not match";
    case Status::Valid:
      return nullptr;
    }
    llvm_unreachable("bad status");
  }

  /// Emits a diagnostic for all out-of-date compiled or forwarding modules
  /// encountered while trying to load a module.
  template<typename... DiagArgs>
  void diagnose(ASTContext &ctx, DiagnosticEngine &diags,
                StringRef prebuiltCacheDir, SourceLoc loc,
                DiagArgs &&...diagArgs) {
    diags.diagnose(loc, std::forward<DiagArgs>(diagArgs)...);
    auto SDKVer = getSDKBuildVersion(ctx.SearchPathOpts.getSDKPath());
    llvm::SmallString<64> buffer = prebuiltCacheDir;
    llvm::sys::path::append(buffer, "SystemVersion.plist");
    auto PBMVer = getSDKBuildVersionFromPlist(buffer.str());
    if (!SDKVer.empty() && !PBMVer.empty()) {
      // Remark the potential version difference.
      diags.diagnose(loc, diag::sdk_version_pbm_version, SDKVer,
                         PBMVer);
    }
    // We may have found multiple failing modules, that failed for different
    // reasons. Emit a note for each of them.
    for (auto &mod : candidateModules) {
      // If the compiled module was ignored, diagnose the reason.
      if (mod.reasonIgnored != ReasonIgnored::NotIgnored) {
        diags.diagnose(loc, diag::compiled_module_ignored_reason, mod.path,
                       (unsigned)mod.reasonIgnored);
      } else {
        diags.diagnose(loc, diag::out_of_date_module_here, (unsigned)mod.kind,
                       mod.path);
      }

      // Diagnose any out-of-date dependencies in this module.
      for (auto &dep : mod.outOfDateDependencies) {
        diags.diagnose(loc, diag::module_interface_dependency_out_of_date,
                           dep);
      }

      // Diagnose any missing dependencies in this module.
      for (auto &dep : mod.missingDependencies) {
        diags.diagnose(loc, diag::module_interface_dependency_missing, dep);
      }

      // If there was a compiled module that wasn't able to be read, diagnose
      // the reason we couldn't read it.
      if (auto status = mod.serializationStatus) {
        if (auto reason = invalidModuleReason(*status)) {
          diags.diagnose(loc, diag::compiled_module_invalid_reason,
              mod.path, reason);
        } else {
          diags.diagnose(loc, diag::compiled_module_invalid, mod.path);
        }
      }
    }
  }

  /// Emits a diagnostic for the reason why binary swiftmodules were preferred
  /// over textual swiftinterfaces.
  void diagnoseIgnoredModuleInterfaces(ASTContext &ctx, SourceLoc loc) {
    for (auto &mod : candidateModules) {
      auto interfaceIgnore = mod.reasonModuleInterfaceIgnored;
      if (interfaceIgnore == ReasonModuleInterfaceIgnored::NotIgnored)
        continue;

      ctx.Diags.diagnose(loc, diag::module_interface_ignored_reason,
                         mod.path, (unsigned)interfaceIgnore);
    }
  }
};

/// Constructs the full path of the dependency \p dep by prepending the SDK
/// path if necessary.
StringRef getFullDependencyPath(const FileDependency &dep,
                                const ASTContext &ctx,
                                SmallVectorImpl<char> &scratch) {
  if (!dep.isSDKRelative())
    return dep.getPath();

  path::native(ctx.SearchPathOpts.getSDKPath(), scratch);
  llvm::sys::path::append(scratch, dep.getPath());
  return StringRef(scratch.data(), scratch.size());
}

class UpToDateModuleCheker {
  ASTContext &ctx;
  llvm::vfs::FileSystem &fs;
  RequireOSSAModules_t requiresOSSAModules;

public:
  UpToDateModuleCheker(ASTContext &ctx,
                       RequireOSSAModules_t requiresOSSAModules)
     : ctx(ctx),
       fs(*ctx.SourceMgr.getFileSystem()),
       requiresOSSAModules(requiresOSSAModules) {}
  
  // Check if all the provided file dependencies are up-to-date compared to
  // what's currently on disk.
  bool dependenciesAreUpToDate(StringRef modulePath,
                               ModuleRebuildInfo &rebuildInfo,
                               ArrayRef<FileDependency> deps,
                               bool skipSystemDependencies) {
    SmallString<128> SDKRelativeBuffer;
    for (auto &in : deps) {
      if (skipSystemDependencies && in.isSDKRelative() &&
          in.isModificationTimeBased()) {
        continue;
      }
      StringRef fullPath = getFullDependencyPath(in, ctx, SDKRelativeBuffer);
      switch (checkDependency(modulePath, in, fullPath)) {
      case DependencyStatus::UpToDate:
        LLVM_DEBUG(llvm::dbgs() << "Dep " << fullPath << " is up to date\n");
        break;
      case DependencyStatus::OutOfDate:
        LLVM_DEBUG(llvm::dbgs() << "Dep " << fullPath << " is out of date\n");
        rebuildInfo.addOutOfDateDependency(modulePath, fullPath);
        return false;
      case DependencyStatus::Missing:
        LLVM_DEBUG(llvm::dbgs() << "Dep " << fullPath << " is missing\n");
        rebuildInfo.addMissingDependency(modulePath, fullPath);
        return false;
      }
    }
    return true;
  }

  // Check that the output .swiftmodule file is at least as new as all the
  // dependencies it read when it was built last time.
  bool serializedASTBufferIsUpToDate(
      StringRef path, const llvm::MemoryBuffer &buf,
      ModuleRebuildInfo &rebuildInfo,
      SmallVectorImpl<FileDependency> &allDeps) {

    // Clear the existing dependencies, because we're going to re-fill them
    // in validateSerializedAST.
    allDeps.clear();

    LLVM_DEBUG(llvm::dbgs() << "Validating deps of " << path << "\n");
    auto validationInfo = serialization::validateSerializedAST(
        buf.getBuffer(), requiresOSSAModules,
        ctx.LangOpts.SDKName, /*ExtendedValidationInfo=*/nullptr, &allDeps);

    if (validationInfo.status != serialization::Status::Valid) {
      rebuildInfo.setSerializationStatus(path, validationInfo.status);
      return false;
    }

    bool skipCheckingSystemDependencies =
        ctx.SearchPathOpts.DisableModulesValidateSystemDependencies;
    return dependenciesAreUpToDate(path, rebuildInfo, allDeps,
                                   skipCheckingSystemDependencies);
  }

  // Check that the output .swiftmodule file is at least as new as all the
  // dependencies it read when it was built last time.
  bool swiftModuleIsUpToDate(
    StringRef modulePath, ModuleRebuildInfo &rebuildInfo,
    SmallVectorImpl<FileDependency> &AllDeps,
    std::unique_ptr<llvm::MemoryBuffer> &moduleBuffer) {
    auto OutBuf = fs.getBufferForFile(modulePath);
    if (!OutBuf)
      return false;
    moduleBuffer = std::move(*OutBuf);
    return serializedASTBufferIsUpToDate(modulePath, *moduleBuffer, rebuildInfo, AllDeps);
  }
  
  enum class DependencyStatus {
    UpToDate,
    OutOfDate,
    Missing
  };

  // Checks that a dependency read from the cached module is up to date compared
  // to the interface file it represents.
  DependencyStatus checkDependency(StringRef modulePath,
                                   const FileDependency &dep,
                                   StringRef fullPath) {
    auto status = fs.status(fullPath);
    if (!status)
      return DependencyStatus::Missing;

    // If the sizes differ, then we know the file has changed.
    if (status->getSize() != dep.getSize())
      return DependencyStatus::OutOfDate;

    // Otherwise, if this dependency is verified by modification time, check
    // it vs. the modification time of the file.
    if (dep.isModificationTimeBased()) {
      uint64_t mtime =
        status->getLastModificationTime().time_since_epoch().count();
      return mtime == dep.getModificationTime() ?
          DependencyStatus::UpToDate :
          DependencyStatus::OutOfDate;
    }

    // Slow path: if the dependency is verified by content hash, check it vs.
    // the hash of the file.
    auto buf = fs.getBufferForFile(fullPath, /*FileSize=*/-1,
                                   /*RequiresNullTerminator=*/false);
    if (!buf)
      return DependencyStatus::Missing;

    return xxHash64(buf.get()->getBuffer()) == dep.getContentHash() ?
        DependencyStatus::UpToDate :
        DependencyStatus::OutOfDate;
  }
};

/// Handles the details of loading module interfaces as modules, and will
/// do the necessary lookup to determine if we should be loading from the
/// normal cache, the prebuilt cache, a module adjacent to the interface, or
/// a module that we'll build from a module interface.
class ModuleInterfaceLoaderImpl {
  friend class swift::ModuleInterfaceLoader;
  friend class swift::ModuleInterfaceCheckerImpl;
  ASTContext &ctx;
  llvm::vfs::FileSystem &fs;
  DiagnosticEngine &diags;
  ModuleRebuildInfo rebuildInfo;
  UpToDateModuleCheker upToDateChecker;
  const StringRef modulePath;
  const std::string interfacePath;
  const StringRef moduleName;
  const StringRef prebuiltCacheDir;
  const StringRef backupInterfaceDir;
  const StringRef cacheDir;
  const SourceLoc diagnosticLoc;
  DependencyTracker *const dependencyTracker;
  const ModuleLoadingMode loadMode;
  ModuleInterfaceLoaderOptions Opts;
  RequireOSSAModules_t requiresOSSAModules;

  ModuleInterfaceLoaderImpl(
      ASTContext &ctx, StringRef modulePath, StringRef interfacePath,
      StringRef moduleName, StringRef cacheDir, StringRef prebuiltCacheDir,
      StringRef backupInterfaceDir,
      SourceLoc diagLoc, ModuleInterfaceLoaderOptions Opts,
      RequireOSSAModules_t requiresOSSAModules,
      DependencyTracker *dependencyTracker = nullptr,
      ModuleLoadingMode loadMode = ModuleLoadingMode::PreferSerialized)
      : ctx(ctx), fs(*ctx.SourceMgr.getFileSystem()), diags(ctx.Diags),
        upToDateChecker(ctx, requiresOSSAModules),
        modulePath(modulePath), interfacePath(interfacePath),
        moduleName(moduleName),
        prebuiltCacheDir(prebuiltCacheDir),
        backupInterfaceDir(backupInterfaceDir),
        cacheDir(cacheDir), diagnosticLoc(diagLoc),
        dependencyTracker(dependencyTracker), loadMode(loadMode), Opts(Opts),
        requiresOSSAModules(requiresOSSAModules) {}

  std::string getBackupPublicModuleInterfacePath() {
    return getBackupPublicModuleInterfacePath(ctx.SourceMgr, backupInterfaceDir,
                                              moduleName, interfacePath);
  }

  static std::string getBackupPublicModuleInterfacePath(SourceManager &SM,
                                                        StringRef backupInterfaceDir,
                                                        StringRef moduleName,
                                                        StringRef interfacePath) {
    if (backupInterfaceDir.empty())
      return std::string();
    auto &fs = *SM.getFileSystem();
    auto fileName = llvm::sys::path::filename(interfacePath);
    {
      llvm::SmallString<256> path(backupInterfaceDir);
      llvm::sys::path::append(path, llvm::Twine(moduleName) + ".swiftmodule");
      llvm::sys::path::append(path, fileName);
      if (fs.exists(path.str())) {
        return path.str().str();
      }
    }
    {
      llvm::SmallString<256> path(backupInterfaceDir);
      llvm::sys::path::append(path, fileName);
      if (fs.exists(path.str())) {
        return path.str().str();
      }
    }
    return std::string();
  }

  // Check that a "forwarding" .swiftmodule file is at least as new as all the
  // dependencies it read when it was built last time. Requires that the
  // forwarding module has been loaded from disk.
  bool forwardingModuleIsUpToDate(
      StringRef path, const ForwardingModule &fwd,
      SmallVectorImpl<FileDependency> &deps,
      std::unique_ptr<llvm::MemoryBuffer> &moduleBuffer) {

    // Clear the existing dependencies, because we're going to re-fill them
    // from the forwarding module.
    deps.clear();

    LLVM_DEBUG(llvm::dbgs() << "Validating deps of " << path << "\n");

    // First, make sure the underlying module path exists and is valid.
    auto modBuf = fs.getBufferForFile(fwd.underlyingModulePath);
    if (!modBuf)
      return false;

    auto looksValid = serializedASTLooksValid(*modBuf.get(),
                                              requiresOSSAModules,
                                              ctx.LangOpts.SDKName);
    if (!looksValid)
      return false;

    // Next, check the dependencies in the forwarding file.
    for (auto &dep : fwd.dependencies) {
      deps.push_back(
        FileDependency::modTimeBased(
          dep.path, dep.isSDKRelative, dep.size, dep.lastModificationTime));
    }

    bool skipCheckingSystemDependencies =
        ctx.SearchPathOpts.DisableModulesValidateSystemDependencies;
    if (!upToDateChecker.dependenciesAreUpToDate(path, rebuildInfo, deps,
                                                 skipCheckingSystemDependencies))
      return false;

    moduleBuffer = std::move(*modBuf);
    return true;
  }

  bool canInterfaceHavePrebuiltModule() {
    StringRef sdkPath = ctx.SearchPathOpts.getSDKPath();
    if (!sdkPath.empty() &&
        hasPrefix(path::begin(interfacePath), path::end(interfacePath),
                  path::begin(sdkPath), path::end(sdkPath))) {
      return !(StringRef(interfacePath).endswith(".private.swiftinterface") ||
               StringRef(interfacePath).endswith(".package.swiftinterface"));
    }
    return false;
  }

  std::optional<StringRef>
  computePrebuiltModulePath(llvm::SmallString<256> &scratch) {
    namespace path = llvm::sys::path;

    // Check if this is a public interface file from the SDK.
    if (!canInterfaceHavePrebuiltModule())
      return std::nullopt;

    // Assemble the expected path: $PREBUILT_CACHE/Foo.swiftmodule or
    // $PREBUILT_CACHE/Foo.swiftmodule/arch.swiftmodule. Note that there's no
    // cache key here.
    scratch = prebuiltCacheDir;

    // FIXME: Would it be possible to only have architecture-specific names
    // here? Then we could skip this check.
    StringRef inParentDirName =
      path::filename(path::parent_path(interfacePath));
    if (path::extension(inParentDirName) == ".swiftmodule") {
      assert(path::stem(inParentDirName) ==
             ctx.getRealModuleName(ctx.getIdentifier(moduleName)).str());
      path::append(scratch, inParentDirName);
    }
    path::append(scratch, path::filename(modulePath));

    // If there isn't a file at this location, skip returning a path.
    if (!fs.exists(scratch))
      return std::nullopt;

    return scratch.str();
  }

  /// Hack to deal with build systems (including the Swift standard library, at
  /// the time of this comment) that aren't yet using target-specific names for
  /// multi-target swiftmodules, in case the prebuilt cache is.
  std::optional<StringRef>
  computeFallbackPrebuiltModulePath(llvm::SmallString<256> &scratch) {
    namespace path = llvm::sys::path;
    StringRef sdkPath = ctx.SearchPathOpts.getSDKPath();

    // Check if this is a public interface file from the SDK.
    if (sdkPath.empty() ||
        !hasPrefix(path::begin(interfacePath), path::end(interfacePath),
                   path::begin(sdkPath), path::end(sdkPath)) ||
        StringRef(interfacePath).endswith(".private.swiftinterface") ||
         StringRef(interfacePath).endswith(".package.swiftinterface"))
      return std::nullopt;

    // If the module isn't target-specific, there's no fallback path.
    StringRef inParentDirName =
        path::filename(path::parent_path(interfacePath));
    if (path::extension(inParentDirName) != ".swiftmodule")
      return std::nullopt;

    // If the interface is already using the target-specific name, there's
    // nothing else to try.
    auto normalizedTarget = getTargetSpecificModuleTriple(ctx.LangOpts.Target);
    if (path::stem(modulePath) == normalizedTarget.str())
      return std::nullopt;

    // Assemble the expected path:
    // $PREBUILT_CACHE/Foo.swiftmodule/target.swiftmodule. Note that there's no
    // cache key here.
    scratch = prebuiltCacheDir;
    path::append(scratch, inParentDirName);
    path::append(scratch, normalizedTarget.str());
    scratch += ".swiftmodule";

    // If there isn't a file at this location, skip returning a path.
    if (!fs.exists(scratch))
      return std::nullopt;

    return scratch.str();
  }

  bool isInResourceDir(StringRef path) {
    StringRef resourceDir = ctx.SearchPathOpts.RuntimeResourcePath;
    if (resourceDir.empty()) return false;
    return pathStartsWith(resourceDir, path);
  }

  bool isInResourceHostDir(StringRef path) {
    StringRef resourceDir = ctx.SearchPathOpts.RuntimeResourcePath;
    if (resourceDir.empty()) return false;

    SmallString<128> hostPath;
    llvm::sys::path::append(hostPath,
                            resourceDir, "host");
    return pathStartsWith(hostPath, path);
  }

  bool isInSDK(StringRef path) {
    StringRef sdkPath = ctx.SearchPathOpts.getSDKPath();
    if (sdkPath.empty()) return false;
    return pathStartsWith(sdkPath, path);
  }

  bool isInSystemFrameworks(StringRef path, bool publicFramework) {
    StringRef sdkPath = ctx.SearchPathOpts.getSDKPath();
    if (sdkPath.empty()) return false;

    SmallString<128> frameworksPath;
    llvm::sys::path::append(frameworksPath,
                            sdkPath, "System", "Library",
                            publicFramework ? "Frameworks" : "PrivateFrameworks");

    return pathStartsWith(frameworksPath, path);
  }

  std::pair<std::string, std::string> getCompiledModuleCandidates() {
    using ReasonIgnored = ModuleRebuildInfo::ReasonIgnored;
    using ReasonModuleInterfaceIgnored =
                               ModuleRebuildInfo::ReasonModuleInterfaceIgnored;
    std::pair<std::string, std::string> result;

    bool ignoreByDefault = ctx.blockListConfig.hasBlockListAction(
                                       "Swift_UseSwiftinterfaceByDefault",
                                       BlockListKeyKind::ModuleName,
                                       BlockListAction::ShouldUseBinaryModule);
    bool shouldLoadAdjacentModule;
    if (ignoreByDefault) {
      ReasonModuleInterfaceIgnored ignore =
        ReasonModuleInterfaceIgnored::NotIgnored;

      if (!isInSDK(modulePath) &&
          !isInResourceHostDir(modulePath)) {
        ignore = ReasonModuleInterfaceIgnored::LocalModule;
      } else if (ctx.blockListConfig.hasBlockListAction(moduleName,
                                     BlockListKeyKind::ModuleName,
                                     BlockListAction::ShouldUseBinaryModule)) {
        ignore = ReasonModuleInterfaceIgnored::Blocklisted;
      } else if (ctx.LangOpts.DebuggerSupport) {
        ignore = ReasonModuleInterfaceIgnored::Debugger;
      }

      shouldLoadAdjacentModule =
        ignore != ReasonModuleInterfaceIgnored::NotIgnored;
      if (shouldLoadAdjacentModule) {
        // Prefer the swiftmodule.
        rebuildInfo.addIgnoredModuleInterface(modulePath, ignore);
      } else {
        // Prefer the swiftinterface.
        rebuildInfo.addIgnoredModule(modulePath,
                                 ReasonIgnored::DistributedInterfaceByDefault);
      }
    } else {
      // Should we attempt to load a swiftmodule adjacent to the swiftinterface?
      shouldLoadAdjacentModule = !ctx.IgnoreAdjacentModules;

      if (modulePath.contains(".sdk")) {
        if (ctx.blockListConfig.hasBlockListAction(moduleName,
                                    BlockListKeyKind::ModuleName,
                                    BlockListAction::ShouldUseTextualModule)) {
          shouldLoadAdjacentModule = false;
          rebuildInfo.addIgnoredModule(modulePath, ReasonIgnored::Blocklisted);
        }
      }

      // Don't use the adjacent swiftmodule for frameworks from the public
      // Frameworks folder of the SDK.
      if (isInSystemFrameworks(modulePath, /*publicFramework*/true)) {
        shouldLoadAdjacentModule = false;
        rebuildInfo.addIgnoredModule(modulePath,
                                     ReasonIgnored::PublicFramework);
      } else if (isInResourceHostDir(modulePath)) {
        shouldLoadAdjacentModule = false;
        rebuildInfo.addIgnoredModule(modulePath,
                                     ReasonIgnored::CompilerHostModule);
      }
    }

    switch (loadMode) {
    case ModuleLoadingMode::OnlyInterface:
      // Always skip both the caches and adjacent modules, and always build the
      // module interface.
      return {};
    case ModuleLoadingMode::PreferInterface:
      // If we're in the load mode that prefers .swiftinterfaces, specifically
      // skip the module adjacent to the interface, but use the caches if
      // they're present.
      shouldLoadAdjacentModule = false;
      rebuildInfo.addIgnoredModule(modulePath,
                                   ReasonIgnored::InterfacePreferred);
      break;
    case ModuleLoadingMode::PreferSerialized:
      // The rest of the function should be covered by this.
      break;
    case ModuleLoadingMode::OnlySerialized:
      llvm_unreachable("module interface loader should not have been created");
    }
    // [NOTE: ModuleInterfaceLoader-defer-to-ImplicitSerializedModuleLoader]
    // If there's a module adjacent to the .swiftinterface that we can
    // _likely_ load (it validates OK and is up to date), bail early with
    // errc::not_supported, so the next (serialized) loader in the chain will
    // load it.
    // Alternately, if there's a .swiftmodule present but we can't even
    // read it (for whatever reason), we should let the other module loader
    // diagnose it.

    if (shouldLoadAdjacentModule) {
      if (fs.exists(modulePath)) {
        result.first = modulePath.str();
      }
    }

    // If we have a prebuilt cache path, check that too if the interface comes
    // from the SDK.
    if (!prebuiltCacheDir.empty()) {
      llvm::SmallString<256> scratch;
      std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
      std::optional<StringRef> path = computePrebuiltModulePath(scratch);
      if (!path) {
        // Hack: deal with prebuilds of modules that still use the target-based
        // names.
        path = computeFallbackPrebuiltModulePath(scratch);
      }
      if (path) {
        if (fs.exists(*path)) {
          result.second = path->str();
        }
      }
    }

    return result;
  }

  llvm::ErrorOr<DiscoveredModule>
  discoverUpToDateCompiledModuleForInterface(SmallVectorImpl<FileDependency> &deps,
                                             std::string &UsableModulePath) {
    std::string adjacentMod, prebuiltMod;
    std::tie(adjacentMod, prebuiltMod) = getCompiledModuleCandidates();
    if (!adjacentMod.empty()) {
      auto adjacentModuleBuffer = fs.getBufferForFile(adjacentMod);
      if (adjacentModuleBuffer) {
        if (upToDateChecker.serializedASTBufferIsUpToDate(adjacentMod, *adjacentModuleBuffer.get(),
                                                          rebuildInfo, deps)) {
          LLVM_DEBUG(llvm::dbgs() << "Found up-to-date module at "
                                  << adjacentMod
                                  << "; deferring to serialized module loader\n");
          UsableModulePath = adjacentMod;
          return std::make_error_code(std::errc::not_supported);
        } else if (isInResourceDir(adjacentMod) &&
                   loadMode == ModuleLoadingMode::PreferSerialized &&
                   !version::isCurrentCompilerTagged() &&
                   rebuildInfo.getOrInsertCandidateModule(adjacentMod).serializationStatus !=
                     serialization::Status::SDKMismatch) {
          // Special-case here: If we're loading a .swiftmodule from the resource
          // dir adjacent to the compiler, defer to the serialized loader instead
          // of falling back. This is to support local development of Swift,
          // where one might change the module format version but forget to
          // recompile the standard library. If that happens, don't fall back
          // and silently recompile the standard library, raise an error
          // instead.
          //
          // This logic is disabled for tagged compilers, so distributed
          // compilers should ignore this restriction and rebuild all modules
          // from a swiftinterface when required.
          //
          // Still accept modules built with a different SDK, allowing the use
          // of one toolchain against a different SDK.
          LLVM_DEBUG(llvm::dbgs() << "Found out-of-date module in the "
                                     "resource-dir at "
                                  << adjacentMod
                                  << "; deferring to serialized module loader "
                                     "to diagnose\n");
          return std::make_error_code(std::errc::not_supported);
        } else {
          LLVM_DEBUG(llvm::dbgs() << "Found out-of-date module at "
                                  << adjacentMod << "\n");
          rebuildInfo.setModuleKind(adjacentMod,
                                    ModuleRebuildInfo::ModuleKind::Normal);
        }
      } else {
        LLVM_DEBUG(llvm::dbgs() << "Found unreadable module at "
                                << adjacentMod
                                << "; deferring to serialized module loader\n");
        return std::make_error_code(std::errc::not_supported);
      }
    }

    if(!prebuiltMod.empty()) {
      std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
      if (upToDateChecker.swiftModuleIsUpToDate(prebuiltMod, rebuildInfo,
                                                deps, moduleBuffer)) {
        LLVM_DEBUG(llvm::dbgs() << "Found up-to-date prebuilt module at "
                                << prebuiltMod << "\n");
        UsableModulePath = prebuiltMod;
        return DiscoveredModule::prebuilt(prebuiltMod, std::move(moduleBuffer));
      } else {
        LLVM_DEBUG(llvm::dbgs() << "Found out-of-date prebuilt module at "
                                << prebuiltMod << "\n");
        rebuildInfo.setModuleKind(prebuiltMod,
                                  ModuleRebuildInfo::ModuleKind::Prebuilt);
      }
    }
    // We cannot find any proper compiled module to use.
    return std::make_error_code(std::errc::no_such_file_or_directory);
  }

  /// Finds the most appropriate .swiftmodule, whose dependencies are up to
  /// date, that we can load for the provided .swiftinterface file.
  llvm::ErrorOr<DiscoveredModule> discoverUpToDateModuleForInterface(
    StringRef cachedOutputPath,
    SmallVectorImpl<FileDependency> &deps) {

    // First, check the cached module path. Whatever's in this cache represents
    // the most up-to-date knowledge we have about the module.
    if (auto cachedBufOrError = fs.getBufferForFile(cachedOutputPath)) {
      auto buf = std::move(*cachedBufOrError);

      // Check to see if the module is a serialized AST. If it's not, then we're
      // probably dealing with a Forwarding Module, which is a YAML file.
      bool isForwardingModule =
        !serialization::isSerializedAST(buf->getBuffer());

      // If it's a forwarding module, load the YAML file from disk and check
      // if it's up-to-date.
      if (isForwardingModule) {
        if (auto forwardingModule = ForwardingModule::load(*buf)) {
          std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
          if (forwardingModuleIsUpToDate(cachedOutputPath,
                                         *forwardingModule, deps,
                                         moduleBuffer)) {
            LLVM_DEBUG(llvm::dbgs() << "Found up-to-date forwarding module at "
                                    << cachedOutputPath << "\n");
            return DiscoveredModule::forwarded(
              forwardingModule->underlyingModulePath, std::move(moduleBuffer));
          }

          LLVM_DEBUG(llvm::dbgs() << "Found out-of-date forwarding module at "
                     << cachedOutputPath << "\n");
          rebuildInfo.setModuleKind(cachedOutputPath,
                                    ModuleRebuildInfo::ModuleKind::Forwarding);
        }
      // Otherwise, check if the AST buffer itself is up to date.
      } else if (upToDateChecker.serializedASTBufferIsUpToDate(cachedOutputPath, *buf,
                                                               rebuildInfo, deps)) {
        LLVM_DEBUG(llvm::dbgs() << "Found up-to-date cached module at "
                                << cachedOutputPath << "\n");
        return DiscoveredModule::normal(cachedOutputPath, std::move(buf));
      } else {
        LLVM_DEBUG(llvm::dbgs() << "Found out-of-date cached module at "
                   << cachedOutputPath << "\n");
        rebuildInfo.setModuleKind(cachedOutputPath,
                                  ModuleRebuildInfo::ModuleKind::Cached);
      }
    }
    std::string usableModulePath;
    return discoverUpToDateCompiledModuleForInterface(deps, usableModulePath);
  }

  /// Writes the "forwarding module" that will forward to a module in the
  /// prebuilt cache.
  ///
  /// Since forwarding modules track dependencies separately from the module
  /// they point to, we'll need to grab the up-to-date file status while doing
  /// this. If the write was successful, it also updates the
  /// list of dependencies to match what was written to the forwarding file.
  bool writeForwardingModuleAndUpdateDeps(
      const DiscoveredModule &mod, llvm::vfs::OutputBackend &backend,
      StringRef outputPath, SmallVectorImpl<FileDependency> &deps) {
    assert(mod.isPrebuilt() &&
           "cannot write forwarding file for non-prebuilt module");
    ForwardingModule fwd(mod.path);

    SmallVector<FileDependency, 16> depsAdjustedToMTime;

    // FIXME: We need to avoid re-statting all these dependencies, otherwise
    //        we may record out-of-date information.
    SmallString<128> SDKRelativeBuffer;
    auto addDependency = [&](FileDependency dep) -> FileDependency {
      auto status = fs.status(getFullDependencyPath(dep, ctx, SDKRelativeBuffer));
      uint64_t mtime =
        status->getLastModificationTime().time_since_epoch().count();
      fwd.addDependency(dep.getPath(), dep.isSDKRelative(), status->getSize(),
                        mtime);

      // Construct new FileDependency matching what we've added to the
      // forwarding module.
      return FileDependency::modTimeBased(dep.getPath(), dep.isSDKRelative(),
                                          status->getSize(), mtime);
    };

    // Add the prebuilt module as a dependency of the forwarding module, but
    // don't add it to the outer dependency list.
    (void)addDependency(FileDependency::hashBased(fwd.underlyingModulePath,
                                                  /*SDKRelative*/false,
                                                  /*size*/0, /*hash*/0));

    // Add all the dependencies from the prebuilt module, and update our list
    // of dependencies to reflect what's recorded in the forwarding module.
    for (auto dep : deps) {
      auto adjustedDep = addDependency(dep);
      depsAdjustedToMTime.push_back(adjustedDep);
    }

    auto hadError = withOutputPath(diags, backend, outputPath,
                                      [&](llvm::raw_pwrite_stream &out) {
                                        llvm::yaml::Output yamlWriter(out);
                                        yamlWriter << fwd;
                                        return false;
                                      });

    if (hadError)
      return true;

    // If and only if we succeeded writing the forwarding file, update the
    // provided list of dependencies.
    deps = depsAdjustedToMTime;
    return false;
  }

  /// Looks up the best module to load for a given interface, and returns a
  /// buffer of the module's contents. Also reports the module's dependencies
  /// to the parent \c dependencyTracker if it came from the cache, or was built
  /// from the given interface. See the main comment in
  /// \c ModuleInterfaceLoader.h for an explanation of the module
  /// loading strategy.
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
  findOrBuildLoadableModule() {

    // Track system dependencies if the parent tracker is set to do so.
    bool trackSystemDependencies = false;
    if (dependencyTracker) {
      auto ClangDependencyTracker = dependencyTracker->getClangCollector();
      trackSystemDependencies = ClangDependencyTracker->needSystemDependencies();
    }
    InterfaceSubContextDelegateImpl astDelegate(
        ctx.SourceMgr, &ctx.Diags, ctx.SearchPathOpts, ctx.LangOpts,
        ctx.ClangImporterOpts, ctx.CASOpts, Opts,
        /*buildModuleCacheDirIfAbsent*/ true, cacheDir, prebuiltCacheDir,
        backupInterfaceDir,
        /*serializeDependencyHashes*/ false, trackSystemDependencies,
        requiresOSSAModules);

    // Compute the output path if we're loading or emitting a cached module.
    llvm::SmallString<256> cachedOutputPath;
    StringRef CacheHash;
    astDelegate.computeCachedOutputPath(moduleName, interfacePath,
                                        ctx.SearchPathOpts.getSDKPath(),
                                        cachedOutputPath, CacheHash);

    // Try to find the right module for this interface, either alongside it,
    // in the cache, or in the prebuilt cache.
    SmallVector<FileDependency, 16> allDeps;
    auto moduleOrErr =
      discoverUpToDateModuleForInterface(cachedOutputPath, allDeps);

    // If we errored with anything other than 'no such file or directory',
    // fail this load and let the other module loader diagnose it.
    if (!moduleOrErr &&
        moduleOrErr.getError() != std::errc::no_such_file_or_directory) {
      rebuildInfo.diagnoseIgnoredModuleInterfaces(ctx, diagnosticLoc);
      return moduleOrErr.getError();
    }

    // We discovered a module! Return that module's buffer so we can load it.
    if (moduleOrErr) {
      auto module = std::move(moduleOrErr.get());

      // If it's prebuilt, use this time to generate a forwarding module and
      // update the dependencies to use modification times.
      if (module.isPrebuilt())
        if (writeForwardingModuleAndUpdateDeps(module, ctx.getOutputBackend(),
                                               cachedOutputPath, allDeps))
          return std::make_error_code(std::errc::not_supported);

      // Report the module's dependencies to the dependencyTracker
      if (dependencyTracker) {
        SmallString<128> SDKRelativeBuffer;
        for (auto &dep: allDeps) {
          StringRef fullPath = getFullDependencyPath(dep, ctx, SDKRelativeBuffer);
          dependencyTracker->addDependency(fullPath,
                                           /*IsSystem=*/dep.isSDKRelative());
        }
      }


      return std::move(module.moduleBuffer);
    }
    // If building from interface is disabled, return error.
    if (Opts.disableBuildingInterface) {
      return std::make_error_code(std::errc::not_supported);
    }

    std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;

    // We didn't discover a module corresponding to this interface.
    // Diagnose that we didn't find a loadable module, if we were asked to.
    //
    // Note that we use `diags` so that we emit this remark even when we're
    // emitting other messages to `emptyDiags` (see below); these act as status
    // messages to explain what's taking so long.
    auto remarkRebuildAll = [&]() {
      rebuildInfo.diagnose(ctx, diags, prebuiltCacheDir, diagnosticLoc,
                           diag::rebuilding_module_from_interface, moduleName,
                           interfacePath);
    };
    auto remarkRebuild = Opts.remarkOnRebuildFromInterface
                       ? llvm::function_ref<void()>(remarkRebuildAll)
                       : nullptr;

    bool failed = false;
    std::string backupPath = getBackupPublicModuleInterfacePath();
    {
      DiagnosticEngine emptyDiags(ctx.SourceMgr);
      std::unique_ptr<llvm::SaveAndRestore<DiagnosticEngine*>> saver;
      DiagnosticEngine *diagsToUse = &ctx.Diags;
      // Avoid emitting diagnostics if we have a backup interface to use.
      // If we succeed in building this canonical interface, it's not interesting
      // to see those diagnostics.
      // If we failed in build, we will use the back up interface and it's interesting
      // to see diagnostics there.
      if (!backupPath.empty()) {
        diagsToUse = &emptyDiags;
        saver = std::make_unique<llvm::SaveAndRestore<DiagnosticEngine*>>(
          astDelegate.Diags, diagsToUse);
      }
      // Set up a builder if we need to build the module. It'll also set up
      // the genericSubInvocation we'll need to use to compute the cache paths.
      Identifier realName = ctx.getRealModuleName(ctx.getIdentifier(moduleName));
      ImplicitModuleInterfaceBuilder builder(
        ctx.SourceMgr, diagsToUse,
        astDelegate, interfacePath,
        ctx.SearchPathOpts.getSDKPath(),
        realName.str(), cacheDir,
        prebuiltCacheDir, backupInterfaceDir, StringRef(),
        Opts.disableInterfaceLock,
        ctx.IgnoreAdjacentModules, diagnosticLoc,
        dependencyTracker);
      // If we found an out-of-date .swiftmodule, we still want to add it as
      // a dependency of the .swiftinterface. That way if it's updated, but
      // the .swiftinterface remains the same, we invalidate the cache and
      // check the new .swiftmodule, because it likely has more information
      // about the state of the world.
      if (rebuildInfo.sawOutOfDateModule(modulePath))
        builder.addExtraDependency(modulePath);
      failed = builder.buildSwiftModule(cachedOutputPath,
                                        /*shouldSerializeDeps*/true,
                                        &moduleBuffer, remarkRebuild);
    }
    if (!failed) {
      // If succeeded, we are done.
      assert(moduleBuffer &&
             "failed to write module buffer but returned success?");
      return std::move(moduleBuffer);
    } else if (backupPath.empty()) {
      // If failed and we don't have a backup interface file, return error code.
      return std::make_error_code(std::errc::invalid_argument);
    }
    assert(failed);
    assert(!backupPath.empty());
    while (1) {
      diags.diagnose(diagnosticLoc, diag::interface_file_backup_used,
                     interfacePath, backupPath);
      // Set up a builder if we need to build the module. It'll also set up
      // the genericSubInvocation we'll need to use to compute the cache paths.
      ImplicitModuleInterfaceBuilder fallbackBuilder(
        ctx.SourceMgr, &ctx.Diags, astDelegate, backupPath,
        ctx.SearchPathOpts.getSDKPath(),
        moduleName, cacheDir,
        prebuiltCacheDir, backupInterfaceDir, StringRef(),
        Opts.disableInterfaceLock,
        ctx.IgnoreAdjacentModules, diagnosticLoc,
        dependencyTracker);
      if (rebuildInfo.sawOutOfDateModule(modulePath))
        fallbackBuilder.addExtraDependency(modulePath);
      // Add the canonical interface path as a dependency of this module.
      // This ensures that after the user manually fixed the canonical interface
      // file and removed the fallback interface file, we can rebuild the cache.
      fallbackBuilder.addExtraDependency(interfacePath);
      // Use cachedOutputPath as the output file path. This output path was
      // calculated using the canonical interface file path to make sure we
      // can find it from the canonical interface file.
      auto failedAgain = fallbackBuilder.buildSwiftModule(cachedOutputPath,
                                                          /*shouldSerializeDeps*/true,
                                                          &moduleBuffer,
                                                          remarkRebuild);
      if (failedAgain)
        return std::make_error_code(std::errc::invalid_argument);
      assert(moduleBuffer);
      return std::move(moduleBuffer);
    }
  }
};

} // end anonymous namespace

bool ModuleInterfaceCheckerImpl::isCached(StringRef DepPath) {
  if (!CacheDir.empty() && DepPath.starts_with(CacheDir))
    return true;
  return !PrebuiltCacheDir.empty() && DepPath.starts_with(PrebuiltCacheDir);
}

bool ModuleInterfaceLoader::isCached(StringRef DepPath) {
  return InterfaceChecker.isCached(DepPath);
}

/// Load a .swiftmodule associated with a .swiftinterface either from a
/// cache or by converting it in a subordinate \c CompilerInstance, caching
/// the results.
std::error_code ModuleInterfaceLoader::findModuleFilesInDirectory(
    ImportPath::Element ModuleID, const SerializedModuleBaseName &BaseName,
    SmallVectorImpl<char> *ModuleInterfacePath,
    SmallVectorImpl<char> *ModuleInterfaceSourcePath,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
    bool skipBuildingInterface, bool IsFramework,
    bool isTestableImport) {

  // If running in OnlySerialized mode, ModuleInterfaceLoader
  // should not have been constructed at all.
  assert(LoadMode != ModuleLoadingMode::OnlySerialized);

  std::string ModPath{BaseName.getName(file_types::TY_SwiftModuleFile)};

  // First check to see if the .swiftinterface exists at all. Bail if not.
  auto &fs = *Ctx.SourceMgr.getFileSystem();
  auto InPath = BaseName.findInterfacePath(fs, Ctx);
  if (!InPath) {
    if (fs.exists(ModPath)) {
      LLVM_DEBUG(llvm::dbgs()
                 << "No .swiftinterface file found adjacent to module file "
                 << ModPath << "\n");
      return std::make_error_code(std::errc::not_supported);
    }
    return std::make_error_code(std::errc::no_such_file_or_directory);
  }

  if (ModuleInterfaceSourcePath)
    ModuleInterfaceSourcePath->assign(InPath->begin(), InPath->end());

  // If we've been told to skip building interfaces, we are done here and do
  // not need to have the module actually built. For example, if we are
  // currently answering a `canImport` query, it is enough to have found
  // the interface.
  if (skipBuildingInterface) {
    if (ModuleInterfacePath)
      ModuleInterfacePath->assign(InPath->begin(), InPath->end());
    return std::error_code();
  }

  // Create an instance of the Impl to do the heavy lifting.
  auto ModuleName = ModuleID.Item.str();
  ModuleInterfaceLoaderImpl Impl(
      Ctx, ModPath, *InPath, ModuleName, InterfaceChecker.CacheDir,
      InterfaceChecker.PrebuiltCacheDir, InterfaceChecker.BackupInterfaceDir,
      ModuleID.Loc, InterfaceChecker.Opts,
      InterfaceChecker.RequiresOSSAModules,
      dependencyTracker,
      llvm::is_contained(PreferInterfaceForModules, ModuleName)
          ? ModuleLoadingMode::PreferInterface
          : LoadMode);

  // Ask the impl to find us a module that we can load or give us an error
  // telling us that we couldn't load it.
  auto ModuleBufferOrErr = Impl.findOrBuildLoadableModule();
  if (!ModuleBufferOrErr)
    return ModuleBufferOrErr.getError();

  if (ModuleBuffer) {
    *ModuleBuffer = std::move(*ModuleBufferOrErr);
    if (ModuleInterfacePath)
      ModuleInterfacePath->assign(InPath->begin(), InPath->end());
  }

  // Open .swiftsourceinfo file if it's present.
  if (auto SourceInfoError = openModuleSourceInfoFileIfPresent(ModuleID,
                                                               BaseName,
                                                       ModuleSourceInfoBuffer))
    return SourceInfoError;

  // Delegate back to the serialized module loader to load the module doc.
  if (auto DocLoadErr = openModuleDocFileIfPresent(ModuleID, BaseName,
                                                   ModuleDocBuffer))
    return DocLoadErr;

  return std::error_code();
}

std::vector<std::string>
ModuleInterfaceCheckerImpl::getCompiledModuleCandidatesForInterface(
    StringRef moduleName, StringRef interfacePath) {
  // Derive .swiftmodule path from the .swiftinterface path.
  auto interfaceExt = file_types::getExtension(file_types::TY_SwiftModuleInterfaceFile);
  auto newExt = file_types::getExtension(file_types::TY_SwiftModuleFile);
  llvm::SmallString<32> modulePath;

  // When looking up the module for a private or package interface, strip
  // the '.private.' or '.package.'section of the base name
  if (interfacePath.endswith(".private." + interfaceExt.str()) ||
      interfacePath.endswith(".package." + interfaceExt.str())) {
    auto newBaseName = llvm::sys::path::stem(llvm::sys::path::stem(interfacePath));
    modulePath = llvm::sys::path::parent_path(interfacePath);
    llvm::sys::path::append(modulePath, newBaseName + "." + newExt.str());
  } else {
    modulePath = interfacePath;
    llvm::sys::path::replace_extension(modulePath, newExt);
  }

  ModuleInterfaceLoaderImpl Impl(Ctx, modulePath, interfacePath, moduleName,
                                 CacheDir, PrebuiltCacheDir, BackupInterfaceDir,
                                 SourceLoc(), Opts, RequiresOSSAModules,
                                 nullptr, Ctx.SearchPathOpts.ModuleLoadMode);
  std::vector<std::string> results;
  std::string adjacentMod, prebuiltMod;
  std::tie(adjacentMod, prebuiltMod) = Impl.getCompiledModuleCandidates();

  auto validateModule = [&](StringRef modulePath) {
    // Legacy behavior do not validate module.
    if (!Ctx.SearchPathOpts.ScannerModuleValidation)
      return true;

    // If we picked the other module already, no need to validate this one since
    // it should not be used anyway.
    if (!results.empty())
      return false;
    SmallVector<FileDependency, 16> deps;
    std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
    return Impl.upToDateChecker.swiftModuleIsUpToDate(
        modulePath, Impl.rebuildInfo, deps, moduleBuffer);
  };

  // Add compiled module candidates only when they are non-empty and up-to-date.
  if (!adjacentMod.empty() && validateModule(adjacentMod))
    results.push_back(adjacentMod);
  if (!prebuiltMod.empty() && validateModule(prebuiltMod))
    results.push_back(prebuiltMod);
  return results;
}

bool ModuleInterfaceCheckerImpl::tryEmitForwardingModule(
    StringRef moduleName, StringRef interfacePath,
    ArrayRef<std::string> candidates, llvm::vfs::OutputBackend &backend,
    StringRef outputPath) {
  // Derive .swiftmodule path from the .swiftinterface path.
  auto newExt = file_types::getExtension(file_types::TY_SwiftModuleFile);
  llvm::SmallString<32> modulePath = interfacePath;
  llvm::sys::path::replace_extension(modulePath, newExt);
  ModuleInterfaceLoaderImpl Impl(Ctx, modulePath, interfacePath, moduleName,
                                 CacheDir, PrebuiltCacheDir,
                                 BackupInterfaceDir, SourceLoc(), Opts,
                                 RequiresOSSAModules,
                                 nullptr,
                                 ModuleLoadingMode::PreferSerialized);
  SmallVector<FileDependency, 16> deps;
  std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
  for (auto mod: candidates) {
    // Check if the candidate compiled module is still up-to-date.
    if (Impl.upToDateChecker.swiftModuleIsUpToDate(mod, Impl.rebuildInfo,
                                                   deps, moduleBuffer)) {
      // If so, emit a forwarding module to the candidate.
      ForwardingModule FM(mod);
      auto hadError = withOutputPath(Ctx.Diags, backend, outputPath,
                                        [&](llvm::raw_pwrite_stream &out) {
                                          llvm::yaml::Output yamlWriter(out);
                                          yamlWriter << FM;
                                          return false;
                                        });
      if (!hadError)
        return true;
    }
  }
  return false;
}

bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
    SourceManager &SourceMgr, DiagnosticEngine &Diags,
    const SearchPathOptions &SearchPathOpts, const LangOptions &LangOpts,
    const ClangImporterOptions &ClangOpts, const CASOptions &CASOpts,
    StringRef CacheDir, StringRef PrebuiltCacheDir,
    StringRef BackupInterfaceDir, StringRef ModuleName, StringRef InPath,
    StringRef OutPath, StringRef ABIOutputPath, bool SerializeDependencyHashes,
    bool TrackSystemDependencies, ModuleInterfaceLoaderOptions LoaderOpts,
    RequireOSSAModules_t RequireOSSAModules,
    bool silenceInterfaceDiagnostics) {
  InterfaceSubContextDelegateImpl astDelegate(
      SourceMgr, &Diags, SearchPathOpts, LangOpts, ClangOpts, CASOpts, LoaderOpts,
      /*CreateCacheDirIfAbsent*/ true, CacheDir, PrebuiltCacheDir,
      BackupInterfaceDir,
      SerializeDependencyHashes, TrackSystemDependencies,
      RequireOSSAModules);
  ImplicitModuleInterfaceBuilder builder(SourceMgr, &Diags, astDelegate, InPath,
                                         SearchPathOpts.getSDKPath(),
                                         ModuleName, CacheDir, PrebuiltCacheDir,
                                         BackupInterfaceDir, ABIOutputPath,
                                         LoaderOpts.disableInterfaceLock,
                                         silenceInterfaceDiagnostics);
  // FIXME: We really only want to serialize 'important' dependencies here, if
  //        we want to ship the built swiftmodules to another machine.
  auto failed = builder.buildSwiftModule(OutPath, /*shouldSerializeDeps*/true,
                                         /*ModuleBuffer*/nullptr, nullptr,
                                         SearchPathOpts.CandidateCompiledModules);
  if (!failed)
    return false;
  auto backInPath =
    ModuleInterfaceLoaderImpl::getBackupPublicModuleInterfacePath(SourceMgr,
      BackupInterfaceDir, ModuleName, InPath);
  if (backInPath.empty())
    return true;
  assert(failed);
  assert(!backInPath.empty());
  ImplicitModuleInterfaceBuilder backupBuilder(SourceMgr, &Diags, astDelegate, backInPath,
                                               SearchPathOpts.getSDKPath(),
                                               ModuleName, CacheDir, PrebuiltCacheDir,
                                               BackupInterfaceDir, ABIOutputPath,
                                               LoaderOpts.disableInterfaceLock,
                                               silenceInterfaceDiagnostics);
  // Ensure we can rebuild module after user changed the original interface file.
  backupBuilder.addExtraDependency(InPath);
  // FIXME: We really only want to serialize 'important' dependencies here, if
  //        we want to ship the built swiftmodules to another machine.
  return backupBuilder.buildSwiftModule(OutPath, /*shouldSerializeDeps*/true,
                                        /*ModuleBuffer*/nullptr, nullptr,
                                        SearchPathOpts.CandidateCompiledModules);
}

static bool readSwiftInterfaceVersionAndArgs(
    SourceManager &SM, DiagnosticEngine &Diags, llvm::StringSaver &ArgSaver,
    SwiftInterfaceInfo &interfaceInfo, StringRef interfacePath,
    SourceLoc diagnosticLoc, llvm::Triple preferredTarget) {
  llvm::vfs::FileSystem &fs = *SM.getFileSystem();
  auto FileOrError = swift::vfs::getFileOrSTDIN(fs, interfacePath);
  if (!FileOrError) {
    // Don't use this->diagnose() because it'll just try to re-open
    // interfacePath.
    Diags.diagnose(diagnosticLoc, diag::error_open_input_file, interfacePath,
                   FileOrError.getError().message());
    return true;
  }
  auto SB = FileOrError.get()->getBuffer();
  auto VersRe = getSwiftInterfaceFormatVersionRegex();
  auto CompRe = getSwiftInterfaceCompilerVersionRegex();
  SmallVector<StringRef, 2> VersMatches, CompMatches;

  if (!VersRe.match(SB, &VersMatches)) {
    InterfaceSubContextDelegateImpl::diagnose(
        interfacePath, diagnosticLoc, SM, &Diags,
        diag::error_extracting_version_from_module_interface);
    return true;
  }

  if (extractCompilerFlagsFromInterface(interfacePath, SB, ArgSaver,
                                        interfaceInfo.Arguments,
                                        preferredTarget)) {
    InterfaceSubContextDelegateImpl::diagnose(
        interfacePath, diagnosticLoc, SM, &Diags,
        diag::error_extracting_version_from_module_interface);
    return true;
  }

  assert(VersMatches.size() == 2);
  // FIXME We should diagnose this at a location that makes sense:
  auto Vers =
      VersionParser::parseVersionString(VersMatches[1], SourceLoc(), &Diags);
  if (!Vers.has_value()) {
    InterfaceSubContextDelegateImpl::diagnose(
        interfacePath, diagnosticLoc, SM, &Diags,
        diag::error_extracting_version_from_module_interface);
    return true;
  }

  if (CompRe.match(SB, &CompMatches)) {
    assert(CompMatches.size() == 2);
    interfaceInfo.CompilerVersion = ArgSaver.save(CompMatches[1]).str();

    // For now, successfully parsing the tools version out of the interface is
    // optional.
    auto ToolsVersRe = getSwiftInterfaceCompilerToolsVersionRegex();
    SmallVector<StringRef, 2> VendorToolsVersMatches;
    if (ToolsVersRe.match(interfaceInfo.CompilerVersion,
                          &VendorToolsVersMatches)) {
      interfaceInfo.CompilerToolsVersion = VersionParser::parseVersionString(
          VendorToolsVersMatches[1], SourceLoc(), nullptr);
    }
  } else {
    // Don't diagnose; handwritten module interfaces don't include this field.
    interfaceInfo.CompilerVersion = "(unspecified, file possibly handwritten)";
  }

  // For now: we support anything with the same "major version" and assume
  // minor versions might be interesting for debugging, or special-casing a
  // compatible field variant.
  if (Vers->asMajorVersion() != InterfaceFormatVersion.asMajorVersion()) {
    InterfaceSubContextDelegateImpl::diagnose(
        interfacePath, diagnosticLoc, SM, &Diags,
        diag::unsupported_version_of_module_interface, interfacePath, *Vers);
    return true;
  }
  return false;
}

bool ModuleInterfaceLoader::buildExplicitSwiftModuleFromSwiftInterface(
    CompilerInstance &Instance, const StringRef moduleCachePath,
    const StringRef backupInterfaceDir, const StringRef prebuiltCachePath,
    const StringRef ABIDescriptorPath, StringRef interfacePath,
    StringRef outputPath, bool ShouldSerializeDeps,
    ArrayRef<std::string> CompiledCandidates,
    DependencyTracker *tracker) {
  
  if (!Instance.getInvocation().getIRGenOptions().AlwaysCompile) {
    // First, check if the expected output already exists and possibly
    // up-to-date w.r.t. all of the dependencies it was built with. If so, early
    // exit.
    UpToDateModuleCheker checker(
        Instance.getASTContext(),
        RequireOSSAModules_t(Instance.getSILOptions()));
    ModuleRebuildInfo rebuildInfo;
    SmallVector<FileDependency, 3> allDeps;
    std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
    if (checker.swiftModuleIsUpToDate(outputPath, rebuildInfo, allDeps,
                                      moduleBuffer)) {
      if (Instance.getASTContext()
              .LangOpts.EnableSkipExplicitInterfaceModuleBuildRemarks) {
        Instance.getDiags().diagnose(
            SourceLoc(), diag::explicit_interface_build_skipped, outputPath);
      }
      return false;
    }
  }
  
  // Read out the compiler version.
  llvm::BumpPtrAllocator alloc;
  llvm::StringSaver ArgSaver(alloc);
  SwiftInterfaceInfo InterfaceInfo;
  readSwiftInterfaceVersionAndArgs(
      Instance.getSourceMgr(), Instance.getDiags(), ArgSaver, InterfaceInfo,
      interfacePath, SourceLoc(),
      Instance.getInvocation().getLangOptions().Target);

  auto Builder = ExplicitModuleInterfaceBuilder(
      Instance, &Instance.getDiags(), Instance.getSourceMgr(),
      moduleCachePath, backupInterfaceDir, prebuiltCachePath,
      ABIDescriptorPath, {});
  auto error = Builder.buildSwiftModuleFromInterface(
      interfacePath, outputPath, ShouldSerializeDeps, /*ModuleBuffer*/nullptr,
      CompiledCandidates, InterfaceInfo.CompilerVersion);
  if (!error)
    return false;
  else
    return true;
}

void ModuleInterfaceLoader::collectVisibleTopLevelModuleNames(
    SmallVectorImpl<Identifier> &names) const {
  collectVisibleTopLevelModuleNamesImpl(
      names,
      file_types::getExtension(file_types::TY_SwiftModuleInterfaceFile));
}

void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
    const SearchPathOptions &SearchPathOpts, const LangOptions &LangOpts,
    const ClangImporterOptions &clangImporterOpts, const CASOptions &casOpts,
    bool suppressRemarks, RequireOSSAModules_t RequireOSSAModules) {
  GenericArgs.push_back("-frontend");
  // Start with a genericSubInvocation that copies various state from our
  // invoking ASTContext.
  GenericArgs.push_back("-compile-module-from-interface");
  genericSubInvocation.setTargetTriple(LangOpts.Target);

  auto triple = ArgSaver.save(genericSubInvocation.getTargetTriple());
  if (!triple.empty()) {
    GenericArgs.push_back("-target");
    GenericArgs.push_back(triple);
  }

  if (LangOpts.ClangTarget.has_value()) {
    genericSubInvocation.getLangOptions().ClangTarget = LangOpts.ClangTarget;
    auto triple = ArgSaver.save(genericSubInvocation.getLangOptions()
      .ClangTarget->getTriple());
    assert(!triple.empty());
    // In explicit module build, all PCMs will be built using the given clang target.
    // So the Swift interface should know that as well to load these PCMs properly.
    GenericArgs.push_back("-clang-target");
    GenericArgs.push_back(triple);
  }

  // Inherit the Swift language version
  genericSubInvocation.getLangOptions().EffectiveLanguageVersion =
    LangOpts.EffectiveLanguageVersion;
  GenericArgs.push_back("-swift-version");
  GenericArgs.push_back(ArgSaver.save(genericSubInvocation.getLangOptions()
    .EffectiveLanguageVersion.asAPINotesVersionString()));

  genericSubInvocation.setImportSearchPaths(
      SearchPathOpts.getImportSearchPaths());
  genericSubInvocation.setFrameworkSearchPaths(
      SearchPathOpts.getFrameworkSearchPaths());
  if (!SearchPathOpts.getSDKPath().empty()) {
    // Add -sdk arguments to the module building commands.
    // Module building commands need this because dependencies sometimes use
    // sdk-relative paths (prebuilt modules for example). Without -sdk, the command
    // will not be able to local these dependencies, leading to unnecessary
    // building from textual interfaces.
    GenericArgs.push_back("-sdk");
    GenericArgs.push_back(ArgSaver.save(SearchPathOpts.getSDKPath()));
    genericSubInvocation.setSDKPath(SearchPathOpts.getSDKPath().str());
  }

  if (SearchPathOpts.PlatformAvailabilityInheritanceMapPath) {
    GenericArgs.push_back("-platform-availability-inheritance-map-path");
    GenericArgs.push_back(ArgSaver.save(*SearchPathOpts.PlatformAvailabilityInheritanceMapPath));
    genericSubInvocation.setPlatformAvailabilityInheritanceMapPath(*SearchPathOpts.PlatformAvailabilityInheritanceMapPath);
  }

  for (auto &entry : SearchPathOpts.PluginSearchOpts) {
    switch (entry.getKind()) {
    case PluginSearchOption::Kind::LoadPluginLibrary: {
      auto &val = entry.get<PluginSearchOption::LoadPluginLibrary>();
      GenericArgs.push_back("-load-plugin-library");
      GenericArgs.push_back(ArgSaver.save(val.LibraryPath));
      break;
    }
    case PluginSearchOption::Kind::LoadPluginExecutable: {
      auto &val = entry.get<PluginSearchOption::LoadPluginExecutable>();
      for (auto &moduleName : val.ModuleNames) {
        GenericArgs.push_back("-load-plugin-executable");
        GenericArgs.push_back(
            ArgSaver.save(val.ExecutablePath + "#" + moduleName));
      }
      break;
    }
    case PluginSearchOption::Kind::PluginPath: {
      auto &val = entry.get<PluginSearchOption::PluginPath>();
      GenericArgs.push_back("-plugin-path");
      GenericArgs.push_back(ArgSaver.save(val.SearchPath));
      break;
    }
    case PluginSearchOption::Kind::ExternalPluginPath: {
      auto &val = entry.get<PluginSearchOption::ExternalPluginPath>();
      GenericArgs.push_back("-external-plugin-path");
      GenericArgs.push_back(
          ArgSaver.save(val.SearchPath + "#" + val.ServerPath));
      break;
    }
    }
  }

  genericSubInvocation.getFrontendOptions().InputMode
      = FrontendOptions::ParseInputMode::SwiftModuleInterface;
  if (!SearchPathOpts.RuntimeResourcePath.empty()) {
    genericSubInvocation.setRuntimeResourcePath(SearchPathOpts.RuntimeResourcePath);
  }

  // Inhibit warnings from the genericSubInvocation since we are assuming the user
  // is not in a position to address them.
  genericSubInvocation.getDiagnosticOptions().SuppressWarnings = true;
  GenericArgs.push_back("-suppress-warnings");
  // Inherit the parent invocation's setting on whether to suppress remarks
  if (suppressRemarks) {
    genericSubInvocation.getDiagnosticOptions().SuppressRemarks = true;
    GenericArgs.push_back("-suppress-remarks");
  }

  // Inherit this setting down so that it can affect error diagnostics (mostly
  // by making them non-fatal).
  genericSubInvocation.getLangOptions().DebuggerSupport = LangOpts.DebuggerSupport;
  if (LangOpts.DebuggerSupport) {
    GenericArgs.push_back("-debugger-support");
  }

  // Disable this; deinitializers always get printed with `@objc` even in
  // modules that don't import Foundation.
  genericSubInvocation.getLangOptions().EnableObjCAttrRequiresFoundation = false;
  GenericArgs.push_back("-disable-objc-attr-requires-foundation-module");

  // If we are supposed to use RequireOSSAModules, do so.
  genericSubInvocation.getSILOptions().EnableOSSAModules =
      bool(RequireOSSAModules);
  if (LangOpts.DisableAvailabilityChecking) {
    genericSubInvocation.getLangOptions().DisableAvailabilityChecking = true;
    GenericArgs.push_back("-disable-availability-checking");
  }

  // Pass-down the obfuscators so we can get the serialized search paths properly.
  genericSubInvocation.setSerializedPathObfuscator(
    SearchPathOpts.DeserializedPathRecoverer);
  SearchPathOpts.DeserializedPathRecoverer
    .forEachPair([&](StringRef lhs, StringRef rhs) {
      GenericArgs.push_back("-serialized-path-obfuscate");
      std::string pair = (llvm::Twine(lhs) + "=" + rhs).str();
      GenericArgs.push_back(ArgSaver.save(pair));
  });

  if (LangOpts.hasFeature(Feature::LayoutPrespecialization)) {
    genericSubInvocation.getLangOptions().enableFeature(
        Feature::LayoutPrespecialization);
  }

  genericSubInvocation.getClangImporterOptions().DirectClangCC1ModuleBuild =
      clangImporterOpts.DirectClangCC1ModuleBuild;
  genericSubInvocation.getClangImporterOptions().ClangImporterDirectCC1Scan =
      clangImporterOpts.ClangImporterDirectCC1Scan;

  // Validate Clang modules once per-build session flags must be consistent
  // across all module sub-invocations
  if (clangImporterOpts.ValidateModulesOnce) {
    genericSubInvocation.getClangImporterOptions().ValidateModulesOnce = true;
    genericSubInvocation.getClangImporterOptions().BuildSessionFilePath = clangImporterOpts.BuildSessionFilePath;
    GenericArgs.push_back("-validate-clang-modules-once");
    GenericArgs.push_back("-clang-build-session-file");
    GenericArgs.push_back(clangImporterOpts.BuildSessionFilePath);
  }

  if (casOpts.EnableCaching) {
    genericSubInvocation.getCASOptions().EnableCaching = casOpts.EnableCaching;
    genericSubInvocation.getCASOptions().CASOpts = casOpts.CASOpts;
    casOpts.enumerateCASConfigurationFlags(
        [&](StringRef Arg) { GenericArgs.push_back(ArgSaver.save(Arg)); });
    // ClangIncludeTree is default on when caching is enabled.
    genericSubInvocation.getClangImporterOptions().UseClangIncludeTree = true;
  }

  if (!clangImporterOpts.UseClangIncludeTree) {
    genericSubInvocation.getClangImporterOptions().UseClangIncludeTree = false;
    GenericArgs.push_back("-no-clang-include-tree");
  }
}

bool InterfaceSubContextDelegateImpl::extractSwiftInterfaceVersionAndArgs(
    CompilerInvocation &subInvocation, SwiftInterfaceInfo &interfaceInfo,
    StringRef interfacePath, SourceLoc diagnosticLoc) {
  if (readSwiftInterfaceVersionAndArgs(SM, *Diags, ArgSaver, interfaceInfo,
                                       interfacePath, diagnosticLoc,
                                       subInvocation.getLangOptions().Target))
    return true;

  // Prior to Swift 5.9, swiftinterfaces were always built (accidentally) with
  // `-target-min-inlining-version target` prepended to the argument list. To
  // preserve compatibility we must continue to prepend those flags to the
  // invocation when the interface was generated by an older compiler.
  if (auto toolsVersion = interfaceInfo.CompilerToolsVersion) {
    if (toolsVersion < version::Version{5, 9}) {
      interfaceInfo.Arguments.push_back("-target-min-inlining-version");
      interfaceInfo.Arguments.push_back("target");
    }
  }

  SmallString<32> ExpectedModuleName = subInvocation.getModuleName();
  if (subInvocation.parseArgs(interfaceInfo.Arguments, *Diags)) {
    return true;
  }

  if (subInvocation.getModuleName() != ExpectedModuleName) {
    auto DiagKind = diag::serialization_name_mismatch;
    if (subInvocation.getLangOptions().DebuggerSupport)
      DiagKind = diag::serialization_name_mismatch_repl;
    diagnose(interfacePath, diagnosticLoc,
             DiagKind, subInvocation.getModuleName(), ExpectedModuleName);
    return true;
  }

  return false;
}

InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
    SourceManager &SM, DiagnosticEngine *Diags,
    const SearchPathOptions &searchPathOpts, const LangOptions &langOpts,
    const ClangImporterOptions &clangImporterOpts, const CASOptions &casOpts,
    ModuleInterfaceLoaderOptions LoaderOpts, bool buildModuleCacheDirIfAbsent,
    StringRef moduleCachePath, StringRef prebuiltCachePath,
    StringRef backupModuleInterfaceDir,
    bool serializeDependencyHashes, bool trackSystemDependencies,
    RequireOSSAModules_t requireOSSAModules)
    : SM(SM), Diags(Diags), ArgSaver(Allocator) {
  genericSubInvocation.setMainExecutablePath(LoaderOpts.mainExecutablePath);
  inheritOptionsForBuildingInterface(searchPathOpts, langOpts,
                                     clangImporterOpts, casOpts,
                                     Diags->getSuppressRemarks(),
                                     requireOSSAModules);
  // Configure front-end input.
  auto &SubFEOpts = genericSubInvocation.getFrontendOptions();
  SubFEOpts.RequestedAction = LoaderOpts.requestedAction;
  SubFEOpts.StrictImplicitModuleContext =
      LoaderOpts.strictImplicitModuleContext;
  if (!moduleCachePath.empty()) {
    genericSubInvocation.setClangModuleCachePath(moduleCachePath);
  }
  if (!prebuiltCachePath.empty()) {
    genericSubInvocation.getFrontendOptions().PrebuiltModuleCachePath =
      prebuiltCachePath.str();
  }
  if (!backupModuleInterfaceDir.empty()) {
    genericSubInvocation.getFrontendOptions().BackupModuleInterfaceDir =
      backupModuleInterfaceDir.str();
  }
  if (trackSystemDependencies) {
    genericSubInvocation.getFrontendOptions().IntermoduleDependencyTracking =
        IntermoduleDepTrackingMode::IncludeSystem;
    GenericArgs.push_back("-track-system-dependencies");
  } else {
    // Always track at least the non-system dependencies for interface building.
    genericSubInvocation.getFrontendOptions().IntermoduleDependencyTracking =
        IntermoduleDepTrackingMode::ExcludeSystem;
  }
  if (LoaderOpts.disableImplicitSwiftModule) {
    genericSubInvocation.getFrontendOptions().DisableImplicitModules = true;
    GenericArgs.push_back("-disable-implicit-swift-modules");
    GenericArgs.push_back("-Xcc");
    GenericArgs.push_back("-fno-implicit-modules");
    GenericArgs.push_back("-Xcc");
    GenericArgs.push_back("-fno-implicit-module-maps");
  }
  // If building an application extension, make sure API use
  // is restricted accordingly in downstream dependnecies.
  if (langOpts.EnableAppExtensionRestrictions) {
    genericSubInvocation.getLangOptions().EnableAppExtensionRestrictions = true;
    GenericArgs.push_back("-application-extension");
  }

  // Pass down -explicit-swift-module-map-file
  StringRef explicitSwiftModuleMap = searchPathOpts.ExplicitSwiftModuleMapPath;
  genericSubInvocation.getSearchPathOptions().ExplicitSwiftModuleMapPath =
    explicitSwiftModuleMap.str();

  // Pass down VFSOverlay flags (do not need to inherit the options because
  // FileSystem is shared).
  for (auto &Overlay : searchPathOpts.VFSOverlayFiles) {
    GenericArgs.push_back("-vfsoverlay");
    GenericArgs.push_back(Overlay);
  }

  // Load plugin libraries for macro expression as default arguments
  genericSubInvocation.getSearchPathOptions().PluginSearchOpts =
      searchPathOpts.PluginSearchOpts;

  // Get module loading behavior options.
  genericSubInvocation.getSearchPathOptions().ScannerModuleValidation = searchPathOpts.ScannerModuleValidation;
  genericSubInvocation.getSearchPathOptions().ModuleLoadMode =
      searchPathOpts.ModuleLoadMode;

  auto &subClangImporterOpts = genericSubInvocation.getClangImporterOptions();
  // Respect the detailed-record preprocessor setting of the parent context.
  // This, and the "raw" clang module format it implicitly enables, are
  // required by sourcekitd.
  subClangImporterOpts.DetailedPreprocessingRecord =
    clangImporterOpts.DetailedPreprocessingRecord;

  std::vector<std::string> inheritedParentContextClangArgs;
  if (LoaderOpts.requestedAction ==
      FrontendOptions::ActionType::ScanDependencies) {
    // For a dependency scanning action, interface build command generation must
    // inherit `-Xcc` flags used for configuration of the building instance's
    // `ClangImporter`. However, we can ignore Clang search path flags because
    // explicit Swift module build tasks will not rely on them and they may be
    // source-target-context-specific and hinder module sharing across
    // compilation source targets.
    // Clang module dependecies of this Swift dependency will be distinguished
    // by their context hash for different variants, so would still cause a
    // difference in the Swift compile commands, when different.
    inheritedParentContextClangArgs =
        clangImporterOpts.getReducedExtraArgsForSwiftModuleDependency();
    genericSubInvocation.getFrontendOptions()
        .DependencyScanningSubInvocation = true;
  } else if (LoaderOpts.strictImplicitModuleContext ||
             // Explicit module Interface verification jobs still spawn a sub-instance
             // and we must ensure this sub-instance gets all of the Xcc flags.
             LoaderOpts.disableImplicitSwiftModule) {
    // If the compiler has been asked to be strict with ensuring downstream
    // dependencies get the parent invocation's context, inherit the extra Clang
    // arguments also. Inherit any clang-specific state of the compilation
    // (macros, clang flags, etc.)
    inheritedParentContextClangArgs = clangImporterOpts.ExtraArgs;
  }
  subClangImporterOpts.ExtraArgs = inheritedParentContextClangArgs;
  // If using DirectCC1Scan, the command-line reduction is handled inside
  // `getSwiftExplicitModuleDirectCC1Args()`, there is no need to inherit
  // anything here as the ExtraArgs from the invocation are clang driver
  // options, not cc1 options.
  if (!clangImporterOpts.ClangImporterDirectCC1Scan) {
    for (auto arg : subClangImporterOpts.ExtraArgs) {
      GenericArgs.push_back("-Xcc");
      GenericArgs.push_back(ArgSaver.save(arg));
    }
  }

  subClangImporterOpts.EnableClangSPI = clangImporterOpts.EnableClangSPI;
  if (!subClangImporterOpts.EnableClangSPI) {
    GenericArgs.push_back("-disable-clang-spi");
  }

  // Tell the genericSubInvocation to serialize dependency hashes if asked to do
  // so.
  auto &frontendOpts = genericSubInvocation.getFrontendOptions();
  frontendOpts.SerializeModuleInterfaceDependencyHashes =
    serializeDependencyHashes;
  if (serializeDependencyHashes) {
    GenericArgs.push_back("-serialize-module-interface-dependency-hashes");
  }

  // Tell the genericSubInvocation to remark on rebuilds from an interface if asked
  // to do so.
  frontendOpts.RemarkOnRebuildFromModuleInterface =
    LoaderOpts.remarkOnRebuildFromInterface;
  if (LoaderOpts.remarkOnRebuildFromInterface) {
    GenericArgs.push_back("-Rmodule-interface-rebuild");
  }
  // This flag only matters when we are verifying an textual interface.
  frontendOpts.DowngradeInterfaceVerificationError =
    LoaderOpts.downgradeInterfaceVerificationError;
  // Note that we don't assume cachePath is the same as the Clang
  // module cache path at this point.
  if (buildModuleCacheDirIfAbsent && !moduleCachePath.empty())
    (void)llvm::sys::fs::create_directories(moduleCachePath);

  // Inherit all block list configuration files
  frontendOpts.BlocklistConfigFilePaths = langOpts.BlocklistConfigFilePaths;
  for (auto &blocklist: langOpts.BlocklistConfigFilePaths) {
    GenericArgs.push_back("-blocklist-file");
    GenericArgs.push_back(blocklist);
  }

  // For now, we only inherit the C++ interoperability mode in
  // Explicit Module Builds.
  if (langOpts.EnableCXXInterop &&
      (frontendOpts.DisableImplicitModules ||
       LoaderOpts.requestedAction ==
           FrontendOptions::ActionType::ScanDependencies)) {
    // Modelled after a reverse of validateCxxInteropCompatibilityMode
    genericSubInvocation.getLangOptions().EnableCXXInterop = true;
    genericSubInvocation.getLangOptions().cxxInteropCompatVersion =
        langOpts.cxxInteropCompatVersion;
    std::string compatVersion;
    if (langOpts.cxxInteropCompatVersion.empty())
      compatVersion = "default";
    else if (langOpts.cxxInteropCompatVersion[0] == 5)
      compatVersion = "swift-5.9";
    else if (langOpts.cxxInteropCompatVersion[0] == 6)
      compatVersion = "swift-6";
    else if (langOpts.cxxInteropCompatVersion[0] ==
             version::getUpcomingCxxInteropCompatVersion())
      compatVersion = "upcoming-swift";
    else // TODO: This may need to be updated once more versions are added
      compatVersion = "default";

    GenericArgs.push_back(
        ArgSaver.save("-cxx-interoperability-mode=" + compatVersion));
  }
}

/// Calculate an output filename in \p genericSubInvocation's cache path that
/// includes a hash of relevant key data.
StringRef InterfaceSubContextDelegateImpl::computeCachedOutputPath(
                             StringRef moduleName,
                             StringRef useInterfacePath,
                             StringRef sdkPath,
                             llvm::SmallString<256> &OutPath,
                             StringRef &CacheHash) {
  OutPath = genericSubInvocation.getClangModuleCachePath();
  llvm::sys::path::append(OutPath, moduleName);
  OutPath.append("-");
  auto hashStart = OutPath.size();
  OutPath.append(getCacheHash(useInterfacePath, sdkPath));
  CacheHash = OutPath.str().substr(hashStart);
  OutPath.append(".");
  auto OutExt = file_types::getExtension(file_types::TY_SwiftModuleFile);
  OutPath.append(OutExt);
  return OutPath.str();
}

/// Construct a cache key for the .swiftmodule being generated. There is a
/// balance to be struck here between things that go in the cache key and
/// things that go in the "up to date" check of the cache entry. We want to
/// avoid fighting over a single cache entry too much when (say) running
/// different compiler versions on the same machine or different inputs
/// that happen to have the same short module name, so we will disambiguate
/// those in the key. But we want to invalidate and rebuild a cache entry
/// -- rather than making a new one and potentially filling up the cache
/// with dead entries -- when other factors change, such as the contents of
/// the .swiftinterface input or its dependencies.
std::string
InterfaceSubContextDelegateImpl::getCacheHash(StringRef useInterfacePath,
                                              StringRef sdkPath) {
  auto normalizedTargetTriple =
      getTargetSpecificModuleTriple(genericSubInvocation.getLangOptions().Target);
  std::string sdkBuildVersion = getSDKBuildVersion(sdkPath);

  llvm::hash_code H = hash_combine(
      // Start with the compiler version (which will be either tag names or
      // revs). Explicitly don't pass in the "effective" language version --
      // this would mean modules built in different -swift-version modes would
      // rebuild their dependencies.
      swift::version::getSwiftFullVersion(),

      // Simplest representation of input "identity" (not content) is just a
      // pathname, and probably all we can get from the VFS in this regard
      // anyways.
      useInterfacePath,

      // Include the normalized target triple. In practice, .swiftinterface
      // files will be in target-specific subdirectories and would have
      // target-specific pieces #if'd out. However, it doesn't hurt to include
      // it, and it guards against mistakenly reusing cached modules across
      // targets. Note that this normalization explicitly doesn't include the
      // minimum deployment target (e.g. the '12.0' in 'ios12.0').
      normalizedTargetTriple.str(),

      // The SDK path is going to affect how this module is imported, so
      // include it.
      genericSubInvocation.getSDKPath(),

      // The SDK build version may identify differences in headers
      // that affects references serialized in the cached file.
      sdkBuildVersion,

      // Applying the distribution channel of the current compiler enables
      // different compilers to share a module cache location.
      version::getCurrentCompilerChannel(),

      // Whether or not we're tracking system dependencies affects the
      // invalidation behavior of this cache item.
      genericSubInvocation.getFrontendOptions().shouldTrackSystemDependencies(),

      // Whether or not caching is enabled affects if the instance is able to
      // correctly load the dependencies.
      genericSubInvocation.getCASOptions().getModuleScanningHashComponents(),

      // Whether or not OSSA modules are enabled.
      //
      // If OSSA modules are enabled, we use a separate namespace of modules to
      // ensure that we compile all swift interface files with the option set.
      unsigned(genericSubInvocation.getSILOptions().EnableOSSAModules)
      );

  return llvm::toString(llvm::APInt(64, H), 36, /*Signed=*/false);
}

std::error_code
InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName,
                                                 StringRef interfacePath,
                                                 StringRef sdkPath,
                                                 StringRef outputPath,
                                                 SourceLoc diagLoc,
    llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*, ArrayRef<StringRef>,
                            ArrayRef<StringRef>, StringRef)> action) {
  return runInSubCompilerInstance(moduleName, interfacePath, sdkPath, outputPath,
                                  diagLoc, /*silenceErrors=*/false,
                                  [&](SubCompilerInstanceInfo &info){
    return action(info.Instance->getASTContext(),
                  info.Instance->getMainModule(),
                  info.BuildArguments,
                  info.ExtraPCMArgs,
                  info.Hash);
  });
}

std::error_code
InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
                                                          StringRef interfacePath,
                                                          StringRef sdkPath,
                                                          StringRef outputPath,
                                                          SourceLoc diagLoc,
                                                          bool silenceErrors,
                  llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) {
  // We are about to mess up the compiler invocation by using the compiler
  // arguments in the textual interface file. So copy to use a new compiler
  // invocation.
  CompilerInvocation subInvocation = genericSubInvocation;

  // save `StrictImplicitModuleContext`
  bool StrictImplicitModuleContext =
      subInvocation.getFrontendOptions().StrictImplicitModuleContext;

  // It isn't appropriate to restrict use of experimental features in another
  // module since it may have been built with a different compiler that allowed
  // the use of the feature.
  subInvocation.getLangOptions().RestrictNonProductionExperimentalFeatures =
      false;

  // Save the target triple from the original context.
  llvm::Triple originalTargetTriple(subInvocation.getLangOptions().Target);

  std::vector<StringRef> BuildArgs(GenericArgs.begin(), GenericArgs.end());
  assert(BuildArgs.size() == GenericArgs.size());
  // Configure inputs
  subInvocation.getFrontendOptions().InputsAndOutputs
    .addInputFile(interfacePath);
  BuildArgs.push_back(interfacePath);
  subInvocation.setModuleName(moduleName);
  BuildArgs.push_back("-module-name");
  BuildArgs.push_back(moduleName);

  // Calculate output path of the module.
  llvm::SmallString<256> buffer;
  StringRef CacheHash;
  auto hashedOutput = computeCachedOutputPath(moduleName, interfacePath,
                                              sdkPath, buffer, CacheHash);
  // If no specific output path is given, use the hashed output path.
  if (outputPath.empty()) {
    outputPath = hashedOutput;
  }

  // Configure the outputs in front-end options. There must be an equal number of
  // inputs and outputs.
  std::vector<std::string> outputFiles{"/<unused>"};
  std::vector<SupplementaryOutputPaths> ModuleOutputPaths;
  ModuleOutputPaths.emplace_back();
  if (subInvocation.getFrontendOptions().RequestedAction ==
          FrontendOptions::ActionType::EmitModuleOnly) {
    ModuleOutputPaths.back().ModuleOutputPath = outputPath.str();
  }
  assert(subInvocation.getFrontendOptions().InputsAndOutputs.isWholeModule());
  subInvocation.getFrontendOptions().InputsAndOutputs
    .setMainAndSupplementaryOutputs(outputFiles, ModuleOutputPaths);

  SwiftInterfaceInfo interfaceInfo;
  // Extract compiler arguments from the interface file and use them to configure
  // the compiler invocation.
  if (extractSwiftInterfaceVersionAndArgs(subInvocation, interfaceInfo,
                                          interfacePath, diagLoc)) {
    return std::make_error_code(std::errc::not_supported);
  }

  // Insert arguments collected from the interface file.
  BuildArgs.insert(BuildArgs.end(), interfaceInfo.Arguments.begin(),
                   interfaceInfo.Arguments.end());

  // restore `StrictImplicitModuleContext`
  subInvocation.getFrontendOptions().StrictImplicitModuleContext =
      StrictImplicitModuleContext;

  CompilerInstance subInstance;
  SubCompilerInstanceInfo info;
  info.Instance = &subInstance;
  info.CompilerVersion = interfaceInfo.CompilerVersion;

  subInstance.getSourceMgr().setFileSystem(SM.getFileSystem());

  ForwardingDiagnosticConsumer FDC(*Diags);
  NullDiagnosticConsumer noopConsumer;
  if (!silenceErrors) {
    subInstance.addDiagnosticConsumer(&FDC);
  } else {
    subInstance.addDiagnosticConsumer(&noopConsumer);
  }

  std::string InstanceSetupError;
  if (subInstance.setup(subInvocation, InstanceSetupError)) {
    return std::make_error_code(std::errc::not_supported);
  }

  info.BuildArguments = BuildArgs;
  info.Hash = CacheHash;
  auto target = *(std::find(BuildArgs.rbegin(), BuildArgs.rend(), "-target") - 1);
  auto langVersion = *(std::find(BuildArgs.rbegin(), BuildArgs.rend(),
                                 "-swift-version") - 1);

  std::vector<StringRef> ExtraPCMArgs = {
    // PCMs should use the effective Swift language version for apinotes.
    "-Xcc",
    ArgSaver.save((llvm::Twine("-fapinotes-swift-version=") + langVersion).str())
  };
  if (!subInvocation.getLangOptions().ClangTarget.has_value()) {
    ExtraPCMArgs.insert(ExtraPCMArgs.begin(), {"-Xcc", "-target",
                                               "-Xcc", target});
  }

  info.ExtraPCMArgs = ExtraPCMArgs;
  // Run the action under the sub compiler instance.
  return action(info);
}

struct ExplicitSwiftModuleLoader::Implementation {
  ASTContext &Ctx;
  llvm::BumpPtrAllocator Allocator;
  llvm::StringMap<ExplicitSwiftModuleInputInfo> ExplicitModuleMap;
  Implementation(ASTContext &Ctx) : Ctx(Ctx) {}

  void parseSwiftExplicitModuleMap(StringRef fileName) {
    ExplicitModuleMapParser parser(Allocator);
    llvm::StringMap<ExplicitClangModuleInputInfo> ExplicitClangModuleMap;
    // Load the input file.
    llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileBufOrErr =
        llvm::MemoryBuffer::getFile(fileName);
    if (!fileBufOrErr) {
      Ctx.Diags.diagnose(SourceLoc(), diag::explicit_swift_module_map_missing,
                         fileName);
      return;
    }

    auto hasError = parser.parseSwiftExplicitModuleMap(
        (*fileBufOrErr)->getMemBufferRef(), ExplicitModuleMap,
        ExplicitClangModuleMap);

    if (hasError)
      Ctx.Diags.diagnose(SourceLoc(), diag::explicit_swift_module_map_corrupted,
                         fileName);

    // A single module map can define multiple modules; keep track of the ones
    // we've seen so that we don't generate duplicate flags.
    std::set<std::string> moduleMapsSeen;
    std::vector<std::string> &extraClangArgs = Ctx.ClangImporterOpts.ExtraArgs;
    for (auto &entry : ExplicitClangModuleMap) {
      const auto &moduleMapPath = entry.getValue().moduleMapPath;
      if (!moduleMapPath.empty() &&
          entry.getValue().isBridgingHeaderDependency &&
          moduleMapsSeen.find(moduleMapPath) == moduleMapsSeen.end()) {
        moduleMapsSeen.insert(moduleMapPath);
        extraClangArgs.push_back(
            (Twine("-fmodule-map-file=") + moduleMapPath).str());
      }

      const auto &modulePath = entry.getValue().modulePath;
      if (!modulePath.empty()) {
        extraClangArgs.push_back(
            (Twine("-fmodule-file=") + entry.getKey() + "=" + modulePath)
                .str());
      }
    }
  }

  void addCommandLineExplicitInputs(
      const std::vector<std::pair<std::string, std::string>>
          &commandLineExplicitInputs) {
    for (const auto &moduleInput : commandLineExplicitInputs) {
      ExplicitSwiftModuleInputInfo entry(moduleInput.second, {}, {}, {});
      ExplicitModuleMap.try_emplace(moduleInput.first, std::move(entry));
    }
  }
};

ExplicitSwiftModuleLoader::ExplicitSwiftModuleLoader(
      ASTContext &ctx,
      DependencyTracker *tracker,
      ModuleLoadingMode loadMode,
      bool IgnoreSwiftSourceInfoFile):
        SerializedModuleLoaderBase(ctx, tracker, loadMode,
                                   IgnoreSwiftSourceInfoFile),
        Impl(*new Implementation(ctx)) {}

ExplicitSwiftModuleLoader::~ExplicitSwiftModuleLoader() { delete &Impl; }

bool ExplicitSwiftModuleLoader::findModule(
    ImportPath::Element ModuleID, SmallVectorImpl<char> *ModuleInterfacePath,
    SmallVectorImpl<char> *ModuleInterfaceSourcePath,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
    bool skipBuildingInterface, bool isTestableDependencyLookup,
    bool &IsFramework, bool &IsSystemModule) {
  // Find a module with an actual, physical name on disk, in case
  // -module-alias is used (otherwise same).
  //
  // For example, if '-module-alias Foo=Bar' is passed in to the frontend, and an
  // input file has 'import Foo', a module called Bar (real name) should be searched.
  StringRef moduleName = Ctx.getRealModuleName(ModuleID.Item).str();

  auto it = Impl.ExplicitModuleMap.find(moduleName);
  // If no explicit module path is given matches the name, return with an
  // error code.
  if (it == Impl.ExplicitModuleMap.end()) {
    return false;
  }
  auto &moduleInfo = it->getValue();

  // Set IsFramework bit according to the moduleInfo
  IsFramework = moduleInfo.isFramework;
  IsSystemModule = moduleInfo.isSystem;

  auto &fs = *Ctx.SourceMgr.getFileSystem();
  // Open .swiftmodule file
  auto moduleBuf = fs.getBufferForFile(moduleInfo.modulePath);
  if (!moduleBuf) {
    // We cannot read the module content, diagnose.
    Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
                       moduleInfo.modulePath);
    return false;
  }

  assert(moduleBuf);
  const bool isForwardingModule = !serialization::isSerializedAST(moduleBuf
    .get()->getBuffer());
  // If the module is a forwarding module, read the actual content from the path
  // encoded in the forwarding module as the actual module content.
  if (isForwardingModule) {
    auto forwardingModule = ForwardingModule::load(*moduleBuf.get());
    if (forwardingModule) {
      moduleBuf = fs.getBufferForFile(forwardingModule->underlyingModulePath);
      if (!moduleBuf) {
        // We cannot read the module content, diagnose.
        Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
                           moduleInfo.modulePath);
        return false;
      }
    } else {
      // We cannot read the module content, diagnose.
      Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
                         moduleInfo.modulePath);
      return false;
    }
  }
  assert(moduleBuf);
  // Move the opened module buffer to the caller.
  *ModuleBuffer = std::move(moduleBuf.get());

  // Open .swiftdoc file
  if (moduleInfo.moduleDocPath.has_value()) {
    auto moduleDocBuf = fs.getBufferForFile(moduleInfo.moduleDocPath.value());
    if (moduleBuf)
      *ModuleDocBuffer = std::move(moduleDocBuf.get());
  }
  // Open .swiftsourceinfo file
  if (moduleInfo.moduleSourceInfoPath.has_value()) {
    auto moduleSourceInfoBuf = fs.getBufferForFile(moduleInfo.moduleSourceInfoPath.value());
    if (moduleSourceInfoBuf)
      *ModuleSourceInfoBuffer = std::move(moduleSourceInfoBuf.get());
  }
  return true;
}

std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
    ImportPath::Element ModuleID, const SerializedModuleBaseName &BaseName,
    SmallVectorImpl<char> *ModuleInterfacePath,
    SmallVectorImpl<char> *ModuleInterfaceSourcePath,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
    bool skipBuildingInterface, bool IsFramework,
    bool IsTestableDependencyLookup) {
  llvm_unreachable("Not supported in the Explicit Swift Module Loader.");
  return std::make_error_code(std::errc::not_supported);
}

bool ExplicitSwiftModuleLoader::canImportModule(
    ImportPath::Module path, SourceLoc loc, ModuleVersionInfo *versionInfo,
    bool isTestableDependencyLookup) {
  // FIXME: Swift submodules?
  if (path.hasSubmodule())
    return false;
  ImportPath::Element mID = path.front();
  // Look up the module with the real name (physical name on disk);
  // in case `-module-alias` is used, the name appearing in source files
  // and the real module name are different. For example, '-module-alias Foo=Bar'
  // maps Foo appearing in source files, e.g. 'import Foo', to the real module
  // name Bar (on-disk name), which should be searched for loading.
  StringRef moduleName = Ctx.getRealModuleName(mID.Item).str();
  auto it = Impl.ExplicitModuleMap.find(moduleName);
  // If no provided explicit module matches the name, then it cannot be imported.
  if (it == Impl.ExplicitModuleMap.end()) {
    return false;
  }

  // If the caller doesn't want version info we're done.
  if (!versionInfo)
    return true;

  // Open .swiftmodule file and read out the version
  auto &fs = *Ctx.SourceMgr.getFileSystem();
  auto moduleBuf = fs.getBufferForFile(it->second.modulePath);
  if (!moduleBuf) {
    Ctx.Diags.diagnose(loc, diag::error_opening_explicit_module_file,
                       it->second.modulePath);
    return false;
  }

  // If it's a forwarding module, load the YAML file from disk and get the path
  // to the actual module for the version check.
  if (!serialization::isSerializedAST((*moduleBuf)->getBuffer())) {
    if (auto forwardingModule = ForwardingModule::load(**moduleBuf)) {
      moduleBuf = fs.getBufferForFile(forwardingModule->underlyingModulePath);
      if (!moduleBuf) {
        Ctx.Diags.diagnose(loc, diag::error_opening_explicit_module_file,
                           forwardingModule->underlyingModulePath);
        return false;
      }
    }
  }

  auto metaData = serialization::validateSerializedAST(
      (*moduleBuf)->getBuffer(),
      Ctx.SILOpts.EnableOSSAModules,
      Ctx.LangOpts.SDKName);
  versionInfo->setVersion(metaData.userModuleVersion,
                          ModuleVersionSourceKind::SwiftBinaryModule);
  return true;
}

void ExplicitSwiftModuleLoader::collectVisibleTopLevelModuleNames(
      SmallVectorImpl<Identifier> &names) const {
  for (auto &entry: Impl.ExplicitModuleMap) {
    names.push_back(Ctx.getIdentifier(entry.getKey()));
  }
}

std::unique_ptr<ExplicitSwiftModuleLoader>
ExplicitSwiftModuleLoader::create(ASTContext &ctx,
    DependencyTracker *tracker, ModuleLoadingMode loadMode,
    StringRef ExplicitSwiftModuleMap,
    const std::vector<std::pair<std::string, std::string>> &ExplicitSwiftModuleInputs,
    bool IgnoreSwiftSourceInfoFile) {
  auto result = std::unique_ptr<ExplicitSwiftModuleLoader>(
    new ExplicitSwiftModuleLoader(ctx, tracker, loadMode,
                                  IgnoreSwiftSourceInfoFile));
  auto &Impl = result->Impl;
  // If the explicit module map is given, try parse it.
  if (!ExplicitSwiftModuleMap.empty()) {
    // Parse a JSON file to collect explicitly built modules.
    Impl.parseSwiftExplicitModuleMap(ExplicitSwiftModuleMap);
  }
  // If some modules are provided with explicit
  // '-swift-module-file' options, add those as well.
  if (!ExplicitSwiftModuleInputs.empty()) {
    Impl.addCommandLineExplicitInputs(ExplicitSwiftModuleInputs);
  }

  return result;
}

struct ExplicitCASModuleLoader::Implementation {
  ASTContext &Ctx;
  llvm::BumpPtrAllocator Allocator;
  llvm::cas::ObjectStore &CAS;
  llvm::cas::ActionCache &Cache;

  llvm::StringMap<ExplicitSwiftModuleInputInfo> ExplicitModuleMap;

  Implementation(ASTContext &Ctx, llvm::cas::ObjectStore &CAS,
                 llvm::cas::ActionCache &Cache)
      : Ctx(Ctx), CAS(CAS), Cache(Cache) {}

  llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> loadBuffer(StringRef ID) {
    auto key = CAS.parseID(ID);
    if (!key)
      return key.takeError();

    auto ref = CAS.getReference(*key);
    if (!ref)
      return nullptr;

    auto loaded = CAS.getProxy(*ref);
    if (!loaded)
      return loaded.takeError();

    return loaded->getMemoryBuffer();
  }

  // Same as the regular explicit module map but must be loaded from
  // CAS, instead of a file that is not tracked by the dependency.
  void parseSwiftExplicitModuleMap(StringRef ID) {
    ExplicitModuleMapParser parser(Allocator);
    llvm::StringMap<ExplicitClangModuleInputInfo> ExplicitClangModuleMap;
    auto buf = loadBuffer(ID);
    if (!buf) {
      Ctx.Diags.diagnose(SourceLoc(), diag::error_cas,
                         toString(buf.takeError()));
      return;
    }
    if (!*buf) {
      Ctx.Diags.diagnose(SourceLoc(), diag::explicit_swift_module_map_missing,
                         ID);
      return;
    }
    llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileBufOrErr =
        llvm::MemoryBuffer::getFile(ID);

    auto hasError = parser.parseSwiftExplicitModuleMap(
        (*buf)->getMemBufferRef(), ExplicitModuleMap, ExplicitClangModuleMap);

    if (hasError)
      Ctx.Diags.diagnose(SourceLoc(), diag::explicit_swift_module_map_corrupted,
                         ID);

    std::set<std::string> moduleMapsSeen;
    std::vector<std::string> &extraClangArgs = Ctx.ClangImporterOpts.ExtraArgs;
    // Append -Xclang if we are not in direct cc1 mode.
    auto appendXclang = [&]() {
      if (!Ctx.ClangImporterOpts.DirectClangCC1ModuleBuild)
        extraClangArgs.push_back("-Xclang");
    };
    for (auto &entry : ExplicitClangModuleMap) {
      const auto &moduleMapPath = entry.getValue().moduleMapPath;
      if (!moduleMapPath.empty() &&
          !Ctx.ClangImporterOpts.UseClangIncludeTree &&
          moduleMapsSeen.find(moduleMapPath) == moduleMapsSeen.end()) {
        moduleMapsSeen.insert(moduleMapPath);
        extraClangArgs.push_back(
            (Twine("-fmodule-map-file=") + moduleMapPath).str());
      }

      const auto &modulePath = entry.getValue().modulePath;
      if (!modulePath.empty()) {
        extraClangArgs.push_back(
            (Twine("-fmodule-file=") + entry.getKey() + "=" + modulePath)
                .str());
      }
      auto cachePath = entry.getValue().moduleCacheKey;
      if (cachePath) {
        appendXclang();
        extraClangArgs.push_back("-fmodule-file-cache-key");
        appendXclang();
        extraClangArgs.push_back(modulePath);
        appendXclang();
        extraClangArgs.push_back(*cachePath);
      }
    }
  }

  void addCommandLineExplicitInputs(
      const std::vector<std::pair<std::string, std::string>>
          &commandLineExplicitInputs) {
    for (const auto &moduleInput : commandLineExplicitInputs) {
      ExplicitSwiftModuleInputInfo entry(moduleInput.second, {}, {}, {});
      ExplicitModuleMap.try_emplace(moduleInput.first, std::move(entry));
    }
  }

  llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
  loadFileBuffer(StringRef ID, StringRef Name) {
    auto key = CAS.parseID(ID);
    if (!key)
      return key.takeError();

    auto moduleLookup = Cache.get(*key);
    if (!moduleLookup)
      return moduleLookup.takeError();
    if (!*moduleLookup)
      return nullptr;

    auto moduleRef = CAS.getReference(**moduleLookup);
    if (!moduleRef)
      return nullptr;

    auto proxy = CAS.getProxy(*moduleRef);
    if (!proxy)
      return proxy.takeError();

    swift::cas::CompileJobResultSchema schema(CAS);
    if (!schema.isRootNode(*proxy))
      return nullptr;

    auto result = schema.load(*moduleRef);
    if (!result)
      return result.takeError();

    auto output = result->getOutput(file_types::ID::TY_SwiftModuleFile);
    if (!output)
      return nullptr;

    auto buf = CAS.getProxy(output->Object);
    if (!buf)
      return buf.takeError();

    return buf->getMemoryBuffer(Name);
  }

  llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
  loadModuleFromPath(StringRef Path, DiagnosticEngine &Diags) {
    for (auto &Deps : ExplicitModuleMap) {
      if (Deps.second.modulePath == Path) {
        if (!Deps.second.moduleCacheKey)
          return nullptr;
        return loadCachedCompileResultFromCacheKey(
            CAS, Cache, Diags, *Deps.second.moduleCacheKey,
            file_types::ID::TY_SwiftModuleFile, Path);
      }
    }
    return nullptr;
  }
};

ExplicitCASModuleLoader::ExplicitCASModuleLoader(ASTContext &ctx,
                                                 llvm::cas::ObjectStore &CAS,
                                                 llvm::cas::ActionCache &cache,
                                                 DependencyTracker *tracker,
                                                 ModuleLoadingMode loadMode,
                                                 bool IgnoreSwiftSourceInfoFile)
    : SerializedModuleLoaderBase(ctx, tracker, loadMode,
                                 IgnoreSwiftSourceInfoFile),
      Impl(*new Implementation(ctx, CAS, cache)) {}

ExplicitCASModuleLoader::~ExplicitCASModuleLoader() { delete &Impl; }

bool ExplicitCASModuleLoader::findModule(
    ImportPath::Element ModuleID, SmallVectorImpl<char> *ModuleInterfacePath,
    SmallVectorImpl<char> *ModuleInterfaceSourcePath,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
    bool skipBuildingInterface, bool isTestableDependencyLookup,
    bool &IsFramework, bool &IsSystemModule) {
  // Find a module with an actual, physical name on disk, in case
  // -module-alias is used (otherwise same).
  //
  // For example, if '-module-alias Foo=Bar' is passed in to the frontend, and
  // an input file has 'import Foo', a module called Bar (real name) should be
  // searched.
  StringRef moduleName = Ctx.getRealModuleName(ModuleID.Item).str();

  auto it = Impl.ExplicitModuleMap.find(moduleName);
  // If no explicit module path is given matches the name, return with an
  // error code.
  if (it == Impl.ExplicitModuleMap.end()) {
    return false;
  }
  auto &moduleInfo = it->getValue();

  // Set IsFramework bit according to the moduleInfo
  IsFramework = moduleInfo.isFramework;
  IsSystemModule = moduleInfo.isSystem;

  // Fallback check for module cache key passed on command-line as module path.
  std::string moduleCASID = moduleInfo.moduleCacheKey
                                ? *moduleInfo.moduleCacheKey
                                : moduleInfo.modulePath;

  // FIXME: the loaded module buffer doesn't set an identifier so it
  // is not tracked in dependency tracker, which doesn't handle modules
  // that are not located on disk.
  auto moduleBuf = loadCachedCompileResultFromCacheKey(
      Impl.CAS, Impl.Cache, Ctx.Diags, moduleCASID,
      file_types::ID::TY_SwiftModuleFile, moduleInfo.modulePath);
  if (!moduleBuf) {
    // We cannot read the module content, diagnose.
    Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
                       moduleInfo.modulePath);
    return false;
  }

  const bool isForwardingModule =
      !serialization::isSerializedAST(moduleBuf->getBuffer());
  // If the module is a forwarding module, read the actual content from the path
  // encoded in the forwarding module as the actual module content.
  if (isForwardingModule) {
    auto forwardingModule = ForwardingModule::load(*moduleBuf.get());
    if (forwardingModule) {
      // Look through ExplicitModuleMap for paths.
      // TODO: need to have dependency scanner reports forwarded module as
      // dependency for this compilation and ingested into CAS.
      auto moduleOrErr = Impl.loadModuleFromPath(
          forwardingModule->underlyingModulePath, Ctx.Diags);
      if (!moduleOrErr) {
        llvm::consumeError(moduleOrErr.takeError());
        Ctx.Diags.diagnose(SourceLoc(),
                           diag::error_opening_explicit_module_file,
                           moduleInfo.modulePath);
        return false;
      }
      moduleBuf = std::move(*moduleOrErr);
      if (!moduleBuf) {
        // We cannot read the module content, diagnose.
        Ctx.Diags.diagnose(SourceLoc(),
                           diag::error_opening_explicit_module_file,
                           moduleInfo.modulePath);
        return false;
      }
    } else {
      // We cannot read the module content, diagnose.
      Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
                         moduleInfo.modulePath);
      return false;
    }
  }
  assert(moduleBuf);
  // Move the opened module buffer to the caller.
  *ModuleBuffer = std::move(moduleBuf);

  // TODO: support .swiftdoc file and .swiftsourceinfo file
  return true;
}

std::error_code ExplicitCASModuleLoader::findModuleFilesInDirectory(
    ImportPath::Element ModuleID, const SerializedModuleBaseName &BaseName,
    SmallVectorImpl<char> *ModuleInterfacePath,
    SmallVectorImpl<char> *ModuleInterfaceSourcePath,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
    std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
    bool skipBuildingInterface, bool IsFramework,
    bool IsTestableDependencyLookup) {
  llvm_unreachable("Not supported in the Explicit Swift Module Loader.");
  return std::make_error_code(std::errc::not_supported);
}

bool ExplicitCASModuleLoader::canImportModule(
    ImportPath::Module path, SourceLoc loc, ModuleVersionInfo *versionInfo,
    bool isTestableDependencyLookup) {
  // FIXME: Swift submodules?
  if (path.hasSubmodule())
    return false;
  ImportPath::Element mID = path.front();
  // Look up the module with the real name (physical name on disk);
  // in case `-module-alias` is used, the name appearing in source files
  // and the real module name are different. For example, '-module-alias
  // Foo=Bar' maps Foo appearing in source files, e.g. 'import Foo', to the real
  // module name Bar (on-disk name), which should be searched for loading.
  StringRef moduleName = Ctx.getRealModuleName(mID.Item).str();
  auto it = Impl.ExplicitModuleMap.find(moduleName);
  // If no provided explicit module matches the name, then it cannot be
  // imported.
  if (it == Impl.ExplicitModuleMap.end()) {
    return false;
  }

  // If the caller doesn't want version info we're done.
  if (!versionInfo)
    return true;

  // Open .swiftmodule file and read out the version
  std::string moduleCASID = it->second.moduleCacheKey
                                ? *it->second.moduleCacheKey
                                : it->second.modulePath;
  auto moduleBuf = Impl.loadFileBuffer(moduleCASID, it->second.modulePath);
  if (!moduleBuf) {
    Ctx.Diags.diagnose(loc, diag::error_cas, toString(moduleBuf.takeError()));
    return false;
  }
  if (!*moduleBuf) {
    Ctx.Diags.diagnose(loc, diag::error_opening_explicit_module_file,
                       it->second.modulePath);
    return false;
  }
  auto metaData = serialization::validateSerializedAST(
      (*moduleBuf)->getBuffer(), Ctx.SILOpts.EnableOSSAModules,
      Ctx.LangOpts.SDKName);
  versionInfo->setVersion(metaData.userModuleVersion,
                          ModuleVersionSourceKind::SwiftBinaryModule);
  return true;
}

void ExplicitCASModuleLoader::collectVisibleTopLevelModuleNames(
    SmallVectorImpl<Identifier> &names) const {
  for (auto &entry : Impl.ExplicitModuleMap) {
    names.push_back(Ctx.getIdentifier(entry.getKey()));
  }
}

std::unique_ptr<ExplicitCASModuleLoader> ExplicitCASModuleLoader::create(
    ASTContext &ctx, llvm::cas::ObjectStore &CAS, llvm::cas::ActionCache &cache,
    DependencyTracker *tracker, ModuleLoadingMode loadMode,
    StringRef ExplicitSwiftModuleMap,
    const std::vector<std::pair<std::string, std::string>>
        &ExplicitSwiftModuleInputs,
    bool IgnoreSwiftSourceInfoFile) {
  auto result =
      std::unique_ptr<ExplicitCASModuleLoader>(new ExplicitCASModuleLoader(
          ctx, CAS, cache, tracker, loadMode, IgnoreSwiftSourceInfoFile));
  auto &Impl = result->Impl;
  // If the explicit module map is given, try parse it.
  if (!ExplicitSwiftModuleMap.empty()) {
    // Parse a JSON file to collect explicitly built modules.
    Impl.parseSwiftExplicitModuleMap(ExplicitSwiftModuleMap);
  }
  // If some modules are provided with explicit
  // '-swift-module-file' options, add those as well.
  if (!ExplicitSwiftModuleInputs.empty()) {
    Impl.addCommandLineExplicitInputs(ExplicitSwiftModuleInputs);
  }

  return result;
}