Source file env.ml
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
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
let msupport_raise_error =
ref (fun ?ignore_unify:_ _ ->
failwith "Env.raise_error should be filled in with Msupport.raise_error")
module StdlibMap = Map
open Cmi_format
open Misc
open Asttypes
open Longident
open Path
open Types
open Local_store
module Jkind = Btype.Jkind0
module String = Misc.Stdlib.String
let add_delayed_check_forward = ref (fun _ -> assert false)
type 'a usage_tbl = (Uid.t, ('a -> unit)) Stamped_hashtable.t
(** This table is used to track usage of value declarations.
A declaration is identified by its uid.
The callback attached to a declaration is called whenever the value (or
type, or ...) is used explicitly (lookup_value, ...) or implicitly
(inclusion test between signatures, cf Includemod.value_descriptions, ...).
*)
let local_stamped n : Stamped_hashtable.changelog * ('a usage_tbl) =
let changelog = Stamped_hashtable.create_changelog () in
changelog, Stamped_hashtable.create changelog n
let stamped_value_declarations = s_table local_stamped 32
let value_declarations_changelog, value_declarations = !stamped_value_declarations
let stamped_type_declarations = s_table local_stamped 32
let type_declarations_changelog, type_declarations = !stamped_type_declarations
let stamped_module_declarations = s_table local_stamped 32
let module_declarations_changelog, module_declarations = !stamped_module_declarations
let stamped_jkind_declarations = s_table local_stamped 32
let jkind_declarations_changelog, jkind_declarations = !stamped_jkind_declarations
let stamped_mutated_mutable_values = s_table local_stamped 32
let mutated_mutable_values_changelog, mutated_mutable_values = !stamped_mutated_mutable_values
type constructor_usage = Positive | Pattern | Exported_private | Exported
type constructor_usages =
{
mutable cu_positive: bool;
mutable cu_pattern: bool;
mutable cu_exported_private: bool;
}
let add_constructor_usage cu usage =
match usage with
| Positive -> cu.cu_positive <- true
| Pattern -> cu.cu_pattern <- true
| Exported_private -> cu.cu_exported_private <- true
| Exported ->
cu.cu_positive <- true;
cu.cu_pattern <- true;
cu.cu_exported_private <- true
let constructor_usages () =
{cu_positive = false; cu_pattern = false; cu_exported_private = false}
let constructor_usage_complaint ~rebind priv cu
: Warnings.constructor_usage_warning option =
match priv, rebind with
| Asttypes.Private, _ | _, true ->
if cu.cu_positive || cu.cu_pattern || cu.cu_exported_private then None
else Some Unused
| Asttypes.Public, false -> begin
match cu.cu_positive, cu.cu_pattern, cu.cu_exported_private with
| true, _, _ -> None
| false, false, false -> Some Unused
| false, true, _ -> Some Not_constructed
| false, false, true -> Some Only_exported_private
end
let stamped_used_constructors = s_table local_stamped 32
let used_constructors_changelog, used_constructors = !stamped_used_constructors
type label_usage =
Projection | Mutation | Construct | Exported_private | Exported
type label_usages =
{
mutable lu_projection: bool;
mutable lu_mutation: bool;
mutable lu_construct: bool;
}
let add_label_usage lu usage =
match usage with
| Projection -> lu.lu_projection <- true;
| Mutation -> lu.lu_mutation <- true
| Construct -> lu.lu_construct <- true
| Exported_private ->
lu.lu_projection <- true
| Exported ->
lu.lu_projection <- true;
lu.lu_mutation <- true;
lu.lu_construct <- true
let is_mutating_label_usage = function
| Mutation -> true
| (Projection | Construct | Exported_private | Exported) -> false
let label_usages () =
{lu_projection = false; lu_mutation = false; lu_construct = false}
let label_usage_complaint priv mut lu
: Warnings.field_usage_warning option =
match priv, mut with
| Asttypes.Private, _ ->
if lu.lu_projection then None
else Some Unused
| Asttypes.Public, Types.Immutable -> begin
match lu.lu_projection, lu.lu_construct with
| true, _ -> None
| false, false -> Some Unused
| false, true -> Some Not_read
end
| Asttypes.Public, Types.Mutable _ -> begin
match lu.lu_projection, lu.lu_mutation, lu.lu_construct with
| true, true, _ -> None
| false, false, false -> Some Unused
| false, _, _ -> Some Not_read
| true, false, _ -> Some Not_mutated
end
let stamped_used_labels = s_table local_stamped 32
let used_labels_changelog, used_labels = !stamped_used_labels
(** Map indexed by the name of module components. *)
module NameMap = String.Map
(** Runtime metaprogramming stage. Computed as the difference between the
number of surrounding quotes minus the number of surrounding splices. *)
type stage = int
(** Occurence of a path at a specific stage.
Used for local constraints, as they are only valid at a fixed stage. *)
module StagedPath = struct
type t = { stage : stage; path : Path.t }
module T = struct
type nonrec t = t
let compare { stage; path } { stage = stage'; path = path' } =
match Int.compare stage stage' with
| 0 -> Path.compare path path'
| x -> x
end
module Map = StdlibMap.Make(T)
end
type value_unbound_reason =
| Val_unbound_instance_variable
| Val_unbound_self
| Val_unbound_ancestor
| Val_unbound_ghost_recursive of Location.t
type module_unbound_reason =
| Mod_unbound_illegal_recursion of
{ container : string option; unbound : string }
type stage_lock =
| Quotation_lock
| Splice_lock
type lock =
| Const_closure_lock of bool * Mode.Hint.pinpoint *
Mode.Value.Comonadic.Const.t
| Closure_lock of Mode.Hint.pinpoint * Mode.Value.Comonadic.r
| Region_lock
| Exclave_lock
| Unboxed_lock
type lock_or_stage =
| Nonstage_lock of lock
| Stage_lock of stage_lock
let partition_locks locks =
List.partition_map
(function | Stage_lock lock -> Left lock | Nonstage_lock lock -> Right lock)
locks
type locks = lock list
type summary =
Env_empty
| Env_value of summary * Ident.t * value_description * Mode.Value.l
| Env_type of summary * Ident.t * type_declaration
| Env_extension of summary * Ident.t * extension_constructor
| Env_module of summary * Ident.t * module_presence * module_declaration *
Mode.Value.l * locks
| Env_modtype of summary * Ident.t * modtype_declaration
| Env_class of summary * Ident.t * class_declaration
| Env_cltype of summary * Ident.t * class_type_declaration
| Env_open of summary * Path.t
| Env_functor_arg of summary * Ident.t
| Env_constraints of summary * type_declaration StagedPath.Map.t
| Env_copy_types of summary
| Env_persistent of summary * Ident.t
| Env_value_unbound of summary * string * value_unbound_reason
| Env_module_unbound of summary * string * module_unbound_reason
| Env_jkind of summary * Ident.t * jkind_declaration
let map_summary f = function
Env_empty -> Env_empty
| Env_value (s, id, d, m) -> Env_value (f s, id, d, m)
| Env_type (s, id, d) -> Env_type (f s, id, d)
| Env_extension (s, id, d) -> Env_extension (f s, id, d)
| Env_module (s, id, p, d, m, l) -> Env_module (f s, id, p, d, m, l)
| Env_modtype (s, id, d) -> Env_modtype (f s, id, d)
| Env_class (s, id, d) -> Env_class (f s, id, d)
| Env_cltype (s, id, d) -> Env_cltype (f s, id, d)
| Env_open (s, p) -> Env_open (f s, p)
| Env_functor_arg (s, id) -> Env_functor_arg (f s, id)
| Env_constraints (s, m) -> Env_constraints (f s, m)
| Env_copy_types s -> Env_copy_types (f s)
| Env_persistent (s, id) -> Env_persistent (f s, id)
| Env_value_unbound (s, u, r) -> Env_value_unbound (f s, u, r)
| Env_module_unbound (s, u, r) -> Env_module_unbound (f s, u, r)
| Env_jkind (s, id, d) -> Env_jkind (f s, id, d)
type address = Persistent_env.address =
| Aunit of Compilation_unit.t
| Alocal of Ident.t
| Adot of address * module_representation * int
module TycompTbl =
struct
(** This module is used to store components of types (i.e. labels
and constructors). We keep a representation of each nested
"open" and the set of local bindings between each of them. *)
type ('lock, 'a) t = {
current: 'a Ident.tbl;
(** Local bindings since the last open. *)
layer: ('lock, 'a) layer;
(** Symbolic representation of the last (innermost) layer *)
}
and ('lock, 'a) layer =
| Open of {
components: ('a list) NameMap.t;
(** Components from the opened module. We keep a list of
bindings for each name, as in comp_labels and
comp_constrs. *)
root: Path.t;
(** Only used to check removal of open *)
using: (string -> ('a * 'a) option -> unit) option;
(** A callback to be applied when a component is used from this
"open". This is used to detect unused "opens". The
arguments are used to detect shadowing. *)
next: ('lock, 'a) t;
(** The table before opening the module. *)
locks: 'lock list;
(** List of locks from the definition of [root] to this [Open], in that
order *)
}
| Lock of {
lock : 'lock;
next : ('lock, 'a) t;
}
| Nothing
let empty = { current = Ident.empty; layer = Nothing }
let add id x tbl =
{tbl with current = Ident.add id x tbl.current}
let add_open slot wrap root components locks next =
let using =
match slot with
| None -> None
| Some f -> Some (fun s x -> f s (wrap x))
in
{
current = Ident.empty;
layer = Open {using; components; root; locks; next};
}
let remove_last_open rt tbl =
match tbl.layer with
| Open {root; next; _} when Path.same rt root ->
{ next with current =
Ident.fold_all Ident.add tbl.current next.current }
| _ ->
assert false
let rec find_same id tbl =
try Ident.find_same id tbl.current
with Not_found as exn ->
begin match tbl.layer with
| Open {next; _} -> find_same id next
| Lock {next; _} -> find_same id next
| Nothing -> raise exn
end
let nothing = fun () -> ()
let mk_callback rest name desc using =
match using with
| None -> nothing
| Some f ->
(fun () ->
match rest with
| [] -> f name None
| (_, hidden, _) :: _ -> f name (Some (desc, hidden)))
let add_lock lock next =
{ current = Ident.empty; layer = Lock {lock; next} }
let rec find_all_and_locks ~mark name tbl acc =
List.map (fun (id, desc) -> (Pident id, desc, (acc, nothing)))
(Ident.find_all name tbl.current) @
match tbl.layer with
| Nothing -> []
| Lock {lock;next} ->
find_all_and_locks ~mark name next (lock :: acc)
| Open {using; next; components; locks; root} ->
let rest = find_all_and_locks ~mark name next acc in
let using = if mark then using else None in
match NameMap.find name components with
| exception Not_found -> rest
| opened ->
List.map
(fun desc -> Pdot (root, name), desc,
(locks @ acc, mk_callback rest name desc using))
opened
@ rest
let find_all_and_locks ~mark name tbl =
find_all_and_locks ~mark name tbl []
let rec fold_name f tbl acc =
let acc = Ident.fold_name (fun _id d -> f d) tbl.current acc in
match tbl.layer with
| Open {using = _; next; components; root = _} ->
acc
|> NameMap.fold
(fun _name -> List.fold_right f)
components
|> fold_name f next
| Lock {next; _} -> fold_name f next acc
| Nothing ->
acc
let rec local_keys tbl acc =
let acc = Ident.fold_all (fun k _ accu -> k::accu) tbl.current acc in
match tbl.layer with
| Open o -> local_keys o.next acc
| Lock {next; _} -> local_keys next acc
| Nothing -> acc
let diff_keys is_local tbl1 tbl2 =
let keys2 = local_keys tbl2 [] in
List.filter
(fun id ->
is_local (find_same id tbl2) &&
try ignore (find_same id tbl1); false
with Not_found -> true)
keys2
end
type empty = |
type mode_with_locks = Mode.Value.l * locks
let locks_empty = []
let locks_is_empty l = l = locks_empty
module IdTbl =
struct
(** This module is used to store all kinds of components except
(labels and constructors) in environments. We keep a
representation of each nested "open" and the set of local
bindings between each of them. *)
type ('lock, 'a, 'b) t = {
current: 'a Ident.tbl;
(** Local bindings since the last open or lock *)
layer: ('lock, 'a, 'b) layer;
(** Symbolic representation of the last (innermost) open, if any. *)
}
and ('lock, 'a, 'b) layer =
| Open of {
root: Path.t;
(** The path of the opened module, to be prefixed in front of
its local names to produce a valid path in the current
environment. *)
components: 'b NameMap.t;
(** Components from the opened module. *)
using: (string -> ('a * 'a) option -> unit) option;
(** A callback to be applied when a component is used from this
"open". This is used to detect unused "opens". The
arguments are used to detect shadowing. *)
next: ('lock, 'a, 'b) t;
(** The table before opening the module. *)
locks: 'lock list;
(** The locks from the definition of [root] to this [open], in
that order. *)
}
| Map of {
f: ('a -> 'a);
next: ('lock, 'a, 'b) t;
}
| Lock of {
lock: 'lock;
next: ('lock, 'a, 'b) t;
}
| Nothing
let empty = { current = Ident.empty; layer = Nothing }
let add id x tbl =
{tbl with current = Ident.add id x tbl.current}
let remove id tbl =
{tbl with current = Ident.remove id tbl.current}
let add_open slot wrap root components locks next =
let using =
match slot with
| None -> None
| Some f -> Some (fun s x -> f s (wrap x))
in
{
current = Ident.empty;
layer = Open {using; root; components; next; locks};
}
let remove_last_open rt tbl =
match tbl.layer with
| Open {root; next; _} when Path.same rt root ->
{ next with current =
Ident.fold_all Ident.add tbl.current next.current }
| _ ->
assert false
let add_lock lock next =
{ current = Ident.empty; layer = Lock {lock; next} }
let map f next =
{
current = Ident.empty;
layer = Map {f; next}
}
let rec find_same_without_locks id tbl =
try Ident.find_same id tbl.current
with Not_found as exn ->
begin match tbl.layer with
| Open {next; _} -> find_same_without_locks id next
| Map {f; next} -> f (find_same_without_locks id next)
| Lock {lock=_; next} -> find_same_without_locks id next
| Nothing -> raise exn
end
let find_same id (tbl : (empty, _, _) t) =
find_same_without_locks id tbl
let rec find_same_and_locks id tbl macc =
try
let desc = Ident.find_same id tbl.current in
desc, macc
with Not_found as exn ->
begin match tbl.layer with
| Open {next; _} -> find_same_and_locks id next macc
| Map {f; next} ->
let desc, locks = find_same_and_locks id next macc in
f desc, locks
| Lock {lock; next} -> find_same_and_locks id next (lock :: macc)
| Nothing -> raise exn
end
let find_same_and_locks id tbl = find_same_and_locks id tbl []
let rec find_name_and_locks wrap ~mark name tbl macc : _ Result.t =
try
let (id, desc) = Ident.find_name name tbl.current in
Ok (Pident id, macc, desc)
with Not_found ->
begin match tbl.layer with
| Open {using; root; next; components; locks} ->
begin try
let descr = wrap (NameMap.find name components) in
let res = Pdot (root, name), (locks @ macc), descr in
if mark then begin match using with
| None -> ()
| Some f -> begin match
(find_name_and_locks wrap ~mark:false name next macc : _ Result.t)
with
| Error _ -> f name None
| Ok (_, _, descr') -> f name (Some (descr', descr))
end
end;
Ok res
with Not_found ->
find_name_and_locks wrap ~mark name next macc
end
| Map {f; next} ->
find_name_and_locks wrap ~mark name next macc
|> Result.map (fun (p, macc, desc) -> p, macc, f desc)
| Lock {lock; next} ->
find_name_and_locks wrap ~mark name next (lock :: macc)
| Nothing ->
Error macc
end
let find_name_and_locks wrap ~mark name tbl =
find_name_and_locks wrap ~mark name tbl []
(** Find all the locks in the context. Equivalent to [find_name_and_locks]
on a missing name. *)
let rec get_all_locks tbl macc =
match tbl.layer with
| Open {next; _}
| Map {next; _} -> get_all_locks next macc
| Lock {lock; next} -> get_all_locks next (lock :: macc)
| Nothing -> macc
let get_all_locks tbl =
get_all_locks tbl []
(** Find item by name whose accesses are not affected by locks, and thus
shouldn't encounter any locks. *)
let find_name wrap ~mark name tbl =
match (find_name_and_locks wrap ~mark name tbl
: (_ * empty list * _, empty list) Result.t) with
| Ok (id, [], desc) -> id, desc
| Ok (_, _ :: _, _) -> .
| Error [] -> raise Not_found
| Error (_ :: _) -> .
let rec find_all wrap name tbl =
List.map
(fun (id, desc) -> Pident id, desc)
(Ident.find_all name tbl.current) @
match tbl.layer with
| Nothing -> []
| Open {root; using = _; next; components} ->
begin try
let desc = wrap (NameMap.find name components) in
(Pdot (root, name), desc) :: find_all wrap name next
with Not_found ->
find_all wrap name next
end
| Map {f; next} ->
List.map (fun (p, desc) -> (p, f desc))
(find_all wrap name next)
| Lock {lock=_;next} ->
find_all wrap name next
let rec find_all_idents name tbl () =
let current =
Ident.find_all_seq name tbl.current
|> Seq.map (fun (id, _) -> Some id)
in
let next () =
match tbl.layer with
| Nothing -> Seq.Nil
| Open { next; components; _ } ->
if NameMap.mem name components then
Seq.Cons(None, find_all_idents name next)
else
find_all_idents name next ()
| Map {next; _ } -> find_all_idents name next ()
| Lock {lock=_;next} ->
find_all_idents name next ()
in
Seq.append current next ()
let rec fold_name wrap f tbl acc =
let acc =
Ident.fold_name
(fun id d -> f (Ident.name id) (Pident id, d))
tbl.current acc
in
match tbl.layer with
| Open {root; using = _; next; components} ->
acc
|> NameMap.fold
(fun name desc -> f name (Pdot (root, name), wrap desc))
components
|> fold_name wrap f next
| Nothing ->
acc
| Map {f=g; next} ->
acc
|> fold_name wrap
(fun name (path, desc) -> f name (path, g desc))
next
| Lock {lock=_; next} ->
fold_name wrap f next acc
let rec local_keys tbl acc =
let acc = Ident.fold_all (fun k _ accu -> k::accu) tbl.current acc in
match tbl.layer with
| Open {next; _ } | Map {next; _} | Lock {next; _} -> local_keys next acc
| Nothing -> acc
let rec iter wrap f tbl =
Ident.iter (fun id desc -> f id (Pident id, desc)) tbl.current;
match tbl.layer with
| Open {root; using = _; next; components} ->
NameMap.iter
(fun s x ->
let root_scope = Path.scope root in
f (Ident.create_scoped ~scope:root_scope s)
(Pdot (root, s), wrap x))
components;
iter wrap f next
| Map {f=g; next} ->
iter wrap (fun id (path, desc) -> f id (path, g desc)) next
| Lock {lock=_; next} ->
iter wrap f next
| Nothing -> ()
let diff_keys tbl1 tbl2 =
let keys2 = local_keys tbl2 [] in
List.filter
(fun id ->
try ignore (find_same_without_locks id tbl1); false
with Not_found -> true)
keys2
end
type type_descr_kind =
(label_description, unboxed_label_description, constructor_description)
type_kind
type type_descriptions = type_descr_kind
let stamped_changelog =
s_table Stamped_hashtable.create_changelog ()
let stamped_path_add table path value =
let rec path_stamp = function
| Pident id -> Ident.stamp id
| Pdot (t, _) -> path_stamp t
| Papply (t1, t2) -> Int.max (path_stamp t1) (path_stamp t2)
| Pextra_ty (t, _) -> path_stamp t
in
let stamp = path_stamp path in
let stamp = if stamp = 0 then None else Some stamp in
Stamped_hashtable.add table ?stamp path value
let stamped_uid_add table uid value =
let stamp = Types.Uid.stamp_of_uid uid in
Stamped_hashtable.add table ?stamp uid value
let stamped_mem table value =
Stamped_hashtable.mem table value
let stamped_find table value =
Stamped_hashtable.find table value
let stamped_find_opt table value =
if Stamped_hashtable.mem table value
then Some (Stamped_hashtable.find table value)
else None
let stamped_create n =
Stamped_hashtable.create !stamped_changelog n
let in_signature_flag = 0x01
type t = {
values: (lock_or_stage, value_entry, value_data) IdTbl.t;
constrs: (lock_or_stage, constructor_data) TycompTbl.t;
labels: (stage_lock, label_data) TycompTbl.t;
unboxed_labels: (stage_lock, unboxed_label_description) TycompTbl.t;
types: (stage_lock, type_data, type_data) IdTbl.t;
modules: (lock_or_stage, module_entry, module_data) IdTbl.t;
modtypes: (stage_lock, modtype_data, modtype_data) IdTbl.t;
classes: (lock_or_stage, class_data, class_data) IdTbl.t;
cltypes: (empty, cltype_data, cltype_data) IdTbl.t;
functor_args: unit Ident.tbl;
jkinds : (empty, jkind_data, jkind_data) IdTbl.t;
summary: summary;
local_constraints: type_declaration StagedPath.Map.t;
implicit_jkinds: jkind_lr loc String.Map.t;
flags: int;
short_paths: Short_paths.t option;
short_paths_additions: short_paths_addition list;
stage: stage;
toplevel_scope: int
}
and module_components =
{
alerts: alerts;
uid: Uid.t;
comps:
(components_maker,
(module_components_repr, module_components_failure) result)
Lazy_backtrack.t;
}
and components_maker = {
cm_env: t;
cm_prefixing_subst: Subst.t;
cm_path: Path.t;
cm_addr: address_lazy;
cm_mty: Subst.Lazy.module_type;
cm_mode : Mode.Value.l;
cm_shape: Shape.t;
}
and module_components_repr =
Structure_comps of structure_components
| Functor_comps of functor_components
and module_components_failure =
| No_components_abstract of Path.t
| No_components_alias of Path.t
and structure_components = {
mutable comp_values: value_data NameMap.t;
mutable comp_constrs: constructor_data list NameMap.t;
mutable comp_labels: label_data list NameMap.t;
mutable comp_unboxed_labels: unboxed_label_description list NameMap.t;
mutable comp_types: type_data NameMap.t;
mutable comp_modules: module_data NameMap.t;
mutable comp_modtypes: modtype_data NameMap.t;
mutable comp_classes: class_data NameMap.t;
mutable comp_cltypes: cltype_data NameMap.t;
mutable comp_jkinds: jkind_data NameMap.t
}
and functor_components = {
fcomp_arg: functor_parameter;
fcomp_res: module_type;
fcomp_shape: Shape.t;
fcomp_cache: (Path.t, module_components) Stamped_hashtable.t;
fcomp_subst_cache: (Path.t, module_type) Stamped_hashtable.t;
}
and address_unforced =
| Projection of
{ parent : address_lazy;
module_repr: module_representation;
pos : int }
| ModAlias of { env : t; path : Path.t; }
and address_lazy = (address_unforced, address) Lazy_backtrack.t
and value_data =
{ vda_description : Subst.Lazy.value_description;
vda_address : address_lazy;
vda_mode : Mode.Value.l;
vda_shape : Shape.t }
and value_entry =
| Val_bound of value_data
| Val_unbound of value_unbound_reason
and constructor_data =
{ cda_description : constructor_description;
cda_address : address_lazy option;
cda_shape: Shape.t; }
and label_data = label_description
and type_data =
{ tda_declaration : type_declaration;
tda_descriptions : type_descriptions;
tda_shape : Shape.t;
tda_unboxed_version_descriptions : type_descriptions option }
and module_data =
{ mda_declaration : Subst.Lazy.module_declaration;
mda_components : module_components;
mda_address : address_lazy;
mda_mode : Mode.Value.l;
mda_shape: Shape.t; }
and module_alias_locks = locks
(** If the module is an alias for another module, this is the list of locks
from the original module to this module. This is accumulative: write
[module B = A;; module C = B;;], then [C] will record all locks from [A]
to [C]. Empty if not an alias. *)
and module_entry =
| Mod_local of module_data * module_alias_locks
| Mod_persistent
| Mod_unbound of module_unbound_reason
and modtype_data =
{ mtda_declaration : Subst.Lazy.modtype_declaration;
mtda_shape : Shape.t; }
and class_data =
{ clda_declaration : class_declaration;
clda_address : address_lazy;
clda_shape : Shape.t }
and cltype_data =
{ cltda_declaration : class_type_declaration;
cltda_shape : Shape.t }
and short_paths_addition =
| Type of Ident.t * type_declaration
| Class_type of Ident.t * class_type_declaration
| Module_type of Ident.t * Subst.Lazy.modtype_declaration
| Module of Ident.t * Subst.Lazy.module_declaration * module_components
| Type_open of Path.t * type_data NameMap.t
| Class_type_open of Path.t * class_type_declaration NameMap.t
| Module_type_open of Path.t * Subst.Lazy.modtype_declaration NameMap.t
| Module_open of Path.t * module_data NameMap.t
and jkind_data =
{ jkda_declaration : jkind_declaration;
jkda_shape : Shape.t }
let clda_mode = Types.class_mode |> Mode.Value.disallow_right
(** In this file, a functor's return is only used to access the types inside
(such as F(M).t). Therefore, the return having the weakest mode is sufficient.
*)
let fcomp_res_mode = Mode.Value.(max |> disallow_right)
(** Accessing `F(M).t` is not closing over anything *)
let fcomp_res_mode_with_locks = (fcomp_res_mode, locks_empty)
let empty_structure =
Structure_comps {
comp_values = NameMap.empty;
comp_constrs = NameMap.empty;
comp_labels = NameMap.empty;
comp_unboxed_labels = NameMap.empty;
comp_types = NameMap.empty;
comp_modules = NameMap.empty; comp_modtypes = NameMap.empty;
comp_classes = NameMap.empty;
comp_cltypes = NameMap.empty;
comp_jkinds = NameMap.empty }
type unbound_value_hint =
| No_hint
| Missing_rec of Location.t
type structure_components_reason =
| Project
| Open
type no_open_quotations_context =
| Object_qt
| Struct_qt
| Sig_qt
| Open_qt
| Object_field_with_attribute_qt
| Variant_tag_with_attribute_qt
| Jkind_annotation_qt
| Layout_polymorphism_qt
| Tconst_pat_qt of Longident.t
| Class_type_qt
let print_structure_components_reason ppf = function
| Project -> Format_doc.fprintf ppf "have any components"
| Open -> Format_doc.fprintf ppf "be opend"
type none_in_quotations_context =
| Constructor
| Label
type lookup_error =
| Unbound_value of Longident.t * unbound_value_hint
| Unbound_type of Longident.t
| Unbound_constructor of Longident.t
| Unbound_label of Longident.t * record_form_packed * label_usage
| Unbound_module of Longident.t
| Unbound_class of Longident.t
| Unbound_modtype of Longident.t
| Unbound_cltype of Longident.t
| Unbound_jkind of Longident.t
| Unbound_settable_variable of string
| Not_a_settable_variable of string
| Masked_instance_variable of Longident.t
| Masked_self_variable of Longident.t
| Masked_ancestor_variable of Longident.t
| Structure_used_as_functor of Longident.t
| Abstract_used_as_functor of Longident.t * Path.t
| Functor_used_as_structure of Longident.t * structure_components_reason
| Abstract_used_as_structure of Longident.t * Path.t * structure_components_reason
| Generative_used_as_applicative of Longident.t
| Illegal_reference_to_recursive_module of
{ container: string option; unbound : string }
| Illegal_reference_to_recursive_class_type of
{ container : string option;
unbound : string;
unbound_class_type : Longident.t;
container_class_type : string;
}
| Cannot_scrape_alias of Longident.t * Path.t
| Local_value_used_in_exclave of Mode.Hint.lock_item * Longident.t
| Non_value_used_in_object of Longident.t * type_expr * Jkind.Violation.t
| No_unboxed_version of Longident.t * type_declaration
| Error_from_persistent_env of Persistent_env.error
| Mutable_value_used_in_closure of Mode.Hint.pinpoint
| Incompatible_stage of Longident.t * Location.t * stage * Location.t * stage
| Unbound_in_stage of
none_in_quotations_context * Longident.t * Location.t * stage * stage
type error =
| Missing_module of Location.t * Path.t * Path.t
| Illegal_value_name of Location.t * string
| Lookup_error of Location.t * t * lookup_error
| Incomplete_instantiation of { unset_param : Global_module.Parameter_name.t }
| Toplevel_splice of Location.t
| Unsupported_inside_quotation of Location.t * no_open_quotations_context
exception Error of error
let error err = raise (Error err)
let lookup_error loc env err =
error (Lookup_error(loc, env, err))
let env_labels (type rep) (record_form : rep record_form) env
: (stage_lock, rep gen_label_description) TycompTbl.t =
match record_form with
| Legacy -> env.labels
| Unboxed_product -> env.unboxed_labels
let add_label (type rep) (record_form : rep record_form) env lbl_id
(lbl : rep gen_label_description) =
match record_form with
| Legacy ->
{ env with labels = TycompTbl.add lbl_id lbl env.labels }
| Unboxed_product ->
{ env with unboxed_labels = TycompTbl.add lbl_id lbl env.unboxed_labels }
let comp_labels (type rep) (record_form : rep record_form) sc
: rep gen_label_description list NameMap.t =
match record_form with
| Legacy -> sc.comp_labels
| Unboxed_product -> sc.comp_unboxed_labels
let same_type_declarations e1 e2 =
e1.types == e2.types &&
e1.modules == e2.modules &&
e1.local_constraints == e2.local_constraints
let same_constr = ref (fun _ _ _ -> assert false)
let constrain_type_jkind = ref (fun _ _ _ -> assert false)
let check_well_formed_module = ref (fun _ -> assert false)
let check_shadowing env = function
| `Constructor (Some (cda1, cda2))
when not (!same_constr env
cda1.cda_description.cstr_res
cda2.cda_description.cstr_res) ->
Some "constructor"
| `Label (Some (l1, l2))
when not (!same_constr env l1.lbl_res l2.lbl_res) ->
Some "label"
| `Unboxed_label (Some (l1, l2))
when not (!same_constr env l1.lbl_res l2.lbl_res) ->
Some "unboxed label"
| `Value (Some (Val_unbound _, _)) -> None
| `Value (Some (_, _)) -> Some "value"
| `Type (Some _) -> Some "type"
| `Module (Some (Mod_unbound _, _)) -> None
| `Module (Some _) | `Component (Some _) ->
Some "module"
| `Module_type (Some _) -> Some "module type"
| `Class (Some _) -> Some "class"
| `Class_type (Some _) -> Some "class type"
| `Constructor _ | `Label _ | `Unboxed_label _
| `Value None | `Type None | `Module None | `Module_type None
| `Class None | `Class_type None | `Component None ->
None
let empty = {
values = IdTbl.empty; constrs = TycompTbl.empty;
labels = TycompTbl.empty; unboxed_labels = TycompTbl.empty;
types = IdTbl.empty;
modules = IdTbl.empty; modtypes = IdTbl.empty;
classes = IdTbl.empty; cltypes = IdTbl.empty;
summary = Env_empty; local_constraints = StagedPath.Map.empty;
implicit_jkinds = String.Map.empty;
flags = 0;
functor_args = Ident.empty;
jkinds = IdTbl.empty;
short_paths = None;
short_paths_additions = [];
stage = 0;
toplevel_scope = Ident.lowest_scope
}
let path_at_current_stage env path = { StagedPath.stage = env.stage; path }
let in_signature b env =
let flags =
if b then env.flags lor in_signature_flag
else env.flags land (lnot in_signature_flag)
in
{env with flags}
let is_in_signature env = env.flags land in_signature_flag <> 0
let has_local_constraints env =
not (StagedPath.Map.is_empty env.local_constraints)
let is_ext cda =
match cda.cda_description with
| {cstr_tag = Extension _} -> true
| _ -> false
let is_local_ext cda =
match cda.cda_description with
| {cstr_tag = Extension p} -> begin
match p with
| Pident _ -> true
| Pdot _ | Papply _ | Pextra_ty _ -> false
end
| _ -> false
let diff env1 env2 =
IdTbl.diff_keys env1.values env2.values @
TycompTbl.diff_keys is_local_ext env1.constrs env2.constrs @
IdTbl.diff_keys env1.modules env2.modules @
IdTbl.diff_keys env1.classes env2.classes
let wrap_identity x = x
let wrap_value vda = Val_bound vda
let wrap_module mda = Mod_local (mda, locks_empty)
let components_of_module_maker' =
ref ((fun _ -> assert false) :
components_maker ->
(module_components_repr, module_components_failure) result)
let components_of_functor_appl' =
ref ((fun ~loc:_ ~f_path:_ ~f_comp:_ ~arg:_ _env -> assert false) :
loc:Location.t -> f_path:Path.t -> f_comp:functor_components ->
arg:Path.t -> t -> module_components)
let check_functor_application =
ref ((fun ~errors:_ ~loc:_
~lid_whole_app:_ ~f0_path:_ ~args:_
~arg_path:_ ~arg_mty:_ ~param_mty:_
_env
-> assert false) :
errors:bool -> loc:Location.t ->
lid_whole_app:Longident.t ->
f0_path:Path.t -> args:(Path.t * Types.module_type) list ->
arg_path:Path.t -> arg_mty:module_type -> param_mty:module_type ->
t -> unit)
let scrape_alias =
ref ((fun _env _mty -> assert false) :
t -> Subst.Lazy.module_type -> Subst.Lazy.module_type)
let shorten_module_path =
ref ((fun _ _ -> assert false) :
t -> Path.t -> Path.t)
let md md_type =
{md_type; md_modalities = Mode.Modality.undefined; md_attributes=[];
md_loc=Location.none; md_uid = Uid.internal_not_actually_unique}
(** The caller is not interested in modes, and thus [val_modalities] is
invalidated. *)
let vda_description vda =
let vda_description = vda.vda_description in
{vda_description with val_modalities = Mode.Modality.undefined}
module Normalize_mode = struct
type t =
| Normalize_exn (** Normalize a mode; raise if already normalized. *)
| Assert_normalized
(** Assert that the mode is already normalized, and return the mode *)
| Normalize
(** Normalize a mode; do nothing if already normalized.
Use the other two instead whenever possible *)
let modality t modality mode =
let normalized = Mode.Modality.is_undefined modality in
match t, normalized with
| Normalize, true -> modality, mode
| Normalize_exn, true ->
Misc.fatal_error "mode is already normalized but expected otherwise"
| Assert_normalized, true -> modality, mode
| (Normalize | Normalize_exn), false ->
Mode.Modality.undefined,
Mode.Modality.apply ~hint:{monadic = Unknown; comonadic = Unknown}
modality mode
| Assert_normalized, false ->
Misc.fatal_error "mode is not already normalized but expected otherwise"
let vd t (vd : Subst.Lazy.value_description) mode =
let val_modalities, mode = modality t vd.val_modalities mode in
let vd = {vd with val_modalities} in
vd, mode
let vda norm vda = vd norm vda.vda_description vda.vda_mode
let md t (md : Subst.Lazy.module_declaration) mode =
let md_modalities, mode = modality t md.md_modalities mode in
let md = {md with md_modalities} in
md, mode
let mda norm mda = md norm mda.mda_declaration mda.mda_mode
end
let rec print_address ppf = function
| Aunit cu -> Format.fprintf ppf "%s" (Compilation_unit.full_path_as_string cu)
| Alocal id -> Format.fprintf ppf "%s" (Ident.name id)
| Adot(a, _, pos) -> Format.fprintf ppf "%a.[%i]" print_address a pos
type address_head =
| AHunit of Compilation_unit.t
| AHlocal of Ident.t
let rec address_head = function
| Aunit cu -> AHunit cu
| Alocal id -> AHlocal id
| Adot (a, _, _) -> address_head a
module Current_unit_name : sig
val get : unit -> Unit_info.t option
val get_cu : unit -> Compilation_unit.t option
val set : Unit_info.t option -> unit
val is : string -> bool
val is_ident : Ident.t -> bool
val is_path : Path.t -> bool
end = struct
let current_unit : Unit_info.t option ref =
ref None
let get () =
!current_unit
let set unit_info =
current_unit := unit_info
let get_cu () =
Option.map Unit_info.modname (get ())
let get_name () =
Option.map Compilation_unit.name (get_cu ())
let is name =
let current_name_string =
Option.map Compilation_unit.Name.to_string (get_name ())
in
Option.equal String.equal current_name_string (Some name)
let is_ident id =
Ident.is_global id && is (Ident.name id)
let is_path = function
| Pident id -> is_ident id
| Pdot _ | Papply _ | Pextra_ty _ -> false
end
let set_unit_name = Current_unit_name.set
let get_unit_name = Current_unit_name.get
let in_current_unit_and_stage ~name ~stage =
Current_unit_name.is name && stage = 0
let find_same_module id tbl =
match IdTbl.find_same_without_locks id tbl with
| x -> x
| exception Not_found
when Ident.is_global id && not (Current_unit_name.is_ident id) ->
Mod_persistent
let find_name_module ~mark ~stage name tbl =
match IdTbl.find_name_and_locks wrap_module ~mark name tbl with
| Ok x -> x
| Error locks when not (in_current_unit_and_stage ~name ~stage) ->
let path = Pident(Ident.create_persistent name) in
path, locks, Mod_persistent
| _ ->
raise Not_found
let short_paths_module_components_desc' = ref (fun _ -> assert false)
let short_paths_components name pm =
let name_as_string = Global_module.Name.to_string name in
let path = Pident (Ident.create_persistent name_as_string) in
lazy (!short_paths_module_components_desc' empty path pm.mda_components)
let add_persistent_structure id env =
if not (Ident.is_global id) then invalid_arg "Env.add_persistent_structure";
if Current_unit_name.is_ident id then env
else begin
let material =
match
IdTbl.find_name_and_locks wrap_module ~mark:false (Ident.name id) env.modules
with
| Error _ | Ok (_, _, Mod_persistent) -> false
| _ -> true
in
let summary =
if material then Env_persistent (env.summary, id)
else env.summary
in
let modules =
if material || not !Clflags.transparent_modules then
IdTbl.add id Mod_persistent env.modules
else
env.modules
in
{ env with modules; summary }
end
let components_of_module ~alerts ~uid env ps path addr mty mode shape =
{
alerts;
uid;
comps = Lazy_backtrack.create {
cm_env = env;
cm_prefixing_subst = ps;
cm_path = path;
cm_addr = addr;
cm_mty = mty;
cm_mode = mode;
cm_shape = shape;
}
}
let mode_unit ~staticity =
let hint : _ Mode.Hint.const = Legacy Compilation_unit in
Mode.Value.of_const
{ areality = Global;
linearity = Many;
uniqueness = Aliased;
portability = Nonportable;
contention = Uncontended;
forkable = Forkable;
yielding = Unyielding;
statefulness = Stateful;
visibility = Read_write;
staticity;
}
~hint_monadic:hint ~hint_comonadic:hint
let read_sign_of_cmi (sign, staticity) name uid ~shape ~address:addr ~flags =
let id = Ident.create_global name in
let path = Pident id in
let alerts =
List.fold_left (fun acc -> function Alerts s -> s | _ -> acc)
Misc.Stdlib.String.Map.empty
flags
in
let md =
{ Subst.Lazy.md_type = Mty_signature sign;
md_modalities = Mode.Modality.undefined;
md_loc = Location.none;
md_attributes = [];
md_uid = uid;
}
in
let mda_address = Lazy_backtrack.create_forced addr in
let mda_declaration = md in
let mda_mode = Mode.Value.disallow_right (mode_unit ~staticity) in
let mda_shape = shape in
let mda_components =
let mty = md.md_type in
components_of_module ~alerts ~uid:md.md_uid
empty Subst.identity
path mda_address mty mda_mode mda_shape
in
{
mda_declaration;
mda_components;
mda_address;
mda_mode;
mda_shape;
}
let persistent_env : module_data Persistent_env.t ref =
s_table Persistent_env.empty ()
let without_cmis f x =
Persistent_env.without_cmis !persistent_env f x
let imports () = Persistent_env.imports !persistent_env
let import_crcs ~source crcs =
Persistent_env.import_crcs !persistent_env ~source crcs
let runtime_parameter_bindings () =
Persistent_env.runtime_parameter_bindings !persistent_env
let is_bound_to_runtime_parameter id =
Persistent_env.is_bound_to_runtime_parameter !persistent_env id
let parameters () = Persistent_env.parameters !persistent_env
let read_pers_mod modname cmi =
Persistent_env.read !persistent_env modname cmi
let find_pers_mod name ~allow_hidden ~allow_excess_args =
Persistent_env.find ~allow_hidden !persistent_env
read_sign_of_cmi short_paths_components name ~allow_excess_args
let check_pers_mod ~allow_hidden ~loc name =
Persistent_env.check ~allow_hidden !persistent_env
read_sign_of_cmi short_paths_components ~loc name
let crc_of_unit name =
Persistent_env.crc_of_unit !persistent_env name
let is_imported_opaque modname =
Persistent_env.is_imported_opaque !persistent_env modname
let register_import_as_opaque modname =
Persistent_env.register_import_as_opaque !persistent_env modname
let is_parameter_unit modname =
Persistent_env.is_parameter_import !persistent_env modname
let is_imported_parameter modname =
Persistent_env.is_imported_parameter !persistent_env modname
let implemented_parameter modname =
Persistent_env.implemented_parameter !persistent_env modname
let reset_declaration_caches () =
Stamped_hashtable.clear value_declarations;
Stamped_hashtable.clear type_declarations;
Stamped_hashtable.clear module_declarations;
Stamped_hashtable.clear mutated_mutable_values;
Stamped_hashtable.clear jkind_declarations;
Stamped_hashtable.clear used_constructors;
Stamped_hashtable.clear used_labels;
()
let reset_cache ~preserve_persistent_env =
Current_unit_name.set None;
if not preserve_persistent_env then
Persistent_env.clear !persistent_env;
reset_declaration_caches ();
()
let reset_cache_toplevel () =
Persistent_env.clear_missing !persistent_env;
reset_declaration_caches ();
()
let get_components_res c =
match Persistent_env.can_load_cmis !persistent_env with
| Persistent_env.Can_load_cmis ->
Lazy_backtrack.force !components_of_module_maker' c.comps
| Persistent_env.Cannot_load_cmis log ->
Lazy_backtrack.force_logged log !components_of_module_maker' c.comps
let get_components c =
match get_components_res c with
| Error _ -> empty_structure
| Ok c -> c
let modtype_of_functor_appl fcomp p1 p2 =
match fcomp.fcomp_res with
| Mty_alias _ as mty -> mty
| mty ->
try
stamped_find fcomp.fcomp_subst_cache p2
with Not_found ->
let scope = Path.scope (Papply(p1, p2)) in
let mty =
let subst =
match fcomp.fcomp_arg with
| Unit
| Named (None, _, _) -> Subst.identity
| Named (Some param, _, _) ->
Subst.add_module param p2 Subst.identity
in
Subst.modtype (Rescope scope) subst mty
in
stamped_path_add fcomp.fcomp_subst_cache p2 mty;
mty
let check_functor_appl
~errors ~loc ~lid_whole_app ~f0_path ~args
~f_comp
~arg_path ~arg_mty ~param_mty
env =
if not (stamped_mem f_comp.fcomp_cache arg_path) then
!check_functor_application
~errors ~loc ~lid_whole_app ~f0_path ~args
~arg_path ~arg_mty ~param_mty
env
let find_ident_module id env =
match find_same_module id env.modules with
| Mod_local (data, _) -> data
| Mod_unbound _ -> raise Not_found
| Mod_persistent ->
match Ident.to_global id with
| Some global_name ->
let allow_excess_args =
true
in
find_pers_mod ~allow_hidden:true ~allow_excess_args global_name
| None -> Misc.fatal_errorf "Not global: %a" Ident.print id
let rec find_module_components path env =
match path with
| Pident id -> (find_ident_module id env).mda_components
| Pdot(p, s) ->
let sc = find_structure_components p env in
(NameMap.find s sc.comp_modules).mda_components
| Papply(f_path, arg) ->
let f_comp = find_functor_components f_path env in
let loc = Location.(in_file !input_name) in
!components_of_functor_appl' ~loc ~f_path ~f_comp ~arg env
| Pextra_ty _ -> raise Not_found
and find_structure_components path env =
match get_components (find_module_components path env) with
| Structure_comps c -> c
| Functor_comps _ -> raise Not_found
and find_functor_components path env =
match get_components (find_module_components path env) with
| Functor_comps f -> f
| Structure_comps _ -> raise Not_found
let find_module path env =
match path with
| Pident id ->
let data = find_ident_module id env in
Subst.Lazy.force_module_decl data.mda_declaration
| Pdot(p, s) ->
let sc = find_structure_components p env in
let data = NameMap.find s sc.comp_modules in
Subst.Lazy.force_module_decl data.mda_declaration
| Papply(p1, p2) ->
let fc = find_functor_components p1 env in
md (modtype_of_functor_appl fc p1 p2)
| Pextra_ty _ -> raise Not_found
let find_module_lazy ~alias path env =
match path with
| Pident id ->
let data = find_ident_module id env in
data.mda_declaration
| Pdot(p, s) ->
let sc = find_structure_components p env in
let data = NameMap.find s sc.comp_modules in
data.mda_declaration
| Papply(p1, p2) ->
let fc = find_functor_components p1 env in
let md =
if alias then md (fc.fcomp_res)
else md (modtype_of_functor_appl fc p1 p2)
in
Subst.Lazy.of_module_decl md
| Pextra_ty _ -> raise Not_found
let find_value_full path env =
match path with
| Pident id -> begin
match IdTbl.find_same_without_locks id env.values with
| Val_bound data -> data
| Val_unbound _ -> raise Not_found
end
| Pdot(p, s) ->
let sc = find_structure_components p env in
NameMap.find s sc.comp_values
| Papply _ | Pextra_ty _ -> raise Not_found
let find_extension_full path env =
match path with
| Pident id -> TycompTbl.find_same id env.constrs
| Pdot(p, s) -> begin
let comps = find_structure_components p env in
let cstrs = NameMap.find s comps.comp_constrs in
let exts = List.filter is_ext cstrs in
match exts with
| [cda] -> cda
| _ -> raise Not_found
end
| Papply _ | Pextra_ty _ -> raise Not_found
let type_of_cstr path = function
| {cstr_inlined = Some decl; _} ->
let labels =
List.map snd (Datarepr.labels_of_type path decl)
in
begin match decl.type_kind with
| Type_record (_, repr, umc) ->
{
tda_declaration = decl;
tda_descriptions = Type_record (labels, repr, umc);
tda_shape = Shape.leaf decl.type_uid;
tda_unboxed_version_descriptions = None;
}
| _ -> assert false
end
| _ -> assert false
type unboxed_version_step =
| Lacks_unboxed_version
| Aliases of Path.t * type_expr list
| Has_unboxed_version of type_declaration
let step_find_unboxed_version decl =
match decl.type_unboxed_version with
| Some ud -> Has_unboxed_version ud
| None ->
match decl.type_kind with
| Type_record_unboxed_product _ | Type_variant _ | Type_open
| Type_record _ ->
Lacks_unboxed_version
| Type_abstract _ ->
match decl.type_manifest with
| None -> Lacks_unboxed_version
| Some ty ->
match get_desc ty with
| Tconstr (path, args, _) -> Aliases (path, args)
| _ -> Lacks_unboxed_version
let rec find_type_data path env seen =
match
StagedPath.Map.find (path_at_current_stage env path) env.local_constraints
with
| decl ->
{
tda_declaration = decl;
tda_descriptions = Type_abstract (Btype.type_origin decl);
tda_shape = Shape.leaf decl.type_uid;
tda_unboxed_version_descriptions = None;
}
| exception Not_found -> begin
match path with
| Pident id ->
IdTbl.find_same_without_locks id env.types
| Pdot(p, s) ->
let sc = find_structure_components p env in
NameMap.find s sc.comp_types
| Papply _ -> raise Not_found
| Pextra_ty (p, ) -> begin
match extra with
| Pcstr_ty s ->
let cstr = find_cstr p s env in
type_of_cstr path cstr
| Pext_ty ->
let cda = find_extension_full p env in
type_of_cstr path cda.cda_description
| Punboxed_ty ->
find_type_unboxed_version_data p env seen
end
end
and find_cstr path name env =
let tda = find_type_data path env Path.Set.empty in
match tda.tda_descriptions with
| Type_variant (cstrs, _, _) ->
List.find (fun cstr -> cstr.cstr_name = name) cstrs
| Type_record _ | Type_record_unboxed_product _ | Type_abstract _
| Type_open ->
raise Not_found
and find_type_unboxed_version path env seen =
if Path.Set.mem path seen then raise Not_found else
let seen = Path.Set.add path seen in
let decl = (find_type_data path env seen).tda_declaration in
match step_find_unboxed_version decl with
| Has_unboxed_version ud -> ud
| Lacks_unboxed_version -> raise Not_found
| Aliases (path, args) ->
let ud = find_type_unboxed_version path env seen in
let man =
Btype.newgenty
(Tconstr (Path.unboxed_version path, args, ref Mnil)) in
let jkind = ud.type_jkind in
let max_sep_of_ud =
List.fold_left Separability.max Separability.Ind ud.type_separability
in
let type_separability =
List.map (fun _ -> max_sep_of_ud) decl.type_separability
in
{
type_params = decl.type_params;
type_arity = decl.type_arity;
type_kind = decl.type_kind;
type_jkind = jkind;
type_ikind =
Types.ikinds_todo
(Format_doc.asprintf "env unboxed alias path=%a" Path.print path);
type_private = decl.type_private;
type_manifest = Some man;
type_variance = decl.type_variance;
type_separability;
type_is_newtype = false;
type_expansion_scope = Btype.lowest_level;
type_loc = decl.type_loc;
type_attributes = decl.type_attributes;
type_unboxed_default = false;
type_uid = Uid.unboxed_version decl.type_uid;
type_unboxed_version = None;
}
and find_type_unboxed_version_data path env seen =
let tda_declaration = find_type_unboxed_version path env seen in
let descrs =
match (find_type_data path env seen).tda_unboxed_version_descriptions with
| Some descrs -> descrs
| None -> Type_abstract Definition
in
{
tda_declaration;
tda_descriptions = descrs;
tda_shape = Shape.leaf tda_declaration.type_uid;
tda_unboxed_version_descriptions = None
}
let find_modtype_lazy path env =
match path with
| Pident id ->
let modtype = IdTbl.find_same_without_locks id env.modtypes in
modtype.mtda_declaration
| Pdot(p, s) ->
let sc = find_structure_components p env in
(NameMap.find s sc.comp_modtypes).mtda_declaration
| Papply _ | Pextra_ty _ -> raise Not_found
let find_modtype path env =
Subst.Lazy.force_modtype_decl (find_modtype_lazy path env)
let find_class_full path env =
match path with
| Pident id -> IdTbl.find_same_without_locks id env.classes
| Pdot(p, s) ->
let sc = find_structure_components p env in
NameMap.find s sc.comp_classes
| Papply _ | Pextra_ty _ -> raise Not_found
let find_cltype path env =
match path with
| Pident id -> (IdTbl.find_same id env.cltypes).cltda_declaration
| Pdot(p, s) ->
let sc = find_structure_components p env in
(NameMap.find s sc.comp_cltypes).cltda_declaration
| Papply _ | Pextra_ty _ -> raise Not_found
let find_value path env =
find_value_full path env |> vda_description
let find_value_no_locks_exn id env =
match IdTbl.find_same_and_locks id env.values with
| Val_bound _, _ :: _ -> Misc.fatal_error "locks encountered"
| Val_bound data, [] -> Normalize_mode.vda Normalize data
| Val_unbound _, _ -> raise Not_found
let find_class path env =
(find_class_full path env).clda_declaration
let find_ident_constructor id env =
(TycompTbl.find_same id env.constrs).cda_description
let find_ident_label record_form id env =
TycompTbl.find_same id (env_labels record_form env)
let find_type p env =
(find_type_data p env Path.Set.empty).tda_declaration
let find_type_descrs p env =
(find_type_data p env Path.Set.empty).tda_descriptions
let find_jkind p env =
match p with
| Pident id -> (IdTbl.find_same id env.jkinds).jkda_declaration
| Pdot(p, s) ->
let sc = find_structure_components p env in
(NameMap.find s sc.comp_jkinds).jkda_declaration
| Papply _ | Pextra_ty _ -> raise Not_found
let rec find_module_address path env =
match path with
| Pident id -> find_ident_module_address id env
| Pdot(p, s) ->
let c = find_structure_components p env in
get_address (NameMap.find s c.comp_modules).mda_address
| Papply _ | Pextra_ty _ -> raise Not_found
and find_ident_module_address id env =
get_address (find_ident_module id env).mda_address
and force_address = function
| Projection { parent; module_repr; pos } ->
Adot(get_address parent, module_repr, pos)
| ModAlias { env; path } -> find_module_address path env
and get_address a =
Lazy_backtrack.force force_address a
let find_value_address path env =
get_address (find_value_full path env).vda_address
let find_class_address path env =
get_address (find_class_full path env).clda_address
let rec get_constrs_address = function
| [] -> raise Not_found
| cda :: rest ->
match cda.cda_address with
| None -> get_constrs_address rest
| Some a -> get_address a
let find_constructor_address path env =
match path with
| Pident id -> begin
let cda = TycompTbl.find_same id env.constrs in
match cda.cda_address with
| None -> raise Not_found
| Some addr -> get_address addr
end
| Pdot(p, s) ->
let c = find_structure_components p env in
get_constrs_address (NameMap.find s c.comp_constrs)
| Papply _ | Pextra_ty _ -> raise Not_found
let find_hash_type path env =
match path with
| Pident id ->
let name = Ident.name id in
let _, cltda =
IdTbl.find_name wrap_identity ~mark:false name env.cltypes
in
cltda.cltda_declaration.clty_hash_type
| Pdot(p, name) ->
let c = find_structure_components p env in
let cltda = NameMap.find name c.comp_cltypes in
cltda.cltda_declaration.clty_hash_type
| Papply _ | Pextra_ty _ -> raise Not_found
let find_implicit_jkind name env =
match String.Map.find_opt name env.implicit_jkinds with
| Some { txt = jkind; loc = _ } -> Some jkind
| None -> None
let global_of_instance_compilation_unit cu =
let global_name = Compilation_unit.to_global_name_exn cu in
let global =
Persistent_env.global_of_global_name !persistent_env global_name ~check:true
~allow_excess_args:false
in
let rec check (global : Global_module.t) =
match global.hidden_args with
| { param = name; _ } :: _ ->
raise (Error (Incomplete_instantiation { unset_param = name }))
| [] ->
List.iter (fun Global_module.Argument.{ value; _ } -> check value)
global.visible_args
in
check global;
global
let probes = ref String.Set.empty
let reset_probes () = probes := String.Set.empty
let add_probe name = probes := String.Set.add name !probes
let has_probe name = String.Set.mem name !probes
let find_shape env (ns : Shape.Sig_component_kind.t) id =
match ns with
| Type ->
let ty = IdTbl.find_same_without_locks id env.types in
ty.tda_shape
| Constructor ->
Shape.leaf ((TycompTbl.find_same id env.constrs).cda_description.cstr_uid)
| Label ->
Shape.leaf ((TycompTbl.find_same id env.labels).lbl_uid)
| Unboxed_label ->
Shape.leaf ((TycompTbl.find_same id env.unboxed_labels).lbl_uid)
| Extension_constructor ->
(TycompTbl.find_same id env.constrs).cda_shape
| Value ->
begin match IdTbl.find_same_without_locks id env.values with
| Val_bound x -> x.vda_shape
| Val_unbound _ -> raise Not_found
end
| Module ->
begin match IdTbl.find_same_without_locks id env.modules with
| Mod_local ({ mda_shape; _ }, _) -> mda_shape
| Mod_persistent -> Shape.for_persistent_unit (Ident.name id)
| Mod_unbound _ ->
assert false
| exception Not_found
when Ident.is_global id && not (Current_unit_name.is_ident id) ->
Shape.for_persistent_unit (Ident.name id)
end
| Module_type ->
let modtype = IdTbl.find_same_without_locks id env.modtypes in
modtype.mtda_shape
| Class ->
(IdTbl.find_same_without_locks id env.classes).clda_shape
| Class_type ->
(IdTbl.find_same id env.cltypes).cltda_shape
| Jkind ->
(IdTbl.find_same id env.jkinds).jkda_shape
let shape_of_path ~namespace env =
Shape.of_path ~namespace ~find_shape:(find_shape env)
let shape_of_path_opt ~namespace env path =
match shape_of_path ~namespace env path with
| shape -> Some shape
| exception Not_found -> None
let shape_for_constr env path ~args =
Option.map (fun sh -> Shape.app_list sh args)
(shape_of_path_opt ~namespace:Type env path)
let shape_or_leaf uid = function
| None -> Shape.leaf uid
| Some shape -> shape
let required_globals = s_ref []
let reset_required_globals () = required_globals := []
let get_required_globals () = !required_globals
let add_required_unit cu =
if not (List.exists (Compilation_unit.equal cu) !required_globals)
then required_globals := cu :: !required_globals
let add_required_ident id env =
if not !Clflags.transparent_modules && Ident.is_global id then
let address = find_ident_module_address id env in
match address_head address with
| AHlocal _ -> ()
| AHunit cu -> add_required_unit cu
let add_required_global path env =
add_required_ident (Path.head path) env
let add_required_global_for_quote path env =
begin match Ident.to_global (Path.head path) with
| None -> ()
| Some global ->
let name = Compilation_unit.Name.of_head_of_global_name global in
if Current_unit_name.is (Compilation_unit.Name.to_string name)
then begin
match Current_unit_name.get_cu () with
| Some cu ->
Persistent_env.require_impl_for_quote !persistent_env cu
| None ->
Misc.fatal_error
"the current compilation unit was required for a quote \
but is unavailable"
end
else begin
let address = find_module_address path env in
match address_head address with
| AHlocal _ -> ()
| AHunit cu ->
add_required_unit cu;
Persistent_env.require_impl_for_quote !persistent_env cu
end;
Persistent_env.require_intf_for_quote !persistent_env name
end
let quoted_intfs () = Persistent_env.quoted_intfs !persistent_env
let loaded_transitive_dependencies intfs =
Persistent_env.loaded_transitive_dependencies !persistent_env intfs
let quoted_impls () = Persistent_env.quoted_impls !persistent_env
let rec normalize_module_path lax env = function
| Pident id as path when lax && Ident.is_global id ->
path
| Pdot (p, s) as path ->
let p' = normalize_module_path lax env p in
if p == p' then expand_module_path lax env path
else expand_module_path lax env (Pdot(p', s))
| Papply (p1, p2) as path ->
let p1' = normalize_module_path lax env p1 in
let p2' = normalize_module_path true env p2 in
if p1 == p1' && p2 == p2' then expand_module_path lax env path
else expand_module_path lax env (Papply(p1', p2'))
| Pident _ as path ->
expand_module_path lax env path
| Pextra_ty _ -> assert false
and expand_module_path lax env path =
try match find_module_lazy ~alias:true path env with
{md_type=Mty_alias path1} ->
let path' = normalize_module_path lax env path1 in
if not (lax || !Clflags.transparent_modules) then begin
let id = Path.head path in
if Ident.is_global_or_predef id && not (Ident.same id (Path.head path'))
then add_required_global (Pident id) env
end;
path'
| _ -> path
with Not_found when lax
|| (match path with Pident id -> not (Ident.is_global id) | _ -> true) ->
path
let normalize_module_path oloc env path =
try normalize_module_path (oloc = None) env path
with Not_found ->
match oloc with None -> assert false
| Some loc ->
error (Missing_module(loc, path,
normalize_module_path true env path))
let rec normalize_path_prefix oloc env path =
match path with
| Pdot(p, s) ->
let p2 = normalize_module_path oloc env p in
if p == p2 then path else Pdot(p2, s)
| Pident _ ->
path
| Pextra_ty (p, ) ->
let p2 = normalize_path_prefix oloc env p in
if p == p2 then path else Pextra_ty (p2, extra)
| Papply _ ->
assert false
let normalize_type_path = normalize_path_prefix
let normalize_value_path = normalize_path_prefix
let rec normalize_modtype_path env path =
let path = normalize_path_prefix None env path in
expand_modtype_path env path
and expand_modtype_path env path =
match (find_modtype_lazy path env).mtd_type with
| Some (Mty_ident path) -> normalize_modtype_path env path
| _ | exception Not_found -> path
let normalize_instance_names_in_ident ident =
if Ident.is_instance ident then
let modname = Ident.to_global_exn ident in
let modname2 =
Persistent_env.normalize_global_name !persistent_env modname
in
if modname == modname2 then ident else Ident.create_global modname2
else
ident
let rec normalize_instance_names_in_module_path path =
match path with
| Pident i ->
let i2 = normalize_instance_names_in_ident i in
if i == i2 then path else Pident i2
| Pdot (p, s) ->
let p2 = normalize_instance_names_in_module_path p in
if p == p2 then path else Pdot (p2, s)
| Pextra_ty (p, ) ->
let p2 = normalize_instance_names_in_module_path p in
if p == p2 then path else Pextra_ty (p2, extra)
| Papply (p, a) ->
let p2 = normalize_instance_names_in_module_path p in
if p == p2 then path else Papply (p2, a)
let find_module_lazy path env =
find_module_lazy ~alias:false path env
let find_type_expansion path env =
let decl = find_type path env in
match decl.type_manifest with
| Some body when decl.type_private = Public
|| not (Btype.type_kind_is_abstract decl)
|| Btype.has_constr_row body ->
(decl.type_params, body, decl.type_expansion_scope)
| _ -> raise Not_found
let find_type_expansion_opt path env =
let decl = find_type path env in
match decl.type_manifest with
| Some body ->
(decl.type_params, body, decl.type_expansion_scope)
| _ -> raise Not_found
let find_jkind_expansion path env =
let decl = find_jkind path env in
match decl.jkind_manifest with
| None -> raise Not_found
| Some body -> body
let find_modtype_expansion_lazy path env =
match (find_modtype_lazy path env).mtd_type with
| None -> raise Not_found
| Some mty -> mty
let find_modtype_expansion path env =
Subst.Lazy.force_modtype (find_modtype_expansion_lazy path env)
let is_parameter_module_ident id =
match Ident.to_global id with
| Some global -> Persistent_env.is_parameter_import !persistent_env global
| None -> false
let rec is_functor_arg path env =
match path with
Pident id ->
begin try Ident.find_same id env.functor_args; true
with Not_found ->
is_parameter_module_ident id
end
| Pdot (p, _) | Pextra_ty (p, _) -> is_functor_arg p env
| Papply _ -> true
let make_copy_of_types env0 =
let memo = Hashtbl.create 16 in
let copy t =
try
Hashtbl.find memo (get_id t)
with Not_found ->
let t2 = Subst.Lazy.substitute Subst.identity (Subst.Lazy.of_value t) in
Hashtbl.add memo (get_id t) t2;
t2
in
let f = function
| Val_unbound _ as entry -> entry
| Val_bound vda ->
let desc = vda.vda_description in
let t = copy (Subst.Lazy.force_type_expr desc.val_type) in
let desc = { desc with val_type = t } in
Val_bound { vda with vda_description = desc }
in
let values =
IdTbl.map f env0.values
in
(fun env ->
{env with values; summary = Env_copy_types env.summary}
)
type iter_cont = unit -> unit
let iter_env_cont = ref []
let global_ident_is_looked_up id =
Persistent_env.looked_up !persistent_env (Ident.to_global_exn id)
let rec scrape_alias_for_visit env mty =
let open Subst.Lazy in
match mty with
| Mty_alias path -> begin
match path with
| Pident id
when Ident.is_global id
&& not (global_ident_is_looked_up id) ->
false
| path ->
try
scrape_alias_for_visit env (find_module_lazy path env).md_type
with Not_found -> false
end
| _ -> true
let iter_env wrap proj1 proj2 f env () =
IdTbl.iter wrap (fun id x -> f (Pident id) x) (proj1 env);
let rec iter_components path path' mcomps =
let cont () =
let visit =
match Lazy_backtrack.get_arg mcomps.comps with
| None -> true
| Some { cm_mty; _ } ->
scrape_alias_for_visit env cm_mty
in
if not visit then () else
match get_components mcomps with
Structure_comps comps ->
NameMap.iter
(fun s d -> f (Pdot (path, s)) (Pdot (path', s), d))
(proj2 comps);
NameMap.iter
(fun s mda ->
iter_components
(Pdot (path, s)) (Pdot (path', s)) mda.mda_components)
comps.comp_modules
| Functor_comps _ -> ()
in iter_env_cont := (path, cont) :: !iter_env_cont
in
IdTbl.iter wrap_module
(fun id (path, entry) ->
match entry with
| Mod_unbound _ -> ()
| Mod_local (data, _) ->
iter_components (Pident id) path data.mda_components
| Mod_persistent -> ())
env.modules;
Persistent_env.fold !persistent_env (fun name data () ->
let id = Ident.create_global name in
let path = Pident id in
iter_components path path data.mda_components) ()
let run_iter_cont l =
iter_env_cont := [];
List.iter (fun c -> c ()) l;
let cont = List.rev !iter_env_cont in
iter_env_cont := [];
cont
let iter_types f =
iter_env wrap_identity (fun env -> env.types) (fun sc -> sc.comp_types)
(fun p1 (p2, tda) ->
f p1 (p2, tda.tda_declaration);
Option.iter
(fun du ->
f (Path.unboxed_version p1) (Path.unboxed_version p2, du))
tda.tda_declaration.type_unboxed_version)
let same_types env1 env2 =
env1.types == env2.types && env1.modules == env2.modules
let used_persistent () =
Persistent_env.fold !persistent_env
(fun s _m r ->
Compilation_unit.Name.Set.add
(s |> Compilation_unit.Name.of_head_of_global_name)
r)
Compilation_unit.Name.Set.empty
let find_all_comps wrap proj s (p, mda) =
match get_components mda.mda_components with
Functor_comps _ -> []
| Structure_comps comps ->
try
let c = NameMap.find s (proj comps) in
[Pdot(p,s), wrap c]
with Not_found -> []
let rec find_shadowed_comps path env =
match path with
| Pident id ->
List.filter_map
(fun (p, data) ->
match data with
| Mod_local (x, _) -> Some (p, x)
| Mod_unbound _ | Mod_persistent -> None)
(IdTbl.find_all wrap_module (Ident.name id) env.modules)
| Pdot (p, s) ->
let l = find_shadowed_comps p env in
let l' =
List.map
(find_all_comps wrap_identity
(fun comps -> comps.comp_modules) s) l
in
List.flatten l'
| Papply _ | Pextra_ty _ -> []
let find_shadowed wrap proj1 proj2 path env =
match path with
Pident id ->
IdTbl.find_all wrap (Ident.name id) (proj1 env)
| Pdot (p, s) ->
let l = find_shadowed_comps p env in
let l' = List.map (find_all_comps wrap proj2 s) l in
List.flatten l'
| Papply _ | Pextra_ty _ -> []
let find_shadowed_types path env =
List.map fst
(find_shadowed wrap_identity
(fun env -> env.types) (fun comps -> comps.comp_types) path env)
let prefix_idents root prefixing_sub sg =
let open Subst.Lazy in
let rec prefix_idents root items_and_paths prefixing_sub =
function
| [] -> (List.rev items_and_paths, prefixing_sub)
| Sig_value(id, _, _) as item :: rem ->
let p = Pdot(root, Ident.name id) in
prefix_idents root
((item, p) :: items_and_paths) prefixing_sub rem
| Sig_type(id, td, rs, vis) :: rem ->
let p = Pdot(root, Ident.name id) in
prefix_idents root
((Sig_type(id, td, rs, vis), p) :: items_and_paths)
(Subst.add_type id p prefixing_sub)
rem
| Sig_typext(id, ec, es, vis) :: rem ->
let p = Pdot(root, Ident.name id) in
prefix_idents root
((Sig_typext(id, ec, es, vis), p) :: items_and_paths)
(Subst.add_type id p prefixing_sub)
rem
| Sig_module(id, pres, md, rs, vis) :: rem ->
let p = Pdot(root, Ident.name id) in
prefix_idents root
((Sig_module(id, pres, md, rs, vis), p) :: items_and_paths)
(Subst.add_module id p prefixing_sub)
rem
| Sig_modtype(id, mtd, vis) :: rem ->
let p = Pdot(root, Ident.name id) in
prefix_idents root
((Sig_modtype(id, mtd, vis), p) :: items_and_paths)
(Subst.add_modtype id p prefixing_sub)
rem
| Sig_class(id, cd, rs, vis) :: rem ->
let p = Pdot(root, Ident.name id) in
prefix_idents root
((Sig_class(id, cd, rs, vis), p) :: items_and_paths)
(Subst.add_type id p prefixing_sub)
rem
| Sig_class_type(id, ctd, rs, vis) :: rem ->
let p = Pdot(root, Ident.name id) in
prefix_idents root
((Sig_class_type(id, ctd, rs, vis), p) :: items_and_paths)
(Subst.add_type id p prefixing_sub)
rem
| Sig_jkind(id, jkd, vis) :: rem ->
let p = Pdot(root, Ident.name id) in
prefix_idents root
((Sig_jkind(id, jkd, vis), p) :: items_and_paths)
(Subst.add_jkind id p prefixing_sub)
rem
in
let sg = Subst.Lazy.force_signature_once sg in
prefix_idents root [] prefixing_sub sg
let short_paths_type ~long_path predef id decl old =
if long_path || (not predef && !Clflags.real_paths) then old
else Type(id, decl) :: old
let short_paths_type_open path decls old =
if !Clflags.real_paths then old
else Type_open(path, decls) :: old
let unbound_class = Path.Pident (Ident.create_local "*undef*")
let is_dummy_class decl =
Path.same decl.clty_path unbound_class
let short_paths_class_type id decl old =
if !Clflags.real_paths || is_dummy_class decl then old
else Class_type(id, decl) :: old
let short_paths_class_type_open path decls old =
let decls = NameMap.map (fun cltda -> cltda.cltda_declaration) decls in
if !Clflags.real_paths then old
else Class_type_open(path, decls) :: old
let short_paths_module_type id decl old =
if !Clflags.real_paths then old
else Module_type(id, decl) :: old
let short_paths_module_type_open path decls old =
let decls = NameMap.map (fun mtda -> mtda.mtda_declaration) decls in
if !Clflags.real_paths then old
else Module_type_open(path, decls) :: old
let short_paths_module id decl comps old =
if !Clflags.real_paths then old
else Module(id, decl, comps) :: old
let short_paths_module_open path comps old =
if !Clflags.real_paths then old
else Module_open(path, comps) :: old
let add_to_tbl id decl tbl =
let decls = try NameMap.find id tbl with Not_found -> [] in
NameMap.add id (decl :: decls) tbl
let primitive_address_error =
Invalid_argument "Primitives don't have addresses"
let mutable_variable_address_error =
Invalid_argument "Mutable variables don't have addresses"
let value_declaration_address (_ : t) id decl =
match decl.Subst.Lazy.val_kind with
| Val_prim _ -> Lazy_backtrack.create_failed primitive_address_error
| _ -> Lazy_backtrack.create_forced (Alocal id)
let extension_declaration_address (_ : t) id (_ : extension_constructor) =
Lazy_backtrack.create_forced (Alocal id)
let class_declaration_address (_ : t) id (_ : class_declaration) =
Lazy_backtrack.create_forced (Alocal id)
let module_declaration_address env id presence md =
match presence with
| Mp_absent -> begin
let open Subst.Lazy in
match md.md_type with
| Mty_alias path -> Lazy_backtrack.create (ModAlias {env; path})
| _ -> Misc.fatal_error "Env.module_declaration_address"
end
| Mp_present ->
Lazy_backtrack.create_forced (Alocal id)
let is_identchar c =
match c with
| 'A'..'Z' | 'a'..'z' | '_' | '\192'..'\214'
| '\216'..'\246' | '\248'..'\255' | '\'' | '0'..'9' ->
true
| _ ->
false
let rec components_of_module_maker
{cm_env; cm_prefixing_subst;
cm_path; cm_addr; cm_mty; cm_mode; cm_shape} : _ result =
match !scrape_alias cm_env cm_mty with
Mty_signature sg ->
let c =
{ comp_values = NameMap.empty;
comp_constrs = NameMap.empty;
comp_labels = NameMap.empty; comp_unboxed_labels = NameMap.empty;
comp_types = NameMap.empty;
comp_modules = NameMap.empty; comp_modtypes = NameMap.empty;
comp_classes = NameMap.empty; comp_cltypes = NameMap.empty;
comp_jkinds = NameMap.empty }
in
let items_and_paths, sub =
prefix_idents cm_path cm_prefixing_subst sg
in
let env = ref cm_env in
let pos = ref 0 in
let module_repr =
List.filter_map
(fun (item, _) -> Subst.Lazy.sort_of_signature_item item)
items_and_paths
|> Array.of_list
in
let next_address () =
let addr : address_unforced =
Projection
{ parent = cm_addr; module_repr; pos = !pos }
in
incr pos;
Lazy_backtrack.create addr
in
List.iter (fun ((item : Subst.Lazy.signature_item), path) ->
match item with
Sig_value(id, decl, _) ->
let decl' = Subst.Lazy.value_description sub decl in
let addr =
match decl.val_kind with
| Val_prim _ -> Lazy_backtrack.create_failed primitive_address_error
| Val_reg _ -> next_address ()
| Val_ivar _ | Val_self _ | Val_anc _ ->
next_address ()
| Val_mut _ ->
Lazy_backtrack.create_failed mutable_variable_address_error
in
let vda_shape = Shape.proj cm_shape (Shape.Item.value id) in
let vda =
{ vda_description = decl'; vda_address = addr;
vda_mode = cm_mode; vda_shape }
in
c.comp_values <- NameMap.add (Ident.name id) vda c.comp_values;
| Sig_type(id, decl, _, _) ->
let final_decl = Subst.type_declaration sub decl in
Btype.set_static_row_name final_decl
(Subst.type_path sub (Path.Pident id));
let store_decl path final_decl =
match final_decl.type_kind with
| Type_variant (_,repr,umc) ->
let cstrs = List.map snd
(Datarepr.constructors_of_type path final_decl
~current_unit:(get_unit_name ()))
in
List.iter
(fun descr ->
let cda_shape = Shape.leaf descr.cstr_uid in
let cda = {
cda_description = descr;
cda_address = None;
cda_shape }
in
c.comp_constrs <-
add_to_tbl descr.cstr_name cda c.comp_constrs
) cstrs;
Type_variant (cstrs, repr, umc)
| Type_record (_, repr, umc) ->
let lbls = List.map snd
(Datarepr.labels_of_type path final_decl)
in
List.iter
(fun descr ->
c.comp_labels <-
add_to_tbl descr.lbl_name descr c.comp_labels)
lbls;
Type_record (lbls, repr, umc)
| Type_record_unboxed_product (_, repr, umc) ->
let (lbls : unboxed_label_description list) = List.map snd
(Datarepr.unboxed_labels_of_type path final_decl)
in
List.iter
(fun descr ->
c.comp_unboxed_labels <-
add_to_tbl descr.lbl_name descr c.comp_unboxed_labels)
lbls;
Type_record_unboxed_product (lbls, repr, umc)
| Type_abstract r -> Type_abstract r
| Type_open -> Type_open
in
let descrs = store_decl path final_decl in
let unboxed_descrs =
Option.map (store_decl (Path.unboxed_version path))
final_decl.type_unboxed_version
in
let shape = Shape.proj cm_shape (Shape.Item.type_ id) in
let tda =
{ tda_declaration = final_decl;
tda_descriptions = descrs;
tda_shape = shape;
tda_unboxed_version_descriptions = unboxed_descrs }
in
c.comp_types <- NameMap.add (Ident.name id) tda c.comp_types;
env := store_type_infos ~tda_shape:shape id decl !env
| Sig_typext(id, ext, _, _) ->
let ext' = Subst.extension_constructor sub ext in
let descr =
Datarepr.extension_descr ~current_unit:(get_unit_name ()) path
ext'
in
let addr = next_address () in
let cda_shape =
Shape.proj cm_shape (Shape.Item.extension_constructor id)
in
let cda =
{ cda_description = descr; cda_address = Some addr; cda_shape }
in
c.comp_constrs <- add_to_tbl (Ident.name id) cda c.comp_constrs
| Sig_module(id, pres, md, _, _) ->
let md, mode = Normalize_mode.md Normalize_exn md cm_mode in
let md' =
Subst.Lazy.module_decl
(Subst.Rescope (Path.scope cm_path)) sub md
in
let addr =
match pres with
| Mp_absent -> begin
match md.md_type with
| Mty_alias path ->
Lazy_backtrack.create (ModAlias {env = !env; path})
| _ -> assert false
end
| Mp_present -> next_address ()
in
let alerts =
Builtin_attributes.alerts_of_attrs md.md_attributes
in
let shape = Shape.proj cm_shape (Shape.Item.module_ id) in
let comps =
components_of_module ~alerts ~uid:md.md_uid !env
sub path addr md.md_type mode shape
in
let mda =
{ mda_declaration = md';
mda_mode = mode;
mda_components = comps;
mda_address = addr;
mda_shape = shape; }
in
c.comp_modules <-
NameMap.add (Ident.name id) mda c.comp_modules;
env :=
store_module ~update_summary:false ~check:None
id addr pres md mode shape locks_empty !env
| Sig_modtype(id, decl, _) ->
let final_decl =
Subst.Lazy.modtype_decl (Rescope (Path.scope cm_path))
sub decl
in
let shape = Shape.proj cm_shape (Shape.Item.module_type id) in
let mtda =
{ mtda_declaration = final_decl;
mtda_shape = shape; }
in
c.comp_modtypes <-
NameMap.add (Ident.name id) mtda c.comp_modtypes;
env := store_modtype ~update_summary:false id decl shape !env
| Sig_class(id, decl, _, _) ->
let decl' = Subst.class_declaration sub decl in
let addr = next_address () in
let shape = Shape.proj cm_shape (Shape.Item.class_ id) in
let clda =
{ clda_declaration = decl';
clda_address = addr;
clda_shape = shape; }
in
c.comp_classes <- NameMap.add (Ident.name id) clda c.comp_classes
| Sig_class_type(id, decl, _, _) ->
let decl' = Subst.cltype_declaration sub decl in
let shape = Shape.proj cm_shape (Shape.Item.class_type id) in
let cltda = { cltda_declaration = decl'; cltda_shape = shape } in
c.comp_cltypes <-
NameMap.add (Ident.name id) cltda c.comp_cltypes
| Sig_jkind(id, decl, _) ->
let decl' = Subst.jkind_declaration sub decl in
let shape = Shape.proj cm_shape (Shape.Item.jkind id) in
let jkda = { jkda_declaration = decl'; jkda_shape = shape } in
c.comp_jkinds <- NameMap.add (Ident.name id) jkda c.comp_jkinds
)
items_and_paths;
Ok (Structure_comps c)
| Mty_functor(arg, ty_res, _) ->
let sub = cm_prefixing_subst in
let scoping = Subst.Rescope (Path.scope cm_path) in
let open Subst.Lazy in
Ok (Functor_comps {
fcomp_arg =
(match arg with
| Unit -> Unit
| Named (param, ty_arg, m_arg) ->
Named (param, force_modtype (modtype scoping sub ty_arg), m_arg));
fcomp_res = force_modtype (modtype scoping sub ty_res);
fcomp_shape = cm_shape;
fcomp_cache = stamped_create 17;
fcomp_subst_cache = stamped_create 17 })
| Mty_ident p | Mty_strengthen (_, p, _) -> Error (No_components_abstract p)
| Mty_alias p -> Error (No_components_alias p)
| Mty_for_hole -> Error (No_components_abstract (Pident (Ident.create_local "hole")))
and check_usage loc id uid warn tbl =
if not loc.Location.loc_ghost &&
Uid.for_actual_declaration uid &&
Warnings.is_active (warn "")
then begin
let name = Ident.name id in
if stamped_mem tbl uid then ()
else let used = ref false in
stamped_uid_add tbl uid (fun () -> used := true);
if not (name = "" || name.[0] = '_' || name.[0] = '#')
then
!add_delayed_check_forward
(fun () -> if not !used then Location.prerr_warning loc (warn name))
end;
and check_value_name name loc =
if String.length name > 0 && not (is_identchar name.[0]) then
for i = 1 to String.length name - 1 do
if name.[i] = '#' then
error (Illegal_value_name(loc, name))
done
and store_value ?check ~mode id addr decl shape env =
let open Subst.Lazy in
check_value_name (Ident.name id) decl.val_loc;
Builtin_attributes.mark_alerts_used decl.val_attributes;
Option.iter
(fun f ->
check_usage decl.val_loc id decl.val_uid f value_declarations;
match decl.val_kind with
| Val_mut _ ->
check_usage decl.val_loc id decl.val_uid f mutated_mutable_values
| _ -> ())
check;
let vda =
{ vda_description = decl;
vda_address = addr;
vda_mode = mode;
vda_shape = shape }
in
{ env with
values = IdTbl.add id (Val_bound vda) env.values;
summary =
Env_value(env.summary, id, Subst.Lazy.force_value_description decl, mode) }
and store_constructor ~check type_decl type_id cstr_id cstr env =
Builtin_attributes.warning_scope cstr.cstr_attributes (fun () ->
if check && not type_decl.type_loc.Location.loc_ghost
&& Warnings.is_active (Warnings.Unused_constructor ("", Unused))
then begin
let ty_name = Ident.name type_id in
let name = cstr.cstr_name in
let loc = cstr.cstr_loc in
let k = cstr.cstr_uid in
let priv = type_decl.type_private in
if not (stamped_mem used_constructors k) then begin
let used = constructor_usages () in
stamped_uid_add used_constructors k
(add_constructor_usage used);
if not (ty_name = "" || ty_name.[0] = '_')
then
!add_delayed_check_forward
(fun () ->
Option.iter
(fun complaint ->
if not (is_in_signature env) then
Location.prerr_warning loc
(Warnings.Unused_constructor(name, complaint)))
(constructor_usage_complaint ~rebind:false priv used));
end;
end);
Builtin_attributes.mark_alerts_used cstr.cstr_attributes;
Builtin_attributes.mark_warn_on_literal_pattern_used
cstr.cstr_attributes;
let cda_shape = Shape.leaf cstr.cstr_uid in
{ env with
constrs =
TycompTbl.add cstr_id
{ cda_description = cstr; cda_address = None; cda_shape } env.constrs;
}
and store_label
: 'rep. record_form:'rep record_form -> check:_ -> _ -> _ -> _ ->
'rep gen_label_description -> _ -> _ =
fun ~record_form ~check type_decl type_id lbl_id lbl env ->
Builtin_attributes.warning_scope lbl.lbl_attributes (fun () ->
if check && not type_decl.type_loc.Location.loc_ghost
&& Warnings.is_active
(Warnings.Unused_field { form = ""; field = ""; complaint = Unused })
then begin
let ty_name = Ident.name type_id in
let priv = type_decl.type_private in
let name = lbl.lbl_name in
let loc = lbl.lbl_loc in
let mut = lbl.lbl_mut in
let k = lbl.lbl_uid in
match k with
| Unboxed_version k_boxed ->
begin match stamped_find_opt used_labels k_boxed with
| Some boxed_usages ->
stamped_uid_add used_labels k boxed_usages
| None ->
()
end
| _ ->
if not (stamped_mem used_labels k) then
let used = label_usages () in
stamped_uid_add used_labels k
(add_label_usage used);
if not (ty_name = "" || ty_name.[0] = '_' || name.[0] = '_')
then !add_delayed_check_forward
(fun () ->
Option.iter
(fun complaint ->
if not (is_in_signature env) then
let form = record_form_to_string record_form in
Location.prerr_warning
loc
(Warnings.Unused_field { form; field = name; complaint }))
(label_usage_complaint priv mut used))
end);
Builtin_attributes.mark_alerts_used lbl.lbl_attributes;
begin match lbl.lbl_mut with
| Mutable _ ->
Builtin_attributes.mark_deprecated_mutable_used lbl.lbl_attributes;
| Immutable -> ()
end;
add_label record_form env lbl_id lbl
and store_type ~check ~long_path ~predef id info shape env =
let loc = info.type_loc in
if check then
check_usage loc id info.type_uid
(fun s -> Warnings.Unused_type_declaration s)
type_declarations;
let store_decl path info env =
match info.type_kind with
| Type_variant (_,repr,umc) ->
let constructors = Datarepr.constructors_of_type path info
~current_unit:(get_unit_name ())
in
Type_variant (List.map snd constructors, repr, umc),
List.fold_left
(fun env (cstr_id, cstr) ->
store_constructor ~check info id cstr_id cstr env)
env constructors
| Type_record (_, repr, umc) ->
let labels = Datarepr.labels_of_type path info in
Type_record (List.map snd labels, repr, umc),
List.fold_left
(fun env (lbl_id, lbl) ->
store_label ~record_form:Legacy ~check info id lbl_id lbl env)
env labels
| Type_record_unboxed_product (_, repr, umc) ->
let labels = Datarepr.unboxed_labels_of_type path info in
Type_record_unboxed_product (List.map snd labels, repr, umc),
List.fold_left
(fun env (lbl_id, lbl) ->
store_label ~record_form:Unboxed_product ~check info id lbl_id lbl
env)
env labels
| Type_abstract r -> Type_abstract r, env
| Type_open -> Type_open, env
in
let descrs, env = store_decl (Pident id) info env in
let unboxed_descrs, env =
match info.type_unboxed_version with
| Some uinfo ->
let unboxed_descrs, env =
store_decl (Path.unboxed_version (Pident id)) uinfo env
in
Some unboxed_descrs, env
| None -> None, env
in
let tda =
{ tda_declaration = info;
tda_descriptions = descrs;
tda_shape = shape;
tda_unboxed_version_descriptions = unboxed_descrs }
in
Builtin_attributes.mark_alerts_used info.type_attributes;
{ env with
types = IdTbl.add id tda env.types;
summary = Env_type(env.summary, id, info);
short_paths_additions =
short_paths_type ~long_path predef id info env.short_paths_additions; }
and store_type_infos ~tda_shape id info env =
let tda =
{
tda_declaration = info;
tda_descriptions = Type_abstract (Btype.type_origin info);
tda_shape;
tda_unboxed_version_descriptions =
Option.map (fun uinfo -> Type_abstract (Btype.type_origin uinfo))
info.type_unboxed_version;
}
in
{ env with
types = IdTbl.add id tda env.types;
summary = Env_type(env.summary, id, info);
short_paths_additions =
short_paths_type ~long_path:false false id info env.short_paths_additions; }
and store_extension ~check ~rebind id addr ext shape env =
let loc = ext.ext_loc in
let cstr =
Datarepr.extension_descr ~current_unit:(get_unit_name ()) (Pident id) ext
in
let cda =
{ cda_description = cstr;
cda_address = Some addr;
cda_shape = shape }
in
Builtin_attributes.mark_alerts_used ext.ext_attributes;
Builtin_attributes.mark_warn_on_literal_pattern_used cstr.cstr_attributes;
Builtin_attributes.warning_scope ext.ext_attributes (fun () ->
if check && not loc.Location.loc_ghost &&
Warnings.is_active (Warnings.Unused_extension ("", false, Unused))
then begin
let priv = ext.ext_private in
let is_exception = Path.same ext.ext_type_path Predef.path_exn in
let name = cstr.cstr_name in
let k = cstr.cstr_uid in
if not (stamped_mem used_constructors k) then begin
let used = constructor_usages () in
stamped_uid_add used_constructors k
(add_constructor_usage used);
!add_delayed_check_forward
(fun () ->
Option.iter
(fun complaint ->
if not (is_in_signature env) then
Location.prerr_warning loc
(Warnings.Unused_extension
(name, is_exception, complaint)))
(constructor_usage_complaint ~rebind priv used))
end;
end);
{ env with
constrs = TycompTbl.add id cda env.constrs;
summary = Env_extension(env.summary, id, ext) }
and store_module ?(update_summary=true) ~check
id addr presence md mode shape alias_locks env =
let open Subst.Lazy in
let loc = md.md_loc in
Option.iter
(fun f -> check_usage loc id md.md_uid f module_declarations) check;
Builtin_attributes.mark_alerts_used md.md_attributes;
let alerts = Builtin_attributes.alerts_of_attrs md.md_attributes in
let md, mode = Normalize_mode.md Normalize md mode in
let comps =
components_of_module ~alerts ~uid:md.md_uid
env Subst.identity (Pident id) addr md.md_type mode shape
in
let mda =
{ mda_declaration = md;
mda_mode = mode;
mda_components = comps;
mda_address = addr;
mda_shape = shape }
in
let summary =
if not update_summary then env.summary
else Env_module (env.summary, id, presence, force_module_decl md, mode,
alias_locks)
in
{ env with
modules = IdTbl.add id (Mod_local (mda, alias_locks)) env.modules;
summary;
short_paths_additions =
short_paths_module id md comps env.short_paths_additions; }
and store_modtype ?(update_summary=true) id info shape env =
Builtin_attributes.mark_alerts_used info.Subst.Lazy.mtd_attributes;
let mtda = { mtda_declaration = info; mtda_shape = shape } in
let summary =
if not update_summary then env.summary
else Env_modtype (env.summary, id, Subst.Lazy.force_modtype_decl info) in
{ env with
modtypes = IdTbl.add id mtda env.modtypes;
summary;
short_paths_additions =
short_paths_module_type id info env.short_paths_additions; }
and store_class id addr desc shape env =
Builtin_attributes.mark_alerts_used desc.cty_attributes;
let clda =
{ clda_declaration = desc;
clda_address = addr;
clda_shape = shape; }
in
{ env with
classes = IdTbl.add id clda env.classes;
summary = Env_class(env.summary, id, desc) }
and store_cltype id desc shape env =
Builtin_attributes.mark_alerts_used desc.clty_attributes;
let cltda = { cltda_declaration = desc; cltda_shape = shape } in
{ env with
cltypes = IdTbl.add id cltda env.cltypes;
summary = Env_cltype(env.summary, id, desc);
short_paths_additions =
short_paths_class_type id desc env.short_paths_additions; }
let store_jkind ~check id decl shape env =
let loc = decl.jkind_loc in
if check then
check_usage loc id decl.jkind_uid
(fun s -> Warnings.Unused_kind_declaration s)
jkind_declarations;
Builtin_attributes.mark_alerts_used decl.jkind_attributes;
let jkda =
{ jkda_declaration = decl;
jkda_shape = shape }
in
{ env with
jkinds = IdTbl.add id jkda env.jkinds;
summary = Env_jkind(env.summary, id, decl) }
let components_of_functor_appl ~loc ~f_path ~f_comp ~arg env =
try
let c = stamped_find f_comp.fcomp_cache arg in
c
with Not_found ->
let p = Papply(f_path, arg) in
let sub =
match f_comp.fcomp_arg with
| Unit
| Named (None, _, _) -> Subst.identity
| Named (Some param, _, _) -> Subst.add_module param arg Subst.identity
in
let mty = Subst.modtype (Rescope (Path.scope p)) sub f_comp.fcomp_res in
let addr = Lazy_backtrack.create_failed Not_found in
!check_well_formed_module env loc
("the signature of " ^ Path.name p) mty;
let shape_arg =
shape_of_path ~namespace:Shape.Sig_component_kind.Module env arg
in
let shape = Shape.app f_comp.fcomp_shape ~arg:shape_arg in
let comps =
components_of_module ~alerts:Misc.Stdlib.String.Map.empty
~uid:Uid.internal_not_actually_unique
env Subst.identity p addr (Subst.Lazy.of_modtype mty)
fcomp_res_mode shape
in
stamped_path_add f_comp.fcomp_cache arg comps;
comps
let _ =
components_of_functor_appl' := components_of_functor_appl;
components_of_module_maker' := components_of_module_maker
let add_functor_arg id env =
{env with
functor_args = Ident.add id () env.functor_args;
summary = Env_functor_arg (env.summary, id)}
let add_value_lazy ?check ?shape ~mode id desc env =
let addr = value_declaration_address env id desc in
let shape = shape_or_leaf desc.Subst.Lazy.val_uid shape in
let mode = Mode.Value.disallow_right mode in
store_value ?check ~mode id addr desc shape env
let add_type ~check ?shape id info env =
let shape = shape_or_leaf info.type_uid shape in
store_type ~check id info shape env
and add_extension ~check ?shape ~rebind id ext env =
let addr = extension_declaration_address env id ext in
let shape = shape_or_leaf ext.ext_uid shape in
store_extension ~check ~rebind id addr ext shape env
and add_module_declaration_lazy
~update_summary ?(arg=false) ?shape ~check id presence md
?(mode = Mode.Value.(allow_right max)) ?(locks = []) env =
let check =
if not check then
None
else if arg && is_in_signature env then
Some (fun s -> Warnings.Unused_functor_parameter s)
else
Some (fun s -> Warnings.Unused_module s)
in
let addr = module_declaration_address env id presence md in
let shape = shape_or_leaf md.Subst.Lazy.md_uid shape in
let mode = Mode.Value.disallow_right mode in
let env =
store_module ~update_summary ~check id addr presence md mode shape locks env
in
if arg then add_functor_arg id env else env
let add_jkind ~check ?shape id decl env =
let shape = shape_or_leaf decl.jkind_uid shape in
store_jkind ~check id decl shape env
let add_module_declaration ?(arg=false) ?shape ~check id presence md
?mode ?locks env =
add_module_declaration_lazy ~update_summary:true ~arg ?shape ~check id
presence (Subst.Lazy.of_module_decl md) ?mode ?locks env
and add_modtype_lazy ~update_summary ?shape id info env =
let shape = shape_or_leaf info.Subst.Lazy.mtd_uid shape in
store_modtype ~update_summary id info shape env
let add_modtype ?shape id info env =
add_modtype_lazy ~update_summary:true ?shape id
(Subst.Lazy.of_modtype_decl info) env
and add_class ?shape id ty env =
let addr = class_declaration_address env id ty in
let shape = shape_or_leaf ty.cty_uid shape in
store_class id addr ty shape env
and add_cltype ?shape id ty env =
let shape = shape_or_leaf ty.clty_uid shape in
store_cltype id ty shape env
let add_module_lazy ~update_summary id presence mty ?mode env =
let md = Subst.Lazy.{md_type = mty;
md_modalities = Mode.Modality.undefined;
md_attributes = [];
md_loc = Location.none;
md_uid = Uid.internal_not_actually_unique}
in
add_module_declaration_lazy ~update_summary ~check:false id presence md ?mode
env
let add_module ?arg ?shape id presence mty ?mode env =
add_module_declaration ~check:false ?arg ?shape id presence (md mty) ?mode env
let add_local_constraint ~stage path info env =
{ env with
local_constraints =
StagedPath.Map.add { stage; path } info env.local_constraints }
let add_implicit_jkind ~loc name jkind env =
{ env with
implicit_jkinds =
String.Map.add name { txt = jkind; loc } env.implicit_jkinds }
let clear_implicit_jkinds env =
{ env with implicit_jkinds = String.Map.empty }
let enter_value ?check ~mode name desc env =
let id = Ident.create_local name in
let desc = Subst.Lazy.of_value_description desc in
let addr = value_declaration_address env id desc in
let mode = Mode.Value.disallow_right mode in
let env =
store_value ?check ~mode id addr desc (Shape.leaf desc.val_uid)
env
in
(id, env)
let enter_type ~scope name info env =
let id = Ident.create_scoped ~scope name in
let env = store_type ~check:true ~predef:false ~long_path:false
id info (Shape.leaf info.type_uid) env
in
(id, env)
let enter_extension ~scope ~rebind name ext env =
let id = Ident.create_scoped ~scope name in
let addr = extension_declaration_address env id ext in
let shape = Shape.leaf ext.ext_uid in
let env = store_extension ~check:true ~rebind id addr ext shape env in
(id, env)
let enter_module_declaration ~scope ?arg ?shape s presence md ?mode ?locks env =
let id = Ident.create_scoped ~scope s in
(id, add_module_declaration ?arg ?shape ~check:true id presence md ?mode
?locks env)
let enter_modtype ~scope name mtd env =
let id = Ident.create_scoped ~scope name in
let shape = Shape.leaf mtd.mtd_uid in
let env = store_modtype id (Subst.Lazy.of_modtype_decl mtd) shape env in
(id, env)
let enter_jkind ~scope name decl env =
let id = Ident.create_scoped ~scope name in
let shape = Shape.leaf decl.jkind_uid in
let env = store_jkind ~check:false id decl shape env in
(id, env)
let enter_class ~scope name desc env =
let id = Ident.create_scoped ~scope name in
let addr = class_declaration_address env id desc in
let env = store_class id addr desc (Shape.leaf desc.cty_uid) env in
(id, env)
let enter_cltype ~scope name desc env =
let id = Ident.create_scoped ~scope name in
let env = store_cltype id desc (Shape.leaf desc.clty_uid) env in
(id, env)
let enter_module ~scope ?arg s presence mty ?mode env =
enter_module_declaration ~scope ?arg s presence (md mty) ?mode env
let add_stage_lock lock env =
{ env with
values = IdTbl.add_lock (Stage_lock lock) env.values;
types = IdTbl.add_lock lock env.types;
modules = IdTbl.add_lock (Stage_lock lock) env.modules;
modtypes = IdTbl.add_lock lock env.modtypes;
classes = IdTbl.add_lock (Stage_lock lock) env.classes;
constrs = TycompTbl.add_lock (Stage_lock lock) env.constrs;
labels = TycompTbl.add_lock lock env.labels;
unboxed_labels = TycompTbl.add_lock lock env.unboxed_labels;
}
let add_lock lock env =
let lock = Nonstage_lock lock in
{ env with
values = IdTbl.add_lock lock env.values;
modules = IdTbl.add_lock lock env.modules;
classes = IdTbl.add_lock lock env.classes;
constrs = TycompTbl.add_lock lock env.constrs;
}
let add_const_closure_lock ?(ghost = false) closure_context comonadic env =
let lock = Const_closure_lock (ghost, closure_context, comonadic) in
add_lock lock env
let add_closure_lock closure_context comonadic env =
let lock = Closure_lock
(closure_context,
Mode.Value.Comonadic.disallow_left comonadic)
in
add_lock lock env
let add_region_lock env = add_lock Region_lock env
let add_exclave_lock env = add_lock Exclave_lock env
let add_unboxed_lock env = add_lock Unboxed_lock env
let enter_quotation env =
add_stage_lock Quotation_lock {env with stage = env.stage + 1}
let enter_splice ~loc env =
if env.stage = 0 then
raise (Error (Toplevel_splice loc));
add_stage_lock Splice_lock {env with stage = env.stage - 1}
let enter_future env =
{ env with stage = Ident.highest_scope }
let mark_toplevel_in_quotations ~scope env =
{ env with toplevel_scope = scope }
let check_no_open_quotations loc env context =
if env.stage = 0
then ()
else raise (Error (Unsupported_inside_quotation (loc, context)))
let stage env = env.stage
let stage_locks_offset locks =
List.fold_right
(fun lock rel_stage ->
match lock with
| Quotation_lock -> rel_stage + 1
| Splice_lock -> rel_stage - 1)
locks 0
let proj_shape map mod_shape item =
match mod_shape with
| None -> map, None
| Some mod_shape ->
let shape = Shape.proj mod_shape item in
Shape.Map.add map item shape, Some shape
module Add_signature(T : Types.Wrapped)(M : sig
val add_value: ?shape:Shape.t -> mode:(Mode.allowed * 'r0) Mode.Value.t -> Ident.t ->
T.value_description -> t -> t
val add_module_declaration: ?arg:bool -> ?shape:Shape.t -> check:bool
-> Ident.t -> module_presence -> T.module_declaration
-> ?mode:(Mode.allowed * 'r) Mode.Value.t -> ?locks:locks ->
t -> t
val add_modtype: ?shape:Shape.t -> Ident.t -> T.modtype_declaration -> t -> t
end) = struct
open T
let add_item map mod_shape comp mode env =
match comp with
| Sig_value(id, decl, _) ->
let map, shape = proj_shape map mod_shape (Shape.Item.value id) in
map, M.add_value ?shape ~mode id decl env
| Sig_type(id, decl, _, _) ->
let map, shape = proj_shape map mod_shape (Shape.Item.type_ id) in
map,
add_type ~long_path:false ~check:false ~predef:false ?shape id decl env
| Sig_typext(id, ext, _, _) ->
let map, shape = proj_shape map mod_shape (Shape.Item.extension_constructor id) in
map, add_extension ~check:false ?shape ~rebind:false id ext env
| Sig_module(id, presence, md, _, _) ->
let map, shape = proj_shape map mod_shape (Shape.Item.module_ id) in
map, M.add_module_declaration ~check:false ?shape id presence md ~mode
env
| Sig_modtype(id, decl, _) ->
let map, shape = proj_shape map mod_shape (Shape.Item.module_type id) in
map, M.add_modtype ?shape id decl env
| Sig_class(id, decl, _, _) ->
let map, shape = proj_shape map mod_shape (Shape.Item.class_ id) in
map, add_class ?shape id decl env
| Sig_class_type(id, decl, _, _) ->
let map, shape = proj_shape map mod_shape (Shape.Item.class_type id) in
map, add_cltype ?shape id decl env
| Sig_jkind(id, decl, _) ->
let map, shape = proj_shape map mod_shape (Shape.Item.jkind id) in
map, add_jkind ~check:false ?shape id decl env
let rec add_signature map mod_shape sg ?(mode = Mode.Value.(allow_right max))
env =
match sg with
[] -> map, env
| comp :: rem ->
let map, env = add_item map mod_shape comp mode env in
add_signature map mod_shape rem ~mode env
end
let add_signature map mod_shape sg ?mode env =
let module M = Add_signature(Types)(struct
let add_value ?shape ~mode id vd =
add_value_lazy ?shape ~mode id (Subst.Lazy.of_value_description vd)
let add_module_declaration = add_module_declaration
let add_modtype = add_modtype
end)
in
M.add_signature map mod_shape sg ?mode env
let add_signature_lazy =
let module M = Add_signature(Subst.Lazy)(struct
let add_value ?shape ~mode = add_value_lazy ?check:None ?shape ~mode
let add_module_declaration ?arg ?shape ~check id pres md ?mode ?locks env =
add_module_declaration_lazy ~update_summary:true ?arg ?shape ~check id
pres md ?mode ?locks env
let add_modtype = add_modtype_lazy ~update_summary:true
end)
in
M.add_signature
let enter_signature_and_shape ~scope ~parent_shape mod_shape sg ?mode env =
let sg = Subst.signature (Rescope scope) Subst.identity sg in
let shape, env = add_signature parent_shape mod_shape sg ?mode env in
sg, shape, env
let enter_signature ?mod_shape ~scope sg ?mode env =
let sg, _, env =
enter_signature_and_shape ~scope ~parent_shape:Shape.Map.empty
mod_shape sg ?mode env
in
sg, env
let enter_signature_and_shape ~scope ~parent_shape mod_shape sg ?mode env =
enter_signature_and_shape ~scope ~parent_shape (Some mod_shape) sg ?mode env
let add_value_lazy = add_value_lazy ?shape:None
let add_value ?check ~mode id vd =
add_value_lazy ?check ~mode id (Subst.Lazy.of_value_description vd)
let add_class = add_class ?shape:None
let add_cltype = add_cltype ?shape:None
let add_modtype_lazy = add_modtype_lazy ?shape:None
let add_modtype = add_modtype ?shape:None
let add_module_declaration_lazy ?(arg=false) =
add_module_declaration_lazy ~arg ?shape:None ~check:false
let add_signature sg env =
let _, env = add_signature Shape.Map.empty None sg env in
env
let add_signature_lazy sg env =
let _, env = add_signature_lazy Shape.Map.empty None sg env in
env
let enter_unbound_value name reason env =
let id = Ident.create_local name in
{ env with
values = IdTbl.add id (Val_unbound reason) env.values;
summary = Env_value_unbound(env.summary, name, reason) }
let enter_unbound_module name reason env =
let id = Ident.create_local name in
{ env with
modules = IdTbl.add id (Mod_unbound reason) env.modules;
summary = Env_module_unbound(env.summary, name, reason) }
let read_signature modname cmi =
let mty, staticity = read_pers_mod modname cmi in
Subst.Lazy.force_signature mty, staticity
let register_parameter modname =
Persistent_env.register_parameter !persistent_env modname
let unit_name_of_filename fn =
match Filename.extension fn with
| ".cmi" ->
let modname = Unit_info.modname_from_source fn in
if Unit_info.is_unit_name modname then Some modname
else None
| _ -> None
let persistent_structures_of_basenames basenames =
basenames
|> List.to_seq
|> Seq.filter_map unit_name_of_filename
|> String.Set.of_seq
let persistent_structures_of_dir dir =
Load_path.Dir.basenames dir
|> persistent_structures_of_basenames
let save_signature_with_transform cmi_transform ~alerts (sg, staticity) modname
kind cmi_info =
Btype.cleanup_abbrev ();
Subst.reset_additional_action_id ();
let sg = Subst.Lazy.of_signature sg
|> Subst.Lazy.signature Make_local
(Subst.with_additional_action Prepare_for_saving Subst.identity)
in
let cmi =
Persistent_env.make_cmi !persistent_env modname kind (sg, staticity) alerts
|> cmi_transform in
let filename = Unit_info.Artifact.filename cmi_info in
let pers_sig =
Persistent_env.Persistent_signature.{
filename; cmi;
visibility = Visible { cmx_guaranteed = false }
}
in
Persistent_env.save_cmi !persistent_env pers_sig;
cmi
let save_signature ~alerts sg modname cu cmi =
save_signature_with_transform (fun cmi -> cmi) ~alerts sg modname cu cmi
let save_signature_with_imports ~alerts sg modname cu cmi imports =
let with_imports cmi = { cmi with cmi_crcs = imports } in
save_signature_with_transform with_imports ~alerts sg modname cu cmi
let initial () =
let add_type_and_remember_decl (type_ident : Ident.t) decl env =
match !Clflags.shape_format with
| Clflags.Old_merlin -> add_type type_ident decl env ~check:false ~predef:true ~long_path:false
| Clflags.Debugging_shapes ->
let shape =
Type_shape.Type_decl_shape.of_type_declaration type_ident decl
(shape_for_constr env)
in
Uid.Tbl.add Type_shape.all_type_decls decl.type_uid shape;
add_type type_ident ~shape decl env ~check:false ~predef:true ~long_path:false
in
let initial_env =
Predef.build_initial_env add_type_and_remember_decl
(add_extension ~check:false ~rebind:false)
(add_jkind ~check:false)
empty
in
initial_env
let add_type_long_path ~check ?shape id info env =
add_type ~check ?shape ~predef:false ~long_path:true id info env
let add_type ~check ?shape id info env =
add_type ~check ?shape ~predef:false ~long_path:false id info env
let add_language_extension_types env =
let add ext lvl f env =
match Language_extension.is_at_least ext lvl with
| true ->
f (add_type ?shape:None ~check:false) env
| false -> env
in
lazy
Language_extension.(env ()
|> add SIMD Stable Predef.add_simd_stable_extension_types
|> add SIMD Beta Predef.add_simd_beta_extension_types
|> add SIMD Alpha Predef.add_simd_alpha_extension_types
|> add Small_numbers Stable Predef.add_small_number_extension_types
|> add Small_numbers Beta Predef.add_small_number_beta_extension_types
|> add Layouts Stable Predef.add_or_null
|> add Runtime_metaprogramming () Predef.add_runtime_metaprogramming_types)
let initial = add_language_extension_types initial
let mark_module_used uid =
match Stamped_hashtable.find module_declarations uid with
| mark -> mark ()
| exception Not_found -> ()
let mark_modtype_used _uid = ()
let mark_value_used uid =
match Stamped_hashtable.find value_declarations uid with
| mark -> mark ()
| exception Not_found -> ()
let mark_value_mutated uid =
match Stamped_hashtable.find mutated_mutable_values uid with
| mark -> mark ()
| exception Not_found -> ()
let mark_type_used (uid : Uid.t) =
let uid =
match uid with
| Unboxed_version uid -> uid
| _ -> uid
in
match Stamped_hashtable.find type_declarations uid with
| mark -> mark ()
| exception Not_found -> ()
let mark_type_path_used env path =
match find_type path env with
| decl -> mark_type_used decl.type_uid
| exception Not_found -> ()
let mark_constructor_used usage uid =
match stamped_find used_constructors uid with
| mark -> mark usage
| exception Not_found -> ()
let mark_extension_used usage uid =
match stamped_find used_constructors uid with
| mark -> mark usage
| exception Not_found -> ()
let mark_label_used usage uid =
match stamped_find used_labels uid with
| mark -> mark usage
| exception Not_found -> ()
let mark_constructor_description_used usage env cstr =
let ty_path = Btype.cstr_type_path cstr in
mark_type_path_used env ty_path;
match stamped_find used_constructors cstr.cstr_uid with
| mark -> mark usage
| exception Not_found -> ()
let mark_label_description_used usage env lbl =
let ty_path =
match get_desc lbl.lbl_res with
| Tconstr(path, _, _) -> path
| _ -> assert false
in
mark_type_path_used env ty_path;
match stamped_find used_labels lbl.lbl_uid with
| mark -> mark usage
| exception Not_found -> ()
let mark_class_used uid =
match stamped_find type_declarations uid with
| mark -> mark ()
| exception Not_found -> ()
let mark_cltype_used uid =
match stamped_find type_declarations uid with
| mark -> mark ()
| exception Not_found -> ()
let mark_jkind_used uid =
match stamped_find jkind_declarations uid with
| mark -> mark ()
| exception Not_found -> ()
let set_value_used_callback vd callback =
stamped_uid_add value_declarations vd.Subst.Lazy.val_uid callback
let set_value_mutated_callback vd callback =
Stamped_hashtable.add mutated_mutable_values vd.Subst.Lazy.val_uid callback
let set_type_used_callback td callback =
if Uid.for_actual_declaration td.type_uid then
let old =
try stamped_find type_declarations td.type_uid
with Not_found -> ignore
in
Stamped_hashtable.replace type_declarations td.type_uid
(fun () -> callback old)
let may_lookup_error report_errors loc env err =
if report_errors then lookup_error loc env err
else raise Not_found
let path_is_toplevel_in_quotations env path =
Path.scope path <= env.toplevel_scope
let does_not_cross_quotation env path locks =
if path_is_toplevel_in_quotations env path
then Ok ()
else
(match stage_locks_offset locks with
| 0 -> Ok ()
| n -> Result.Error n)
let check_cross_quotation ~errors ~loc_use ~loc_def env path lid
locks =
match does_not_cross_quotation env path locks with
| Ok () -> ()
| Error n ->
may_lookup_error errors loc_use env
(Incompatible_stage (lid, loc_use, env.stage, loc_def, env.stage - n))
let assert_does_not_cross_quotation ~loc_use ~loc_def env path locks =
match does_not_cross_quotation env path locks with
| Ok () -> ()
| Error _ ->
Misc.fatal_errorf_doc
"Identifier %a defined at %a crosses quotation at %a."
Path.print path
(Location.Doc.loc ~capitalize_first:false) loc_def
(Location.Doc.loc ~capitalize_first:false) loc_use
let report_module_unbound ~errors ~loc env reason =
match reason with
| Mod_unbound_illegal_recursion { container; unbound } ->
may_lookup_error errors loc env
(Illegal_reference_to_recursive_module { container; unbound })
let report_value_unbound ~errors ~loc env reason lid =
match reason with
| Val_unbound_instance_variable ->
may_lookup_error errors loc env (Masked_instance_variable lid)
| Val_unbound_self ->
may_lookup_error errors loc env (Masked_self_variable lid)
| Val_unbound_ancestor ->
may_lookup_error errors loc env (Masked_ancestor_variable lid)
| Val_unbound_ghost_recursive rloc ->
let show_hint =
not loc.Location.loc_ghost
&& not rloc.Location.loc_ghost
in
let hint =
if show_hint then Missing_rec rloc else No_hint
in
may_lookup_error errors loc env (Unbound_value(lid, hint))
let use_module ~use ~loc path mda =
if use then begin
let comps = mda.mda_components in
mark_module_used comps.uid;
Misc.Stdlib.String.Map.iter
(fun kind message ->
let message = if message = "" then "" else "\n" ^ message in
Location.alert ~kind loc
(Printf.sprintf "module %s%s" (Path.name path) message)
)
comps.alerts
end
let use_value ~use ~loc path vda =
if use then begin
let desc = vda.vda_description in
mark_value_used desc.val_uid;
Builtin_attributes.check_alerts loc desc.val_attributes
(Path.name path)
end
let mutate_value ~use ~loc path vda =
if use then begin
let desc = vda.vda_description in
mark_value_mutated desc.val_uid;
Builtin_attributes.check_alerts loc desc.val_attributes
(Path.name path)
end
let use_type ~use ~loc path tda =
if use then begin
let decl = tda.tda_declaration in
mark_type_used decl.type_uid;
Builtin_attributes.check_alerts loc decl.type_attributes
(Path.name path)
end
let use_modtype ~use ~loc path desc =
let open Subst.Lazy in
if use then begin
mark_modtype_used desc.mtd_uid;
Builtin_attributes.check_alerts loc desc.mtd_attributes
(Path.name path)
end
let use_class ~use ~loc path clda =
if use then begin
let desc = clda.clda_declaration in
mark_class_used desc.cty_uid;
Builtin_attributes.check_alerts loc desc.cty_attributes
(Path.name path)
end
let use_cltype ~use ~loc path desc =
if use then begin
mark_cltype_used desc.clty_uid;
Builtin_attributes.check_alerts loc desc.clty_attributes
(Path.name path)
end
let use_jkind ~use ~loc path jkda =
if use then begin
let decl = jkda.jkda_declaration in
mark_jkind_used decl.jkind_uid;
Builtin_attributes.check_alerts loc decl.jkind_attributes (Path.name path)
end
let use_label ~use ~loc usage env lbl =
if use then begin
mark_label_description_used usage env lbl;
Builtin_attributes.check_alerts loc lbl.lbl_attributes lbl.lbl_name;
if is_mutating_label_usage usage then
Builtin_attributes.check_deprecated_mutable loc lbl.lbl_attributes
lbl.lbl_name
end
let use_constructor_desc ~use ~loc usage env cstr =
if use then begin
mark_constructor_description_used usage env cstr;
Builtin_attributes.check_alerts loc cstr.cstr_attributes cstr.cstr_name
end
let use_constructor ~use ~loc usage env cda =
use_constructor_desc ~use ~loc usage env cda.cda_description
type _ load =
| Load : module_data load
| Don't_load : unit load
let lookup_global_name_module_no_locks
(type a) (load : a load) ~errors ~use ~loc name env =
let path = Pident(Ident.create_global name) in
match load with
| Don't_load ->
check_pers_mod ~allow_hidden:false ~loc name;
path, (() : a)
| Load -> begin
match find_pers_mod ~allow_hidden:false name ~allow_excess_args:false with
| mda ->
use_module ~use ~loc path mda;
path, (mda : a)
| exception Not_found ->
let s = Global_module.Name.to_string name in
may_lookup_error errors loc env (Unbound_module (Lident s))
| exception Persistent_env.Error err ->
may_lookup_error errors loc env (Error_from_persistent_env err)
end
let lookup_ident_module (type a) (load : a load) ~errors ~use ~loc s env =
let path, locks, data =
match find_name_module ~mark:use ~stage:env.stage s env.modules with
| path, locks, data -> begin
let stage_locks, locks = partition_locks locks in
check_cross_quotation ~errors ~loc_use:loc ~loc_def:Location.none env
path (Lident s) stage_locks;
path, locks, data
end
| exception Not_found ->
may_lookup_error errors loc env (Unbound_module (Lident s))
in
match data with
| Mod_local (mda, alias_locks) -> begin
use_module ~use ~loc path mda;
let _, mode = Normalize_mode.mda Assert_normalized mda in
let locks = alias_locks @ locks in
match load with
| Load -> path, (mode, locks), (mda : a)
| Don't_load -> path, (mode, locks), (() : a)
end
| Mod_unbound reason ->
report_module_unbound ~errors ~loc env reason
| Mod_persistent -> begin
let name = Global_module.Name.create_no_args s in
let path, a =
lookup_global_name_module_no_locks load ~errors ~use ~loc name env
in
let mode =
match load with
| Load -> (a : module_data).mda_mode
| Don't_load ->
Mode.Value.disallow_right
(mode_unit ~staticity:Mode.Staticity.Dynamic)
in
path, (mode, locks), a
end
let closure_mode ~loc ~item ~lid
{Mode.monadic; comonadic} closure_context comonadic0 =
let pp : Mode.Hint.pinpoint = (loc, Ident {category = item; lid}) in
let hint_comonadic : _ Mode.Hint.morph =
Is_closed_by (Comonadic, {closure = closure_context; closed = pp})
in
Mode.Value.Comonadic.submode_err pp
comonadic (Mode.Value.Comonadic.apply_hint hint_comonadic comonadic0);
let hint_monadic : _ Mode.Hint.morph =
Is_closed_by (Monadic, {closure = closure_context; closed = pp})
in
let monadic =
Mode.Value.Monadic.join
[ monadic;
Mode.Value.comonadic_to_monadic_min ~hint:hint_monadic comonadic0 ]
in
{Mode.monadic; comonadic}
let const_closure_mode ~loc ~item ~lid {Mode.monadic; comonadic}
closure_context comonadic0 =
let pp : Mode.Hint.pinpoint = (loc, Ident {category = item; lid}) in
Mode.Value.Comonadic.(submode_err pp comonadic
(of_const ~hint:(Is_used_in closure_context) comonadic0));
let monadic =
Mode.Value.(Monadic.join
[ monadic;
Const.comonadic_to_monadic_min comonadic0
|> Monadic.of_const ~hint:(Is_used_in closure_context) ])
in
{Mode.monadic; comonadic}
let exclave_mode ~errors ~env ~loc ~item ~lid vmode =
match
Mode.Regionality.submode
(Mode.Value.proj_comonadic Areality vmode)
Mode.Regionality.regional
with
| Ok () -> vmode |> Mode.value_to_alloc_r2l |> Mode.alloc_as_value
| Error _ ->
may_lookup_error errors loc env
(Local_value_used_in_exclave (item, lid))
let region_mode vmode =
vmode |> Mode.value_to_alloc_r2l |> Mode.alloc_to_value_l2r
let unboxed_type ~errors ~env ~loc ~lid ty =
match ty with
| None -> ()
| Some ty ->
match
!constrain_type_jkind env ty
Jkind.Builtin.(value_or_null ~why:Captured_in_object)
with
| Ok () -> ()
| Result.Error err ->
may_lookup_error errors loc env (Non_value_used_in_object (lid, ty, err))
(** Takes the [mode] and [ty] of a value at definition site, walks through the
list of locks and constrains [mode] and [ty]. Return the access mode of the
value allowed by the locks.
[ty] is optional as the function works on modules and classes as well, for
which [ty] should be [None]. *)
let walk_locks ~errors ~env ~loc ~item ~lid mode ty locks =
List.fold_left
(fun vmode lock ->
try match lock with
| Region_lock -> region_mode vmode
| Const_closure_lock (_, closure_context, comonadic) ->
const_closure_mode ~loc ~item ~lid vmode closure_context comonadic
| Closure_lock (closure_context, comonadic) ->
closure_mode ~loc ~item ~lid vmode closure_context comonadic
| Exclave_lock ->
exclave_mode ~errors ~env ~loc ~item ~lid vmode
| Unboxed_lock ->
unboxed_type ~errors ~env ~loc ~lid ty;
vmode
with exn ->
!msupport_raise_error exn;
vmode
) mode locks
(** Takes [m0] which is the parameter of [let mutable x] at declaration site,
and [locks] which is the locks between the declaration and the usage (either
reading or writing) of [x], and:
- Raises error if the usage is forbidden by the locks
- Returns the expected mode of the new value at the usage site (if the usage is
a write).
*)
let walk_locks_for_mutable_mode ~errors ~loc ~env locks m0 =
let mode =
m0
|> mutable_mode |> Mode.Value.disallow_left
in
List.fold_left
(fun (mode : Mode.Value.r) lock ->
match lock with
| Region_lock ->
let mode = mode |> Mode.value_to_alloc_r2g |> Mode.alloc_as_value in
Mode.Value.meet
[mode;
Mode.Value.max_with_comonadic Areality
(Mode.Regionality.regional)]
| Exclave_lock ->
mode |> Mode.value_to_alloc_r2l |> Mode.alloc_as_value
| Const_closure_lock (true, _, _) ->
mode
| Const_closure_lock (false, pp, _) | Closure_lock (pp, _) ->
may_lookup_error errors loc env
(Mutable_value_used_in_closure pp)
| Unboxed_lock -> mode
) mode locks
let lookup_ident_value ~errors ~use ~loc name env =
match IdTbl.find_name_and_locks wrap_value ~mark:use name env.values with
| Ok (path, locks, Val_bound vda) ->
let stage_locks, locks = partition_locks locks in
begin match vda with
| {vda_description={val_kind=Val_mut (m0, _); _}; _} ->
m0
|> walk_locks_for_mutable_mode ~errors ~loc ~env locks
|> ignore
| _ -> () end;
check_cross_quotation ~errors ~loc_use:loc
~loc_def:vda.vda_description.val_loc env path (Lident name) stage_locks;
use_value ~use ~loc path vda;
path, locks, vda
| Ok (_, _, Val_unbound reason) ->
report_value_unbound ~errors ~loc env reason (Lident name)
| Error _ ->
may_lookup_error errors loc env (Unbound_value (Lident name, No_hint))
let lookup_ident_type ~errors ~use ~loc s env =
match IdTbl.find_name_and_locks wrap_identity ~mark:use s env.types with
| Ok (path, locks, tda) ->
check_cross_quotation ~errors ~loc_use:loc
~loc_def:tda.tda_declaration.type_loc env path (Lident s) locks;
use_type ~use ~loc path tda;
path, tda
| Error _ ->
may_lookup_error errors loc env (Unbound_type (Lident s))
let lookup_ident_modtype ~errors ~use ~loc s env =
match IdTbl.find_name_and_locks wrap_identity ~mark:use s env.modtypes with
| Ok (path, locks, data) ->
check_cross_quotation ~errors ~loc_use:loc
~loc_def:data.mtda_declaration.mtd_loc env path (Lident s) locks;
use_modtype ~use ~loc path data.mtda_declaration;
(path, data.mtda_declaration)
| Error _ ->
may_lookup_error errors loc env (Unbound_modtype (Lident s))
let lookup_ident_class ~errors ~use ~loc s env =
match IdTbl.find_name_and_locks wrap_identity ~mark:use s env.classes with
| Ok (path, locks, clda) ->
let stage_locks, locks = partition_locks locks in
check_cross_quotation ~errors ~loc_def:loc
~loc_use:clda.clda_declaration.cty_loc env path (Lident s) stage_locks;
use_class ~use ~loc path clda;
path, locks, clda.clda_declaration
| Error _ ->
may_lookup_error errors loc env (Unbound_class (Lident s))
let lookup_ident_cltype ~errors ~use ~loc s env =
match IdTbl.find_name wrap_identity ~mark:use s env.cltypes with
| path, cltda ->
use_cltype ~use ~loc path cltda.cltda_declaration;
path, cltda.cltda_declaration
| exception Not_found ->
may_lookup_error errors loc env (Unbound_cltype (Lident s))
let lookup_ident_jkind ~errors ~use ~loc s env =
match IdTbl.find_name wrap_identity ~mark:use s env.jkinds with
| path, jkind ->
use_jkind ~use ~loc path jkind;
path, jkind.jkda_declaration
| exception Not_found ->
may_lookup_error errors loc env (Unbound_jkind (Lident s))
let find_all_labels (type rep) ~(record_form : rep record_form) ~mark s env
: (_ * rep gen_label_description * (stage_lock list * (unit -> unit))) list =
match record_form with
| Legacy -> TycompTbl.find_all_and_locks ~mark s env.labels
| Unboxed_product -> TycompTbl.find_all_and_locks ~mark s env.unboxed_labels
let lookup_all_ident_labels (type rep) ~(record_form : rep record_form) ~errors
~use ~loc usage s env =
let lbls = find_all_labels ~record_form ~mark:use s env in
let lbls_filtered =
List.filter_map
(fun (path, lbl, (locks, use_fn)) ->
does_not_cross_quotation env path locks
|> Result.map (fun () -> (path, lbl, use_fn))
|> Result.to_option)
lbls
in
match lbls_filtered with
| [] -> begin
match lbls with
| [] ->
may_lookup_error errors loc env
(Unbound_label (Lident s, P record_form, usage))
| (_, _, (locks, _)) :: _ ->
let lbl_stage = env.stage - stage_locks_offset locks in
may_lookup_error errors loc env
(Unbound_in_stage (Label, Lident s, loc, env.stage, lbl_stage))
end
| lbls -> begin
List.map
(fun (_, lbl, use_fn) ->
let use_fn () =
use_label ~use ~loc usage env lbl;
use_fn ()
in
(lbl, use_fn))
lbls
end
let lookup_all_ident_constructors ~errors ~use ~loc usage s env =
let cstrs = TycompTbl.find_all_and_locks ~mark:use s env.constrs in
let cstrs_filtered =
List.filter_map
(fun (path, cda, (locks, use_fn)) ->
let stage_locks, locks = partition_locks locks in
does_not_cross_quotation env path stage_locks
|> Result.map (fun () -> (path, cda, (locks, use_fn)))
|> Result.to_option)
cstrs
in
match cstrs_filtered with
| [] -> begin
match cstrs with
| [] -> may_lookup_error errors loc env (Unbound_constructor (Lident s))
| (_, _, (locks, _)) :: _ ->
let stage_locks, _locks = partition_locks locks in
let lbl_stage = env.stage - stage_locks_offset stage_locks in
may_lookup_error errors loc env
(Unbound_in_stage (Constructor, Lident s, loc, env.stage, lbl_stage))
end
| cstrs ->
List.map
(fun (_path, cda, (locks, use_fn)) ->
let use_fn () =
use_constructor ~use ~loc usage env cda;
use_fn ()
in
((cda.cda_description, locks), use_fn))
cstrs
let rec lookup_module_components ~errors ~use ~loc lid env =
match lid with
| Lident s ->
let path, (_, locks), data =
lookup_ident_module Load ~errors ~use ~loc s env
in
path, (data.mda_mode, locks), data.mda_components
| Ldot(l, s) ->
let path, locks, data = lookup_dot_module ~errors ~use ~loc l s env in
path, (data.mda_mode, locks), data.mda_components
| Lapply _ as lid ->
let f_path, f_comp, arg = lookup_apply ~errors ~use ~loc lid env in
let comps =
!components_of_functor_appl' ~loc ~f_path ~f_comp ~arg env in
Papply (f_path, arg), fcomp_res_mode_with_locks, comps
and lookup_structure_components ~errors ~use ~loc ?(reason = Project) lid env =
let path, mode_with_locks, comps =
lookup_module_components ~errors ~use ~loc lid env
in
match get_components_res comps with
| Ok (Structure_comps comps) -> path, mode_with_locks, comps
| Ok (Functor_comps _) ->
may_lookup_error errors loc env (Functor_used_as_structure (lid, reason))
| Error (No_components_abstract p) ->
may_lookup_error errors loc env (Abstract_used_as_structure (lid, p, reason))
| Error (No_components_alias p) ->
may_lookup_error errors loc env (Cannot_scrape_alias (lid, p))
and get_functor_components ~errors ~loc lid env comps =
match get_components_res comps with
| Ok (Functor_comps fcomps) -> begin
match fcomps.fcomp_arg with
| Unit ->
may_lookup_error errors loc env (Generative_used_as_applicative lid)
| Named (_, arg, _) -> fcomps, arg
end
| Ok (Structure_comps _) ->
may_lookup_error errors loc env (Structure_used_as_functor lid)
| Error (No_components_abstract p) ->
may_lookup_error errors loc env (Abstract_used_as_functor (lid, p))
| Error (No_components_alias p) ->
may_lookup_error errors loc env (Cannot_scrape_alias (lid, p))
and lookup_all_args ~errors ~use ~loc lid0 env =
let rec loop_lid_arg args = function
| Lident _ | Ldot _ as f_lid ->
(f_lid, args)
| Lapply (f_lid, arg_lid) ->
let arg_path, arg_md, _ = lookup_module ~errors ~use ~loc arg_lid env in
loop_lid_arg ((f_lid,arg_path,arg_md.md_type)::args) f_lid
in
loop_lid_arg [] lid0
and lookup_apply ~errors ~use ~loc lid0 env =
let f0_lid, args0 = lookup_all_args ~errors ~use ~loc lid0 env in
let args_for_errors = List.map (fun (_,p,mty) -> (p,mty)) args0 in
let f0_path, _, f0_comp =
lookup_module_components ~errors ~use ~loc f0_lid env
in
let check_one_apply ~errors ~loc ~f_lid ~f_comp ~arg_path ~arg_mty env =
let f_comp, param_mty =
get_functor_components ~errors ~loc f_lid env f_comp
in
check_functor_appl
~errors ~loc ~lid_whole_app:lid0
~f0_path ~args:args_for_errors ~f_comp
~arg_path ~arg_mty ~param_mty
env;
arg_path, f_comp
in
let rec check_apply ~path:f_path ~comp:f_comp = function
| [] -> invalid_arg "Env.lookup_apply: empty argument list"
| [ f_lid, arg_path, arg_mty ] ->
let arg_path, comps =
check_one_apply ~errors ~loc ~f_lid ~f_comp
~arg_path ~arg_mty env
in
f_path, comps, arg_path
| (f_lid, arg_path, arg_mty) :: args ->
let arg_path, f_comp =
check_one_apply ~errors ~loc ~f_lid ~f_comp
~arg_path ~arg_mty env
in
let comp =
!components_of_functor_appl' ~loc ~f_path ~f_comp ~arg:arg_path env
in
let path = Papply (f_path, arg_path) in
check_apply ~path ~comp args
in
check_apply ~path:f0_path ~comp:f0_comp args0
and lookup_module_lazy ~errors ~use ~loc lid env =
match lid with
| Lident s ->
let path, mode_with_locks, data =
lookup_ident_module Load ~errors ~use ~loc s env
in
path, data.mda_declaration, mode_with_locks
| Ldot(l, s) ->
let path, locks, data = lookup_dot_module ~errors ~use ~loc l s env in
let md, mode = Normalize_mode.mda Assert_normalized data in
path, md, (mode, locks)
| Lapply _ as lid ->
let path_f, comp_f, path_arg = lookup_apply ~errors ~use ~loc lid env in
let md =
md (modtype_of_functor_appl comp_f path_f path_arg)
|> Subst.Lazy.of_module_decl
in
Papply(path_f, path_arg), md, fcomp_res_mode_with_locks
and lookup_module ~errors ~use ~loc lid env =
let path, md, mode_with_locks =
lookup_module_lazy ~errors ~use ~loc lid env
in
let md = Subst.Lazy.force_module_decl md in
path, md, mode_with_locks
and lookup_dot_module ~errors ~use ~loc l s env =
let p, (_, locks), comps =
lookup_structure_components ~errors ~use ~loc l env
in
match NameMap.find s comps.comp_modules with
| mda ->
let path = Pdot(p, s) in
use_module ~use ~loc path mda;
(path, locks, mda)
| exception Not_found ->
may_lookup_error errors loc env (Unbound_module (Ldot(l, s)))
let lookup_dot_value ~errors ~use ~loc l s env =
let (path, (_, locks), comps) =
lookup_structure_components ~errors ~use ~loc l env
in
match NameMap.find s comps.comp_values with
| vda ->
let path = Pdot(path, s) in
use_value ~use ~loc path vda;
(path, locks, vda)
| exception Not_found ->
may_lookup_error errors loc env (Unbound_value (Ldot(l, s), No_hint))
let lookup_dot_type ~errors ~use ~loc l s env =
let (p, _, comps) =
lookup_structure_components ~errors ~use ~loc l env
in
match NameMap.find s comps.comp_types with
| tda ->
let path = Pdot(p, s) in
use_type ~use ~loc path tda;
(path, tda)
| exception Not_found ->
may_lookup_error errors loc env (Unbound_type (Ldot(l, s)))
let lookup_dot_modtype ~errors ~use ~loc l s env =
let (p, _, comps) =
lookup_structure_components ~errors ~use ~loc l env
in
match NameMap.find s comps.comp_modtypes with
| mta ->
let path = Pdot(p, s) in
use_modtype ~use ~loc path mta.mtda_declaration;
(path, mta.mtda_declaration)
| exception Not_found ->
may_lookup_error errors loc env (Unbound_modtype (Ldot(l, s)))
let lookup_dot_class ~errors ~use ~loc l s env =
let (p, (_, locks), comps) =
lookup_structure_components ~errors ~use ~loc l env
in
match NameMap.find s comps.comp_classes with
| clda ->
let path = Pdot(p, s) in
use_class ~use ~loc path clda;
(path, locks, clda.clda_declaration)
| exception Not_found ->
may_lookup_error errors loc env (Unbound_class (Ldot(l, s)))
let lookup_dot_cltype ~errors ~use ~loc l s env =
let (p, _, comps) = lookup_structure_components ~errors ~use ~loc l env in
match NameMap.find s comps.comp_cltypes with
| cltda ->
let path = Pdot(p, s) in
use_cltype ~use ~loc path cltda.cltda_declaration;
(path, cltda.cltda_declaration)
| exception Not_found ->
may_lookup_error errors loc env (Unbound_cltype (Ldot(l, s)))
let lookup_dot_jkind ~errors ~use ~loc l s env =
let (p, _, comps) = lookup_structure_components ~errors ~use ~loc l env in
match NameMap.find s comps.comp_jkinds with
| jkind ->
let path = Pdot(p, s) in
use_jkind ~use ~loc path jkind;
(path, jkind.jkda_declaration)
| exception Not_found ->
may_lookup_error errors loc env (Unbound_jkind (Ldot(l, s)))
let lookup_all_dot_labels ~record_form ~errors ~use ~loc usage l s env =
let (_, _, comps) = lookup_structure_components ~errors ~use ~loc l env in
match NameMap.find s (comp_labels record_form comps) with
| [] | exception Not_found ->
may_lookup_error errors loc env
(Unbound_label (Ldot(l, s), P record_form, usage))
| lbls ->
List.map
(fun lbl ->
let use_fun () = use_label ~use ~loc usage env lbl in
(lbl, use_fun))
lbls
let lookup_all_dot_constructors ~errors ~use ~loc usage l s env =
match l with
| Longident.Lident "*predef*" ->
lookup_all_ident_constructors
~errors ~use ~loc usage s (Lazy.force initial)
| _ ->
let (_, (_, locks), comps) =
lookup_structure_components ~errors ~use ~loc l env
in
match NameMap.find s comps.comp_constrs with
| [] | exception Not_found ->
may_lookup_error errors loc env (Unbound_constructor (Ldot(l, s)))
| cstrs ->
List.map
(fun cda ->
let use_fun () = use_constructor ~use ~loc usage env cda in
((cda.cda_description, locks), use_fun))
cstrs
let add_components slot root env0 comps (locks : locks) =
let locks' = List.map (fun lock -> Nonstage_lock lock) locks in
let add_l w comps env0 =
TycompTbl.add_open slot w root comps ([] : stage_lock list) env0
in
let add_c w comps env0 =
TycompTbl.add_open slot w root comps locks' env0
in
let add_v w comps env0 =
IdTbl.add_open slot w root comps locks' env0
in
let add_s w comps env0 =
IdTbl.add_open slot w root comps ([] : stage_lock list) env0
in
let add w comps env0 =
IdTbl.add_open slot w root comps ([] : empty list) env0
in
let add_types w comps env0 additions =
let types = add_s w comps env0 in
let additions = short_paths_type_open root comps additions in
types, additions
in
let add_cltypes w comps env0 additions =
let cltypes = add w comps env0 in
let additions = short_paths_class_type_open root comps additions in
cltypes, additions
in
let add_modtypes w comps env0 additions =
let modtypes = add_s w comps env0 in
let additions = short_paths_module_type_open root comps additions in
modtypes, additions
in
let add_modules w comps env0 additions =
let modules = add_v w comps env0 in
let additions = short_paths_module_open root comps additions in
modules, additions
in
let constrs =
add_c (fun x -> `Constructor x) comps.comp_constrs env0.constrs
in
let labels =
add_l (fun x -> `Label x) comps.comp_labels env0.labels
in
let unboxed_labels =
add_l (fun x -> `Unboxed_label x) comps.comp_unboxed_labels
env0.unboxed_labels
in
let values =
add_v (fun x -> `Value x) comps.comp_values env0.values
in
let types, additions =
add_types (fun x -> `Type x)
comps.comp_types env0.types env0.short_paths_additions
in
let modtypes, additions =
add_modtypes (fun x -> `Module_type x)
comps.comp_modtypes env0.modtypes additions
in
let classes =
add_v (fun x -> `Class x) comps.comp_classes env0.classes
in
let cltypes, additions =
add_cltypes (fun x -> `Class_type x)
comps.comp_cltypes env0.cltypes additions
in
let modules, additions =
add_modules (fun x -> `Module x)
comps.comp_modules env0.modules additions
in
{ env0 with
summary = Env_open(env0.summary, root);
constrs;
labels;
unboxed_labels;
values;
types;
modtypes;
classes;
cltypes;
modules;
short_paths_additions = additions
}
let open_signature_by_path path env0 =
let comps = find_structure_components path env0 in
add_components None path env0 comps locks_empty
let open_signature ~errors ~loc slot lid env0 =
let (root, mode_with_locks, comps) =
lookup_structure_components ~errors ~use:true ~loc ~reason:Open lid env0
in
let _, locks = mode_with_locks in
root, mode_with_locks, add_components slot root env0 comps locks
let remove_last_open root env0 =
let rec filter_summary summary =
match summary with
Env_empty -> raise Exit
| Env_open (s, p) ->
if Path.same p root then s else raise Exit
| Env_value _
| Env_type _
| Env_extension _
| Env_module _
| Env_modtype _
| Env_class _
| Env_cltype _
| Env_functor_arg _
| Env_constraints _
| Env_persistent _
| Env_copy_types _
| Env_value_unbound _
| Env_module_unbound _
| Env_jkind _ ->
map_summary filter_summary summary
in
match filter_summary env0.summary with
| summary ->
let rem_l tbl = TycompTbl.remove_last_open root tbl
and rem tbl = IdTbl.remove_last_open root tbl in
Some { env0 with
summary;
constrs = rem_l env0.constrs;
labels = rem_l env0.labels;
values = rem env0.values;
types = rem env0.types;
modtypes = rem env0.modtypes;
classes = rem env0.classes;
cltypes = rem env0.cltypes;
modules = rem env0.modules; }
| exception Exit ->
None
let open_pers_signature name env =
open_signature ~errors:false ~loc:Location.none None (Lident name) env
let open_signature
~used_slot
~loc ~toplevel
ovf lid env =
let lid_s = Format.asprintf "%a" Pprintast.longident lid.txt in
let unused =
match ovf with
| Asttypes.Fresh -> Warnings.Unused_open lid_s
| Asttypes.Override -> Warnings.Unused_open_bang lid_s
in
let warn_unused =
Warnings.is_active unused
and warn_shadow_id =
Warnings.is_active (Warnings.Open_shadow_identifier ("", ""))
and warn_shadow_lc =
Warnings.is_active (Warnings.Open_shadow_label_constructor ("",""))
in
if not toplevel && not loc.Location.loc_ghost
&& (warn_unused || warn_shadow_id || warn_shadow_lc)
then begin
let used = used_slot in
if warn_unused then
!add_delayed_check_forward
(fun () ->
if not !used then begin
used := true;
Location.prerr_warning loc unused
end
);
let shadowed = ref [] in
let slot s b =
begin match check_shadowing env b with
| Some kind when
ovf = Asttypes.Fresh && not (List.mem (kind, s) !shadowed) ->
shadowed := (kind, s) :: !shadowed;
let w =
match kind with
| "label" | "constructor" ->
Warnings.Open_shadow_label_constructor (kind, s)
| _ -> Warnings.Open_shadow_identifier (kind, s)
in
Location.prerr_warning loc w
| _ -> ()
end;
used := true
in
open_signature ~errors:true ~loc:lid.loc (Some slot) lid.txt env
end
else open_signature ~errors:true ~loc:lid.loc None lid.txt env
let lookup_module_path ~errors ~use ~loc ~load lid env =
match lid with
| Lident s ->
if !Clflags.transparent_modules && not load then
let path, mode_with_locks, () =
lookup_ident_module Don't_load ~errors ~use ~loc s env
in
path, mode_with_locks
else
let path, mode_with_locks, _ =
lookup_ident_module Load ~errors ~use ~loc s env
in
path, mode_with_locks
| Ldot(l, s) ->
let path, locks, data = lookup_dot_module ~errors ~use ~loc l s env in
let _, mode = Normalize_mode.mda Assert_normalized data in
path, (mode, locks)
| Lapply _ as lid ->
let path_f, _comp_f, path_arg = lookup_apply ~errors ~use ~loc lid env in
Papply(path_f, path_arg), fcomp_res_mode_with_locks
let lookup_module_instance_path ~errors ~use ~loc ~load name env =
let locks = IdTbl.get_all_locks env.modules in
let path, loc_def, mode =
if !Clflags.transparent_modules && not load then
let path, () =
lookup_global_name_module_no_locks Don't_load ~errors ~use ~loc name env
in
path, Location.none,
Mode.Value.disallow_right
(mode_unit ~staticity:Mode.Staticity.Dynamic)
else
let path, (mda : module_data) =
lookup_global_name_module_no_locks Load ~errors ~use ~loc name env
in
path, mda.mda_declaration.md_loc, mda.mda_mode
in
let stage_locks, locks = partition_locks locks in
assert_does_not_cross_quotation env ~loc_use:loc ~loc_def path stage_locks;
path, (mode, locks)
let lookup_value_lazy ~errors ~use ~loc lid env =
check_value_name (Longident.last lid) loc;
let path, locks, vda =
match lid with
| Lident s -> lookup_ident_value ~errors ~use ~loc s env
| Ldot(l, s) -> lookup_dot_value ~errors ~use ~loc l s env
| Lapply _ -> assert false
in
let vd, mode = Normalize_mode.vda Normalize vda in
path, vd, (mode, locks)
let lookup_value ~errors ~use ~loc lid env =
let path, vd, mode_with_locks = lookup_value_lazy ~errors ~use ~loc lid env in
let vd = Subst.Lazy.force_value_description vd in
path, vd, mode_with_locks
let lookup_type_full ~errors ~use ~loc lid env =
match lid with
| Lident s -> lookup_ident_type ~errors ~use ~loc s env
| Ldot(l, s) -> lookup_dot_type ~errors ~use ~loc l s env
| Lapply _ -> assert false
let string_without_hash s =
if String.ends_with ~suffix:"#" s then
Some (String.sub s 0 (String.length s - 1))
else
None
let lid_without_hash = function
| Lident s -> begin
match string_without_hash s with
| Some s -> Some (Lident s)
| None -> None
end
| Ldot(l, s) -> begin
match string_without_hash s with
| Some s -> Some (Ldot(l, s))
| None -> None
end
| Lapply _ -> None
let lookup_type ~errors ~use ~loc lid env =
match lid_without_hash lid with
| None ->
let path, tda = lookup_type_full ~errors ~use ~loc lid env in
path, tda.tda_declaration
| Some lid ->
let path, data = lookup_type_full ~errors ~use ~loc lid env in
match find_type_unboxed_version path env Path.Set.empty with
| decl ->
Path.unboxed_version path, decl
| exception Not_found ->
may_lookup_error errors loc env
(No_unboxed_version (lid, data.tda_declaration))
let lookup_modtype_lazy ~errors ~use ~loc lid env =
match lid with
| Lident s -> lookup_ident_modtype ~errors ~use ~loc s env
| Ldot(l, s) -> lookup_dot_modtype ~errors ~use ~loc l s env
| Lapply _ -> assert false
let lookup_modtype ~errors ~use ~loc lid env =
let (path, mt) = lookup_modtype_lazy ~errors ~use ~loc lid env in
path, Subst.Lazy.force_modtype_decl mt
let lookup_class ~errors ~use ~loc lid env =
let path, locks, cld =
match lid with
| Lident s -> lookup_ident_class ~errors ~use ~loc s env
| Ldot(l, s) -> lookup_dot_class ~errors ~use ~loc l s env
| Lapply _ -> assert false
in
let vmode =
if use then
walk_locks ~errors ~loc ~env ~item:Class ~lid clda_mode None locks
else
clda_mode
in
path, cld, vmode
let lookup_cltype ~errors ~use ~loc lid env =
match lid with
| Lident s -> lookup_ident_cltype ~errors ~use ~loc s env
| Ldot(l, s) -> lookup_dot_cltype ~errors ~use ~loc l s env
| Lapply _ -> assert false
let lookup_jkind ~errors ~use ~loc lid env =
match lid with
| Lident s -> lookup_ident_jkind ~errors ~use ~loc s env
| Ldot(l, s) -> lookup_dot_jkind ~errors ~use ~loc l s env
| Lapply _ -> assert false
let lookup_all_labels ~errors ~use ~record_form ~loc usage lid env =
match lid with
| Lident s ->
lookup_all_ident_labels ~errors ~use ~record_form ~loc usage s env
| Ldot(l, s) ->
lookup_all_dot_labels ~errors ~use ~record_form ~loc usage l s env
| Lapply _ -> assert false
let lookup_label ~errors ~use ~record_form ~loc usage lid env =
match lookup_all_labels ~errors ~use ~record_form ~loc usage lid env with
| [] -> assert false
| (desc, use) :: _ -> use (); desc
let lookup_all_labels_from_type (type rep) ~use ~(record_form : rep record_form)
~loc usage ty_path env : (rep gen_label_description * (unit -> unit)) list
=
match (find_type_descrs ty_path env, record_form) with
| exception Not_found -> []
| ((Type_variant _ | Type_abstract _ | Type_open), _) -> []
| (Type_record (lbls, _, _), Legacy) ->
List.map
(fun lbl ->
let use_fun () = use_label ~use ~loc usage env lbl in
(lbl, use_fun))
lbls
| (Type_record_unboxed_product (lbls, _, _), Unboxed_product) ->
List.map
(fun lbl ->
let use_fun () = use_label ~use ~loc usage env lbl in
(lbl, use_fun))
lbls
| (Type_record (_, _, _), Unboxed_product) -> []
| (Type_record_unboxed_product (_, _, _), Legacy) -> []
let lookup_all_constructors ~errors ~use ~loc usage lid env =
match lid with
| Lident s -> lookup_all_ident_constructors ~errors ~use ~loc usage s env
| Ldot(l, s) -> lookup_all_dot_constructors ~errors ~use ~loc usage l s env
| Lapply _ -> assert false
let lookup_constructor ~errors ~use ~loc usage lid env =
match lookup_all_constructors ~errors ~use ~loc usage lid env with
| [] -> assert false
| ((desc, locks), use) :: _ -> use (); desc, locks
let lookup_all_constructors_from_type ~use ~loc usage ty_path env =
match find_type_descrs ty_path env with
| exception Not_found -> []
| Type_record _ | Type_record_unboxed_product _ | Type_abstract _
| Type_open -> []
| Type_variant (cstrs, _, _) ->
List.map
(fun cstr ->
let use_fun () =
use_constructor_desc ~use ~loc usage env cstr
in
((cstr, locks_empty), use_fun))
cstrs
let find_module_by_name_lazy lid env =
let loc = Location.(in_file !input_name) in
let path, desc, _ =
lookup_module_lazy ~errors:false ~use:false ~loc lid env
in
path, desc
let find_value_by_name_lazy lid env =
let loc = Location.(in_file !input_name) in
let path, desc, _ = lookup_value_lazy ~errors:false ~use:false ~loc lid env in
path, desc
let find_value_by_name lid env =
let path, desc = find_value_by_name_lazy lid env in
let desc = Subst.Lazy.force_value_description desc in
path, desc
let find_type_by_name lid env =
let loc = Location.(in_file !input_name) in
lookup_type ~errors:false ~use:false ~loc lid env
let find_modtype_by_name_lazy lid env =
let loc = Location.(in_file !input_name) in
lookup_modtype_lazy ~errors:false ~use:false ~loc lid env
let find_class_by_name lid env =
let loc = Location.(in_file !input_name) in
let path, desc, _ = lookup_class ~errors:false ~use:false ~loc lid env in
path, desc
let find_cltype_by_name lid env =
let loc = Location.(in_file !input_name) in
lookup_cltype ~errors:false ~use:false ~loc lid env
let find_constructor_by_name lid env =
let loc = Location.(in_file !input_name) in
lookup_constructor ~errors:false ~use:false ~loc Positive lid env
|> fst
let find_label_by_name record_form lid env =
let loc = Location.(in_file !input_name) in
lookup_label ~record_form ~errors:false ~use:false ~loc Projection lid env
let find_jkind_by_name lid env =
let loc = Location.(in_file !input_name) in
lookup_jkind ~errors:false ~use:false ~loc lid env
let find_index_tbl ident tbl =
let lbs = IdTbl.find_all_idents (Ident.name ident) tbl in
let find_ident (n,p) = match p with
| Some id -> if Ident.same ident id then Some n else None
| _ -> None
in
Seq.find_map find_ident @@ Seq.mapi (fun i x -> i,x) lbs
let find_value_index id env = find_index_tbl id env.values
let find_type_index id env = find_index_tbl id env.types
let find_module_index id env = find_index_tbl id env.modules
let find_modtype_index id env = find_index_tbl id env.modtypes
let find_class_index id env = find_index_tbl id env.classes
let find_cltype_index id env = find_index_tbl id env.cltypes
let walk_locks ~env ~loc lid ~item ty (mode, locks) =
walk_locks ~errors:true ~loc ~env ~item ~lid mode ty locks
let lookup_module_path ?(use=true) ~loc ~load lid env =
lookup_module_path ~errors:true ~use ~loc ~load lid env
let lookup_module_instance_path ?(use=true) ~loc ~load lid env =
lookup_module_instance_path ~errors:true ~use ~loc ~load lid env
let lookup_module ?(use=true) ~loc lid env =
lookup_module ~errors:true ~use ~loc lid env
let lookup_value ?(use=true) ~loc lid env =
lookup_value ~errors:true ~use ~loc lid env
let lookup_type ?(use=true) ~loc lid env =
lookup_type ~errors:true ~use ~loc lid env
let lookup_modtype ?(use=true) ~loc lid env =
lookup_modtype ~errors:true ~use ~loc lid env
let lookup_modtype_path ?(use=true) ~loc lid env =
fst (lookup_modtype_lazy ~errors:true ~use ~loc lid env)
let lookup_class ?(use=true) ~loc lid env =
let path, desc, vmode = lookup_class ~errors:true ~use ~loc lid env in
path, desc, vmode
let lookup_cltype ?(use=true) ~loc lid env =
lookup_cltype ~errors:true ~use ~loc lid env
let lookup_jkind ?(use=true) ~loc lid env =
lookup_jkind ~errors:true ~use ~loc lid env
let lookup_all_constructors ?(use=true) ~loc usage lid env =
match lookup_all_constructors ~errors:true ~use ~loc usage lid env with
| exception Error(Lookup_error(loc', env', err)) ->
(Error(loc', env', err) : _ result)
| cstrs -> Ok cstrs
let lookup_constructor ?(use=true) ~loc lid env =
lookup_constructor ~errors:true ~use ~loc lid env
let lookup_all_constructors_from_type ?(use=true) ~loc usage ty_path env =
lookup_all_constructors_from_type ~use ~loc usage ty_path env
let lookup_all_labels ?(use=true) ~record_form ~loc usage lid env =
match lookup_all_labels ~errors:true ~use ~record_form ~loc usage lid env with
| exception Error(Lookup_error(loc', env', err)) ->
(Error(loc', env', err) : _ result)
| lbls -> Ok lbls
let lookup_label ?(use=true) ~record_form ~loc lid env =
lookup_label ~errors:true ~use ~record_form ~loc lid env
let lookup_all_labels_from_type ?(use=true) ~record_form ~loc usage ty_path env
=
lookup_all_labels_from_type ~use ~record_form ~loc usage ty_path env
type settable_variable =
| Instance_variable of Path.t * Asttypes.mutable_flag * string * type_expr
| Mutable_variable of Ident.t * Mode.Value.r * type_expr * Jkind_types.Sort.t
let lookup_settable_variable ?(use=true) ~loc name env =
match IdTbl.find_name_and_locks wrap_value ~mark:use name env.values with
| Ok (path, locks, Val_bound vda) -> begin
let stage_locks, locks = partition_locks locks in
check_cross_quotation ~errors:true ~loc_use:loc
~loc_def:vda.vda_description.val_loc env path (Lident name) stage_locks;
let desc = vda.vda_description in
match desc.val_kind, path with
| Val_ivar(mut, cl_num), _ ->
use_value ~use ~loc path vda;
Instance_variable
(path, mut, cl_num, Subst.Lazy.force_type_expr desc.val_type)
| Val_mut (m0, sort), Pident id ->
let val_type = Subst.Lazy.force_type_expr desc.val_type in
let mode =
m0
|> walk_locks_for_mutable_mode ~errors:true ~loc ~env locks
|> Mode.Modality.Const.apply
Typemode.let_mutable_modalities
in
mutate_value ~use ~loc path vda;
Mutable_variable (id, mode, val_type, sort)
| Val_mut _, _ -> assert false
| ((Val_reg _ | Val_prim _ | Val_self _ | Val_anc _), _) ->
lookup_error loc env (Not_a_settable_variable name)
end
| Ok (_, _, Val_unbound Val_unbound_instance_variable) ->
lookup_error loc env (Masked_instance_variable (Lident name))
| Ok (_, _, Val_unbound Val_unbound_self) ->
lookup_error loc env (Not_a_settable_variable name)
| Ok (_, _, Val_unbound Val_unbound_ancestor) ->
lookup_error loc env (Not_a_settable_variable name)
| Ok (_, _, Val_unbound Val_unbound_ghost_recursive _) ->
lookup_error loc env (Unbound_settable_variable name)
| Error _ ->
lookup_error loc env (Unbound_settable_variable name)
let bound_module name env =
match IdTbl.find_name_and_locks wrap_module ~mark:false name env.modules with
| Ok _ -> true
| Error _ ->
if in_current_unit_and_stage ~name ~stage:env.stage then false
else begin
match
find_pers_mod ~allow_hidden:false ~allow_excess_args:false
(Global_module.Name.create_no_args name)
with
| (_ : module_data) -> true
| exception Not_found -> false
end
let bound wrap proj name env =
match IdTbl.find_name_and_locks wrap ~mark:false name (proj env) with
| Ok _ -> true
| Error _ -> false
let bound_value name env =
bound wrap_value (fun env -> env.values) name env
let bound_type name env =
bound wrap_identity (fun env -> env.types) name env
let bound_modtype name env =
bound wrap_identity (fun env -> env.modtypes) name env
let bound_class name env =
bound wrap_identity (fun env -> env.classes) name env
let bound_cltype name env =
bound wrap_identity (fun env -> env.cltypes) name env
let find_all wrap proj1 proj2 f lid env acc =
match lid with
| None ->
IdTbl.fold_name wrap
(fun name (p, data) acc -> f name p data acc)
(proj1 env) acc
| Some l ->
let p, _locks, desc =
lookup_module_components
~errors:false ~use:false ~loc:Location.none l env
in
begin match get_components desc with
| Structure_comps c ->
NameMap.fold
(fun s data acc -> f s (Pdot (p, s)) (wrap data) acc)
(proj2 c) acc
| Functor_comps _ ->
acc
end
let find_all_simple_list proj1 proj2 f lid env acc =
match lid with
| None ->
TycompTbl.fold_name
(fun data acc -> f data acc)
(proj1 env) acc
| Some l ->
let (_p, _locks, desc) =
lookup_module_components
~errors:false ~use:false ~loc:Location.none l env
in
begin match get_components desc with
| Structure_comps c ->
NameMap.fold
(fun _s comps acc ->
match comps with
| [] -> acc
| data :: _ -> f data acc)
(proj2 c) acc
| Functor_comps _ ->
acc
end
let fold_modules f lid env acc =
match lid with
| None ->
IdTbl.fold_name wrap_module
(fun name (p, entry) acc ->
match entry with
| Mod_unbound _ -> acc
| Mod_local (mda, _) ->
f name p mda.mda_declaration acc
| Mod_persistent ->
let modname = Global_module.Name.create_no_args name in
match Persistent_env.find_in_cache !persistent_env modname with
| None -> acc
| Some mda ->
f name p mda.mda_declaration acc)
env.modules
acc
| Some l ->
let p, _locks, desc =
lookup_module_components
~errors:false ~use:false ~loc:Location.none l env
in
begin match get_components desc with
| Structure_comps c ->
NameMap.fold
(fun s mda acc ->
f s (Pdot (p, s)) mda.mda_declaration acc)
c.comp_modules
acc
| Functor_comps _ ->
acc
end
let fold_values f =
find_all wrap_value (fun env -> env.values) (fun sc -> sc.comp_values)
(fun k p ve acc ->
match ve with
| Val_unbound _ -> acc
| Val_bound vda ->
let vd, mode = Normalize_mode.vda Normalize vda in
f k p vd mode acc)
and fold_constructors f =
find_all_simple_list (fun env -> env.constrs) (fun sc -> sc.comp_constrs)
(fun cda acc -> f cda.cda_description acc)
and fold_labels record_form f =
find_all_simple_list
(fun env -> env_labels record_form env)
(fun sc -> comp_labels record_form sc)
f
and fold_types f =
find_all wrap_identity
(fun env -> env.types) (fun sc -> sc.comp_types)
(fun k p tda acc -> f k p tda.tda_declaration acc)
and fold_modtypes f =
find_all wrap_identity
(fun env -> env.modtypes) (fun sc -> sc.comp_modtypes)
(fun k p mta acc -> f k p mta.mtda_declaration acc)
and fold_classes f =
find_all wrap_identity (fun env -> env.classes) (fun sc -> sc.comp_classes)
(fun k p clda acc -> f k p clda.clda_declaration acc)
and fold_cltypes f =
find_all wrap_identity
(fun env -> env.cltypes) (fun sc -> sc.comp_cltypes)
(fun k p cltda acc -> f k p cltda.cltda_declaration acc)
and fold_jkinds f =
find_all wrap_identity
(fun env -> env.jkinds) (fun sc -> sc.comp_jkinds)
(fun k p jkda acc -> f k p jkda.jkda_declaration acc)
let filter_non_loaded_persistent f env =
let to_remove =
IdTbl.fold_name wrap_module
(fun name (_, entry) acc ->
match entry with
| Mod_local _ -> acc
| Mod_unbound _ -> acc
| Mod_persistent ->
let modname = Global_module.Name.create_no_args name in
match Persistent_env.find_in_cache !persistent_env modname with
| Some _ -> acc
| None ->
if f (Ident.create_persistent name) then
acc
else
String.Set.add name acc)
env.modules
String.Set.empty
in
let remove_ids tbl ids =
String.Set.fold
(fun name tbl -> IdTbl.remove (Ident.create_persistent name) tbl)
ids
tbl
in
let rec filter_summary summary ids =
if String.Set.is_empty ids then
summary
else
match summary with
Env_persistent (s, id) when String.Set.mem (Ident.name id) ids ->
filter_summary s (String.Set.remove (Ident.name id) ids)
| Env_empty
| Env_value _
| Env_type _
| Env_extension _
| Env_module _
| Env_modtype _
| Env_class _
| Env_cltype _
| Env_open _
| Env_functor_arg _
| Env_constraints _
| Env_copy_types _
| Env_persistent _
| Env_value_unbound _
| Env_module_unbound _
| Env_jkind _ ->
map_summary (fun s -> filter_summary s ids) summary
in
{ env with
modules = remove_ids env.modules to_remove;
summary = filter_summary env.summary to_remove;
}
let summary env =
if StagedPath.Map.is_empty env.local_constraints then env.summary
else Env_constraints (env.summary, env.local_constraints)
let last_env = s_ref empty
let last_reduced_env = s_ref empty
let keep_only_summary env =
if !last_env == env then !last_reduced_env
else begin
let new_env =
{
empty with
summary = env.summary;
local_constraints = env.local_constraints;
flags = env.flags;
}
in
last_env := env;
last_reduced_env := new_env;
new_env
end
let env_of_only_summary env_from_summary env =
let new_env = env_from_summary env.summary Subst.identity in
{ new_env with
local_constraints = env.local_constraints;
flags = env.flags;
}
let report_jkind_violation_with_offender =
ref ((fun ~offender:_ _ _ _ -> assert false)
: offender:(Format_doc.formatter -> unit) -> t ->
Format_doc.formatter -> Jkind.Violation.t -> unit)
open struct
type env = t
include Format_doc
type t = env
end
let print_longident : Longident.t printer ref = ref (fun _ _ -> assert false)
let pp_longident ppf l = !print_longident ppf l
let print_path: Path.t printer ref = ref (fun _ _ -> assert false)
let print_type_expr : Types.type_expr printer ref =
ref (fun _ _ -> assert false)
let spellcheck ppf env lid =
let choices ~path name = Misc.spellcheck (extract path env) name in
match lid with
| Longident.Lapply _ -> ()
| Longident.Lident s ->
Misc.did_you_mean ppf (fun () -> choices ~path:None s)
| Longident.Ldot (r, s) ->
Misc.did_you_mean ppf (fun () -> choices ~path:(Some r) s)
let spellcheck_name ppf env name =
Misc.did_you_mean ppf
(fun () -> Misc.spellcheck (extract env) name)
let path env =
fold_values (fun name _ _ _ acc -> name :: acc) path env []
let path env =
fold_types (fun name _ _ acc -> name :: acc) path env []
let path env =
fold_modules (fun name _ _ acc -> name :: acc) path env []
let path env =
fold_constructors (fun desc acc -> desc.cstr_name :: acc) path env []
let record_form path env =
fold_labels record_form (fun desc acc -> desc.lbl_name :: acc) path env []
let path env =
fold_classes (fun name _ _ acc -> name :: acc) path env []
let path env =
fold_modtypes (fun name _ _ acc -> name :: acc) path env []
let path env =
fold_cltypes (fun name _ _ acc -> name :: acc) path env []
let path env =
fold_jkinds (fun name _ _ acc -> name :: acc) path env []
let env =
fold_values
(fun name _ descr _ acc ->
match descr.val_kind with
| Val_ivar _ | Val_mut _ -> name :: acc
| _ -> acc) None env []
let print_lock_item ppf (item, lid) =
match (item : Mode.Hint.lock_item) with
| Module ->
fprintf ppf "The module %a is"
(Style.as_inline_code !print_longident) lid
| Class ->
fprintf ppf "%a is a class, and classes are always"
(Style.as_inline_code !print_longident) lid
| Value ->
fprintf ppf "The value %a is"
(Style.as_inline_code !print_longident) lid
| Constructor ->
fprintf ppf "The constructor %a is"
(Style.as_inline_code !print_longident) lid
module Style = Misc.Style
let print_stage ppf stage =
if stage = 0 then fprintf ppf "outside any quotations"
else if stage = 1 then fprintf ppf "inside a quotation (<[ ... ]>)"
else if stage > 1 then
fprintf ppf "inside %d layers of quotation (<[ ... ]>)" stage
else assert false
let print_with_quote_promote ppf (name, intro_stage, usage_stage) =
let stage_diff = intro_stage - usage_stage in
let rec loop fmt stage_diff =
if stage_diff = 1 then Format.fprintf fmt "<[%s]>" name
else if stage_diff = -1 then Format.fprintf fmt "$%s" name
else if stage_diff > 1 then
Format.fprintf fmt "<[%a]>" loop (stage_diff - 1)
else if stage_diff < -1 then
Format.fprintf fmt "$(%a)" loop (stage_diff + 1)
else assert false
in
loop Format.str_formatter stage_diff;
fprintf ppf "%a" Style.inline_code (Format.flush_str_formatter ())
let print_unsupported_quotation ppf =
function
| Object_qt ->
fprintf ppf "Object definition using %a" (Style.inline_code) "object..end"
| Struct_qt ->
fprintf ppf "Module definition using %a" (Style.inline_code) "struct..end"
| Sig_qt ->
fprintf ppf "Module type definition using %a"
(Style.inline_code) "sig..end"
| Open_qt ->
fprintf ppf "Opening a non-identifier module expression"
| Object_field_with_attribute_qt ->
fprintf ppf "Adding attributes on fields in object types"
| Variant_tag_with_attribute_qt ->
fprintf ppf "Adding attributes on tags in polymorphic variant types"
| Jkind_annotation_qt ->
fprintf ppf "Annotating types with kinds"
| Layout_polymorphism_qt ->
fprintf ppf "Layout polymorphism"
| Tconst_pat_qt tconst ->
fprintf ppf "Adding type constraint patterns (here #%s)"
(Format.asprintf "%a" Pprintast.longident tconst)
| Class_type_qt ->
fprintf ppf "Using class type annotations"
let print_unbound_in_quotation ppf =
function
| Label -> fprintf ppf "Label"
| Constructor -> fprintf ppf "Constructor"
let quoted_longident = Style.as_inline_code pp_longident
let report_lookup_error_doc _loc env ppf = function
| Unbound_value(lid, hint) -> begin
fprintf ppf "Unbound value %a" quoted_longident lid;
spellcheck ppf extract_values env lid;
match hint with
| No_hint -> ()
| Missing_rec def_loc ->
let (_, line, _) =
Location.get_pos_info def_loc.Location.loc_start
in
fprintf ppf
"@.@[@{<hint>Hint@}: If this is a recursive definition,@ \
you should add the %a keyword on line %i@]"
Style.inline_code "rec"
line
end
| Unbound_type lid ->
fprintf ppf "Unbound type constructor %a"
quoted_longident lid;
spellcheck ppf extract_types env lid;
| Unbound_module lid -> begin
fprintf ppf "Unbound module %a"
quoted_longident lid;
match find_modtype_by_name_lazy lid env with
| exception Not_found -> spellcheck ppf extract_modules env lid;
| _ ->
fprintf ppf
"@.@[@{<hint>Hint@}: There is a module type named %a, %s@]"
quoted_longident lid
"but module types are not modules"
end
| Unbound_constructor lid ->
fprintf ppf "Unbound constructor %a"
quoted_longident lid;
spellcheck ppf extract_constructors env lid;
| Unbound_label (lid, record_form, usage) ->
let P record_form = record_form in
fprintf ppf "Unbound %s field %a"
(record_form_to_string record_form)
quoted_longident lid;
spellcheck ppf (extract_labels record_form) env lid;
let label_of_other_form = match record_form with
| Legacy ->
(match find_label_by_name Unboxed_product lid env with
| _ -> Some "an unboxed record"
| exception Not_found -> None)
| Unboxed_product ->
(match find_label_by_name Legacy lid env with
| _ -> Some "a boxed record"
| exception Not_found -> None)
in
(match label_of_other_form with
| Some other_form ->
fprintf ppf
"@\n@{<hint>Hint@}: @[There is %s field with this name." other_form;
(match record_form, usage with
| Unboxed_product, _ ->
fprintf ppf
"@ Note that float- and [%@%@unboxed]- records don't get unboxed \
versions."
| Legacy, Projection ->
let print_projection ppf (op, lid) =
fprintf ppf "%s%a" op !print_longident lid
in
fprintf ppf "@ To project an unboxed record field, use %a instead of \
%a."
(Style.as_inline_code print_projection) (".#", lid)
(Style.as_inline_code print_projection) (".", lid)
| _ -> ());
fprintf ppf "@]"
| None -> ());
| Unbound_class lid -> begin
fprintf ppf "Unbound class %a"
quoted_longident lid;
match find_cltype_by_name lid env with
| exception Not_found -> spellcheck ppf extract_classes env lid;
| _ ->
fprintf ppf
"@.@[@{<hint>Hint@}: There is a class type named %a, %s@]"
quoted_longident lid
"but classes are not class types"
end
| Unbound_modtype lid -> begin
fprintf ppf "Unbound module type %a"
quoted_longident lid;
match find_module_by_name_lazy lid env with
| exception Not_found -> spellcheck ppf extract_modtypes env lid;
| _ ->
fprintf ppf
"@.@[@{<hint>Hint@}: There is a module named %a, %s@]"
quoted_longident lid
"but modules are not module types"
end
| Unbound_cltype lid ->
fprintf ppf "Unbound class type %a"
quoted_longident lid;
spellcheck ppf extract_cltypes env lid
| Unbound_jkind lid ->
fprintf ppf "Unbound kind %a"
(Style.as_inline_code !print_longident) lid;
spellcheck ppf extract_jkinds env lid
| Unbound_settable_variable s ->
fprintf ppf "Unbound instance variable or mutable variable %a"
Style.inline_code s;
spellcheck_name ppf extract_settable_variables env s
| Not_a_settable_variable s ->
fprintf ppf "The value %a is not an instance variable or mutable variable"
Style.inline_code s;
spellcheck_name ppf extract_settable_variables env s
| Masked_instance_variable lid ->
fprintf ppf
"The instance variable %a@ \
cannot be accessed from the definition of another instance variable"
quoted_longident lid
| Masked_self_variable lid ->
fprintf ppf
"The self variable %a@ \
cannot be accessed from the definition of an instance variable"
quoted_longident lid
| Masked_ancestor_variable lid ->
fprintf ppf
"The ancestor variable %a@ \
cannot be accessed from the definition of an instance variable"
quoted_longident lid
| Illegal_reference_to_recursive_module { container; unbound } ->
let container = Option.value ~default:"_" container in
let self_or_definition, self_or_unbound =
if String.equal container unbound
then dprintf "its own definition", dprintf "itself"
else
dprintf "the definition of the module %a" Style.inline_code container,
dprintf "the module type of %a" Style.inline_code unbound
in
fprintf ppf
"@[<hov>This module type is recursive.@ \
This use of the recursive module %a@ \
within %t@ \
makes the module type of %a depend on@ %t.@ \
Such recursive definitions of module types are not allowed.@]"
Style.inline_code unbound
self_or_definition
Style.inline_code container
self_or_unbound
| Illegal_reference_to_recursive_class_type
{ container; unbound; unbound_class_type; container_class_type } ->
let container = Option.value ~default:"_" container in
let self_or_unbound =
if String.equal container unbound
then dprintf "itself"
else dprintf "the module type of %a" Style.inline_code unbound
in
fprintf ppf
"@[<hov>This class type is recursive.@ This use of the class type %a@ \
from the recursive module %a@ within the definition of@ \
the class type %a@ in the recursive module %a@ \
makes the module type of %a@ depend on %t.@ \
Such recursive definitions of@ class types within recursive modules@ \
are not allowed.@]"
quoted_longident unbound_class_type
Style.inline_code unbound
Style.inline_code container_class_type
Style.inline_code container
Style.inline_code container
self_or_unbound
| Structure_used_as_functor lid ->
fprintf ppf "@[The module %a is a structure, it cannot be applied@]"
quoted_longident lid
| Abstract_used_as_functor (lid, p) ->
fprintf ppf "@[The module %a is of abstract type %a, it cannot be applied@]"
quoted_longident lid
(Style.as_inline_code !print_path) p
| Functor_used_as_structure (lid, reason) ->
fprintf ppf "@[The module %a is a functor, \
it cannot %a@]"
quoted_longident lid
print_structure_components_reason reason
| Abstract_used_as_structure (lid, p, reason) ->
fprintf ppf "@[The module %a is of abstract type %a, \
it cannot %a@]"
quoted_longident lid
(Style.as_inline_code !print_path) p
print_structure_components_reason reason
| Generative_used_as_applicative lid ->
fprintf ppf "@[The functor %a is generative,@ it@ cannot@ be@ \
applied@ in@ type@ expressions@]"
quoted_longident lid
| Cannot_scrape_alias(lid, p) ->
let cause =
if Current_unit_name.is_path p then "is the current compilation unit"
else "is missing"
in
fprintf ppf
"The module %a is an alias for module %a, which %s"
quoted_longident lid
(Style.as_inline_code !print_path) p cause
| Local_value_used_in_exclave (item, lid) ->
fprintf ppf "@[%a local, so it cannot be used \
inside an exclave_@]"
print_lock_item (item, lid)
| Non_value_used_in_object (lid, typ, err) ->
fprintf ppf "@[%a must have a type of layout value because it is \
captured by an object.@ %a@]"
quoted_longident lid
(fun v -> !report_jkind_violation_with_offender
~offender:(fun ppf -> !print_type_expr ppf typ)
env v)
err
| No_unboxed_version (lid, decl) ->
fprintf ppf "@[The type %a has no unboxed version.@]"
quoted_longident lid;
begin match decl.type_kind with
| Type_record (_, Record_unboxed, _) ->
fprintf ppf
"@.@[@{<hint>Hint@}: \
[%@%@unboxed] records don't get unboxed versions.@]"
| Type_record (_, (Record_float | Record_ufloat | Record_mixed _), _) ->
fprintf ppf
"@.@[@{<hint>Hint@}: Float records don't get unboxed versions.@]";
| Type_record_unboxed_product _ ->
fprintf ppf "@.@[@{<hint>Hint@}: It is already an unboxed record.@]";
| _ -> ()
end
| Error_from_persistent_env err ->
Persistent_env.report_error_doc ppf err
| Mutable_value_used_in_closure ctx ->
fprintf ppf
"@[Mutable variable cannot be used inside %t.@]"
((Mode.print_pinpoint ctx |> Option.get)
~definite:false ~capitalize:false)
| Incompatible_stage (lid, usage_loc, usage_stage, intro_loc, intro_stage) ->
fprintf ppf
"@[Identifier %a is used at %a,@ \
%a;@ \
it is introduced at %a,@ \
%a.@]"
quoted_longident lid
(Location.Doc.loc ~capitalize_first:false) usage_loc
print_stage usage_stage
(Location.Doc.loc ~capitalize_first:false) intro_loc
print_stage intro_stage
| Unbound_in_stage (context, lid, usage_loc, usage_stage, avail_stage) ->
fprintf ppf
"@[%a %a used at %a@ \
cannot be used in this context;@ \
%a is not defined %a.@]\
@.@[@{<hint>Hint@}: %a %a is defined %a.@]"
print_unbound_in_quotation context
quoted_longident lid
(Location.Doc.loc ~capitalize_first:false) usage_loc
quoted_longident lid
print_stage usage_stage
print_unbound_in_quotation context
quoted_longident lid
print_stage avail_stage
let report_error_doc ppf = function
| Missing_module(_, path1, path2) ->
fprintf ppf "@[@[<hov>";
if Path.same path1 path2 then
fprintf ppf "Internal path@ %a@ is dangling."
Style.inline_code (Path.name path1)
else
fprintf ppf "Internal path@ %a@ expands to@ %a@ which is dangling."
Style.inline_code (Path.name path1)
Style.inline_code (Path.name path2);
fprintf ppf "@]@ @[%s@ %a@ %s.@]@]"
"The compiled interface for module"
Style.inline_code (Ident.name (Path.head path2))
"was not found"
| Illegal_value_name(_loc, name) ->
fprintf ppf "%a is not a valid value identifier."
Style.inline_code name
| Lookup_error(loc, t, err) -> report_lookup_error_doc loc t ppf err
| Incomplete_instantiation { unset_param } ->
fprintf ppf "@[<hov>Not enough instance arguments: the parameter@ %a@ is \
required.@]"
Global_module.Parameter_name.print unset_param
| Toplevel_splice loc ->
fprintf ppf
"@[<hov>Splices ($) are not allowed in the initial stage,@ \
as encountered at %a.@,\
Did you forget to insert a quotation?@]"
(Location.Doc.loc ~capitalize_first:false) loc
| Unsupported_inside_quotation (loc, context) ->
fprintf ppf
"@[<hov>%a@ is not supported inside quoted expressions,@ \
as seen at %a.@]"
print_unsupported_quotation context
(Location.Doc.loc ~capitalize_first:false) loc
let () =
Location.register_error_of_exn
(function
| Error err ->
let loc =
match err with
| Missing_module (loc, _, _)
| Illegal_value_name (loc, _)
| Toplevel_splice loc
| Unsupported_inside_quotation (loc, _)
| Lookup_error(loc, _, _) -> loc
| Incomplete_instantiation _ -> Location.none
in
let error_of_printer =
if loc = Location.none
then Location.error_of_printer_file
else Location.error_of_printer ~loc ?sub:None
in
Some
(error_of_printer
report_error_doc err)
| _ ->
None
)
let check_state_consistency () =
let missing modname =
let modname_as_string = Compilation_unit.Name.to_string modname in
match Load_path.find_normalized (modname_as_string ^ ".cmi") with
| _ -> false
| exception Not_found -> true
and found _modname filename ps_name =
match Cmi_cache.get_cached_entry filename with
| cmi_infos -> Compilation_unit.Name.equal ps_name cmi_infos.Cmi_format.cmi_name
| exception Not_found -> false
in
Persistent_env.forall ~found ~missing !persistent_env
let with_cmis f =
Persistent_env.with_cmis !persistent_env f ()
let add_merlin_extension_module id mty env = add_module id Mp_present mty env
let rec index l x =
match l with
[] -> raise Not_found
| a :: l -> if x == a then 0 else 1 + index l x
let rec uniq = function
[] -> true
| a :: l -> not (List.memq a l) && uniq l
let short_paths_type_desc decl =
let open Short_paths.Desc.Type in
match decl.type_manifest with
| None -> Fresh
| Some ty ->
let ty = Transient_expr.repr ty in
if ty.level <> Btype.generic_level then Fresh
else begin
match decl.type_private, decl.type_kind with
| Private, Type_abstract _ -> Fresh
| _, _ -> begin
let params = List.map get_desc decl.type_params in
match ty with
| {desc = Tconstr (path, args, _)} ->
let args = List.map get_desc args in
if List.length params = List.length args
&& List.for_all2 (==) params args
then Alias path
else if List.length params <= List.length args
|| not (uniq args) then Fresh
else begin
match List.map (index params) args with
| exception Not_found -> Fresh
| ns -> Subst(path, ns)
end
| ty -> begin
let ty = Transient_expr.type_expr ty in
match index params (get_desc ty) with
| exception Not_found -> Fresh
| n -> Nth n
end
end
end
let short_paths_class_type_desc clty =
let open Short_paths.Desc.Class_type in
match clty.clty_type with
| Cty_signature _ | Cty_arrow _ -> Fresh
| Cty_constr(path, args, _) ->
let params = List.map get_desc clty.clty_params in
let args = List.map get_desc args in
if List.length params = List.length args
&& List.for_all2 (==) params args
then Alias path
else if List.length params <= List.length args
|| not (uniq args) then Fresh
else begin
match List.map (index params) args with
| exception Not_found -> Fresh
| ns -> Subst(path, ns)
end
let short_paths_module_type_desc (mty : Subst.Lazy.module_type option) =
let open Short_paths.Desc.Module_type in
match mty with
| None | Some Mty_for_hole -> Fresh
| Some (Mty_ident path) -> Alias path
| Some (Mty_signature _ | Mty_functor _) -> Fresh
| Some (Mty_strengthen _) -> Fresh
| Some (Mty_alias _) -> assert false
let deprecated_of_alerts alerts =
if
String.Map.exists (fun key _ ->
match key with
| "deprecated" | "ocaml.deprecated" -> true
| _ -> false
) alerts
then
Short_paths.Desc.Deprecated
else
Short_paths.Desc.Not_deprecated
let deprecated_of_attributes attrs =
deprecated_of_alerts (Builtin_attributes.alerts_of_attrs attrs)
let scrape_lazy =
ref ((fun (_env : t) (_mty : Subst.Lazy.module_type) : Subst.Lazy.module_type ->
assert false))
let rec short_paths_module_desc env mpath mty comp =
let open Short_paths.Desc.Module in
match !scrape_lazy env mty with
| Mty_alias path -> Alias path
| Mty_ident _ -> Fresh (Signature (lazy []))
| Mty_signature _ ->
let components =
lazy (short_paths_module_components_desc env mpath comp)
in
Fresh (Signature components)
| Mty_functor _ ->
let apply path =
short_paths_functor_components_desc env mpath comp path
in
Fresh (Functor apply)
| Mty_strengthen _ -> Fresh (Signature (lazy []))
| Mty_for_hole -> Fresh (Signature (lazy []))
and short_paths_module_components_desc env mpath comp =
match get_components comp with
| Functor_comps _ -> assert false
| Structure_comps c ->
let comps =
String.Map.fold (fun name { tda_declaration = decl; _ } acc ->
let desc = short_paths_type_desc decl in
let depr = deprecated_of_attributes decl.type_attributes in
let item = Short_paths.Desc.Module.Type(name, desc, depr) in
item :: acc
) c.comp_types []
in
let comps =
String.Map.fold (fun name cltda acc ->
let clty = cltda.cltda_declaration in
let desc = short_paths_class_type_desc clty in
let depr = deprecated_of_attributes clty.clty_attributes in
let item = Short_paths.Desc.Module.Class_type(name, desc, depr) in
item :: acc
) c.comp_cltypes comps
in
let comps =
String.Map.fold (fun name mtda acc ->
let desc = short_paths_module_type_desc mtda.mtda_declaration.mtd_type in
let depr = deprecated_of_attributes mtda.mtda_declaration.mtd_attributes in
let item = Short_paths.Desc.Module.Module_type(name, desc, depr) in
item :: acc
) c.comp_modtypes comps
in
let comps =
String.Map.fold (fun name { mda_declaration; mda_components; _ } acc ->
let mpath = Pdot(mpath, name) in
let desc =
short_paths_module_desc env mpath mda_declaration.md_type mda_components
in
let depr = deprecated_of_alerts mda_components.alerts in
let item = Short_paths.Desc.Module.Module(name, desc, depr) in
item :: acc
) c.comp_modules comps
in
comps
and short_paths_functor_components_desc env mpath comp path =
match get_components comp with
| Structure_comps _ -> assert false
| Functor_comps f ->
let mty =
try
stamped_find f.fcomp_subst_cache path
with Not_found ->
let mty =
let subst =
match f.fcomp_arg with
| Unit
| Named (None, _, _) -> Subst.identity
| Named (Some id, _, _) -> Subst.add_module id path Subst.identity
in
Subst.modtype (Rescope (Path.scope (Papply (mpath, path))))
subst f.fcomp_res
in
stamped_path_add f.fcomp_subst_cache path mty;
mty
in
let mty = Subst.Lazy.of_modtype mty in
let loc = Location.(in_file !input_name) in
let comps =
components_of_functor_appl ~loc ~f_comp:f env ~f_path:mpath ~arg:path
in
let mpath = Papply(mpath, path) in
short_paths_module_desc env mpath mty comps
let short_paths_additions_desc env additions =
List.fold_left
(fun acc add ->
match add with
| Type(id, decl) ->
let desc = short_paths_type_desc decl in
let source = Short_paths.Desc.Local in
let depr = deprecated_of_attributes decl.type_attributes in
Short_paths.Desc.Type(id, desc, source, depr) :: acc
| Class_type(id, clty) ->
let desc = short_paths_class_type_desc clty in
let source = Short_paths.Desc.Local in
let depr = deprecated_of_attributes clty.clty_attributes in
Short_paths.Desc.Class_type(id, desc, source, depr) :: acc
| Module_type(id, mtd) ->
let desc = short_paths_module_type_desc mtd.mtd_type in
let source = Short_paths.Desc.Local in
let depr = deprecated_of_attributes mtd.mtd_attributes in
Short_paths.Desc.Module_type(id, desc, source, depr) :: acc
| Module(id, md, comps) ->
let desc = short_paths_module_desc env (Pident id) md.md_type comps in
let source = Short_paths.Desc.Local in
let depr = deprecated_of_alerts comps.alerts in
Short_paths.Desc.Module(id, desc, source, depr) :: acc
| Type_open(root, decls) ->
String.Map.fold
(fun name { tda_declaration = decl; _ } acc ->
let id = Ident.create_local name in
let path = Pdot(root, name) in
let desc = Short_paths.Desc.Type.Alias path in
let source = Short_paths.Desc.Open in
let depr = deprecated_of_attributes decl.type_attributes in
Short_paths.Desc.Type(id, desc, source, depr) :: acc)
decls acc
| Class_type_open(root, decls) ->
String.Map.fold
(fun name clty acc ->
let id = Ident.create_local name in
let path = Pdot(root, name) in
let desc = Short_paths.Desc.Class_type.Alias path in
let source = Short_paths.Desc.Open in
let depr = deprecated_of_attributes clty.clty_attributes in
Short_paths.Desc.Class_type(id, desc, source, depr) :: acc)
decls acc
| Module_type_open(root, decls) ->
String.Map.fold
(fun name (mtd : Subst.Lazy.modtype_declaration) acc ->
let id = Ident.create_local name in
let path = Pdot(root, name) in
let desc = Short_paths.Desc.Module_type.Alias path in
let source = Short_paths.Desc.Open in
let depr = deprecated_of_attributes mtd.mtd_attributes in
Short_paths.Desc.Module_type(id, desc, source, depr) :: acc)
decls acc
| Module_open(root, decls) ->
String.Map.fold
(fun name { mda_components = comps; _ } acc ->
let id = Ident.create_local name in
let path = Pdot(root, name) in
let desc = Short_paths.Desc.Module.Alias path in
let source = Short_paths.Desc.Open in
let depr = deprecated_of_alerts comps.alerts in
Short_paths.Desc.Module(id, desc, source, depr) :: acc)
decls acc)
[] additions
let () =
short_paths_module_components_desc' := short_paths_module_components_desc
let update_short_paths env =
let env, short_paths =
match env.short_paths with
| None ->
let basis = Persistent_env.short_paths_basis !persistent_env in
let short_paths = Short_paths.initial basis in
let env = { env with short_paths = Some short_paths } in
env, short_paths
| Some short_paths -> env, short_paths
in
match env.short_paths_additions with
| [] -> env
| _ :: _ as additions ->
let short_paths =
Short_paths.add short_paths
(lazy (short_paths_additions_desc env additions))
in
{ env with short_paths = Some short_paths;
short_paths_additions = []; }
let short_paths env =
match env.short_paths with
| None ->
let basis = Persistent_env.short_paths_basis !persistent_env in
Short_paths.initial basis
| Some short_paths -> short_paths
let cleanup_functor_caches ~stamp =
Stamped_hashtable.backtrack !stamped_changelog ~stamp
let cleanup_usage_tables ~stamp =
Stamped_hashtable.backtrack value_declarations_changelog ~stamp;
Stamped_hashtable.backtrack type_declarations_changelog ~stamp;
Stamped_hashtable.backtrack module_declarations_changelog ~stamp;
Stamped_hashtable.backtrack jkind_declarations_changelog ~stamp;
Stamped_hashtable.backtrack mutated_mutable_values_changelog ~stamp;
Stamped_hashtable.backtrack used_constructors_changelog ~stamp;
Stamped_hashtable.backtrack used_labels_changelog ~stamp;
type 'acc fold_all_labels_f =
{
fold_all_labels_f : 'rcd. 'rcd record_form -> 'rcd gen_label_description -> 'acc -> 'acc
}
let fold_all_labels f ident env init =
let acc_after_legacy = fold_labels Legacy (f.fold_all_labels_f Legacy) ident env init in
fold_labels Unboxed_product (f.fold_all_labels_f Unboxed_product) ident env acc_after_legacy
let () =
let get_current_compilation_unit () =
Option.map Unit_info.modname (get_unit_name ())
in
Compilation_unit.Private.fwd_get_current := get_current_compilation_unit
let report_lookup_error loc t =
Format_doc.compat (report_lookup_error_doc loc t)
let report_error = Format_doc.compat report_error_doc